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 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
| 29 | #include "mbedtls/platform.h" |
| 30 | #else |
| 31 | #include <stdlib.h> |
| 32 | #define mbedtls_calloc calloc |
| 33 | #define mbedtls_free free |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 34 | #endif |
| 35 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/ssl.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 37 | #include "ssl_misc.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 38 | #include "mbedtls/debug.h" |
| 39 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 40 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 41 | #include "mbedtls/version.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 42 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 43 | #include <string.h> |
| 44 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 45 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 46 | #include "mbedtls/psa_util.h" |
| 47 | #include "psa/crypto.h" |
| 48 | #endif |
| 49 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 50 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 51 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 52 | #endif |
| 53 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 54 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 55 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 56 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 57 | /* Top-level Connection ID API */ |
| 58 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 59 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 60 | size_t len, |
| 61 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 62 | { |
| 63 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 64 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 65 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 66 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 67 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 68 | { |
| 69 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 70 | } |
| 71 | |
| 72 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 73 | conf->cid_len = len; |
| 74 | return( 0 ); |
| 75 | } |
| 76 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 77 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 78 | int enable, |
| 79 | unsigned char const *own_cid, |
| 80 | size_t own_cid_len ) |
| 81 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 82 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 83 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 84 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 85 | ssl->negotiate_cid = enable; |
| 86 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 87 | { |
| 88 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 89 | return( 0 ); |
| 90 | } |
| 91 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 92 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 93 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 94 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 95 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 96 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 97 | (unsigned) own_cid_len, |
| 98 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 99 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 100 | } |
| 101 | |
| 102 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 103 | /* Truncation is not an issue here because |
| 104 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 105 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 106 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 107 | return( 0 ); |
| 108 | } |
| 109 | |
| 110 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 111 | int *enabled, |
| 112 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 113 | size_t *peer_cid_len ) |
| 114 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 115 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 116 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 117 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 118 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 119 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 120 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 121 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 122 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 123 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 124 | * were used, but client and server requested the empty CID. |
| 125 | * This is indistinguishable from not using the CID extension |
| 126 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 127 | if( ssl->transform_in->in_cid_len == 0 && |
| 128 | ssl->transform_in->out_cid_len == 0 ) |
| 129 | { |
| 130 | return( 0 ); |
| 131 | } |
| 132 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 133 | if( peer_cid_len != NULL ) |
| 134 | { |
| 135 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 136 | if( peer_cid != NULL ) |
| 137 | { |
| 138 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 139 | ssl->transform_in->out_cid_len ); |
| 140 | } |
| 141 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 142 | |
| 143 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 144 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 145 | return( 0 ); |
| 146 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 147 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 148 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 152 | /* |
| 153 | * Convert max_fragment_length codes to length. |
| 154 | * RFC 6066 says: |
| 155 | * enum{ |
| 156 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 157 | * } MaxFragmentLength; |
| 158 | * and we add 0 -> extension unused |
| 159 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 160 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 161 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 162 | switch( mfl ) |
| 163 | { |
| 164 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 165 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 166 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 167 | return 512; |
| 168 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 169 | return 1024; |
| 170 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 171 | return 2048; |
| 172 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 173 | return 4096; |
| 174 | default: |
| 175 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 176 | } |
| 177 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 179 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 180 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 181 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 182 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | mbedtls_ssl_session_free( dst ); |
| 184 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 185 | |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame^] | 186 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 187 | dst->ticket = NULL; |
| 188 | #endif |
| 189 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 191 | |
| 192 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 193 | if( src->peer_cert != NULL ) |
| 194 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 195 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 196 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 197 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 198 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 199 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | 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] | 204 | src->peer_cert->raw.len ) ) != 0 ) |
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_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 207 | dst->peer_cert = NULL; |
| 208 | return( ret ); |
| 209 | } |
| 210 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 211 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 212 | if( src->peer_cert_digest != NULL ) |
| 213 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 214 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 215 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 216 | if( dst->peer_cert_digest == NULL ) |
| 217 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 218 | |
| 219 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 220 | src->peer_cert_digest_len ); |
| 221 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 222 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 223 | } |
| 224 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 225 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 227 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 228 | #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] | 229 | if( src->ticket != NULL ) |
| 230 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 231 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 232 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 233 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 234 | |
| 235 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 236 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 237 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 238 | |
| 239 | return( 0 ); |
| 240 | } |
| 241 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 242 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 243 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
| 244 | { |
| 245 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); |
| 246 | if( resized_buffer == NULL ) |
| 247 | return -1; |
| 248 | |
| 249 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 250 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 251 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 252 | * lost, are done outside of this function. */ |
| 253 | memcpy( resized_buffer, *buffer, |
| 254 | ( len_new < *len_old ) ? len_new : *len_old ); |
| 255 | mbedtls_platform_zeroize( *buffer, *len_old ); |
| 256 | mbedtls_free( *buffer ); |
| 257 | |
| 258 | *buffer = resized_buffer; |
| 259 | *len_old = len_new; |
| 260 | |
| 261 | return 0; |
| 262 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 263 | |
| 264 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, |
Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 265 | size_t in_buf_new_len, |
| 266 | size_t out_buf_new_len ) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 267 | { |
| 268 | int modified = 0; |
| 269 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 270 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
| 271 | if( ssl->in_buf != NULL ) |
| 272 | { |
| 273 | written_in = ssl->in_msg - ssl->in_buf; |
| 274 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 275 | len_offset_in = ssl->in_len - ssl->in_buf; |
| 276 | if( downsizing ? |
| 277 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : |
| 278 | ssl->in_buf_len < in_buf_new_len ) |
| 279 | { |
| 280 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) |
| 281 | { |
| 282 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 283 | } |
| 284 | else |
| 285 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 286 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 287 | in_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 288 | modified = 1; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if( ssl->out_buf != NULL ) |
| 294 | { |
| 295 | written_out = ssl->out_msg - ssl->out_buf; |
| 296 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 297 | len_offset_out = ssl->out_len - ssl->out_buf; |
| 298 | if( downsizing ? |
| 299 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : |
| 300 | ssl->out_buf_len < out_buf_new_len ) |
| 301 | { |
| 302 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) |
| 303 | { |
| 304 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 305 | } |
| 306 | else |
| 307 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 308 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 309 | out_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 310 | modified = 1; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | if( modified ) |
| 315 | { |
| 316 | /* Update pointers here to avoid doing it twice. */ |
| 317 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 318 | /* Fields below might not be properly updated with record |
| 319 | * splitting or with CID, so they are manually updated here. */ |
| 320 | ssl->out_msg = ssl->out_buf + written_out; |
| 321 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 322 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 323 | |
| 324 | ssl->in_msg = ssl->in_buf + written_in; |
| 325 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 326 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 327 | } |
| 328 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 329 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 330 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 332 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 333 | |
| 334 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 335 | psa_key_id_t key, |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 336 | psa_algorithm_t alg, |
| 337 | const unsigned char* seed, size_t seed_length, |
| 338 | const unsigned char* label, size_t label_length, |
| 339 | size_t capacity ) |
| 340 | { |
| 341 | psa_status_t status; |
| 342 | |
| 343 | status = psa_key_derivation_setup( derivation, alg ); |
| 344 | if( status != PSA_SUCCESS ) |
| 345 | return( status ); |
| 346 | |
| 347 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 348 | { |
| 349 | status = psa_key_derivation_input_bytes( derivation, |
| 350 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 351 | seed, seed_length ); |
| 352 | if( status != PSA_SUCCESS ) |
| 353 | return( status ); |
| 354 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 355 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 356 | { |
| 357 | status = psa_key_derivation_input_bytes( |
| 358 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 359 | NULL, 0 ); |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | status = psa_key_derivation_input_key( |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 364 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 365 | } |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 366 | if( status != PSA_SUCCESS ) |
| 367 | return( status ); |
| 368 | |
| 369 | status = psa_key_derivation_input_bytes( derivation, |
| 370 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 371 | label, label_length ); |
| 372 | if( status != PSA_SUCCESS ) |
| 373 | return( status ); |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 378 | } |
| 379 | |
| 380 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 381 | if( status != PSA_SUCCESS ) |
| 382 | return( status ); |
| 383 | |
| 384 | return( PSA_SUCCESS ); |
| 385 | } |
| 386 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 387 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 388 | const unsigned char *secret, size_t slen, |
| 389 | const char *label, |
| 390 | const unsigned char *random, size_t rlen, |
| 391 | unsigned char *dstbuf, size_t dlen ) |
| 392 | { |
| 393 | psa_status_t status; |
| 394 | psa_algorithm_t alg; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 395 | psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 396 | psa_key_derivation_operation_t derivation = |
Janos Follath | 8dee877 | 2019-07-30 12:53:32 +0100 | [diff] [blame] | 397 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 398 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 399 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 400 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 401 | else |
| 402 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 403 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 404 | /* Normally a "secret" should be long enough to be impossible to |
| 405 | * find by brute force, and in particular should not be empty. But |
| 406 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 407 | * and for this use case it makes sense to have a 0-length "secret". |
| 408 | * Since the key API doesn't allow importing a key of length 0, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 409 | * keep master_key=0, which setup_psa_key_derivation() understands |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 410 | * to mean a 0-length "secret" input. */ |
| 411 | if( slen != 0 ) |
| 412 | { |
| 413 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 414 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 415 | psa_set_key_algorithm( &key_attributes, alg ); |
| 416 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 417 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 418 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 419 | if( status != PSA_SUCCESS ) |
| 420 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 421 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 422 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 423 | status = setup_psa_key_derivation( &derivation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 424 | master_key, alg, |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 425 | random, rlen, |
| 426 | (unsigned char const *) label, |
| 427 | (size_t) strlen( label ), |
| 428 | dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 429 | if( status != PSA_SUCCESS ) |
| 430 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 431 | psa_key_derivation_abort( &derivation ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 432 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 433 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 434 | } |
| 435 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 436 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 437 | if( status != PSA_SUCCESS ) |
| 438 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 439 | psa_key_derivation_abort( &derivation ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 440 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 441 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 442 | } |
| 443 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 444 | status = psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 445 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 446 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 447 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 448 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 449 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 450 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 451 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 452 | status = psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 453 | if( status != PSA_SUCCESS ) |
| 454 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 455 | |
Andrzej Kurek | 3317126 | 2019-01-15 03:25:18 -0500 | [diff] [blame] | 456 | return( 0 ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 461 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 462 | const unsigned char *secret, size_t slen, |
| 463 | const char *label, |
| 464 | const unsigned char *random, size_t rlen, |
| 465 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 466 | { |
| 467 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 468 | size_t i, j, k, md_len; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 469 | unsigned char *tmp; |
| 470 | size_t tmp_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 471 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 472 | const mbedtls_md_info_t *md_info; |
| 473 | mbedtls_md_context_t md_ctx; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 474 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 477 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 478 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 479 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 481 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 482 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 483 | tmp_len = md_len + strlen( label ) + rlen; |
| 484 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 485 | if( tmp == NULL ) |
| 486 | { |
| 487 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 488 | goto exit; |
| 489 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 490 | |
| 491 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 492 | memcpy( tmp + md_len, label, nb ); |
| 493 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 494 | nb += rlen; |
| 495 | |
| 496 | /* |
| 497 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 498 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 500 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 501 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 502 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 503 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 504 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 505 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 506 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 507 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 508 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 509 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 510 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 511 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 512 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 513 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 514 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 515 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 516 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 517 | |
| 518 | for( j = 0; j < k; j++ ) |
| 519 | dstbuf[i + j] = h_i[j]; |
| 520 | } |
| 521 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 522 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 523 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 524 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 525 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 526 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 527 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 528 | mbedtls_free( tmp ); |
| 529 | |
| 530 | return( ret ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 531 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 532 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 533 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 534 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 535 | const char *label, |
| 536 | const unsigned char *random, size_t rlen, |
| 537 | unsigned char *dstbuf, size_t dlen ) |
| 538 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 539 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 540 | label, random, rlen, dstbuf, dlen ) ); |
| 541 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 543 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 544 | #if defined(MBEDTLS_SHA384_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 545 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 546 | const char *label, |
| 547 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 548 | unsigned char *dstbuf, size_t dlen ) |
| 549 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 550 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 551 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 552 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 553 | #endif /* MBEDTLS_SHA384_C */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 554 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 555 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 558 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 559 | #if defined(MBEDTLS_SHA256_C) |
| 560 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 561 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 563 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 564 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 565 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 567 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 568 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 569 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 570 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 571 | |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 572 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 573 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 574 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 575 | { |
| 576 | if( ssl->conf->f_psk != NULL ) |
| 577 | { |
| 578 | /* If we've used a callback to select the PSK, |
| 579 | * the static configuration is irrelevant. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 580 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 581 | return( 1 ); |
| 582 | |
| 583 | return( 0 ); |
| 584 | } |
| 585 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 586 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 587 | return( 1 ); |
| 588 | |
| 589 | return( 0 ); |
| 590 | } |
| 591 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 592 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 593 | |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 594 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 595 | { |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 596 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 597 | #if defined(MBEDTLS_SHA384_C) |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 598 | if( tls_prf == tls_prf_sha384 ) |
| 599 | { |
| 600 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 601 | } |
| 602 | else |
| 603 | #endif |
| 604 | #if defined(MBEDTLS_SHA256_C) |
| 605 | if( tls_prf == tls_prf_sha256 ) |
| 606 | { |
| 607 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 608 | } |
| 609 | else |
| 610 | #endif |
| 611 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 612 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 613 | } |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 614 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 615 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 616 | const unsigned char *secret, size_t slen, |
| 617 | const char *label, |
| 618 | const unsigned char *random, size_t rlen, |
| 619 | unsigned char *dstbuf, size_t dlen ) |
| 620 | { |
| 621 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 622 | |
| 623 | switch( prf ) |
| 624 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 625 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 626 | #if defined(MBEDTLS_SHA384_C) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 627 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 628 | tls_prf = tls_prf_sha384; |
| 629 | break; |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 630 | #endif /* MBEDTLS_SHA384_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 631 | #if defined(MBEDTLS_SHA256_C) |
| 632 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 633 | tls_prf = tls_prf_sha256; |
| 634 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 635 | #endif /* MBEDTLS_SHA256_C */ |
| 636 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 637 | default: |
| 638 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 639 | } |
| 640 | |
| 641 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 642 | } |
| 643 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 644 | /* Type for the TLS PRF */ |
| 645 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 646 | const unsigned char *, size_t, |
| 647 | unsigned char *, size_t); |
| 648 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 649 | /* |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 650 | * Populate a transform structure with session keys and all the other |
| 651 | * necessary information. |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 652 | * |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 653 | * Parameters: |
| 654 | * - [in/out]: transform: structure to populate |
| 655 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 656 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 657 | * - [in] ciphersuite |
| 658 | * - [in] master |
| 659 | * - [in] encrypt_then_mac |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 660 | * - [in] compression |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 661 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 662 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 663 | * - [in] minor_ver: SSL/TLS minor version |
| 664 | * - [in] endpoint: client or server |
Andrzej Kurek | 5902cd6 | 2021-09-28 10:00:32 -0400 | [diff] [blame] | 665 | * - [in] ssl: used for: |
| 666 | * - ssl->conf->{f,p}_export_keys |
| 667 | * [in] optionally used for: |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 668 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 669 | */ |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 670 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 671 | int ciphersuite, |
| 672 | const unsigned char master[48], |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 673 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 674 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 675 | int encrypt_then_mac, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 676 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 677 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 678 | ssl_tls_prf_t tls_prf, |
| 679 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 680 | int minor_ver, |
| 681 | unsigned endpoint, |
Mateusz Starzyk | e204dbf | 2021-03-15 17:57:20 +0100 | [diff] [blame] | 682 | const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 683 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 684 | int ret = 0; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 685 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 686 | int psa_fallthrough; |
| 687 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 688 | unsigned char keyblk[256]; |
| 689 | unsigned char *key1; |
| 690 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 691 | unsigned char *mac_enc; |
| 692 | unsigned char *mac_dec; |
sander-visser | 3888b03 | 2020-05-06 21:49:46 +0200 | [diff] [blame] | 693 | size_t mac_key_len = 0; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 694 | size_t iv_copy_len; |
Gilles Peskine | 88d681c | 2021-09-01 11:19:33 +0200 | [diff] [blame] | 695 | size_t keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 696 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 697 | const mbedtls_cipher_info_t *cipher_info; |
| 698 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 699 | |
Andrzej Kurek | 324f72e | 2021-09-29 04:21:21 -0400 | [diff] [blame] | 700 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 701 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Andrzej Kurek | a72fe64 | 2021-09-29 15:57:30 -0400 | [diff] [blame] | 702 | if( ssl->f_export_keys == NULL ) |
| 703 | { |
| 704 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 705 | (void) ssl; |
| 706 | } |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 707 | #endif |
| 708 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 709 | /* |
| 710 | * Some data just needs copying into the structure |
| 711 | */ |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 712 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 713 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 714 | transform->encrypt_then_mac = encrypt_then_mac; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 715 | #endif |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 716 | transform->minor_ver = minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 717 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 718 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 719 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 720 | #endif |
| 721 | |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 722 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 723 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 724 | { |
| 725 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 726 | * generation separate. This should never happen. */ |
| 727 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 728 | } |
| 729 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 730 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 731 | /* |
| 732 | * Get various info structures |
| 733 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 734 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 735 | if( ciphersuite_info == NULL ) |
| 736 | { |
| 737 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 738 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 739 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 740 | } |
| 741 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 742 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 743 | if( cipher_info == NULL ) |
| 744 | { |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 745 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 746 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 747 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 748 | } |
| 749 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 750 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 751 | if( md_info == NULL ) |
| 752 | { |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 753 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 754 | (unsigned) ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 755 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 758 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 759 | /* Copy own and peer's CID if the use of the CID |
| 760 | * extension has been negotiated. */ |
| 761 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 762 | { |
| 763 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 764 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 765 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 766 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 767 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 768 | transform->in_cid_len ); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 769 | |
| 770 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 771 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 772 | ssl->handshake->peer_cid_len ); |
| 773 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 774 | transform->out_cid_len ); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 775 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 776 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 777 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 778 | /* |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 779 | * Compute key block using the PRF |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 780 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 781 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 782 | if( ret != 0 ) |
| 783 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 784 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 785 | return( ret ); |
| 786 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 787 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 788 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 789 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 790 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 791 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 792 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 793 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 794 | /* |
| 795 | * Determine the appropriate key, IV and MAC length. |
| 796 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 797 | |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 798 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 799 | |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 800 | #if defined(MBEDTLS_GCM_C) || \ |
| 801 | defined(MBEDTLS_CCM_C) || \ |
| 802 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 803 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM || |
| 804 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM || |
| 805 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 806 | { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 807 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 808 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 809 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 810 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 811 | transform->taglen = |
| 812 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 813 | |
Hanno Becker | 447558d | 2020-05-28 07:36:33 +0100 | [diff] [blame] | 814 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 815 | * with mode and version: |
| 816 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 817 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 818 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 819 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 820 | * sequence number). |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 821 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 822 | transform->ivlen = 12; |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 823 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 824 | transform->fixed_ivlen = 12; |
| 825 | else |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 826 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 827 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 828 | /* Minimum length of encrypted record */ |
| 829 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 830 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 831 | } |
| 832 | else |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 833 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 834 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 835 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM || |
| 836 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 837 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 838 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 839 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 840 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 841 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 842 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 843 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 844 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 845 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 846 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 847 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 848 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 849 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 850 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 851 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 852 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 853 | /* Minimum length */ |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 854 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 855 | transform->minlen = transform->maclen; |
| 856 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 857 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 858 | /* |
| 859 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 860 | * 1. if EtM is in use: one block plus MAC |
| 861 | * otherwise: * first multiple of blocklen greater than maclen |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 862 | * 2. IV |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 863 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 864 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 865 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 866 | { |
| 867 | transform->minlen = transform->maclen |
| 868 | + cipher_info->block_size; |
| 869 | } |
| 870 | else |
| 871 | #endif |
| 872 | { |
| 873 | transform->minlen = transform->maclen |
| 874 | + cipher_info->block_size |
| 875 | - transform->maclen % cipher_info->block_size; |
| 876 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 877 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 878 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 879 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 880 | { |
| 881 | transform->minlen += transform->ivlen; |
| 882 | } |
| 883 | else |
| 884 | #endif |
| 885 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 886 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 887 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 888 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 889 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 890 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 891 | } |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 892 | else |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 893 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 894 | { |
| 895 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 896 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 897 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 898 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 899 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 900 | (unsigned) keylen, |
| 901 | (unsigned) transform->minlen, |
| 902 | (unsigned) transform->ivlen, |
| 903 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 904 | |
| 905 | /* |
| 906 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 907 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 908 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 909 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 910 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 911 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 912 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 913 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 914 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 915 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 916 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 917 | /* |
| 918 | * This is not used in TLS v1.1. |
| 919 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 920 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 921 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 922 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 923 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 924 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 925 | } |
| 926 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 927 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 928 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 929 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 930 | { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 931 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 932 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 933 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 934 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 935 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 936 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 937 | /* |
| 938 | * This is not used in TLS v1.1. |
| 939 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 940 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 941 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 942 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 943 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 944 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 945 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 946 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 947 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 948 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 949 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 950 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 951 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 952 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 953 | |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 954 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 955 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 956 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 957 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 958 | if( mac_key_len != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 959 | { |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 960 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 961 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 962 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 963 | #endif |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 964 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 965 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 966 | ((void) mac_dec); |
| 967 | ((void) mac_enc); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 968 | |
Andrzej Kurek | a72fe64 | 2021-09-29 15:57:30 -0400 | [diff] [blame] | 969 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 970 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 971 | ssl->f_export_keys( ssl->p_export_keys, |
| 972 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 973 | master, 48, |
| 974 | randbytes + 32, |
| 975 | randbytes, |
| 976 | tls_prf_get_type( tls_prf ) ); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 977 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 978 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 979 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | a0b4b0c | 2021-09-21 14:06:33 +0200 | [diff] [blame] | 980 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc, |
| 981 | cipher_info, transform->taglen ); |
| 982 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 983 | { |
Manuel Pégourié-Gonnard | a0b4b0c | 2021-09-21 14:06:33 +0200 | [diff] [blame] | 984 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
| 985 | goto end; |
| 986 | } |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 987 | |
Manuel Pégourié-Gonnard | a0b4b0c | 2021-09-21 14:06:33 +0200 | [diff] [blame] | 988 | if( ret == 0 ) |
| 989 | { |
| 990 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) ); |
| 991 | psa_fallthrough = 0; |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 992 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 993 | else |
Manuel Pégourié-Gonnard | a0b4b0c | 2021-09-21 14:06:33 +0200 | [diff] [blame] | 994 | { |
| 995 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 996 | psa_fallthrough = 1; |
Manuel Pégourié-Gonnard | a0b4b0c | 2021-09-21 14:06:33 +0200 | [diff] [blame] | 997 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 998 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 999 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1000 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1001 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1002 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1003 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1004 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1005 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1006 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1007 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1008 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | a0b4b0c | 2021-09-21 14:06:33 +0200 | [diff] [blame] | 1009 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec, |
| 1010 | cipher_info, transform->taglen ); |
| 1011 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1012 | { |
Manuel Pégourié-Gonnard | a0b4b0c | 2021-09-21 14:06:33 +0200 | [diff] [blame] | 1013 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
| 1014 | goto end; |
| 1015 | } |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1016 | |
Manuel Pégourié-Gonnard | a0b4b0c | 2021-09-21 14:06:33 +0200 | [diff] [blame] | 1017 | if( ret == 0 ) |
| 1018 | { |
| 1019 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) ); |
| 1020 | psa_fallthrough = 0; |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1021 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1022 | else |
Manuel Pégourié-Gonnard | a0b4b0c | 2021-09-21 14:06:33 +0200 | [diff] [blame] | 1023 | { |
| 1024 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1025 | psa_fallthrough = 1; |
Manuel Pégourié-Gonnard | a0b4b0c | 2021-09-21 14:06:33 +0200 | [diff] [blame] | 1026 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1027 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1028 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1029 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1030 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1031 | cipher_info ) ) != 0 ) |
| 1032 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1033 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1034 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1035 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1036 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1037 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Gilles Peskine | 88d681c | 2021-09-01 11:19:33 +0200 | [diff] [blame] | 1038 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1039 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1040 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1041 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1042 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1043 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1044 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1045 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Gilles Peskine | 88d681c | 2021-09-01 11:19:33 +0200 | [diff] [blame] | 1046 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1047 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1048 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1049 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1050 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1051 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1053 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 1054 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1055 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1056 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1057 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1058 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1059 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1060 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1061 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1062 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1063 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1064 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1065 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1066 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1067 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1068 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1069 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1070 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1071 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1072 | |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1073 | end: |
Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 1074 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1075 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1078 | /* |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1079 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1080 | * |
| 1081 | * Inputs: |
| 1082 | * - SSL/TLS minor version |
| 1083 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1084 | * |
Manuel Pégourié-Gonnard | 31d3ef1 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1085 | * Outputs: |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1086 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1087 | */ |
| 1088 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1089 | int minor_ver, |
| 1090 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1091 | { |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1092 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1093 | (void) hash; |
| 1094 | #endif |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1095 | |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1096 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1097 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1098 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1099 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1100 | { |
| 1101 | handshake->tls_prf = tls_prf_sha384; |
| 1102 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1103 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1104 | } |
| 1105 | else |
| 1106 | #endif |
| 1107 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1108 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1109 | { |
| 1110 | handshake->tls_prf = tls_prf_sha256; |
| 1111 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1112 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1113 | } |
| 1114 | else |
| 1115 | #endif |
| 1116 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1117 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1118 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1119 | } |
| 1120 | |
| 1121 | return( 0 ); |
| 1122 | } |
| 1123 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1124 | /* |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1125 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1126 | * |
| 1127 | * Parameters: |
| 1128 | * [in/out] handshake |
| 1129 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1130 | * (PSA-PSK) ciphersuite_info, psk_opaque |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1131 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1132 | * [out] master |
| 1133 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 1134 | * debug: conf->f_dbg, conf->p_dbg |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1135 | * EMS: passed to calc_verify (debug + session_negotiate) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1136 | * PSA-PSA: minor_ver, conf |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1137 | */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1138 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1139 | unsigned char *master, |
Manuel Pégourié-Gonnard | 0d56aaa | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1140 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1141 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1142 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1143 | |
| 1144 | /* cf. RFC 5246, Section 8.1: |
| 1145 | * "The master secret is always exactly 48 bytes in length." */ |
| 1146 | size_t const master_secret_len = 48; |
| 1147 | |
| 1148 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1149 | unsigned char session_hash[48]; |
| 1150 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 1151 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1152 | /* The label for the KDF used for key expansion. |
| 1153 | * This is either "master secret" or "extended master secret" |
| 1154 | * depending on whether the Extended Master Secret extension |
| 1155 | * is used. */ |
| 1156 | char const *lbl = "master secret"; |
| 1157 | |
| 1158 | /* The salt for the KDF used for key expansion. |
| 1159 | * - If the Extended Master Secret extension is not used, |
| 1160 | * this is ClientHello.Random + ServerHello.Random |
| 1161 | * (see Sect. 8.1 in RFC 5246). |
| 1162 | * - If the Extended Master Secret extension is used, |
| 1163 | * this is the transcript of the handshake so far. |
| 1164 | * (see Sect. 4 in RFC 7627). */ |
| 1165 | unsigned char const *salt = handshake->randbytes; |
| 1166 | size_t salt_len = 64; |
| 1167 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1168 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 1169 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 1170 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1171 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1172 | ssl = NULL; /* make sure we don't use it except for those cases */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1173 | (void) ssl; |
| 1174 | #endif |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1175 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1176 | if( handshake->resume != 0 ) |
| 1177 | { |
| 1178 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1179 | return( 0 ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1180 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1181 | |
| 1182 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1183 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1184 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1185 | lbl = "extended master secret"; |
| 1186 | salt = session_hash; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1187 | handshake->calc_verify( ssl, session_hash, &salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1188 | |
Manuel Pégourié-Gonnard | 8faa70e | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1189 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1190 | session_hash, salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1191 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1192 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 1193 | |
| 1194 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1195 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1196 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1197 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1198 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 1199 | { |
| 1200 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1201 | psa_status_t status; |
| 1202 | psa_algorithm_t alg; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1203 | psa_key_id_t psk; |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1204 | psa_key_derivation_operation_t derivation = |
| 1205 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1206 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1207 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1208 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1209 | |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1210 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1211 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1212 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1213 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1214 | else |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1215 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1216 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 1217 | status = setup_psa_key_derivation( &derivation, psk, alg, |
| 1218 | salt, salt_len, |
| 1219 | (unsigned char const *) lbl, |
| 1220 | (size_t) strlen( lbl ), |
| 1221 | master_secret_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1222 | if( status != PSA_SUCCESS ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1223 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1224 | psa_key_derivation_abort( &derivation ); |
| 1225 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1226 | } |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1227 | |
| 1228 | status = psa_key_derivation_output_bytes( &derivation, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1229 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1230 | master_secret_len ); |
| 1231 | if( status != PSA_SUCCESS ) |
| 1232 | { |
| 1233 | psa_key_derivation_abort( &derivation ); |
| 1234 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1235 | } |
| 1236 | |
| 1237 | status = psa_key_derivation_abort( &derivation ); |
| 1238 | if( status != PSA_SUCCESS ) |
| 1239 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1240 | } |
| 1241 | else |
| 1242 | #endif |
| 1243 | { |
| 1244 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1245 | lbl, salt, salt_len, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1246 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1247 | master_secret_len ); |
| 1248 | if( ret != 0 ) |
| 1249 | { |
| 1250 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1251 | return( ret ); |
| 1252 | } |
| 1253 | |
| 1254 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 1255 | handshake->premaster, |
| 1256 | handshake->pmslen ); |
| 1257 | |
| 1258 | mbedtls_platform_zeroize( handshake->premaster, |
| 1259 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | return( 0 ); |
| 1263 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1264 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1265 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1266 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1267 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1268 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1269 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1270 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1271 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1272 | |
| 1273 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1274 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1275 | ssl->minor_ver, |
| 1276 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1277 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1278 | { |
| 1279 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1280 | return( ret ); |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1281 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1282 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1283 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1284 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1285 | ssl->session_negotiate->master, |
| 1286 | ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1287 | if( ret != 0 ) |
| 1288 | { |
| 1289 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1290 | return( ret ); |
| 1291 | } |
| 1292 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1293 | /* Swap the client and server random values: |
| 1294 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1295 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1296 | { |
| 1297 | unsigned char tmp[64]; |
| 1298 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1299 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1300 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1301 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1302 | } |
| 1303 | |
| 1304 | /* Populate transform structure */ |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 1305 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 1306 | ssl->session_negotiate->ciphersuite, |
| 1307 | ssl->session_negotiate->master, |
| 1308 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 1309 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1310 | ssl->session_negotiate->encrypt_then_mac, |
| 1311 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 1312 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1313 | ssl->handshake->tls_prf, |
| 1314 | ssl->handshake->randbytes, |
| 1315 | ssl->minor_ver, |
| 1316 | ssl->conf->endpoint, |
| 1317 | ssl ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1318 | if( ret != 0 ) |
| 1319 | { |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 1320 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1321 | return( ret ); |
| 1322 | } |
| 1323 | |
| 1324 | /* We no longer need Server/ClientHello.random values */ |
| 1325 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1326 | sizeof( ssl->handshake->randbytes ) ); |
| 1327 | |
| 1328 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1329 | |
| 1330 | return( 0 ); |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1331 | } |
| 1332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1333 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1334 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1335 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1336 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1337 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1338 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1339 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1340 | size_t hash_size; |
| 1341 | psa_status_t status; |
| 1342 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1343 | |
| 1344 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 1345 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 1346 | if( status != PSA_SUCCESS ) |
| 1347 | { |
| 1348 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1349 | return; |
| 1350 | } |
| 1351 | |
| 1352 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 1353 | if( status != PSA_SUCCESS ) |
| 1354 | { |
| 1355 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1356 | return; |
| 1357 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1358 | |
| 1359 | *hlen = 32; |
| 1360 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1361 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1362 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1363 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1364 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1365 | mbedtls_sha256_init( &sha256 ); |
| 1366 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1367 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1368 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1369 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 1370 | mbedtls_sha256_finish( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1371 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1372 | *hlen = 32; |
| 1373 | |
| 1374 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1375 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1376 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1377 | mbedtls_sha256_free( &sha256 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1378 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1379 | return; |
| 1380 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1381 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1382 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1383 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1384 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1385 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1386 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1387 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1388 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1389 | size_t hash_size; |
| 1390 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1391 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1392 | |
| 1393 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1394 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1395 | if( status != PSA_SUCCESS ) |
| 1396 | { |
| 1397 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1398 | return; |
| 1399 | } |
| 1400 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1401 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1402 | if( status != PSA_SUCCESS ) |
| 1403 | { |
| 1404 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1405 | return; |
| 1406 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1407 | |
| 1408 | *hlen = 48; |
| 1409 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1410 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1411 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1412 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1413 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1414 | mbedtls_sha512_init( &sha512 ); |
| 1415 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1416 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1417 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1418 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 1419 | mbedtls_sha512_finish( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1420 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1421 | *hlen = 48; |
| 1422 | |
| 1423 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1425 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1426 | mbedtls_sha512_free( &sha512 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1427 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1428 | return; |
| 1429 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1430 | #endif /* MBEDTLS_SHA384_C */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1431 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1432 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1433 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1434 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1435 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1436 | unsigned char *p = ssl->handshake->premaster; |
| 1437 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Guilhem Bryant | b5f04e4 | 2020-04-01 11:23:58 +0100 | [diff] [blame] | 1438 | const unsigned char *psk = NULL; |
Guilhem Bryant | 61b0fe6 | 2020-03-27 11:12:12 +0000 | [diff] [blame] | 1439 | size_t psk_len = 0; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1440 | |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1441 | if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) |
| 1442 | == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1443 | { |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1444 | /* |
| 1445 | * This should never happen because the existence of a PSK is always |
| 1446 | * checked before calling this function |
| 1447 | */ |
Guilhem Bryant | 82194c8 | 2020-03-26 17:04:31 +0000 | [diff] [blame] | 1448 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Guilhem Bryant | 0c9b195 | 2020-04-08 11:02:38 +0100 | [diff] [blame] | 1449 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1450 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1451 | |
| 1452 | /* |
| 1453 | * PMS = struct { |
| 1454 | * opaque other_secret<0..2^16-1>; |
| 1455 | * opaque psk<0..2^16-1>; |
| 1456 | * }; |
| 1457 | * with "other_secret" depending on the particular key exchange |
| 1458 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1459 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1460 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1461 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1462 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1463 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1464 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1465 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 1466 | p += 2; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1467 | |
| 1468 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1469 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1470 | |
| 1471 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1472 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1473 | } |
| 1474 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1475 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1476 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1477 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1478 | { |
| 1479 | /* |
| 1480 | * other_secret already set by the ClientKeyExchange message, |
| 1481 | * and is 48 bytes long |
| 1482 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1483 | if( end - p < 2 ) |
| 1484 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1485 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1486 | *p++ = 0; |
| 1487 | *p++ = 48; |
| 1488 | p += 48; |
| 1489 | } |
| 1490 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1491 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1492 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1493 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1494 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1495 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1496 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1497 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1498 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1499 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1500 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1501 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1502 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1503 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1504 | return( ret ); |
| 1505 | } |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1506 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 1507 | p += 2 + len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1509 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1510 | } |
| 1511 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1512 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1513 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1514 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1515 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1516 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1517 | size_t zlen; |
| 1518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1519 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1520 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1521 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1522 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1523 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1524 | return( ret ); |
| 1525 | } |
| 1526 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1527 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 1528 | p += 2 + zlen; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1529 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1530 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1531 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1532 | } |
| 1533 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1534 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1535 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1536 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1537 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1541 | if( end - p < 2 ) |
| 1542 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1543 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1544 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 1545 | p += 2; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1546 | |
| 1547 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1548 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1549 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1550 | memcpy( p, psk, psk_len ); |
| 1551 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1552 | |
| 1553 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1554 | |
| 1555 | return( 0 ); |
| 1556 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1557 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1559 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1560 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1561 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1562 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 1563 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1564 | { |
| 1565 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 1566 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1567 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1568 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1569 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1570 | unsigned char doublings = 1; |
| 1571 | |
| 1572 | while( ratio != 0 ) |
| 1573 | { |
| 1574 | ++doublings; |
| 1575 | ratio >>= 1; |
| 1576 | } |
| 1577 | |
| 1578 | if( ++ssl->renego_records_seen > doublings ) |
| 1579 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 1580 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1581 | return( 0 ); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | return( ssl_write_hello_request( ssl ) ); |
| 1586 | } |
| 1587 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1588 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1589 | |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 1590 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1591 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 1592 | { |
| 1593 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1594 | if( session->peer_cert != NULL ) |
| 1595 | { |
| 1596 | mbedtls_x509_crt_free( session->peer_cert ); |
| 1597 | mbedtls_free( session->peer_cert ); |
| 1598 | session->peer_cert = NULL; |
| 1599 | } |
| 1600 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1601 | if( session->peer_cert_digest != NULL ) |
| 1602 | { |
| 1603 | /* Zeroization is not necessary. */ |
| 1604 | mbedtls_free( session->peer_cert_digest ); |
| 1605 | session->peer_cert_digest = NULL; |
| 1606 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 1607 | session->peer_cert_digest_len = 0; |
| 1608 | } |
| 1609 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1610 | } |
| 1611 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 1612 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1613 | /* |
| 1614 | * Handshake functions |
| 1615 | */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1616 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1617 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1618 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1619 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1620 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1621 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1623 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1624 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1625 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1626 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1627 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1628 | ssl->state++; |
| 1629 | return( 0 ); |
| 1630 | } |
| 1631 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1632 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1633 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1634 | } |
| 1635 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1637 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1638 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1639 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1641 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1642 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1643 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1644 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1645 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1646 | ssl->state++; |
| 1647 | return( 0 ); |
| 1648 | } |
| 1649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1650 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1651 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1652 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1653 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1654 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1655 | /* Some certificate support -> implement write and parse */ |
| 1656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1657 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1658 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1659 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1660 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1661 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1662 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1663 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1665 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1666 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1667 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1668 | { |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 1669 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 1670 | ssl->state++; |
| 1671 | return( 0 ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1674 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1675 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1676 | { |
| 1677 | if( ssl->client_auth == 0 ) |
| 1678 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1679 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1680 | ssl->state++; |
| 1681 | return( 0 ); |
| 1682 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1683 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1684 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1685 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1686 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1687 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1688 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1689 | { |
Hanno Becker | 9cfe6e9 | 2021-04-30 05:38:24 +0100 | [diff] [blame] | 1690 | /* Should never happen because we shouldn't have picked the |
| 1691 | * ciphersuite if we don't have a certificate. */ |
| 1692 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1693 | } |
| 1694 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1695 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1697 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1698 | |
| 1699 | /* |
| 1700 | * 0 . 0 handshake type |
| 1701 | * 1 . 3 handshake length |
| 1702 | * 4 . 6 length of all certs |
| 1703 | * 7 . 9 length of cert. 1 |
| 1704 | * 10 . n-1 peer certificate |
| 1705 | * n . n+2 length of cert. 2 |
| 1706 | * n+3 . ... upper level cert, etc. |
| 1707 | */ |
| 1708 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1709 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1710 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 1711 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1712 | { |
| 1713 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1714 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1715 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1716 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 1717 | " > %" MBEDTLS_PRINTF_SIZET, |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 1718 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Hanno Becker | 91e1cc3 | 2021-04-30 05:28:49 +0100 | [diff] [blame] | 1719 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Joe Subbiani | fbeb692 | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 1722 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 1723 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 1724 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1725 | |
| 1726 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 1727 | i += n; crt = crt->next; |
| 1728 | } |
| 1729 | |
Joe Subbiani | fbeb692 | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 1730 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 1731 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 1732 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1733 | |
| 1734 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1735 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 1736 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1737 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1738 | ssl->state++; |
| 1739 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1740 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1741 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1742 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1743 | return( ret ); |
| 1744 | } |
| 1745 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1746 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1747 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1748 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 1751 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1752 | |
| 1753 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1754 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 1755 | unsigned char *crt_buf, |
| 1756 | size_t crt_buf_len ) |
| 1757 | { |
| 1758 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 1759 | |
| 1760 | if( peer_crt == NULL ) |
| 1761 | return( -1 ); |
| 1762 | |
| 1763 | if( peer_crt->raw.len != crt_buf_len ) |
| 1764 | return( -1 ); |
| 1765 | |
k-stachowiak | 95b68ef | 2019-09-16 12:21:00 +0200 | [diff] [blame] | 1766 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1767 | } |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1768 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1769 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 1770 | unsigned char *crt_buf, |
| 1771 | size_t crt_buf_len ) |
| 1772 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1773 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1774 | unsigned char const * const peer_cert_digest = |
| 1775 | ssl->session->peer_cert_digest; |
| 1776 | mbedtls_md_type_t const peer_cert_digest_type = |
| 1777 | ssl->session->peer_cert_digest_type; |
| 1778 | mbedtls_md_info_t const * const digest_info = |
| 1779 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 1780 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 1781 | size_t digest_len; |
| 1782 | |
| 1783 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 1784 | return( -1 ); |
| 1785 | |
| 1786 | digest_len = mbedtls_md_get_size( digest_info ); |
| 1787 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 1788 | return( -1 ); |
| 1789 | |
| 1790 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 1791 | if( ret != 0 ) |
| 1792 | return( -1 ); |
| 1793 | |
| 1794 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 1795 | } |
| 1796 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 1797 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1798 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1799 | /* |
| 1800 | * Once the certificate message is read, parse it into a cert chain and |
| 1801 | * perform basic checks, but leave actual verification to the caller |
| 1802 | */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1803 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 1804 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1805 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1806 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1807 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 1808 | int crt_cnt=0; |
| 1809 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1810 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 1811 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1812 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1813 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1814 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1815 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1816 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1817 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1818 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1821 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 1822 | { |
| 1823 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1824 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 1825 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 1826 | } |
| 1827 | |
| 1828 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1829 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1830 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1831 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1832 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1833 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1836 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1837 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1838 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1839 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1840 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1841 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1842 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1843 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1844 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1845 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1846 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1847 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1848 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1849 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1852 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 1853 | i += 3; |
| 1854 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1855 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1856 | while( i < ssl->in_hslen ) |
| 1857 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1858 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1859 | if ( i + 3 > ssl->in_hslen ) { |
| 1860 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1861 | mbedtls_ssl_send_alert_message( ssl, |
| 1862 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1863 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1864 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1865 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1866 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 1867 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1868 | if( ssl->in_msg[i] != 0 ) |
| 1869 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1870 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1871 | mbedtls_ssl_send_alert_message( ssl, |
| 1872 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1873 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 1874 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1877 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1878 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 1879 | | (unsigned int) ssl->in_msg[i + 2]; |
| 1880 | i += 3; |
| 1881 | |
| 1882 | if( n < 128 || i + n > ssl->in_hslen ) |
| 1883 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1884 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1885 | mbedtls_ssl_send_alert_message( ssl, |
| 1886 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1887 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1888 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1891 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1892 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 1893 | if( crt_cnt++ == 0 && |
| 1894 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 1895 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1896 | { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 1897 | /* During client-side renegotiation, check that the server's |
| 1898 | * end-CRTs hasn't changed compared to the initial handshake, |
| 1899 | * mitigating the triple handshake attack. On success, reuse |
| 1900 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1901 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 1902 | if( ssl_check_peer_crt_unchanged( ssl, |
| 1903 | &ssl->in_msg[i], |
| 1904 | n ) != 0 ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1905 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1906 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 1907 | mbedtls_ssl_send_alert_message( ssl, |
| 1908 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1909 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 1910 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1911 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1912 | |
| 1913 | /* Now we can safely free the original chain. */ |
| 1914 | ssl_clear_peer_cert( ssl->session ); |
| 1915 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1916 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 1917 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1918 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 1919 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1920 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 1921 | #else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 1922 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 1923 | * it in-place from the input buffer instead of making a copy. */ |
| 1924 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 1925 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1926 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1927 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1928 | case 0: /*ok*/ |
| 1929 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 1930 | /* Ignore certificate with an unknown algorithm: maybe a |
| 1931 | prior certificate was already trusted. */ |
| 1932 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1933 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1934 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 1935 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 1936 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1937 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1938 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 1939 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 1940 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1941 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1942 | default: |
| 1943 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 1944 | crt_parse_der_failed: |
| 1945 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 1946 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 1947 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | i += n; |
| 1951 | } |
| 1952 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1953 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1954 | return( 0 ); |
| 1955 | } |
| 1956 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1957 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1958 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 1959 | { |
| 1960 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1961 | return( -1 ); |
| 1962 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1963 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1964 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 1965 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 1966 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 1967 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 1968 | { |
| 1969 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 1970 | return( 0 ); |
| 1971 | } |
| 1972 | |
| 1973 | return( -1 ); |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1974 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1975 | } |
| 1976 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1977 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1978 | /* Check if a certificate message is expected. |
| 1979 | * Return either |
| 1980 | * - SSL_CERTIFICATE_EXPECTED, or |
| 1981 | * - SSL_CERTIFICATE_SKIP |
| 1982 | * indicating whether a Certificate message is expected or not. |
| 1983 | */ |
| 1984 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 1985 | #define SSL_CERTIFICATE_SKIP 1 |
| 1986 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 1987 | int authmode ) |
| 1988 | { |
| 1989 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1990 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1991 | |
| 1992 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 1993 | return( SSL_CERTIFICATE_SKIP ); |
| 1994 | |
| 1995 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1996 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1997 | { |
| 1998 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 1999 | return( SSL_CERTIFICATE_SKIP ); |
| 2000 | |
| 2001 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 2002 | { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2003 | ssl->session_negotiate->verify_result = |
| 2004 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 2005 | return( SSL_CERTIFICATE_SKIP ); |
| 2006 | } |
| 2007 | } |
Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 2008 | #else |
| 2009 | ((void) authmode); |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2010 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2011 | |
| 2012 | return( SSL_CERTIFICATE_EXPECTED ); |
| 2013 | } |
| 2014 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2015 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 2016 | int authmode, |
| 2017 | mbedtls_x509_crt *chain, |
| 2018 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2019 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2020 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2021 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2022 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2023 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2024 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2025 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 2026 | void *p_vrfy; |
| 2027 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2028 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 2029 | return( 0 ); |
| 2030 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2031 | if( ssl->f_vrfy != NULL ) |
| 2032 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2033 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2034 | f_vrfy = ssl->f_vrfy; |
| 2035 | p_vrfy = ssl->p_vrfy; |
| 2036 | } |
| 2037 | else |
| 2038 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2039 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2040 | f_vrfy = ssl->conf->f_vrfy; |
| 2041 | p_vrfy = ssl->conf->p_vrfy; |
| 2042 | } |
| 2043 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2044 | /* |
| 2045 | * Main check: verify certificate |
| 2046 | */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2047 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 2048 | if( ssl->conf->f_ca_cb != NULL ) |
| 2049 | { |
| 2050 | ((void) rs_ctx); |
| 2051 | have_ca_chain = 1; |
| 2052 | |
| 2053 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
Jarno Lamsa | 9822c0d | 2019-04-01 16:59:48 +0300 | [diff] [blame] | 2054 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2055 | chain, |
| 2056 | ssl->conf->f_ca_cb, |
| 2057 | ssl->conf->p_ca_cb, |
| 2058 | ssl->conf->cert_profile, |
| 2059 | ssl->hostname, |
| 2060 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2061 | f_vrfy, p_vrfy ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2062 | } |
| 2063 | else |
| 2064 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 2065 | { |
| 2066 | mbedtls_x509_crt *ca_chain; |
| 2067 | mbedtls_x509_crl *ca_crl; |
| 2068 | |
| 2069 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2070 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 2071 | { |
| 2072 | ca_chain = ssl->handshake->sni_ca_chain; |
| 2073 | ca_crl = ssl->handshake->sni_ca_crl; |
| 2074 | } |
| 2075 | else |
| 2076 | #endif |
| 2077 | { |
| 2078 | ca_chain = ssl->conf->ca_chain; |
| 2079 | ca_crl = ssl->conf->ca_crl; |
| 2080 | } |
| 2081 | |
| 2082 | if( ca_chain != NULL ) |
| 2083 | have_ca_chain = 1; |
| 2084 | |
| 2085 | ret = mbedtls_x509_crt_verify_restartable( |
| 2086 | chain, |
| 2087 | ca_chain, ca_crl, |
| 2088 | ssl->conf->cert_profile, |
| 2089 | ssl->hostname, |
| 2090 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2091 | f_vrfy, p_vrfy, rs_ctx ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2092 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2093 | |
| 2094 | if( ret != 0 ) |
| 2095 | { |
| 2096 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 2097 | } |
| 2098 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2099 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2100 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2101 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 2102 | #endif |
| 2103 | |
| 2104 | /* |
| 2105 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 2106 | */ |
| 2107 | |
| 2108 | #if defined(MBEDTLS_ECP_C) |
| 2109 | { |
| 2110 | const mbedtls_pk_context *pk = &chain->pk; |
| 2111 | |
| 2112 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 2113 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 2114 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 2115 | { |
| 2116 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 2117 | |
| 2118 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 2119 | if( ret == 0 ) |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2120 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2121 | } |
| 2122 | } |
| 2123 | #endif /* MBEDTLS_ECP_C */ |
| 2124 | |
| 2125 | if( mbedtls_ssl_check_cert_usage( chain, |
| 2126 | ciphersuite_info, |
| 2127 | ! ssl->conf->endpoint, |
| 2128 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 2129 | { |
| 2130 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 2131 | if( ret == 0 ) |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2132 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 2136 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 2137 | * with details encoded in the verification flags. All other kinds |
| 2138 | * of error codes, including those from the user provided f_vrfy |
| 2139 | * functions, are treated as fatal and lead to a failure of |
| 2140 | * ssl_parse_certificate even if verification was optional. */ |
| 2141 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 2142 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2143 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2144 | { |
| 2145 | ret = 0; |
| 2146 | } |
| 2147 | |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2148 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2149 | { |
| 2150 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 2151 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 2152 | } |
| 2153 | |
| 2154 | if( ret != 0 ) |
| 2155 | { |
| 2156 | uint8_t alert; |
| 2157 | |
| 2158 | /* The certificate may have been rejected for several reasons. |
| 2159 | Pick one and send the corresponding alert. Which alert to send |
| 2160 | may be a subject of debate in some cases. */ |
| 2161 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 2162 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 2163 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 2164 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 2165 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 2166 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2167 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 2168 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2169 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 2170 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2171 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 2172 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2173 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 2174 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2175 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 2176 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 2177 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 2178 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 2179 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 2180 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 2181 | else |
| 2182 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 2183 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2184 | alert ); |
| 2185 | } |
| 2186 | |
| 2187 | #if defined(MBEDTLS_DEBUG_C) |
| 2188 | if( ssl->session_negotiate->verify_result != 0 ) |
| 2189 | { |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2190 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2191 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2192 | } |
| 2193 | else |
| 2194 | { |
| 2195 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 2196 | } |
| 2197 | #endif /* MBEDTLS_DEBUG_C */ |
| 2198 | |
| 2199 | return( ret ); |
| 2200 | } |
| 2201 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2202 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2203 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 2204 | unsigned char *start, size_t len ) |
| 2205 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2206 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2207 | /* Remember digest of the peer's end-CRT. */ |
| 2208 | ssl->session_negotiate->peer_cert_digest = |
| 2209 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 2210 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 2211 | { |
| 2212 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
irwir | 40883e9 | 2019-09-21 17:55:33 +0300 | [diff] [blame] | 2213 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2214 | mbedtls_ssl_send_alert_message( ssl, |
| 2215 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2216 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 2217 | |
| 2218 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2219 | } |
| 2220 | |
| 2221 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 2222 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 2223 | start, len, |
| 2224 | ssl->session_negotiate->peer_cert_digest ); |
| 2225 | |
| 2226 | ssl->session_negotiate->peer_cert_digest_type = |
| 2227 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 2228 | ssl->session_negotiate->peer_cert_digest_len = |
| 2229 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 2230 | |
| 2231 | return( ret ); |
| 2232 | } |
| 2233 | |
| 2234 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 2235 | unsigned char *start, size_t len ) |
| 2236 | { |
| 2237 | unsigned char *end = start + len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2238 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2239 | |
| 2240 | /* Make a copy of the peer's raw public key. */ |
| 2241 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 2242 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 2243 | &ssl->handshake->peer_pubkey ); |
| 2244 | if( ret != 0 ) |
| 2245 | { |
| 2246 | /* We should have parsed the public key before. */ |
| 2247 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2248 | } |
| 2249 | |
| 2250 | return( 0 ); |
| 2251 | } |
| 2252 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2253 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2254 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 2255 | { |
| 2256 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2257 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2258 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2259 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 2260 | ? ssl->handshake->sni_authmode |
| 2261 | : ssl->conf->authmode; |
| 2262 | #else |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 2263 | const int authmode = ssl->conf->authmode; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2264 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2265 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2266 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2267 | |
| 2268 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 2269 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2270 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 2271 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2272 | { |
| 2273 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2274 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2275 | } |
| 2276 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2277 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2278 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2279 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2280 | { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2281 | chain = ssl->handshake->ecrs_peer_cert; |
| 2282 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2283 | goto crt_verify; |
| 2284 | } |
| 2285 | #endif |
| 2286 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 2287 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2288 | { |
| 2289 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 2290 | let it decide whether to alert. */ |
| 2291 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2292 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2293 | } |
| 2294 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2295 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2296 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 2297 | { |
| 2298 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2299 | |
irwir | d3085ab | 2020-04-21 22:26:05 +0300 | [diff] [blame] | 2300 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2301 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2302 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2303 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2304 | } |
| 2305 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2306 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2307 | /* Clear existing peer CRT structure in case we tried to |
| 2308 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2309 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2310 | |
| 2311 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 2312 | if( chain == NULL ) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2313 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2314 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2315 | sizeof( mbedtls_x509_crt ) ) ); |
| 2316 | mbedtls_ssl_send_alert_message( ssl, |
| 2317 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2318 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2319 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2320 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2321 | goto exit; |
| 2322 | } |
| 2323 | mbedtls_x509_crt_init( chain ); |
| 2324 | |
| 2325 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2326 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2327 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2328 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2329 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2330 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2331 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2332 | |
| 2333 | crt_verify: |
| 2334 | if( ssl->handshake->ecrs_enabled) |
| 2335 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 2336 | #endif |
| 2337 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2338 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2339 | chain, rs_ctx ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2340 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2341 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2342 | |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2343 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2344 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2345 | unsigned char *crt_start, *pk_start; |
| 2346 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2347 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2348 | /* We parse the CRT chain without copying, so |
| 2349 | * these pointers point into the input buffer, |
| 2350 | * and are hence still valid after freeing the |
| 2351 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2352 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2353 | crt_start = chain->raw.p; |
| 2354 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2355 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2356 | pk_start = chain->pk_raw.p; |
| 2357 | pk_len = chain->pk_raw.len; |
| 2358 | |
| 2359 | /* Free the CRT structures before computing |
| 2360 | * digest and copying the peer's public key. */ |
| 2361 | mbedtls_x509_crt_free( chain ); |
| 2362 | mbedtls_free( chain ); |
| 2363 | chain = NULL; |
| 2364 | |
| 2365 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2366 | if( ret != 0 ) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2367 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2368 | |
| 2369 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 2370 | if( ret != 0 ) |
| 2371 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2372 | } |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2373 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2374 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2375 | ssl->session_negotiate->peer_cert = chain; |
| 2376 | chain = NULL; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2377 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2378 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2379 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2380 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2381 | exit: |
| 2382 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2383 | if( ret == 0 ) |
| 2384 | ssl->state++; |
| 2385 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2386 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2387 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 2388 | { |
| 2389 | ssl->handshake->ecrs_peer_cert = chain; |
| 2390 | chain = NULL; |
| 2391 | } |
| 2392 | #endif |
| 2393 | |
| 2394 | if( chain != NULL ) |
| 2395 | { |
| 2396 | mbedtls_x509_crt_free( chain ); |
| 2397 | mbedtls_free( chain ); |
| 2398 | } |
| 2399 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2400 | return( ret ); |
| 2401 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2402 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2404 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 2405 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2406 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 2407 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2409 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2410 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2411 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2412 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 2413 | else |
| 2414 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2415 | #if defined(MBEDTLS_SHA256_C) |
| 2416 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2417 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2418 | else |
| 2419 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2420 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2421 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2422 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2423 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2424 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2425 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2427 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2428 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2429 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2430 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2431 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 2432 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2433 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 2434 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2435 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2436 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2437 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2438 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2439 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 2440 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2441 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2442 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2443 | mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2444 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2445 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2446 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2447 | } |
| 2448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2449 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2450 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2451 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2452 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2453 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2454 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2455 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 2456 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2457 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2458 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2459 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2460 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2461 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2462 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2463 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2464 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2465 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2466 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2467 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2470 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2471 | #if defined(MBEDTLS_SHA256_C) |
| 2472 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2473 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2474 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2475 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2476 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 2477 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2478 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2479 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2480 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2481 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2482 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2483 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2484 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2485 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2486 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2487 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2488 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2489 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2490 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2491 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2492 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2493 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2494 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2495 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2496 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2497 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2498 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2499 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2500 | { |
| 2501 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2502 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2503 | unsigned char padbuf[32]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2504 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2505 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 2506 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2507 | psa_status_t status; |
| 2508 | #else |
| 2509 | mbedtls_sha256_context sha256; |
| 2510 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2511 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2512 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2513 | if( !session ) |
| 2514 | session = ssl->session; |
| 2515 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2516 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 2517 | ? "client finished" |
| 2518 | : "server finished"; |
| 2519 | |
| 2520 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2521 | sha256_psa = psa_hash_operation_init(); |
| 2522 | |
| 2523 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 2524 | |
| 2525 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 2526 | if( status != PSA_SUCCESS ) |
| 2527 | { |
| 2528 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 2529 | return; |
| 2530 | } |
| 2531 | |
| 2532 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 2533 | if( status != PSA_SUCCESS ) |
| 2534 | { |
| 2535 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 2536 | return; |
| 2537 | } |
| 2538 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 2539 | #else |
| 2540 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2541 | mbedtls_sha256_init( &sha256 ); |
| 2542 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2543 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2544 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2545 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2546 | |
| 2547 | /* |
| 2548 | * TLSv1.2: |
| 2549 | * hash = PRF( master, finished_label, |
| 2550 | * Hash( handshake ) )[0.11] |
| 2551 | */ |
| 2552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2553 | #if !defined(MBEDTLS_SHA256_ALT) |
| 2554 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2555 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 2556 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2557 | |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2558 | mbedtls_sha256_finish( &sha256, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2559 | mbedtls_sha256_free( &sha256 ); |
| 2560 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2561 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2562 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2563 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2564 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2565 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2566 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2567 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2569 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2570 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2571 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2572 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2573 | #if defined(MBEDTLS_SHA384_C) |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 2574 | |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2575 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2576 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2577 | { |
| 2578 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2579 | const char *sender; |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 2580 | unsigned char padbuf[48]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2581 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2582 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 2583 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2584 | psa_status_t status; |
| 2585 | #else |
| 2586 | mbedtls_sha512_context sha512; |
| 2587 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2589 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2590 | if( !session ) |
| 2591 | session = ssl->session; |
| 2592 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2593 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 2594 | ? "client finished" |
| 2595 | : "server finished"; |
| 2596 | |
| 2597 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2598 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2599 | |
| 2600 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 2601 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2602 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2603 | if( status != PSA_SUCCESS ) |
| 2604 | { |
| 2605 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 2606 | return; |
| 2607 | } |
| 2608 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2609 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2610 | if( status != PSA_SUCCESS ) |
| 2611 | { |
| 2612 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 2613 | return; |
| 2614 | } |
| 2615 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 2616 | #else |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2617 | mbedtls_sha512_init( &sha512 ); |
| 2618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2619 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2620 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2621 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2622 | |
| 2623 | /* |
| 2624 | * TLSv1.2: |
| 2625 | * hash = PRF( master, finished_label, |
| 2626 | * Hash( handshake ) )[0.11] |
| 2627 | */ |
| 2628 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2629 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2630 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 2631 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 2632 | #endif |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2633 | mbedtls_sha512_finish( &sha512, padbuf ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2634 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2635 | mbedtls_sha512_free( &sha512 ); |
| 2636 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2637 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2638 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2639 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2641 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2642 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2643 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2645 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2646 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2647 | #endif /* MBEDTLS_SHA384_C */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2648 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2649 | |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 2650 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2651 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2652 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2653 | |
| 2654 | /* |
| 2655 | * Free our handshake params |
| 2656 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 2657 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2658 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2659 | ssl->handshake = NULL; |
| 2660 | |
| 2661 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2662 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2663 | */ |
| 2664 | if( ssl->transform ) |
| 2665 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2666 | mbedtls_ssl_transform_free( ssl->transform ); |
| 2667 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2668 | } |
| 2669 | ssl->transform = ssl->transform_negotiate; |
| 2670 | ssl->transform_negotiate = NULL; |
| 2671 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2672 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2673 | } |
| 2674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2675 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2676 | { |
| 2677 | int resume = ssl->handshake->resume; |
| 2678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2679 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2680 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2681 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2682 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2683 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2684 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2685 | ssl->renego_records_seen = 0; |
| 2686 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2687 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2688 | |
| 2689 | /* |
| 2690 | * Free the previous session and switch in the current one |
| 2691 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2692 | if( ssl->session ) |
| 2693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2694 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 2695 | /* RFC 7366 3.1: keep the EtM state */ |
| 2696 | ssl->session_negotiate->encrypt_then_mac = |
| 2697 | ssl->session->encrypt_then_mac; |
| 2698 | #endif |
| 2699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2700 | mbedtls_ssl_session_free( ssl->session ); |
| 2701 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2702 | } |
| 2703 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2704 | ssl->session_negotiate = NULL; |
| 2705 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2706 | /* |
| 2707 | * Add cache entry |
| 2708 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2709 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2710 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 2711 | resume == 0 ) |
| 2712 | { |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 2713 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 2714 | ssl->session->id, |
| 2715 | ssl->session->id_len, |
| 2716 | ssl->session ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2717 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 2718 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2720 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2721 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2722 | ssl->handshake->flight != NULL ) |
| 2723 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2724 | /* Cancel handshake timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 2725 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2726 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2727 | /* Keep last flight around in case we need to resend it: |
| 2728 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2729 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2730 | } |
| 2731 | else |
| 2732 | #endif |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 2733 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2734 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2735 | ssl->state++; |
| 2736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2737 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2740 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2741 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2742 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2743 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2744 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2745 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 2746 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 2747 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2748 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2749 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 2750 | /* |
| 2751 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 2752 | * may define some other value. Currently (early 2016), no defined |
| 2753 | * ciphersuite does this (and this is unlikely to change as activity has |
| 2754 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 2755 | */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2756 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2757 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2758 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2759 | ssl->verify_data_len = hash_len; |
| 2760 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2761 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2762 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2763 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2764 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2765 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2766 | |
| 2767 | /* |
| 2768 | * In case of session resuming, invert the client and server |
| 2769 | * ChangeCipherSpec messages order. |
| 2770 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2771 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2772 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2773 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2774 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2775 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2776 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2777 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2778 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2779 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2780 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2781 | } |
| 2782 | else |
| 2783 | ssl->state++; |
| 2784 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2785 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2786 | * Switch to our negotiated transform and session parameters for outbound |
| 2787 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2788 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2789 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2790 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2791 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2792 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2793 | { |
| 2794 | unsigned char i; |
| 2795 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2796 | /* Remember current epoch settings for resending */ |
| 2797 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 2798 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
Jerry Yu | d96a5c2 | 2021-09-29 17:46:51 +0800 | [diff] [blame] | 2799 | sizeof( ssl->handshake->alt_out_ctr ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2800 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2801 | /* Set sequence_number to zero */ |
Jerry Yu | fd320e9 | 2021-10-08 21:52:41 +0800 | [diff] [blame] | 2802 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 2803 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2804 | |
| 2805 | /* Increment epoch */ |
| 2806 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 2807 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2808 | break; |
| 2809 | |
| 2810 | /* The loop goes to its end iff the counter is wrapping */ |
| 2811 | if( i == 0 ) |
| 2812 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2813 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 2814 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2818 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Jerry Yu | fd320e9 | 2021-10-08 21:52:41 +0800 | [diff] [blame] | 2819 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2820 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2821 | ssl->transform_out = ssl->transform_negotiate; |
| 2822 | ssl->session_out = ssl->session_negotiate; |
| 2823 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2824 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2825 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2826 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2827 | #endif |
| 2828 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2829 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2830 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2831 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2832 | return( ret ); |
| 2833 | } |
| 2834 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2835 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2836 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2837 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2838 | { |
| 2839 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 2840 | return( ret ); |
| 2841 | } |
| 2842 | #endif |
| 2843 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2844 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2845 | |
| 2846 | return( 0 ); |
| 2847 | } |
| 2848 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2849 | #define SSL_MAX_HASH_LEN 12 |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2850 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2851 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2852 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2853 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2854 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2855 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2857 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2858 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2859 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2860 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2861 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2862 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2863 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2864 | return( ret ); |
| 2865 | } |
| 2866 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2867 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2868 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2869 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2870 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2871 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2872 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2875 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2876 | |
Hanno Becker | a0ca87e | 2021-06-24 10:27:37 +0100 | [diff] [blame] | 2877 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 2878 | { |
| 2879 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2880 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 2881 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 2882 | } |
| 2883 | |
| 2884 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2885 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2886 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2887 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2888 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | a0ca87e | 2021-06-24 10:27:37 +0100 | [diff] [blame] | 2889 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2892 | if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
Manuel Pégourié-Gonnard | 4abc327 | 2014-09-10 12:02:46 +0000 | [diff] [blame] | 2893 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2894 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2895 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2896 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 2897 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 2898 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2901 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2902 | ssl->verify_data_len = hash_len; |
| 2903 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2904 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2905 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2906 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2907 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2908 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2909 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2910 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2911 | #endif |
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 | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2913 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2914 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2915 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2916 | } |
| 2917 | else |
| 2918 | ssl->state++; |
| 2919 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2920 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2921 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2922 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2923 | #endif |
| 2924 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2925 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2926 | |
| 2927 | return( 0 ); |
| 2928 | } |
| 2929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2930 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2931 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2932 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2933 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2934 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2935 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2936 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2937 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 2938 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 2939 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2940 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2941 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2942 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2943 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2944 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2945 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2946 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 2947 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2948 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2949 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2950 | mbedtls_sha512_starts( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2951 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2952 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2953 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2954 | |
| 2955 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2956 | |
| 2957 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2958 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2959 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 2960 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2961 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2962 | #if defined(MBEDTLS_DHM_C) |
| 2963 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2964 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2965 | #if defined(MBEDTLS_ECDH_C) |
| 2966 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2967 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2968 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 2969 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 2970 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2971 | handshake->ecjpake_cache = NULL; |
| 2972 | handshake->ecjpake_cache_len = 0; |
| 2973 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 2974 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2975 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2976 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 2977 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 2978 | #endif |
| 2979 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2980 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2981 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 2982 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 2983 | |
| 2984 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 2985 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2986 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 2987 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2988 | } |
| 2989 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2990 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2991 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2992 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 2993 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2994 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 2995 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 2996 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 2997 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2998 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 2999 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 3000 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3003 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3004 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3005 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3006 | } |
| 3007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3008 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3009 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3010 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3011 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3012 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3013 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3014 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3015 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3016 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3017 | |
| 3018 | /* |
| 3019 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 3020 | * Now allocate missing structures. |
| 3021 | */ |
| 3022 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3023 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3024 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3025 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3026 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3027 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3028 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3029 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3030 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3031 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 3032 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3033 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3034 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3035 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3036 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3037 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 3038 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3039 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 3040 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3041 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3042 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3043 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3044 | if( ssl->handshake == NULL || |
| 3045 | ssl->transform_negotiate == NULL || |
| 3046 | ssl->session_negotiate == NULL ) |
| 3047 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3048 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3049 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3050 | mbedtls_free( ssl->handshake ); |
| 3051 | mbedtls_free( ssl->transform_negotiate ); |
| 3052 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3053 | |
| 3054 | ssl->handshake = NULL; |
| 3055 | ssl->transform_negotiate = NULL; |
| 3056 | ssl->session_negotiate = NULL; |
| 3057 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3058 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3061 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3062 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3063 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 3064 | ssl_handshake_params_init( ssl->handshake ); |
| 3065 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3066 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3067 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3068 | { |
| 3069 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3070 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3071 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 3072 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 3073 | else |
| 3074 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3075 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3076 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3077 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3078 | #endif |
| 3079 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3080 | /* |
| 3081 | * curve_list is translated to IANA TLS group identifiers here because |
| 3082 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 3083 | * any error codes. |
| 3084 | */ |
| 3085 | #if defined(MBEDTLS_ECP_C) |
| 3086 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3087 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 3088 | if ( ssl->conf->curve_list != NULL ) |
| 3089 | { |
| 3090 | size_t length; |
| 3091 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 3092 | |
| 3093 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 3094 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 3095 | |
| 3096 | /* Leave room for zero termination */ |
| 3097 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 3098 | if ( group_list == NULL ) |
| 3099 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 3100 | |
| 3101 | for( size_t i = 0; i < length; i++ ) |
| 3102 | { |
| 3103 | const mbedtls_ecp_curve_info *info = |
| 3104 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 3105 | if ( info == NULL ) |
| 3106 | { |
| 3107 | mbedtls_free( group_list ); |
| 3108 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 3109 | } |
| 3110 | group_list[i] = info->tls_id; |
| 3111 | } |
| 3112 | |
| 3113 | group_list[length] = 0; |
| 3114 | |
| 3115 | ssl->handshake->group_list = group_list; |
| 3116 | ssl->handshake->group_list_heap_allocated = 1; |
| 3117 | } |
| 3118 | else |
| 3119 | { |
| 3120 | ssl->handshake->group_list = ssl->conf->group_list; |
| 3121 | ssl->handshake->group_list_heap_allocated = 0; |
| 3122 | } |
| 3123 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3124 | #endif /* MBEDTLS_ECP_C */ |
| 3125 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3126 | return( 0 ); |
| 3127 | } |
| 3128 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3129 | #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] | 3130 | /* Dummy cookie callbacks for defaults */ |
| 3131 | static int ssl_cookie_write_dummy( void *ctx, |
| 3132 | unsigned char **p, unsigned char *end, |
| 3133 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3134 | { |
| 3135 | ((void) ctx); |
| 3136 | ((void) p); |
| 3137 | ((void) end); |
| 3138 | ((void) cli_id); |
| 3139 | ((void) cli_id_len); |
| 3140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3141 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | static int ssl_cookie_check_dummy( void *ctx, |
| 3145 | const unsigned char *cookie, size_t cookie_len, |
| 3146 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3147 | { |
| 3148 | ((void) ctx); |
| 3149 | ((void) cookie); |
| 3150 | ((void) cookie_len); |
| 3151 | ((void) cli_id); |
| 3152 | ((void) cli_id_len); |
| 3153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3154 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3155 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3156 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3157 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3158 | /* |
| 3159 | * Initialize an SSL context |
| 3160 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3161 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 3162 | { |
| 3163 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 3164 | } |
| 3165 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3166 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 3167 | { |
| 3168 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 3169 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
| 3170 | { |
| 3171 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3172 | { |
| 3173 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported" ) ); |
| 3174 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3175 | } |
| 3176 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 3177 | return( 0 ); |
| 3178 | } |
| 3179 | #endif |
| 3180 | |
| 3181 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3182 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3183 | { |
| 3184 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 3185 | return( 0 ); |
| 3186 | } |
| 3187 | #endif |
| 3188 | |
| 3189 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 3190 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( ssl->conf ) ) |
| 3191 | { |
| 3192 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) ); |
| 3193 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3194 | } |
| 3195 | #endif |
| 3196 | |
| 3197 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 3198 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 3199 | } |
| 3200 | |
| 3201 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 3202 | { |
| 3203 | int ret; |
| 3204 | ret = ssl_conf_version_check( ssl ); |
| 3205 | if( ret != 0 ) |
| 3206 | return( ret ); |
| 3207 | |
| 3208 | /* Space for further checks */ |
| 3209 | |
| 3210 | return( 0 ); |
| 3211 | } |
| 3212 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3213 | /* |
| 3214 | * Setup an SSL context |
| 3215 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 3216 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3217 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 3218 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3219 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3220 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3221 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3222 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3223 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3224 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3225 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3226 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 3227 | return( ret ); |
| 3228 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3229 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3230 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3231 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 3232 | |
| 3233 | /* Set to NULL in case of an error condition */ |
| 3234 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3235 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3236 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3237 | ssl->in_buf_len = in_buf_len; |
| 3238 | #endif |
| 3239 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3240 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3241 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3242 | 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] | 3243 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3244 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3245 | } |
| 3246 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3247 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3248 | ssl->out_buf_len = out_buf_len; |
| 3249 | #endif |
| 3250 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3251 | if( ssl->out_buf == NULL ) |
| 3252 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3253 | 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] | 3254 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3255 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3258 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3259 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3260 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 3261 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3262 | #endif |
| 3263 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3264 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3265 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3266 | |
| 3267 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3268 | |
| 3269 | error: |
| 3270 | mbedtls_free( ssl->in_buf ); |
| 3271 | mbedtls_free( ssl->out_buf ); |
| 3272 | |
| 3273 | ssl->conf = NULL; |
| 3274 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3275 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3276 | ssl->in_buf_len = 0; |
| 3277 | ssl->out_buf_len = 0; |
| 3278 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3279 | ssl->in_buf = NULL; |
| 3280 | ssl->out_buf = NULL; |
| 3281 | |
| 3282 | ssl->in_hdr = NULL; |
| 3283 | ssl->in_ctr = NULL; |
| 3284 | ssl->in_len = NULL; |
| 3285 | ssl->in_iv = NULL; |
| 3286 | ssl->in_msg = NULL; |
| 3287 | |
| 3288 | ssl->out_hdr = NULL; |
| 3289 | ssl->out_ctr = NULL; |
| 3290 | ssl->out_len = NULL; |
| 3291 | ssl->out_iv = NULL; |
| 3292 | ssl->out_msg = NULL; |
| 3293 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3294 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3298 | * Reset an initialized and used SSL context for re-use while retaining |
| 3299 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3300 | * |
| 3301 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 3302 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3303 | */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3304 | static void ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 3305 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3306 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3307 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3308 | size_t in_buf_len = ssl->in_buf_len; |
| 3309 | size_t out_buf_len = ssl->out_buf_len; |
| 3310 | #else |
| 3311 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3312 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 3313 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3314 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3315 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 3316 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 3317 | #endif |
| 3318 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3319 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3320 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3321 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3322 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 3323 | |
| 3324 | /* Reset incoming message parsing */ |
| 3325 | ssl->in_offt = NULL; |
| 3326 | ssl->nb_zero = 0; |
| 3327 | ssl->in_msgtype = 0; |
| 3328 | ssl->in_msglen = 0; |
| 3329 | ssl->in_hslen = 0; |
| 3330 | ssl->keep_current_message = 0; |
| 3331 | ssl->transform_in = NULL; |
| 3332 | |
| 3333 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3334 | ssl->next_record_offset = 0; |
| 3335 | ssl->in_epoch = 0; |
| 3336 | #endif |
| 3337 | |
| 3338 | /* Keep current datagram if partial == 1 */ |
| 3339 | if( partial == 0 ) |
| 3340 | { |
| 3341 | ssl->in_left = 0; |
| 3342 | memset( ssl->in_buf, 0, in_buf_len ); |
| 3343 | } |
| 3344 | |
| 3345 | /* Reset outgoing message writing */ |
| 3346 | ssl->out_msgtype = 0; |
| 3347 | ssl->out_msglen = 0; |
| 3348 | ssl->out_left = 0; |
| 3349 | memset( ssl->out_buf, 0, out_buf_len ); |
| 3350 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 3351 | ssl->transform_out = NULL; |
| 3352 | |
| 3353 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3354 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 3355 | #endif |
| 3356 | |
| 3357 | if( ssl->transform ) |
| 3358 | { |
| 3359 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3360 | mbedtls_free( ssl->transform ); |
| 3361 | ssl->transform = NULL; |
| 3362 | } |
| 3363 | } |
| 3364 | |
| 3365 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 3366 | { |
| 3367 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3368 | |
| 3369 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3370 | |
| 3371 | ssl_session_reset_msg_layer( ssl, partial ); |
| 3372 | |
| 3373 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3374 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3375 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3376 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3377 | |
| 3378 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3379 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 3380 | 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] | 3381 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3382 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3383 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3384 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 3385 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3386 | if( ssl->session ) |
| 3387 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3388 | mbedtls_ssl_session_free( ssl->session ); |
| 3389 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3390 | ssl->session = NULL; |
| 3391 | } |
| 3392 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3393 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3394 | ssl->alpn_chosen = NULL; |
| 3395 | #endif |
| 3396 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3397 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3398 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3399 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3400 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3401 | { |
| 3402 | mbedtls_free( ssl->cli_id ); |
| 3403 | ssl->cli_id = NULL; |
| 3404 | ssl->cli_id_len = 0; |
| 3405 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 3406 | #endif |
| 3407 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3408 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3409 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3410 | |
| 3411 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 3414 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3415 | * Reset an initialized and used SSL context for re-use while retaining |
| 3416 | * all application-set variables, function pointers and data. |
| 3417 | */ |
| 3418 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 3419 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3420 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3424 | * SSL set accessors |
| 3425 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3426 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3427 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3428 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 3431 | 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] | 3432 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3433 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 3434 | } |
| 3435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3436 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3437 | 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] | 3438 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3439 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3440 | } |
| 3441 | #endif |
| 3442 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3443 | 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] | 3444 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3445 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3446 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3447 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3448 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3449 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 3450 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 3451 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3452 | { |
| 3453 | ssl->disable_datagram_packing = !allow_packing; |
| 3454 | } |
| 3455 | |
| 3456 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 3457 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3458 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3459 | conf->hs_timeout_min = min; |
| 3460 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3461 | } |
| 3462 | #endif |
| 3463 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3464 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3465 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3466 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3469 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3470 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 3471 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3472 | void *p_vrfy ) |
| 3473 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3474 | conf->f_vrfy = f_vrfy; |
| 3475 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3476 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3477 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3478 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3479 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 3480 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3481 | void *p_rng ) |
| 3482 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3483 | conf->f_rng = f_rng; |
| 3484 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3487 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 3488 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3489 | void *p_dbg ) |
| 3490 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3491 | conf->f_dbg = f_dbg; |
| 3492 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3495 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3496 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 3497 | mbedtls_ssl_send_t *f_send, |
| 3498 | mbedtls_ssl_recv_t *f_recv, |
| 3499 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3500 | { |
| 3501 | ssl->p_bio = p_bio; |
| 3502 | ssl->f_send = f_send; |
| 3503 | ssl->f_recv = f_recv; |
| 3504 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 3505 | } |
| 3506 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 3507 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3508 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 3509 | { |
| 3510 | ssl->mtu = mtu; |
| 3511 | } |
| 3512 | #endif |
| 3513 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3514 | 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] | 3515 | { |
| 3516 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3517 | } |
| 3518 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3519 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 3520 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 3521 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 3522 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3523 | { |
| 3524 | ssl->p_timer = p_timer; |
| 3525 | ssl->f_set_timer = f_set_timer; |
| 3526 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3527 | |
| 3528 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3529 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3530 | } |
| 3531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3532 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3533 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 3534 | void *p_cache, |
| 3535 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 3536 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3537 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 3538 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3539 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3540 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3541 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3542 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3543 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3544 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3545 | 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] | 3546 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3547 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3548 | |
| 3549 | if( ssl == NULL || |
| 3550 | session == NULL || |
| 3551 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3552 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3553 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3554 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3555 | } |
| 3556 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3557 | if( ssl->handshake->resume == 1 ) |
| 3558 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3559 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 3560 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 3561 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3562 | return( ret ); |
| 3563 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3564 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3565 | |
| 3566 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3567 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3568 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3569 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 3570 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 3571 | const int *ciphersuites ) |
| 3572 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 3573 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 3576 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 3577 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 3578 | const int kex_modes ) |
| 3579 | { |
Jerry Yu | d85a52c | 2021-08-24 10:55:07 +0800 | [diff] [blame] | 3580 | conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_ALL; |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 3581 | } |
| 3582 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 3583 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3584 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 3585 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 3586 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 3587 | { |
| 3588 | conf->cert_profile = profile; |
| 3589 | } |
| 3590 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3591 | /* Append a new keycert entry to a (possibly empty) list */ |
| 3592 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 3593 | mbedtls_x509_crt *cert, |
| 3594 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3595 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3596 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3597 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3598 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 3599 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3600 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3601 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3602 | new_cert->cert = cert; |
| 3603 | new_cert->key = key; |
| 3604 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3605 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3606 | /* Update head is the list was null, else add to the end */ |
| 3607 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 3608 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3609 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 3610 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3611 | else |
| 3612 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3613 | mbedtls_ssl_key_cert *cur = *head; |
| 3614 | while( cur->next != NULL ) |
| 3615 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3616 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3617 | } |
| 3618 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3619 | return( 0 ); |
| 3620 | } |
| 3621 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3622 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3623 | mbedtls_x509_crt *own_cert, |
| 3624 | mbedtls_pk_context *pk_key ) |
| 3625 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 3626 | 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] | 3627 | } |
| 3628 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3629 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3630 | mbedtls_x509_crt *ca_chain, |
| 3631 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3632 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3633 | conf->ca_chain = ca_chain; |
| 3634 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3635 | |
| 3636 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3637 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 3638 | * cannot be used together. */ |
| 3639 | conf->f_ca_cb = NULL; |
| 3640 | conf->p_ca_cb = NULL; |
| 3641 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3642 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3643 | |
| 3644 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3645 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 3646 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3647 | void *p_ca_cb ) |
| 3648 | { |
| 3649 | conf->f_ca_cb = f_ca_cb; |
| 3650 | conf->p_ca_cb = p_ca_cb; |
| 3651 | |
| 3652 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 3653 | * cannot be used together. */ |
| 3654 | conf->ca_chain = NULL; |
| 3655 | conf->ca_crl = NULL; |
| 3656 | } |
| 3657 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3658 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 3659 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3660 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3661 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 3662 | mbedtls_x509_crt *own_cert, |
| 3663 | mbedtls_pk_context *pk_key ) |
| 3664 | { |
| 3665 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 3666 | own_cert, pk_key ) ); |
| 3667 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 3668 | |
| 3669 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 3670 | mbedtls_x509_crt *ca_chain, |
| 3671 | mbedtls_x509_crl *ca_crl ) |
| 3672 | { |
| 3673 | ssl->handshake->sni_ca_chain = ca_chain; |
| 3674 | ssl->handshake->sni_ca_crl = ca_crl; |
| 3675 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3676 | |
| 3677 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 3678 | int authmode ) |
| 3679 | { |
| 3680 | ssl->handshake->sni_authmode = authmode; |
| 3681 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3682 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 3683 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 3684 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3685 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 3686 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3687 | void *p_vrfy ) |
| 3688 | { |
| 3689 | ssl->f_vrfy = f_vrfy; |
| 3690 | ssl->p_vrfy = p_vrfy; |
| 3691 | } |
| 3692 | #endif |
| 3693 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3694 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3695 | /* |
| 3696 | * Set EC J-PAKE password for current handshake |
| 3697 | */ |
| 3698 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 3699 | const unsigned char *pw, |
| 3700 | size_t pw_len ) |
| 3701 | { |
| 3702 | mbedtls_ecjpake_role role; |
| 3703 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 3704 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3705 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3706 | |
| 3707 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 3708 | role = MBEDTLS_ECJPAKE_SERVER; |
| 3709 | else |
| 3710 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 3711 | |
| 3712 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 3713 | role, |
| 3714 | MBEDTLS_MD_SHA256, |
| 3715 | MBEDTLS_ECP_DP_SECP256R1, |
| 3716 | pw, pw_len ) ); |
| 3717 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3718 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3719 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3720 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3721 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3722 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
| 3723 | { |
| 3724 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3725 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 3726 | return( 1 ); |
| 3727 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3728 | |
| 3729 | if( conf->psk != NULL ) |
| 3730 | return( 1 ); |
| 3731 | |
| 3732 | return( 0 ); |
| 3733 | } |
| 3734 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3735 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 3736 | { |
| 3737 | /* Remove reference to existing PSK, if any. */ |
| 3738 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3739 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3740 | { |
| 3741 | /* The maintenance of the PSK key slot is the |
| 3742 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3743 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3744 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 3745 | /* This and the following branch should never |
| 3746 | * be taken simultaenously as we maintain the |
| 3747 | * invariant that raw and opaque PSKs are never |
| 3748 | * configured simultaneously. As a safeguard, |
| 3749 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3750 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3751 | if( conf->psk != NULL ) |
| 3752 | { |
| 3753 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 3754 | |
| 3755 | mbedtls_free( conf->psk ); |
| 3756 | conf->psk = NULL; |
| 3757 | conf->psk_len = 0; |
| 3758 | } |
| 3759 | |
| 3760 | /* Remove reference to PSK identity, if any. */ |
| 3761 | if( conf->psk_identity != NULL ) |
| 3762 | { |
| 3763 | mbedtls_free( conf->psk_identity ); |
| 3764 | conf->psk_identity = NULL; |
| 3765 | conf->psk_identity_len = 0; |
| 3766 | } |
| 3767 | } |
| 3768 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3769 | /* This function assumes that PSK identity in the SSL config is unset. |
| 3770 | * It checks that the provided identity is well-formed and attempts |
| 3771 | * to make a copy of it in the SSL config. |
| 3772 | * On failure, the PSK identity in the config remains unset. */ |
| 3773 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 3774 | unsigned char const *psk_identity, |
| 3775 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3776 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3777 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3778 | if( psk_identity == NULL || |
| 3779 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3780 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3781 | { |
| 3782 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3783 | } |
| 3784 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3785 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 3786 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3787 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3788 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 3789 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 3790 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 3791 | |
| 3792 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3793 | } |
| 3794 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3795 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 3796 | const unsigned char *psk, size_t psk_len, |
| 3797 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 3798 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3799 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3800 | |
| 3801 | /* We currently only support one PSK, raw or opaque. */ |
| 3802 | if( ssl_conf_psk_is_configured( conf ) ) |
| 3803 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3804 | |
| 3805 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 3806 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3807 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 3808 | if( psk_len == 0 ) |
| 3809 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3810 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 3811 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3812 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3813 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 3814 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 3815 | conf->psk_len = psk_len; |
| 3816 | memcpy( conf->psk, psk, conf->psk_len ); |
| 3817 | |
| 3818 | /* Check and set PSK Identity */ |
| 3819 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 3820 | if( ret != 0 ) |
| 3821 | ssl_conf_remove_psk( conf ); |
| 3822 | |
| 3823 | return( ret ); |
| 3824 | } |
| 3825 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3826 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 3827 | { |
| 3828 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3829 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3830 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3831 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3832 | } |
| 3833 | else |
| 3834 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3835 | if( ssl->handshake->psk != NULL ) |
| 3836 | { |
| 3837 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 3838 | ssl->handshake->psk_len ); |
| 3839 | mbedtls_free( ssl->handshake->psk ); |
| 3840 | ssl->handshake->psk_len = 0; |
| 3841 | } |
| 3842 | } |
| 3843 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3844 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 3845 | const unsigned char *psk, size_t psk_len ) |
| 3846 | { |
| 3847 | if( psk == NULL || ssl->handshake == NULL ) |
| 3848 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3849 | |
| 3850 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 3851 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3852 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3853 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3854 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3855 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3856 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3857 | |
| 3858 | ssl->handshake->psk_len = psk_len; |
| 3859 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 3860 | |
| 3861 | return( 0 ); |
| 3862 | } |
| 3863 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3864 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3865 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3866 | psa_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3867 | const unsigned char *psk_identity, |
| 3868 | size_t psk_identity_len ) |
| 3869 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3870 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3871 | |
| 3872 | /* We currently only support one PSK, raw or opaque. */ |
| 3873 | if( ssl_conf_psk_is_configured( conf ) ) |
| 3874 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3875 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3876 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3877 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3878 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3879 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3880 | |
| 3881 | /* Check and set PSK Identity */ |
| 3882 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 3883 | psk_identity_len ); |
| 3884 | if( ret != 0 ) |
| 3885 | ssl_conf_remove_psk( conf ); |
| 3886 | |
| 3887 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3891 | psa_key_id_t psk ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3892 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3893 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
Ronald Cron | c26f8d4 | 2020-09-01 10:51:51 +0200 | [diff] [blame] | 3894 | ( ssl->handshake == NULL ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3895 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3896 | |
| 3897 | ssl_remove_psk( ssl ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3898 | ssl->handshake->psk_opaque = psk; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3899 | return( 0 ); |
| 3900 | } |
| 3901 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3902 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3903 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3904 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3905 | size_t), |
| 3906 | void *p_psk ) |
| 3907 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3908 | conf->f_psk = f_psk; |
| 3909 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3910 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3911 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 3912 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 3913 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 3914 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 3915 | const unsigned char *dhm_P, size_t P_len, |
| 3916 | const unsigned char *dhm_G, size_t G_len ) |
| 3917 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3918 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 3919 | |
| 3920 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 3921 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 3922 | { |
| 3923 | mbedtls_mpi_free( &conf->dhm_P ); |
| 3924 | mbedtls_mpi_free( &conf->dhm_G ); |
| 3925 | return( ret ); |
| 3926 | } |
| 3927 | |
| 3928 | return( 0 ); |
| 3929 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3930 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3931 | 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] | 3932 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3933 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 3934 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 3935 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 3936 | &conf->dhm_P ) ) != 0 || |
| 3937 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 3938 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 3939 | { |
| 3940 | mbedtls_mpi_free( &conf->dhm_P ); |
| 3941 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 3942 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 3943 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 3944 | |
| 3945 | return( 0 ); |
| 3946 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 3947 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 3948 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 3949 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 3950 | /* |
| 3951 | * Set the minimum length for Diffie-Hellman parameters |
| 3952 | */ |
| 3953 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 3954 | unsigned int bitlen ) |
| 3955 | { |
| 3956 | conf->dhm_min_bitlen = bitlen; |
| 3957 | } |
| 3958 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 3959 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3960 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 3961 | /* |
| 3962 | * Set allowed/preferred hashes for handshake signatures |
| 3963 | */ |
| 3964 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 3965 | const int *hashes ) |
| 3966 | { |
| 3967 | conf->sig_hashes = hashes; |
| 3968 | } |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 3969 | |
| 3970 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Jerry Yu | 7899de8 | 2021-08-17 13:09:23 +0800 | [diff] [blame] | 3971 | /* Configure allowed signature algorithms for use in TLS 1.3 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 3972 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 3973 | const uint16_t* sig_algs ) |
| 3974 | { |
| 3975 | conf->tls13_sig_algs = sig_algs; |
| 3976 | } |
| 3977 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3978 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 3979 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 3980 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3981 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 3982 | /* |
| 3983 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3984 | * |
| 3985 | * mbedtls_ssl_setup() takes the provided list |
| 3986 | * and translates it to a list of IANA TLS group identifiers, |
| 3987 | * stored in ssl->handshake->group_list. |
| 3988 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 3989 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3990 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3991 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 3992 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3993 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3994 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 3995 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3996 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3997 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 3998 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3999 | /* |
| 4000 | * Set the allowed groups |
| 4001 | */ |
| 4002 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 4003 | const uint16_t *group_list ) |
| 4004 | { |
| 4005 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4006 | conf->curve_list = NULL; |
| 4007 | #endif |
| 4008 | conf->group_list = group_list; |
| 4009 | } |
| 4010 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4011 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4012 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4013 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4014 | /* Initialize to suppress unnecessary compiler warning */ |
| 4015 | size_t hostname_len = 0; |
| 4016 | |
| 4017 | /* Check if new hostname is valid before |
| 4018 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4019 | if( hostname != NULL ) |
| 4020 | { |
| 4021 | hostname_len = strlen( hostname ); |
| 4022 | |
| 4023 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 4024 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4025 | } |
| 4026 | |
| 4027 | /* Now it's clear that we will overwrite the old hostname, |
| 4028 | * so we can free it safely */ |
| 4029 | |
| 4030 | if( ssl->hostname != NULL ) |
| 4031 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4032 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4033 | mbedtls_free( ssl->hostname ); |
| 4034 | } |
| 4035 | |
| 4036 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 4037 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4038 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4039 | { |
| 4040 | ssl->hostname = NULL; |
| 4041 | } |
| 4042 | else |
| 4043 | { |
| 4044 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4045 | if( ssl->hostname == NULL ) |
| 4046 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4047 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4048 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4049 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4050 | ssl->hostname[hostname_len] = '\0'; |
| 4051 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4052 | |
| 4053 | return( 0 ); |
| 4054 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 4055 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4056 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4057 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4058 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4059 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4060 | const unsigned char *, size_t), |
| 4061 | void *p_sni ) |
| 4062 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4063 | conf->f_sni = f_sni; |
| 4064 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4065 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4066 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4067 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4068 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4069 | 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] | 4070 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4071 | size_t cur_len, tot_len; |
| 4072 | const char **p; |
| 4073 | |
| 4074 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4075 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 4076 | * MUST NOT be truncated." |
| 4077 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4078 | */ |
| 4079 | tot_len = 0; |
| 4080 | for( p = protos; *p != NULL; p++ ) |
| 4081 | { |
| 4082 | cur_len = strlen( *p ); |
| 4083 | tot_len += cur_len; |
| 4084 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 4085 | if( ( cur_len == 0 ) || |
| 4086 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 4087 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4088 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4089 | } |
| 4090 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4091 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4092 | |
| 4093 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4094 | } |
| 4095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4096 | 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] | 4097 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4098 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4099 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4100 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4101 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4102 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4103 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 4104 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4105 | { |
| 4106 | conf->dtls_srtp_mki_support = support_mki_value; |
| 4107 | } |
| 4108 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4109 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 4110 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 4111 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4112 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4113 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4114 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4115 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4116 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4117 | |
| 4118 | 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] | 4119 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4120 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4121 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4122 | |
| 4123 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 4124 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4125 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4126 | } |
| 4127 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4128 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4129 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4130 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4131 | const mbedtls_ssl_srtp_profile *p; |
| 4132 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4133 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4134 | /* check the profiles list: all entry must be valid, |
| 4135 | * 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] | 4136 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 4137 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 4138 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4139 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4140 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4141 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 4142 | list_size++; |
| 4143 | } |
| 4144 | else |
| 4145 | { |
| 4146 | /* unsupported value, stop parsing and set the size to an error value */ |
| 4147 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4148 | } |
| 4149 | } |
| 4150 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4151 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 4152 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4153 | conf->dtls_srtp_profile_list = NULL; |
| 4154 | conf->dtls_srtp_profile_list_len = 0; |
| 4155 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4156 | } |
| 4157 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 4158 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4159 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4160 | |
| 4161 | return( 0 ); |
| 4162 | } |
| 4163 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4164 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 4165 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4166 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4167 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 4168 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4169 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4170 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4171 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4172 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4173 | else |
| 4174 | { |
| 4175 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4176 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 4177 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4178 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4179 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4180 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 4181 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4182 | 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] | 4183 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4184 | conf->max_major_ver = major; |
| 4185 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4188 | 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] | 4189 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4190 | conf->min_major_ver = major; |
| 4191 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4194 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4195 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 4196 | char cert_req_ca_list ) |
| 4197 | { |
| 4198 | conf->cert_req_ca_list = cert_req_ca_list; |
| 4199 | } |
| 4200 | #endif |
| 4201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4202 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4203 | 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] | 4204 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4205 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 4206 | } |
| 4207 | #endif |
| 4208 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4209 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4210 | 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] | 4211 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4212 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 4213 | } |
| 4214 | #endif |
| 4215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4216 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4217 | 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] | 4218 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4219 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4220 | 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] | 4221 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4222 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4223 | } |
| 4224 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 4225 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4226 | |
| 4227 | return( 0 ); |
| 4228 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4229 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4230 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4231 | 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] | 4232 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4233 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4236 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4237 | 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] | 4238 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4239 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 4240 | } |
| 4241 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4242 | 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] | 4243 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4244 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 4245 | } |
| 4246 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4247 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4248 | const unsigned char period[8] ) |
| 4249 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4250 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4251 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4252 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4254 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4255 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4256 | 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] | 4257 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 4258 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4259 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4260 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4261 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4262 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4263 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 4264 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 4265 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 4266 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4267 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4268 | conf->f_ticket_write = f_ticket_write; |
| 4269 | conf->f_ticket_parse = f_ticket_parse; |
| 4270 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4271 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4272 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4273 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4274 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 4275 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 4276 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 4277 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4278 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 4279 | ssl->f_export_keys = f_export_keys; |
| 4280 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 4281 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4282 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4283 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4284 | void mbedtls_ssl_conf_async_private_cb( |
| 4285 | mbedtls_ssl_config *conf, |
| 4286 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 4287 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 4288 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 4289 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4290 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4291 | { |
| 4292 | conf->f_async_sign_start = f_async_sign; |
| 4293 | conf->f_async_decrypt_start = f_async_decrypt; |
| 4294 | conf->f_async_resume = f_async_resume; |
| 4295 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4296 | conf->p_async_config_data = async_config_data; |
| 4297 | } |
| 4298 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 4299 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 4300 | { |
| 4301 | return( conf->p_async_config_data ); |
| 4302 | } |
| 4303 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4304 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4305 | { |
| 4306 | if( ssl->handshake == NULL ) |
| 4307 | return( NULL ); |
| 4308 | else |
| 4309 | return( ssl->handshake->user_async_ctx ); |
| 4310 | } |
| 4311 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4312 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4313 | void *ctx ) |
| 4314 | { |
| 4315 | if( ssl->handshake != NULL ) |
| 4316 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4317 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4318 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4319 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4320 | /* |
| 4321 | * SSL get accessors |
| 4322 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4323 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4324 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 4325 | if( ssl->session != NULL ) |
| 4326 | return( ssl->session->verify_result ); |
| 4327 | |
| 4328 | if( ssl->session_negotiate != NULL ) |
| 4329 | return( ssl->session_negotiate->verify_result ); |
| 4330 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 4331 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4334 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4335 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4336 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4337 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4338 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4339 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4342 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4343 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4344 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4345 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4346 | { |
| 4347 | switch( ssl->minor_ver ) |
| 4348 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4349 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4350 | return( "DTLSv1.2" ); |
| 4351 | |
| 4352 | default: |
| 4353 | return( "unknown (DTLS)" ); |
| 4354 | } |
| 4355 | } |
| 4356 | #endif |
| 4357 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4358 | switch( ssl->minor_ver ) |
| 4359 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4360 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4361 | return( "TLSv1.2" ); |
| 4362 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4363 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4364 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4365 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4368 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4369 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 4370 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 4371 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4372 | size_t read_mfl; |
| 4373 | |
| 4374 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 4375 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4376 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 4377 | { |
| 4378 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 4379 | } |
| 4380 | |
| 4381 | /* Check if a smaller max length was negotiated */ |
| 4382 | if( ssl->session_out != NULL ) |
| 4383 | { |
| 4384 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 4385 | if( read_mfl < max_len ) |
| 4386 | { |
| 4387 | max_len = read_mfl; |
| 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | // During a handshake, use the value being negotiated |
| 4392 | if( ssl->session_negotiate != NULL ) |
| 4393 | { |
| 4394 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 4395 | if( read_mfl < max_len ) |
| 4396 | { |
| 4397 | max_len = read_mfl; |
| 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | return( max_len ); |
| 4402 | } |
| 4403 | |
| 4404 | 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] | 4405 | { |
| 4406 | size_t max_len; |
| 4407 | |
| 4408 | /* |
| 4409 | * Assume mfl_code is correct since it was checked when set |
| 4410 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4411 | 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] | 4412 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4413 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4414 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4415 | 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] | 4416 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4417 | 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] | 4418 | } |
| 4419 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4420 | /* During a handshake, use the value being negotiated */ |
| 4421 | if( ssl->session_negotiate != NULL && |
| 4422 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 4423 | { |
| 4424 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 4425 | } |
| 4426 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4427 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4428 | } |
| 4429 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4430 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4431 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4432 | 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] | 4433 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 4434 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 4435 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4436 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 4437 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 4438 | return ( 0 ); |
| 4439 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4440 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 4441 | return( ssl->mtu ); |
| 4442 | |
| 4443 | if( ssl->mtu == 0 ) |
| 4444 | return( ssl->handshake->mtu ); |
| 4445 | |
| 4446 | return( ssl->mtu < ssl->handshake->mtu ? |
| 4447 | ssl->mtu : ssl->handshake->mtu ); |
| 4448 | } |
| 4449 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4450 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4451 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 4452 | { |
| 4453 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 4454 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 4455 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4456 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4457 | (void) ssl; |
| 4458 | #endif |
| 4459 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4460 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4461 | 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] | 4462 | |
| 4463 | if( max_len > mfl ) |
| 4464 | max_len = mfl; |
| 4465 | #endif |
| 4466 | |
| 4467 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4468 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4469 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4470 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4471 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 4472 | const size_t overhead = (size_t) ret; |
| 4473 | |
| 4474 | if( ret < 0 ) |
| 4475 | return( ret ); |
| 4476 | |
| 4477 | if( mtu <= overhead ) |
| 4478 | { |
| 4479 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 4480 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4481 | } |
| 4482 | |
| 4483 | if( max_len > mtu - overhead ) |
| 4484 | max_len = mtu - overhead; |
| 4485 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4486 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4487 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 4488 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4489 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4490 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4491 | #endif |
| 4492 | |
| 4493 | return( (int) max_len ); |
| 4494 | } |
| 4495 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 4496 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 4497 | { |
| 4498 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 4499 | |
| 4500 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4501 | (void) ssl; |
| 4502 | #endif |
| 4503 | |
| 4504 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4505 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 4506 | |
| 4507 | if( max_len > mfl ) |
| 4508 | max_len = mfl; |
| 4509 | #endif |
| 4510 | |
| 4511 | return( (int) max_len ); |
| 4512 | } |
| 4513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4514 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4515 | 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] | 4516 | { |
| 4517 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4518 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4519 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 4520 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4521 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 4522 | #else |
| 4523 | return( NULL ); |
| 4524 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4525 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4526 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4527 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4528 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 4529 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 4530 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4531 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4532 | int ret; |
| 4533 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4534 | if( ssl == NULL || |
| 4535 | dst == NULL || |
| 4536 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4537 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4538 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4539 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4540 | } |
| 4541 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4542 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 4543 | * idempotent: Each session can only be exported once. |
| 4544 | * |
| 4545 | * (This is in preparation for TLS 1.3 support where we will |
| 4546 | * need the ability to export multiple sessions (aka tickets), |
| 4547 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 4548 | * multiple times until it fails.) |
| 4549 | * |
| 4550 | * Check whether we have already exported the current session, |
| 4551 | * and fail if so. |
| 4552 | */ |
| 4553 | if( ssl->session->exported == 1 ) |
| 4554 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4555 | |
| 4556 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 4557 | if( ret != 0 ) |
| 4558 | return( ret ); |
| 4559 | |
| 4560 | /* Remember that we've exported the session. */ |
| 4561 | ssl->session->exported = 1; |
| 4562 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4563 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4564 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4565 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4566 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4567 | * Define ticket header determining Mbed TLS version |
| 4568 | * and structure of the ticket. |
| 4569 | */ |
| 4570 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4571 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4572 | * Define bitflag determining compile-time settings influencing |
| 4573 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4574 | */ |
| 4575 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4576 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4577 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4578 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4579 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4580 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4581 | |
| 4582 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4583 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4584 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4585 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4586 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4587 | |
| 4588 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4589 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4590 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4591 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4592 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 4593 | |
| 4594 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4595 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4596 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4597 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4598 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4599 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4600 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4601 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4602 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4603 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4604 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 4605 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4606 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4607 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 4608 | #else |
| 4609 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 4610 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4611 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4612 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 4613 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 4614 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 4615 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 4616 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 4617 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4618 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4619 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4620 | ( (uint16_t) ( \ |
| 4621 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 4622 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 4623 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 4624 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4625 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 4626 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4627 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4628 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4629 | MBEDTLS_VERSION_MAJOR, |
| 4630 | MBEDTLS_VERSION_MINOR, |
| 4631 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4632 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 4633 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4634 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4635 | |
| 4636 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4637 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4638 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4639 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4640 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4641 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 4642 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 4643 | * opaque session_format[2]; // library-version specific 16-bit field |
| 4644 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4645 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4646 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4647 | * Note: When updating the format, remember to keep |
| 4648 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4649 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4650 | * // In this version, `session_format` determines |
| 4651 | * // the setting of those compile-time |
| 4652 | * // configuration options which influence |
| 4653 | * // the structure of mbedtls_ssl_session. |
| 4654 | * |
Hanno Becker | fa0d61e | 2021-08-02 08:56:14 +0100 | [diff] [blame] | 4655 | * uint8_t minor_ver; // Protocol-version. Possible values: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4656 | * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3) |
| 4657 | * |
| 4658 | * select (serialized_session.minor_ver) { |
| 4659 | * |
| 4660 | * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2 |
| 4661 | * serialized_session_tls12 data; |
| 4662 | * |
| 4663 | * }; |
| 4664 | * |
| 4665 | * } serialized_session; |
| 4666 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4667 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4668 | |
| 4669 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4670 | /* Serialization of TLS 1.2 sessions: |
| 4671 | * |
| 4672 | * struct { |
| 4673 | * uint64 start_time; |
| 4674 | * uint8 ciphersuite[2]; // defined by the standard |
| 4675 | * uint8 compression; // 0 or 1 |
| 4676 | * uint8 session_id_len; // at most 32 |
| 4677 | * opaque session_id[32]; |
| 4678 | * opaque master[48]; // fixed length in the standard |
| 4679 | * uint32 verify_result; |
| 4680 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 4681 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 4682 | * uint32 ticket_lifetime; |
| 4683 | * uint8 mfl_code; // up to 255 according to standard |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4684 | * uint8 encrypt_then_mac; // 0 or 1 |
| 4685 | * } serialized_session_tls12; |
| 4686 | * |
| 4687 | */ |
| 4688 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 4689 | unsigned char *buf, |
| 4690 | size_t buf_len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4691 | { |
| 4692 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4693 | size_t used = 0; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4694 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4695 | #if defined(MBEDTLS_HAVE_TIME) |
| 4696 | uint64_t start; |
| 4697 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4698 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4699 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4700 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4701 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4702 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4703 | |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4704 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4705 | * Time |
| 4706 | */ |
| 4707 | #if defined(MBEDTLS_HAVE_TIME) |
| 4708 | used += 8; |
| 4709 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4710 | if( used <= buf_len ) |
| 4711 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4712 | start = (uint64_t) session->start; |
| 4713 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4714 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 4715 | p += 8; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4716 | } |
| 4717 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4718 | |
| 4719 | /* |
| 4720 | * Basic mandatory fields |
| 4721 | */ |
| 4722 | used += 2 /* ciphersuite */ |
| 4723 | + 1 /* compression */ |
| 4724 | + 1 /* id_len */ |
| 4725 | + sizeof( session->id ) |
| 4726 | + sizeof( session->master ) |
| 4727 | + 4; /* verify_result */ |
| 4728 | |
| 4729 | if( used <= buf_len ) |
| 4730 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4731 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 4732 | p += 2; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4733 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4734 | *p++ = MBEDTLS_BYTE_0( session->compression ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4735 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4736 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4737 | memcpy( p, session->id, 32 ); |
| 4738 | p += 32; |
| 4739 | |
| 4740 | memcpy( p, session->master, 48 ); |
| 4741 | p += 48; |
| 4742 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4743 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 4744 | p += 4; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4745 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4746 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4747 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4748 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4749 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4750 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4751 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4752 | if( session->peer_cert == NULL ) |
| 4753 | cert_len = 0; |
| 4754 | else |
| 4755 | cert_len = session->peer_cert->raw.len; |
| 4756 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4757 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4758 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4759 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4760 | { |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4761 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 4762 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 4763 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4764 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4765 | if( session->peer_cert != NULL ) |
| 4766 | { |
| 4767 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 4768 | p += cert_len; |
| 4769 | } |
| 4770 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4771 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4772 | if( session->peer_cert_digest != NULL ) |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4773 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4774 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 4775 | if( used <= buf_len ) |
| 4776 | { |
| 4777 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 4778 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 4779 | memcpy( p, session->peer_cert_digest, |
| 4780 | session->peer_cert_digest_len ); |
| 4781 | p += session->peer_cert_digest_len; |
| 4782 | } |
| 4783 | } |
| 4784 | else |
| 4785 | { |
| 4786 | used += 2; |
| 4787 | if( used <= buf_len ) |
| 4788 | { |
| 4789 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 4790 | *p++ = 0; |
| 4791 | } |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4792 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4793 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4794 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4795 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4796 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4797 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4798 | */ |
| 4799 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4800 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4801 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4802 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4803 | { |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4804 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 4805 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 4806 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4807 | |
| 4808 | if( session->ticket != NULL ) |
| 4809 | { |
| 4810 | memcpy( p, session->ticket, session->ticket_len ); |
| 4811 | p += session->ticket_len; |
| 4812 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4813 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4814 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 4815 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4816 | } |
| 4817 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 4818 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4819 | /* |
| 4820 | * Misc extension-related info |
| 4821 | */ |
| 4822 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4823 | used += 1; |
| 4824 | |
| 4825 | if( used <= buf_len ) |
| 4826 | *p++ = session->mfl_code; |
| 4827 | #endif |
| 4828 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4829 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4830 | used += 1; |
| 4831 | |
| 4832 | if( used <= buf_len ) |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4833 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4834 | #endif |
| 4835 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4836 | return( used ); |
| 4837 | } |
| 4838 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4839 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4840 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 4841 | unsigned char omit_header, |
| 4842 | unsigned char *buf, |
| 4843 | size_t buf_len, |
| 4844 | size_t *olen ) |
| 4845 | { |
| 4846 | unsigned char *p = buf; |
| 4847 | size_t used = 0; |
| 4848 | |
| 4849 | if( !omit_header ) |
| 4850 | { |
| 4851 | /* |
| 4852 | * Add Mbed TLS version identifier |
| 4853 | */ |
| 4854 | |
| 4855 | used += sizeof( ssl_serialized_session_header ); |
| 4856 | |
| 4857 | if( used <= buf_len ) |
| 4858 | { |
| 4859 | memcpy( p, ssl_serialized_session_header, |
| 4860 | sizeof( ssl_serialized_session_header ) ); |
| 4861 | p += sizeof( ssl_serialized_session_header ); |
| 4862 | } |
| 4863 | } |
| 4864 | |
| 4865 | /* |
| 4866 | * TLS version identifier |
| 4867 | */ |
| 4868 | used += 1; |
| 4869 | if( used <= buf_len ) |
| 4870 | { |
| 4871 | *p++ = session->minor_ver; |
| 4872 | } |
| 4873 | |
| 4874 | /* Forward to version-specific serialization routine. */ |
| 4875 | switch( session->minor_ver ) |
| 4876 | { |
| 4877 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4878 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 4879 | { |
| 4880 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; |
| 4881 | used += ssl_session_save_tls12( session, p, remaining_len ); |
| 4882 | break; |
| 4883 | } |
| 4884 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4885 | |
| 4886 | default: |
| 4887 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4888 | } |
| 4889 | |
| 4890 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4891 | if( used > buf_len ) |
| 4892 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4893 | |
| 4894 | return( 0 ); |
| 4895 | } |
| 4896 | |
| 4897 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4898 | * Public wrapper for ssl_session_save() |
| 4899 | */ |
| 4900 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 4901 | unsigned char *buf, |
| 4902 | size_t buf_len, |
| 4903 | size_t *olen ) |
| 4904 | { |
| 4905 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 4906 | } |
| 4907 | |
| 4908 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4909 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4910 | * |
| 4911 | * This internal version is wrapped by a public function that cleans up in |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4912 | * case of error, and has an extra option omit_header. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4913 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4914 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 4915 | const unsigned char *buf, |
| 4916 | size_t len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4917 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4918 | #if defined(MBEDTLS_HAVE_TIME) |
| 4919 | uint64_t start; |
| 4920 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4921 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4922 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4923 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4924 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4925 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4926 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4927 | const unsigned char *p = buf; |
| 4928 | const unsigned char * const end = buf + len; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4929 | |
| 4930 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4931 | * Time |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4932 | */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4933 | #if defined(MBEDTLS_HAVE_TIME) |
| 4934 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4935 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4936 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4937 | start = ( (uint64_t) p[0] << 56 ) | |
| 4938 | ( (uint64_t) p[1] << 48 ) | |
| 4939 | ( (uint64_t) p[2] << 40 ) | |
| 4940 | ( (uint64_t) p[3] << 32 ) | |
| 4941 | ( (uint64_t) p[4] << 24 ) | |
| 4942 | ( (uint64_t) p[5] << 16 ) | |
| 4943 | ( (uint64_t) p[6] << 8 ) | |
| 4944 | ( (uint64_t) p[7] ); |
| 4945 | p += 8; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4946 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4947 | session->start = (time_t) start; |
| 4948 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4949 | |
| 4950 | /* |
| 4951 | * Basic mandatory fields |
| 4952 | */ |
| 4953 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 4954 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4955 | |
| 4956 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 4957 | p += 2; |
| 4958 | |
| 4959 | session->compression = *p++; |
| 4960 | |
| 4961 | session->id_len = *p++; |
| 4962 | memcpy( session->id, p, 32 ); |
| 4963 | p += 32; |
| 4964 | |
| 4965 | memcpy( session->master, p, 48 ); |
| 4966 | p += 48; |
| 4967 | |
| 4968 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 4969 | ( (uint32_t) p[1] << 16 ) | |
| 4970 | ( (uint32_t) p[2] << 8 ) | |
| 4971 | ( (uint32_t) p[3] ); |
| 4972 | p += 4; |
| 4973 | |
| 4974 | /* Immediately clear invalid pointer values that have been read, in case |
| 4975 | * we exit early before we replaced them with valid ones. */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4976 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4977 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4978 | session->peer_cert = NULL; |
| 4979 | #else |
| 4980 | session->peer_cert_digest = NULL; |
| 4981 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4982 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4983 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 4984 | session->ticket = NULL; |
| 4985 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 4986 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4987 | /* |
| 4988 | * Peer certificate |
| 4989 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4990 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4991 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4992 | /* Deserialize CRT from the end of the ticket. */ |
| 4993 | if( 3 > (size_t)( end - p ) ) |
| 4994 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4995 | |
| 4996 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 4997 | p += 3; |
| 4998 | |
| 4999 | if( cert_len != 0 ) |
| 5000 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5001 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5002 | |
| 5003 | if( cert_len > (size_t)( end - p ) ) |
| 5004 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5005 | |
| 5006 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 5007 | |
| 5008 | if( session->peer_cert == NULL ) |
| 5009 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5010 | |
| 5011 | mbedtls_x509_crt_init( session->peer_cert ); |
| 5012 | |
| 5013 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 5014 | p, cert_len ) ) != 0 ) |
| 5015 | { |
| 5016 | mbedtls_x509_crt_free( session->peer_cert ); |
| 5017 | mbedtls_free( session->peer_cert ); |
| 5018 | session->peer_cert = NULL; |
| 5019 | return( ret ); |
| 5020 | } |
| 5021 | |
| 5022 | p += cert_len; |
| 5023 | } |
| 5024 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5025 | /* Deserialize CRT digest from the end of the ticket. */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5026 | if( 2 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5027 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5028 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5029 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 5030 | session->peer_cert_digest_len = (size_t) *p++; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5031 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5032 | if( session->peer_cert_digest_len != 0 ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5033 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5034 | const mbedtls_md_info_t *md_info = |
| 5035 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 5036 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5037 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5038 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 5039 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5040 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5041 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 5042 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5043 | |
| 5044 | session->peer_cert_digest = |
| 5045 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5046 | if( session->peer_cert_digest == NULL ) |
| 5047 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5048 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5049 | memcpy( session->peer_cert_digest, p, |
| 5050 | session->peer_cert_digest_len ); |
| 5051 | p += session->peer_cert_digest_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5052 | } |
| 5053 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5054 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5055 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5056 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5057 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5058 | */ |
| 5059 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5060 | if( 3 > (size_t)( end - p ) ) |
| 5061 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5062 | |
| 5063 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5064 | p += 3; |
| 5065 | |
| 5066 | if( session->ticket_len != 0 ) |
| 5067 | { |
| 5068 | if( session->ticket_len > (size_t)( end - p ) ) |
| 5069 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5070 | |
| 5071 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 5072 | if( session->ticket == NULL ) |
| 5073 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5074 | |
| 5075 | memcpy( session->ticket, p, session->ticket_len ); |
| 5076 | p += session->ticket_len; |
| 5077 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5078 | |
| 5079 | if( 4 > (size_t)( end - p ) ) |
| 5080 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5081 | |
| 5082 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 5083 | ( (uint32_t) p[1] << 16 ) | |
| 5084 | ( (uint32_t) p[2] << 8 ) | |
| 5085 | ( (uint32_t) p[3] ); |
| 5086 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5087 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5088 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5089 | /* |
| 5090 | * Misc extension-related info |
| 5091 | */ |
| 5092 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 5093 | if( 1 > (size_t)( end - p ) ) |
| 5094 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5095 | |
| 5096 | session->mfl_code = *p++; |
| 5097 | #endif |
| 5098 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5099 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5100 | if( 1 > (size_t)( end - p ) ) |
| 5101 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5102 | |
| 5103 | session->encrypt_then_mac = *p++; |
| 5104 | #endif |
| 5105 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5106 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5107 | if( p != end ) |
| 5108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5109 | |
| 5110 | return( 0 ); |
| 5111 | } |
| 5112 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5113 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 5114 | unsigned char omit_header, |
| 5115 | const unsigned char *buf, |
| 5116 | size_t len ) |
| 5117 | { |
| 5118 | const unsigned char *p = buf; |
| 5119 | const unsigned char * const end = buf + len; |
| 5120 | |
| 5121 | if( !omit_header ) |
| 5122 | { |
| 5123 | /* |
| 5124 | * Check Mbed TLS version identifier |
| 5125 | */ |
| 5126 | |
| 5127 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 5128 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5129 | |
| 5130 | if( memcmp( p, ssl_serialized_session_header, |
| 5131 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 5132 | { |
| 5133 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 5134 | } |
| 5135 | p += sizeof( ssl_serialized_session_header ); |
| 5136 | } |
| 5137 | |
| 5138 | /* |
| 5139 | * TLS version identifier |
| 5140 | */ |
| 5141 | if( 1 > (size_t)( end - p ) ) |
| 5142 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5143 | session->minor_ver = *p++; |
| 5144 | |
| 5145 | /* Dispatch according to TLS version. */ |
| 5146 | switch( session->minor_ver ) |
| 5147 | { |
| 5148 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5149 | case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */ |
| 5150 | { |
| 5151 | size_t remaining_len = ( end - p ); |
| 5152 | return( ssl_session_load_tls12( session, p, remaining_len ) ); |
| 5153 | } |
| 5154 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5155 | |
| 5156 | default: |
| 5157 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5158 | } |
| 5159 | } |
| 5160 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5161 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5162 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5163 | */ |
| 5164 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 5165 | const unsigned char *buf, |
| 5166 | size_t len ) |
| 5167 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5168 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5169 | |
| 5170 | if( ret != 0 ) |
| 5171 | mbedtls_ssl_session_free( session ); |
| 5172 | |
| 5173 | return( ret ); |
| 5174 | } |
| 5175 | |
| 5176 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5177 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5178 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5179 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 5180 | { |
| 5181 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5182 | |
| 5183 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 5184 | return( ret ); |
| 5185 | |
| 5186 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5187 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5188 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 5189 | { |
| 5190 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 5191 | return( ret ); |
| 5192 | } |
| 5193 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5194 | |
| 5195 | return( ret ); |
| 5196 | } |
| 5197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5198 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5199 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5200 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5201 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5202 | if( ssl == NULL || |
| 5203 | ssl->conf == NULL || |
| 5204 | ssl->handshake == NULL || |
| 5205 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5206 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5207 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5208 | } |
| 5209 | |
| 5210 | ret = ssl_prepare_handshake_step( ssl ); |
| 5211 | if( ret != 0 ) |
| 5212 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5213 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5214 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 5215 | if( ret != 0 ) |
| 5216 | goto cleanup; |
| 5217 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5218 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5219 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5220 | { |
| 5221 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 5222 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 5223 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5224 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 5225 | |
| 5226 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5227 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 5228 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 5229 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5230 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5231 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5232 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5233 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5234 | { |
| 5235 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 5236 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 5237 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5238 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 5239 | |
| 5240 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5241 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 5242 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 5243 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5244 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5245 | #endif |
| 5246 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5247 | if( ret != 0 ) |
| 5248 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 5249 | /* handshake_step return error. And it is same |
| 5250 | * with alert_reason. |
| 5251 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 5252 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5253 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 5254 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5255 | goto cleanup; |
| 5256 | } |
| 5257 | } |
| 5258 | |
| 5259 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5260 | return( ret ); |
| 5261 | } |
| 5262 | |
| 5263 | /* |
| 5264 | * Perform the SSL handshake |
| 5265 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5266 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5267 | { |
| 5268 | int ret = 0; |
| 5269 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5270 | /* Sanity checks */ |
| 5271 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5272 | if( ssl == NULL || ssl->conf == NULL ) |
| 5273 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5274 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5275 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5276 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5277 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 5278 | { |
| 5279 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 5280 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 5281 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5282 | } |
| 5283 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5285 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5286 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5287 | /* Main handshake loop */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5288 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5289 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5290 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5291 | |
| 5292 | if( ret != 0 ) |
| 5293 | break; |
| 5294 | } |
| 5295 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5296 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5297 | |
| 5298 | return( ret ); |
| 5299 | } |
| 5300 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5301 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5302 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5303 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5304 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5305 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5306 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5307 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5308 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5309 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5310 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5311 | |
| 5312 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5313 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5314 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5315 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5316 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5317 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5318 | 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] | 5319 | return( ret ); |
| 5320 | } |
| 5321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5322 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5323 | |
| 5324 | return( 0 ); |
| 5325 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5326 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5327 | |
| 5328 | /* |
| 5329 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5330 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 5331 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 5332 | * - 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] | 5333 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5334 | * 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] | 5335 | * 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] | 5336 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5337 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5338 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5339 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5340 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5341 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5342 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5343 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 5344 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5345 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5346 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 5347 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5348 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5349 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5350 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5351 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5352 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 5353 | ssl->handshake->out_msg_seq = 1; |
| 5354 | else |
| 5355 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5356 | } |
| 5357 | #endif |
| 5358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5359 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 5360 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5361 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5362 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5363 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5364 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5365 | return( ret ); |
| 5366 | } |
| 5367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5368 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5369 | |
| 5370 | return( 0 ); |
| 5371 | } |
| 5372 | |
| 5373 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5374 | * Renegotiate current connection on client, |
| 5375 | * or request renegotiation on server |
| 5376 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5377 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5378 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5379 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5380 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5381 | if( ssl == NULL || ssl->conf == NULL ) |
| 5382 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5383 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5384 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5385 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5386 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5387 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5388 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5389 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5390 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5391 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5392 | |
| 5393 | /* Did we already try/start sending HelloRequest? */ |
| 5394 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5395 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5396 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5397 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5398 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5399 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5401 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5402 | /* |
| 5403 | * On client, either start the renegotiation process or, |
| 5404 | * if already in progress, continue the handshake |
| 5405 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5406 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5407 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5408 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5409 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5410 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5411 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5412 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5413 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5414 | return( ret ); |
| 5415 | } |
| 5416 | } |
| 5417 | else |
| 5418 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5419 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5420 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5421 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5422 | return( ret ); |
| 5423 | } |
| 5424 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5425 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5426 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 5427 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5428 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5429 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5430 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5431 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5432 | 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] | 5433 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5434 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5435 | |
| 5436 | while( cur != NULL ) |
| 5437 | { |
| 5438 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5439 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5440 | cur = next; |
| 5441 | } |
| 5442 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5443 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5444 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5445 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5446 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5447 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 5448 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5449 | if( handshake == NULL ) |
| 5450 | return; |
| 5451 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5452 | #if defined(MBEDTLS_ECP_C) |
| 5453 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5454 | if ( ssl->handshake->group_list_heap_allocated ) |
| 5455 | mbedtls_free( (void*) handshake->group_list ); |
| 5456 | handshake->group_list = NULL; |
| 5457 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 5458 | #endif /* MBEDTLS_ECP_C */ |
| 5459 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5460 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 5461 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 5462 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 5463 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5464 | handshake->async_in_progress = 0; |
| 5465 | } |
| 5466 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 5467 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5468 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5469 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5470 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5471 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 5472 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5473 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 5474 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5475 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 5476 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5477 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 5478 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5479 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5480 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 5481 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5482 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5483 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5485 | #if defined(MBEDTLS_DHM_C) |
| 5486 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5487 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5488 | #if defined(MBEDTLS_ECDH_C) |
| 5489 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5490 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 5491 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 5492 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 5493 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5494 | mbedtls_free( handshake->ecjpake_cache ); |
| 5495 | handshake->ecjpake_cache = NULL; |
| 5496 | handshake->ecjpake_cache_len = 0; |
| 5497 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 5498 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5499 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 5500 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 5501 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 5502 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5503 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 5504 | #endif |
| 5505 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 5506 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5507 | if( handshake->psk != NULL ) |
| 5508 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5509 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5510 | mbedtls_free( handshake->psk ); |
| 5511 | } |
| 5512 | #endif |
| 5513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5514 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5515 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5516 | /* |
| 5517 | * Free only the linked list wrapper, not the keys themselves |
| 5518 | * since the belong to the SNI callback |
| 5519 | */ |
| 5520 | if( handshake->sni_key_cert != NULL ) |
| 5521 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5522 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5523 | |
| 5524 | while( cur != NULL ) |
| 5525 | { |
| 5526 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5527 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5528 | cur = next; |
| 5529 | } |
| 5530 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5531 | #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] | 5532 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 5533 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 5534 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 5535 | if( handshake->ecrs_peer_cert != NULL ) |
| 5536 | { |
| 5537 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 5538 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 5539 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 5540 | #endif |
| 5541 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 5542 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5543 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5544 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 5545 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5546 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5547 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5548 | mbedtls_free( handshake->verify_cookie ); |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 5549 | mbedtls_ssl_flight_free( handshake->flight ); |
| 5550 | mbedtls_ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 5551 | #endif |
| 5552 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 5553 | #if defined(MBEDTLS_ECDH_C) && \ |
| 5554 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5555 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 5556 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 5557 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5558 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 5559 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 5560 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5561 | mbedtls_free( handshake->transform_earlydata ); |
| 5562 | mbedtls_free( handshake->transform_handshake ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5563 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 5564 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5565 | |
| 5566 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5567 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 5568 | * processes datagrams and the fact that a datagram is allowed to have |
| 5569 | * several records in it, it is possible that the I/O buffers are not |
| 5570 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 5571 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 5572 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5573 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 5574 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5575 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 5576 | mbedtls_platform_zeroize( handshake, |
| 5577 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5578 | } |
| 5579 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5580 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5581 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5582 | if( session == NULL ) |
| 5583 | return; |
| 5584 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5585 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 5586 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 5587 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 5588 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 5589 | #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] | 5590 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 5591 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 5592 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5593 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5594 | } |
| 5595 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 5596 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5597 | |
| 5598 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5599 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 5600 | #else |
| 5601 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 5602 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5603 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5604 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5605 | |
| 5606 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5607 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 5608 | #else |
| 5609 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 5610 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5611 | |
| 5612 | #if defined(MBEDTLS_SSL_ALPN) |
| 5613 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 5614 | #else |
| 5615 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 5616 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5617 | |
| 5618 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 5619 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 5620 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 5621 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 5622 | |
| 5623 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 5624 | ( (uint32_t) ( \ |
| 5625 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 5626 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 5627 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 5628 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 5629 | 0u ) ) |
| 5630 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5631 | static unsigned char ssl_serialized_context_header[] = { |
| 5632 | MBEDTLS_VERSION_MAJOR, |
| 5633 | MBEDTLS_VERSION_MINOR, |
| 5634 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5635 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 5636 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 5637 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 5638 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 5639 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5640 | }; |
| 5641 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5642 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5643 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5644 | * |
| 5645 | * The format of the serialized data is: |
| 5646 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 5647 | * |
| 5648 | * // header |
| 5649 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5650 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5651 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5652 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5653 | * Note: When updating the format, remember to keep these |
| 5654 | * 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] | 5655 | * |
| 5656 | * // session sub-structure |
| 5657 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 5658 | * // transform sub-structure |
| 5659 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 5660 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 5661 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 5662 | * // fields from ssl_context |
| 5663 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 5664 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 5665 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 5666 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 5667 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 5668 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 5669 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 5670 | * |
| 5671 | * Note that many fields of the ssl_context or sub-structures are not |
| 5672 | * serialized, as they fall in one of the following categories: |
| 5673 | * |
| 5674 | * 1. forced value (eg in_left must be 0) |
| 5675 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 5676 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 5677 | * 4. value was temporary (eg content of input buffer) |
| 5678 | * 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] | 5679 | */ |
| 5680 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 5681 | unsigned char *buf, |
| 5682 | size_t buf_len, |
| 5683 | size_t *olen ) |
| 5684 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5685 | unsigned char *p = buf; |
| 5686 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5687 | size_t session_len; |
| 5688 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5689 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5690 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5691 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 5692 | * this function's documentation. |
| 5693 | * |
| 5694 | * These are due to assumptions/limitations in the implementation. Some of |
| 5695 | * them are likely to stay (no handshake in progress) some might go away |
| 5696 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5697 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5698 | /* The initial handshake must be over */ |
| 5699 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5700 | { |
| 5701 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5702 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5703 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5704 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5705 | { |
| 5706 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5707 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5708 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5709 | /* Double-check that sub-structures are indeed ready */ |
| 5710 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5711 | { |
| 5712 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5713 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5714 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5715 | /* There must be no pending incoming or outgoing data */ |
| 5716 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5717 | { |
| 5718 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5719 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5720 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5721 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5722 | { |
| 5723 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5724 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5725 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5726 | /* Protocol must be DLTS, not TLS */ |
| 5727 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5728 | { |
| 5729 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5730 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5731 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5732 | /* Version must be 1.2 */ |
| 5733 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5734 | { |
| 5735 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5736 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5737 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5738 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5739 | { |
| 5740 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5741 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5742 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5743 | /* We must be using an AEAD ciphersuite */ |
| 5744 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5745 | { |
| 5746 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5747 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5748 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5749 | /* Renegotiation must not be enabled */ |
| 5750 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5751 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5752 | { |
| 5753 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5754 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5755 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5756 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5757 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5758 | /* |
| 5759 | * Version and format identifier |
| 5760 | */ |
| 5761 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5762 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5763 | if( used <= buf_len ) |
| 5764 | { |
| 5765 | memcpy( p, ssl_serialized_context_header, |
| 5766 | sizeof( ssl_serialized_context_header ) ); |
| 5767 | p += sizeof( ssl_serialized_context_header ); |
| 5768 | } |
| 5769 | |
| 5770 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5771 | * Session (length + data) |
| 5772 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5773 | 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] | 5774 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 5775 | return( ret ); |
| 5776 | |
| 5777 | used += 4 + session_len; |
| 5778 | if( used <= buf_len ) |
| 5779 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5780 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 5781 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5782 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5783 | ret = ssl_session_save( ssl->session, 1, |
| 5784 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5785 | if( ret != 0 ) |
| 5786 | return( ret ); |
| 5787 | |
| 5788 | p += session_len; |
| 5789 | } |
| 5790 | |
| 5791 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5792 | * Transform |
| 5793 | */ |
| 5794 | used += sizeof( ssl->transform->randbytes ); |
| 5795 | if( used <= buf_len ) |
| 5796 | { |
| 5797 | memcpy( p, ssl->transform->randbytes, |
| 5798 | sizeof( ssl->transform->randbytes ) ); |
| 5799 | p += sizeof( ssl->transform->randbytes ); |
| 5800 | } |
| 5801 | |
| 5802 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5803 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 5804 | if( used <= buf_len ) |
| 5805 | { |
| 5806 | *p++ = ssl->transform->in_cid_len; |
| 5807 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 5808 | p += ssl->transform->in_cid_len; |
| 5809 | |
| 5810 | *p++ = ssl->transform->out_cid_len; |
| 5811 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 5812 | p += ssl->transform->out_cid_len; |
| 5813 | } |
| 5814 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5815 | |
| 5816 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5817 | * Saved fields from top-level ssl_context structure |
| 5818 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5819 | used += 4; |
| 5820 | if( used <= buf_len ) |
| 5821 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5822 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 5823 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5824 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5825 | |
| 5826 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5827 | used += 16; |
| 5828 | if( used <= buf_len ) |
| 5829 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5830 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 5831 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5832 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5833 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 5834 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5835 | } |
| 5836 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5837 | |
| 5838 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5839 | used += 1; |
| 5840 | if( used <= buf_len ) |
| 5841 | { |
| 5842 | *p++ = ssl->disable_datagram_packing; |
| 5843 | } |
| 5844 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5845 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5846 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5847 | if( used <= buf_len ) |
| 5848 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5849 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 5850 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5854 | used += 2; |
| 5855 | if( used <= buf_len ) |
| 5856 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5857 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 5858 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5859 | } |
| 5860 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5861 | |
| 5862 | #if defined(MBEDTLS_SSL_ALPN) |
| 5863 | { |
| 5864 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 5865 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5866 | : 0; |
| 5867 | |
| 5868 | used += 1 + alpn_len; |
| 5869 | if( used <= buf_len ) |
| 5870 | { |
| 5871 | *p++ = alpn_len; |
| 5872 | |
| 5873 | if( ssl->alpn_chosen != NULL ) |
| 5874 | { |
| 5875 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 5876 | p += alpn_len; |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5881 | |
| 5882 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5883 | * Done |
| 5884 | */ |
| 5885 | *olen = used; |
| 5886 | |
| 5887 | if( used > buf_len ) |
| 5888 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5889 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5890 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 5891 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 5892 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5896 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 5897 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 5898 | */ |
| 5899 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 5900 | const char *label, |
| 5901 | const unsigned char *random, size_t rlen, |
| 5902 | unsigned char *dstbuf, size_t dlen ); |
| 5903 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 5904 | { |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 5905 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5906 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5907 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 5908 | |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 5909 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 5910 | return( tls_prf_sha384 ); |
Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 5911 | #else |
| 5912 | (void) ciphersuite_id; |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 5913 | #endif |
| 5914 | return( tls_prf_sha256 ); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5915 | } |
| 5916 | |
| 5917 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5918 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5919 | * |
| 5920 | * This internal version is wrapped by a public function that cleans up in |
| 5921 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5922 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5923 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 5924 | const unsigned char *buf, |
| 5925 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5926 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5927 | const unsigned char *p = buf; |
| 5928 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5929 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5930 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5931 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5932 | /* |
| 5933 | * The context should have been freshly setup or reset. |
| 5934 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 5935 | * (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] | 5936 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 5937 | */ |
| 5938 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 5939 | ssl->session != NULL ) |
| 5940 | { |
| 5941 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5942 | } |
| 5943 | |
| 5944 | /* |
| 5945 | * We can't check that the config matches the initial one, but we can at |
| 5946 | * least check it matches the requirements for serializing. |
| 5947 | */ |
| 5948 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 5949 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 5950 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 5951 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || |
| 5952 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || |
| 5953 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 5954 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5955 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 5956 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5957 | { |
| 5958 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5959 | } |
| 5960 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5961 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 5962 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5963 | /* |
| 5964 | * Check version identifier |
| 5965 | */ |
| 5966 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 5967 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5968 | |
| 5969 | if( memcmp( p, ssl_serialized_context_header, |
| 5970 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 5971 | { |
| 5972 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 5973 | } |
| 5974 | p += sizeof( ssl_serialized_context_header ); |
| 5975 | |
| 5976 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5977 | * Session |
| 5978 | */ |
| 5979 | if( (size_t)( end - p ) < 4 ) |
| 5980 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5981 | |
| 5982 | session_len = ( (size_t) p[0] << 24 ) | |
| 5983 | ( (size_t) p[1] << 16 ) | |
| 5984 | ( (size_t) p[2] << 8 ) | |
| 5985 | ( (size_t) p[3] ); |
| 5986 | p += 4; |
| 5987 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5988 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 5989 | * 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] | 5990 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5991 | ssl->session_in = ssl->session; |
| 5992 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5993 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5994 | |
| 5995 | if( (size_t)( end - p ) < session_len ) |
| 5996 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5997 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5998 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5999 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6000 | { |
| 6001 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6002 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6003 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6004 | |
| 6005 | p += session_len; |
| 6006 | |
| 6007 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6008 | * Transform |
| 6009 | */ |
| 6010 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6011 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6012 | * 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] | 6013 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6014 | ssl->transform_in = ssl->transform; |
| 6015 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6016 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6017 | |
| 6018 | /* Read random bytes and populate structure */ |
| 6019 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 6020 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6021 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6022 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6023 | ssl->session->ciphersuite, |
| 6024 | ssl->session->master, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6025 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 6026 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6027 | ssl->session->encrypt_then_mac, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6028 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 6029 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6030 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 6031 | p, /* currently pointing to randbytes */ |
| 6032 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 6033 | ssl->conf->endpoint, |
| 6034 | ssl ); |
| 6035 | if( ret != 0 ) |
| 6036 | return( ret ); |
| 6037 | |
| 6038 | p += sizeof( ssl->transform->randbytes ); |
| 6039 | |
| 6040 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6041 | /* Read connection IDs and store them */ |
| 6042 | if( (size_t)( end - p ) < 1 ) |
| 6043 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6044 | |
| 6045 | ssl->transform->in_cid_len = *p++; |
| 6046 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 6047 | 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] | 6048 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6049 | |
| 6050 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 6051 | p += ssl->transform->in_cid_len; |
| 6052 | |
| 6053 | ssl->transform->out_cid_len = *p++; |
| 6054 | |
| 6055 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 6056 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6057 | |
| 6058 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 6059 | p += ssl->transform->out_cid_len; |
| 6060 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6061 | |
| 6062 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6063 | * Saved fields from top-level ssl_context structure |
| 6064 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6065 | if( (size_t)( end - p ) < 4 ) |
| 6066 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6067 | |
| 6068 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 6069 | ( (uint32_t) p[1] << 16 ) | |
| 6070 | ( (uint32_t) p[2] << 8 ) | |
| 6071 | ( (uint32_t) p[3] ); |
| 6072 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6073 | |
| 6074 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6075 | if( (size_t)( end - p ) < 16 ) |
| 6076 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6077 | |
| 6078 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 6079 | ( (uint64_t) p[1] << 48 ) | |
| 6080 | ( (uint64_t) p[2] << 40 ) | |
| 6081 | ( (uint64_t) p[3] << 32 ) | |
| 6082 | ( (uint64_t) p[4] << 24 ) | |
| 6083 | ( (uint64_t) p[5] << 16 ) | |
| 6084 | ( (uint64_t) p[6] << 8 ) | |
| 6085 | ( (uint64_t) p[7] ); |
| 6086 | p += 8; |
| 6087 | |
| 6088 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 6089 | ( (uint64_t) p[1] << 48 ) | |
| 6090 | ( (uint64_t) p[2] << 40 ) | |
| 6091 | ( (uint64_t) p[3] << 32 ) | |
| 6092 | ( (uint64_t) p[4] << 24 ) | |
| 6093 | ( (uint64_t) p[5] << 16 ) | |
| 6094 | ( (uint64_t) p[6] << 8 ) | |
| 6095 | ( (uint64_t) p[7] ); |
| 6096 | p += 8; |
| 6097 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6098 | |
| 6099 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6100 | if( (size_t)( end - p ) < 1 ) |
| 6101 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6102 | |
| 6103 | ssl->disable_datagram_packing = *p++; |
| 6104 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6105 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 6106 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6107 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 6108 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 6109 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6110 | |
| 6111 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6112 | if( (size_t)( end - p ) < 2 ) |
| 6113 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6114 | |
| 6115 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 6116 | p += 2; |
| 6117 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6118 | |
| 6119 | #if defined(MBEDTLS_SSL_ALPN) |
| 6120 | { |
| 6121 | uint8_t alpn_len; |
| 6122 | const char **cur; |
| 6123 | |
| 6124 | if( (size_t)( end - p ) < 1 ) |
| 6125 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6126 | |
| 6127 | alpn_len = *p++; |
| 6128 | |
| 6129 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 6130 | { |
| 6131 | /* alpn_chosen should point to an item in the configured list */ |
| 6132 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 6133 | { |
| 6134 | if( strlen( *cur ) == alpn_len && |
| 6135 | memcmp( p, cur, alpn_len ) == 0 ) |
| 6136 | { |
| 6137 | ssl->alpn_chosen = *cur; |
| 6138 | break; |
| 6139 | } |
| 6140 | } |
| 6141 | } |
| 6142 | |
| 6143 | /* can only happen on conf mismatch */ |
| 6144 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 6145 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6146 | |
| 6147 | p += alpn_len; |
| 6148 | } |
| 6149 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6150 | |
| 6151 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6152 | * Forced fields from top-level ssl_context structure |
| 6153 | * |
| 6154 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 6155 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 6156 | */ |
| 6157 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 6158 | |
| 6159 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6160 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 6161 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6162 | /* Adjust pointers for header fields of outgoing records to |
| 6163 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 6164 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6165 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6166 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6167 | ssl->in_epoch = 1; |
| 6168 | #endif |
| 6169 | |
| 6170 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 6171 | * 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] | 6172 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 6173 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6174 | if( ssl->handshake != NULL ) |
| 6175 | { |
| 6176 | mbedtls_ssl_handshake_free( ssl ); |
| 6177 | mbedtls_free( ssl->handshake ); |
| 6178 | ssl->handshake = NULL; |
| 6179 | } |
| 6180 | |
| 6181 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6182 | * Done - should have consumed entire buffer |
| 6183 | */ |
| 6184 | if( p != end ) |
| 6185 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6186 | |
| 6187 | return( 0 ); |
| 6188 | } |
| 6189 | |
| 6190 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6191 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6192 | */ |
| 6193 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 6194 | const unsigned char *buf, |
| 6195 | size_t len ) |
| 6196 | { |
| 6197 | int ret = ssl_context_load( context, buf, len ); |
| 6198 | |
| 6199 | if( ret != 0 ) |
| 6200 | mbedtls_ssl_free( context ); |
| 6201 | |
| 6202 | return( ret ); |
| 6203 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 6204 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6205 | |
| 6206 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6207 | * Free an SSL context |
| 6208 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6209 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6210 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6211 | if( ssl == NULL ) |
| 6212 | return; |
| 6213 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6214 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6215 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6216 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6217 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6218 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6219 | size_t out_buf_len = ssl->out_buf_len; |
| 6220 | #else |
| 6221 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 6222 | #endif |
| 6223 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6224 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6225 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6226 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6227 | } |
| 6228 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6229 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6230 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6231 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6232 | size_t in_buf_len = ssl->in_buf_len; |
| 6233 | #else |
| 6234 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 6235 | #endif |
| 6236 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6237 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6238 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6239 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6240 | } |
| 6241 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6242 | if( ssl->transform ) |
| 6243 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6244 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6245 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6246 | } |
| 6247 | |
| 6248 | if( ssl->handshake ) |
| 6249 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6250 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6251 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 6252 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6254 | mbedtls_free( ssl->handshake ); |
| 6255 | mbedtls_free( ssl->transform_negotiate ); |
| 6256 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6257 | } |
| 6258 | |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 6259 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 6260 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 6261 | mbedtls_free( ssl->transform_application ); |
| 6262 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 6263 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6264 | if( ssl->session ) |
| 6265 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6266 | mbedtls_ssl_session_free( ssl->session ); |
| 6267 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6268 | } |
| 6269 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 6270 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 6271 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6272 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6273 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6274 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6275 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 6276 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6277 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6278 | #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] | 6279 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 6280 | #endif |
| 6281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6282 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 6283 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 6284 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6285 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6286 | } |
| 6287 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6288 | /* |
| 6289 | * Initialze mbedtls_ssl_config |
| 6290 | */ |
| 6291 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 6292 | { |
| 6293 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 6294 | } |
| 6295 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6296 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6297 | /* 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] | 6298 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, |
| 6299 | * for no fundamental reason. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6300 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
| 6301 | * about this list. */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6302 | static int ssl_preset_default_hashes[] = { |
| 6303 | #if defined(MBEDTLS_SHA512_C) |
| 6304 | MBEDTLS_MD_SHA512, |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6305 | #endif |
| 6306 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6307 | MBEDTLS_MD_SHA384, |
| 6308 | #endif |
| 6309 | #if defined(MBEDTLS_SHA256_C) |
| 6310 | MBEDTLS_MD_SHA256, |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6311 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6312 | MBEDTLS_MD_NONE |
| 6313 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 6314 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6315 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6316 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 6317 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6318 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6319 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6320 | * about this list. |
| 6321 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6322 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6323 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6324 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6325 | #endif |
| 6326 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6327 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6328 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6329 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6330 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6331 | #endif |
| 6332 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6333 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6334 | #endif |
| 6335 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6336 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6337 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6338 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6339 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6340 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6341 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6342 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6343 | #endif |
| 6344 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6345 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6346 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6347 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6348 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6349 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6350 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 6351 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 6352 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 6353 | 0 |
| 6354 | }; |
| 6355 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6356 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6357 | static int ssl_preset_suiteb_hashes[] = { |
| 6358 | MBEDTLS_MD_SHA256, |
| 6359 | MBEDTLS_MD_SHA384, |
| 6360 | MBEDTLS_MD_NONE |
| 6361 | }; |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6362 | |
| 6363 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 6364 | static uint16_t ssl_preset_default_sig_algs[] = { |
| 6365 | /* ECDSA algorithms */ |
| 6366 | #if defined(MBEDTLS_ECDSA_C) |
| 6367 | #if defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 6368 | MBEDTLS_TLS13_SIG_ECDSA_SECP256R1_SHA256, |
| 6369 | #endif /* MBEDTLS_SHA256_C && MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 6370 | #if defined(MBEDTLS_SHA512_C) && defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 6371 | MBEDTLS_TLS13_SIG_ECDSA_SECP384R1_SHA384, |
| 6372 | #endif /* MBEDTLS_SHA512_C && MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 6373 | #if defined(MBEDTLS_SHA512_C) && defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 6374 | MBEDTLS_TLS13_SIG_ECDSA_SECP521R1_SHA512, |
| 6375 | #endif /* MBEDTLS_SHA512_C && MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
| 6376 | #endif /* MBEDTLS_ECDSA_C */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6377 | MBEDTLS_TLS13_SIG_NONE |
| 6378 | }; |
| 6379 | |
| 6380 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 6381 | /* ECDSA algorithms */ |
| 6382 | #if defined(MBEDTLS_ECDSA_C) |
| 6383 | #if defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 6384 | MBEDTLS_TLS13_SIG_ECDSA_SECP256R1_SHA256, |
| 6385 | #endif /* MBEDTLS_SHA256_C && MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 6386 | #if defined(MBEDTLS_SHA512_C) && defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 6387 | MBEDTLS_TLS13_SIG_ECDSA_SECP384R1_SHA384, |
| 6388 | #endif /* MBEDTLS_SHA512_C && MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 6389 | #endif /* MBEDTLS_ECDSA_C */ |
| 6390 | MBEDTLS_TLS13_SIG_NONE |
| 6391 | }; |
| 6392 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6393 | #endif |
| 6394 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6395 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6396 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6397 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6398 | #endif |
| 6399 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6400 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6401 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6402 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6403 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6404 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6405 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 6406 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6407 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6408 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6409 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6410 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6411 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6412 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6413 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6414 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 6415 | /* Use the functions here so that they are covered in tests, |
| 6416 | * but otherwise access member directly for efficiency */ |
| 6417 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 6418 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6419 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6420 | /* |
| 6421 | * Things that are common to all presets |
| 6422 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6423 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6424 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6425 | { |
| 6426 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 6427 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 6428 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 6429 | #endif |
| 6430 | } |
| 6431 | #endif |
| 6432 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6433 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6434 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 6435 | #endif |
| 6436 | |
| 6437 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6438 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 6439 | #endif |
| 6440 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6441 | #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] | 6442 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 6443 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 6444 | #endif |
| 6445 | |
| 6446 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6447 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 6448 | #endif |
| 6449 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6450 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6451 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 6452 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6453 | #endif |
| 6454 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6455 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6456 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 6457 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 6458 | #endif |
| 6459 | |
| 6460 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6461 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 6462 | memset( conf->renego_period, 0x00, 2 ); |
| 6463 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6464 | #endif |
| 6465 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6466 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 6467 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6468 | { |
| 6469 | const unsigned char dhm_p[] = |
| 6470 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 6471 | const unsigned char dhm_g[] = |
| 6472 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 6473 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 6474 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 6475 | dhm_p, sizeof( dhm_p ), |
| 6476 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 6477 | { |
| 6478 | return( ret ); |
| 6479 | } |
| 6480 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 6481 | #endif |
| 6482 | |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 6483 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 6484 | /* |
| 6485 | * Allow all TLS 1.3 key exchange modes by default. |
| 6486 | */ |
| 6487 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_ALL; |
| 6488 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 6489 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6490 | /* |
| 6491 | * Preset-specific defaults |
| 6492 | */ |
| 6493 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6494 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6495 | /* |
| 6496 | * NSA Suite B |
| 6497 | */ |
| 6498 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 6499 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6500 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 6501 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6502 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6503 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6504 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6505 | |
| 6506 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6507 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6508 | #endif |
| 6509 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6510 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6511 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6512 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 6513 | conf->tls13_sig_algs = ssl_preset_suiteb_sig_algs; |
| 6514 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6515 | #endif |
| 6516 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6517 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 6518 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6519 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6520 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 6521 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6522 | |
| 6523 | /* |
| 6524 | * Default |
| 6525 | */ |
| 6526 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 6527 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 6528 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 6529 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 6530 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 6531 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 6532 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 6533 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 6534 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6535 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6536 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6537 | |
| 6538 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6539 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
TRodziewicz | ef73f01 | 2021-05-13 14:53:36 +0200 | [diff] [blame] | 6540 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6541 | #endif |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6542 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6543 | |
| 6544 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6545 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 6546 | #endif |
| 6547 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6548 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6549 | conf->sig_hashes = ssl_preset_default_hashes; |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6550 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 6551 | conf->tls13_sig_algs = ssl_preset_default_sig_algs; |
| 6552 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 6553 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6554 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6555 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 6556 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6557 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6558 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6559 | |
| 6560 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 6561 | conf->dhm_min_bitlen = 1024; |
| 6562 | #endif |
| 6563 | } |
| 6564 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6565 | return( 0 ); |
| 6566 | } |
| 6567 | |
| 6568 | /* |
| 6569 | * Free mbedtls_ssl_config |
| 6570 | */ |
| 6571 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 6572 | { |
| 6573 | #if defined(MBEDTLS_DHM_C) |
| 6574 | mbedtls_mpi_free( &conf->dhm_P ); |
| 6575 | mbedtls_mpi_free( &conf->dhm_G ); |
| 6576 | #endif |
| 6577 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6578 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6579 | if( conf->psk != NULL ) |
| 6580 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6581 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6582 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6583 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6584 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 6585 | } |
| 6586 | |
| 6587 | if( conf->psk_identity != NULL ) |
| 6588 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6589 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 6590 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6591 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6592 | conf->psk_identity_len = 0; |
| 6593 | } |
| 6594 | #endif |
| 6595 | |
| 6596 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6597 | ssl_key_cert_free( conf->key_cert ); |
| 6598 | #endif |
| 6599 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6600 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6601 | } |
| 6602 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 6603 | #if defined(MBEDTLS_PK_C) && \ |
| 6604 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6605 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6606 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6607 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6608 | 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] | 6609 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6610 | #if defined(MBEDTLS_RSA_C) |
| 6611 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 6612 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6613 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6614 | #if defined(MBEDTLS_ECDSA_C) |
| 6615 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 6616 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6617 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6618 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6619 | } |
| 6620 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6621 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 6622 | { |
| 6623 | switch( type ) { |
| 6624 | case MBEDTLS_PK_RSA: |
| 6625 | return( MBEDTLS_SSL_SIG_RSA ); |
| 6626 | case MBEDTLS_PK_ECDSA: |
| 6627 | case MBEDTLS_PK_ECKEY: |
| 6628 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 6629 | default: |
| 6630 | return( MBEDTLS_SSL_SIG_ANON ); |
| 6631 | } |
| 6632 | } |
| 6633 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6634 | 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] | 6635 | { |
| 6636 | switch( sig ) |
| 6637 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6638 | #if defined(MBEDTLS_RSA_C) |
| 6639 | case MBEDTLS_SSL_SIG_RSA: |
| 6640 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6641 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6642 | #if defined(MBEDTLS_ECDSA_C) |
| 6643 | case MBEDTLS_SSL_SIG_ECDSA: |
| 6644 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6645 | #endif |
| 6646 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6647 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6648 | } |
| 6649 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 6650 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6651 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6652 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6653 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6654 | |
| 6655 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 6656 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 6657 | mbedtls_pk_type_t sig_alg ) |
| 6658 | { |
| 6659 | switch( sig_alg ) |
| 6660 | { |
| 6661 | case MBEDTLS_PK_RSA: |
| 6662 | return( set->rsa ); |
| 6663 | case MBEDTLS_PK_ECDSA: |
| 6664 | return( set->ecdsa ); |
| 6665 | default: |
| 6666 | return( MBEDTLS_MD_NONE ); |
| 6667 | } |
| 6668 | } |
| 6669 | |
| 6670 | /* Add a signature-hash-pair to a signature-hash set */ |
| 6671 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 6672 | mbedtls_pk_type_t sig_alg, |
| 6673 | mbedtls_md_type_t md_alg ) |
| 6674 | { |
| 6675 | switch( sig_alg ) |
| 6676 | { |
| 6677 | case MBEDTLS_PK_RSA: |
| 6678 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 6679 | set->rsa = md_alg; |
| 6680 | break; |
| 6681 | |
| 6682 | case MBEDTLS_PK_ECDSA: |
| 6683 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 6684 | set->ecdsa = md_alg; |
| 6685 | break; |
| 6686 | |
| 6687 | default: |
| 6688 | break; |
| 6689 | } |
| 6690 | } |
| 6691 | |
| 6692 | /* Allow exactly one hash algorithm for each signature. */ |
| 6693 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 6694 | mbedtls_md_type_t md_alg ) |
| 6695 | { |
| 6696 | set->rsa = md_alg; |
| 6697 | set->ecdsa = md_alg; |
| 6698 | } |
| 6699 | |
| 6700 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6701 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6702 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6703 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6704 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6705 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6706 | 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] | 6707 | { |
| 6708 | switch( hash ) |
| 6709 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6710 | #if defined(MBEDTLS_MD5_C) |
| 6711 | case MBEDTLS_SSL_HASH_MD5: |
| 6712 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6713 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6714 | #if defined(MBEDTLS_SHA1_C) |
| 6715 | case MBEDTLS_SSL_HASH_SHA1: |
| 6716 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6717 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6718 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6719 | case MBEDTLS_SSL_HASH_SHA224: |
| 6720 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6721 | #endif |
| 6722 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6723 | case MBEDTLS_SSL_HASH_SHA256: |
| 6724 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6725 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6726 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6727 | case MBEDTLS_SSL_HASH_SHA384: |
| 6728 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6729 | #endif |
| 6730 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6731 | case MBEDTLS_SSL_HASH_SHA512: |
| 6732 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6733 | #endif |
| 6734 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6735 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6736 | } |
| 6737 | } |
| 6738 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6739 | /* |
| 6740 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 6741 | */ |
| 6742 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 6743 | { |
| 6744 | switch( md ) |
| 6745 | { |
| 6746 | #if defined(MBEDTLS_MD5_C) |
| 6747 | case MBEDTLS_MD_MD5: |
| 6748 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 6749 | #endif |
| 6750 | #if defined(MBEDTLS_SHA1_C) |
| 6751 | case MBEDTLS_MD_SHA1: |
| 6752 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 6753 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6754 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6755 | case MBEDTLS_MD_SHA224: |
| 6756 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6757 | #endif |
| 6758 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6759 | case MBEDTLS_MD_SHA256: |
| 6760 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 6761 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6762 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6763 | case MBEDTLS_MD_SHA384: |
| 6764 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6765 | #endif |
| 6766 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6767 | case MBEDTLS_MD_SHA512: |
| 6768 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 6769 | #endif |
| 6770 | default: |
| 6771 | return( MBEDTLS_SSL_HASH_NONE ); |
| 6772 | } |
| 6773 | } |
| 6774 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 6775 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6776 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6777 | * 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] | 6778 | * 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] | 6779 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6780 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6781 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6782 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6783 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6784 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6785 | return( -1 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6786 | uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id(grp_id)->tls_id; |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6787 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6788 | for( ; *group_list != 0; group_list++ ) |
| 6789 | { |
| 6790 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6791 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6792 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6793 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6794 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6795 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 6796 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6797 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6798 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6799 | /* |
| 6800 | * Check if a hash proposed by the peer is in our list. |
| 6801 | * Return 0 if we're willing to use it, -1 otherwise. |
| 6802 | */ |
| 6803 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 6804 | mbedtls_md_type_t md ) |
| 6805 | { |
| 6806 | const int *cur; |
| 6807 | |
| 6808 | if( ssl->conf->sig_hashes == NULL ) |
| 6809 | return( -1 ); |
| 6810 | |
| 6811 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 6812 | if( *cur == (int) md ) |
| 6813 | return( 0 ); |
| 6814 | |
| 6815 | return( -1 ); |
| 6816 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6817 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6818 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6819 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6820 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 6821 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 6822 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 6823 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6824 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 6825 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6826 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 6827 | const char *ext_oid; |
| 6828 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 6829 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6830 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6831 | { |
| 6832 | /* Server part of the key exchange */ |
| 6833 | switch( ciphersuite->key_exchange ) |
| 6834 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6835 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 6836 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 6837 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6838 | break; |
| 6839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6840 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 6841 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 6842 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 6843 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6844 | break; |
| 6845 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6846 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 6847 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 6848 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6849 | break; |
| 6850 | |
| 6851 | /* 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] | 6852 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 6853 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 6854 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 6855 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 6856 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6857 | usage = 0; |
| 6858 | } |
| 6859 | } |
| 6860 | else |
| 6861 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6862 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 6863 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6864 | } |
| 6865 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6866 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 6867 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 6868 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 6869 | ret = -1; |
| 6870 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6871 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6872 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 6873 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6874 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 6875 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 6876 | } |
| 6877 | else |
| 6878 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6879 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 6880 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 6881 | } |
| 6882 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6883 | 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] | 6884 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 6885 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 6886 | ret = -1; |
| 6887 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 6888 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 6889 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6890 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6891 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 6892 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6893 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 6894 | { |
| 6895 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6896 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 6897 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6898 | |
| 6899 | switch( md ) |
| 6900 | { |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 6901 | #if defined(MBEDTLS_SHA384_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6902 | case MBEDTLS_SSL_HASH_SHA384: |
| 6903 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6904 | break; |
| 6905 | #endif |
| 6906 | #if defined(MBEDTLS_SHA256_C) |
| 6907 | case MBEDTLS_SSL_HASH_SHA256: |
| 6908 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6909 | break; |
| 6910 | #endif |
| 6911 | default: |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 6912 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6913 | } |
| 6914 | |
| 6915 | return 0; |
| 6916 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6917 | (void) ssl; |
| 6918 | (void) md; |
| 6919 | |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 6920 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6921 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6922 | } |
| 6923 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 6924 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6925 | |
| 6926 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6927 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 6928 | unsigned char *hash, size_t *hashlen, |
| 6929 | unsigned char *data, size_t data_len, |
| 6930 | mbedtls_md_type_t md_alg ) |
| 6931 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6932 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 6933 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6934 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 6935 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 6936 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6937 | |
| 6938 | if( ( status = psa_hash_setup( &hash_operation, |
| 6939 | hash_alg ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6940 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6941 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6942 | goto exit; |
| 6943 | } |
| 6944 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6945 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 6946 | 64 ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6947 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6948 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6949 | goto exit; |
| 6950 | } |
| 6951 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6952 | if( ( status = psa_hash_update( &hash_operation, |
| 6953 | data, data_len ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6954 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6955 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6956 | goto exit; |
| 6957 | } |
| 6958 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6959 | if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, |
| 6960 | hashlen ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6961 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6962 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6963 | goto exit; |
| 6964 | } |
| 6965 | |
| 6966 | exit: |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6967 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6968 | { |
| 6969 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6970 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6971 | switch( status ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6972 | { |
| 6973 | case PSA_ERROR_NOT_SUPPORTED: |
| 6974 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6975 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6976 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 6977 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 6978 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 6979 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 6980 | default: |
TRodziewicz | b579ccd | 2021-04-13 14:28:28 +0200 | [diff] [blame] | 6981 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6982 | } |
| 6983 | } |
| 6984 | return( 0 ); |
| 6985 | } |
| 6986 | |
| 6987 | #else |
| 6988 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6989 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 6990 | unsigned char *hash, size_t *hashlen, |
| 6991 | unsigned char *data, size_t data_len, |
| 6992 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6993 | { |
| 6994 | int ret = 0; |
| 6995 | mbedtls_md_context_t ctx; |
| 6996 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 6997 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6998 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 6999 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7000 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7001 | mbedtls_md_init( &ctx ); |
| 7002 | |
| 7003 | /* |
| 7004 | * digitally-signed struct { |
| 7005 | * opaque client_random[32]; |
| 7006 | * opaque server_random[32]; |
| 7007 | * ServerDHParams params; |
| 7008 | * }; |
| 7009 | */ |
| 7010 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7011 | { |
| 7012 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7013 | goto exit; |
| 7014 | } |
| 7015 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7016 | { |
| 7017 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7018 | goto exit; |
| 7019 | } |
| 7020 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7021 | { |
| 7022 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7023 | goto exit; |
| 7024 | } |
| 7025 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7026 | { |
| 7027 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7028 | goto exit; |
| 7029 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7030 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7031 | { |
| 7032 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7033 | goto exit; |
| 7034 | } |
| 7035 | |
| 7036 | exit: |
| 7037 | mbedtls_md_free( &ctx ); |
| 7038 | |
| 7039 | if( ret != 0 ) |
| 7040 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7041 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7042 | |
| 7043 | return( ret ); |
| 7044 | } |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7045 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7046 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 7047 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7048 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7049 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7050 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 7051 | const mbedtls_md_type_t md, |
| 7052 | unsigned char *dst, |
| 7053 | size_t dst_len, |
| 7054 | size_t *olen ) |
| 7055 | { |
| 7056 | ((void) ssl); |
| 7057 | ((void) md); |
| 7058 | ((void) dst); |
| 7059 | ((void) dst_len); |
| 7060 | *olen = 0; |
| 7061 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
| 7062 | } |
| 7063 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7064 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7065 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7066 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 7067 | unsigned char *dst, |
| 7068 | size_t dst_len, |
| 7069 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7070 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7071 | int ret; |
| 7072 | mbedtls_sha512_context sha512; |
| 7073 | |
| 7074 | if( dst_len < 48 ) |
| 7075 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7076 | |
| 7077 | mbedtls_sha512_init( &sha512 ); |
| 7078 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 7079 | |
| 7080 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 7081 | { |
| 7082 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 7083 | goto exit; |
| 7084 | } |
| 7085 | |
| 7086 | *olen = 48; |
| 7087 | |
| 7088 | exit: |
| 7089 | |
| 7090 | mbedtls_sha512_free( &sha512 ); |
| 7091 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7092 | } |
| 7093 | #endif /* MBEDTLS_SHA384_C */ |
| 7094 | |
| 7095 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7096 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 7097 | unsigned char *dst, |
| 7098 | size_t dst_len, |
| 7099 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7100 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7101 | int ret; |
| 7102 | mbedtls_sha256_context sha256; |
| 7103 | |
| 7104 | if( dst_len < 32 ) |
| 7105 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7106 | |
| 7107 | mbedtls_sha256_init( &sha256 ); |
| 7108 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 7109 | |
| 7110 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 7111 | { |
| 7112 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 7113 | goto exit; |
| 7114 | } |
| 7115 | |
| 7116 | *olen = 32; |
| 7117 | |
| 7118 | exit: |
| 7119 | |
| 7120 | mbedtls_sha256_free( &sha256 ); |
| 7121 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7122 | } |
| 7123 | #endif /* MBEDTLS_SHA256_C */ |
| 7124 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7125 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 7126 | const mbedtls_md_type_t md, |
| 7127 | unsigned char *dst, |
| 7128 | size_t dst_len, |
| 7129 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7130 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7131 | switch( md ) |
| 7132 | { |
| 7133 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7134 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7135 | case MBEDTLS_MD_SHA384: |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7136 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7137 | #endif /* MBEDTLS_SHA384_C */ |
| 7138 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7139 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7140 | case MBEDTLS_MD_SHA256: |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7141 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7142 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7143 | |
| 7144 | default: |
| 7145 | break; |
| 7146 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7147 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7148 | } |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7149 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7151 | #endif /* MBEDTLS_SSL_TLS_C */ |