Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2 | * TLS shared functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | * http://www.ietf.org/rfc/rfc2246.txt |
| 21 | * http://www.ietf.org/rfc/rfc4346.txt |
| 22 | */ |
| 23 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #if defined(MBEDTLS_PLATFORM_C) |
| 31 | #include "mbedtls/platform.h" |
| 32 | #else |
| 33 | #include <stdlib.h> |
Jerry Yu | 6ade743 | 2022-01-25 10:39:33 +0800 | [diff] [blame] | 34 | #include <stdio.h> |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | #define mbedtls_calloc calloc |
| 36 | #define mbedtls_free free |
Jerry Yu | 6ade743 | 2022-01-25 10:39:33 +0800 | [diff] [blame] | 37 | #define mbedtls_printf printf |
| 38 | #endif /* !MBEDTLS_PLATFORM_C */ |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 39 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/ssl.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 41 | #include "ssl_misc.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 42 | #include "mbedtls/debug.h" |
| 43 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 44 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 45 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 46 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 47 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 48 | #include <string.h> |
| 49 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 50 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 51 | #include "mbedtls/psa_util.h" |
| 52 | #include "psa/crypto.h" |
| 53 | #endif |
| 54 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 55 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 56 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 57 | #endif |
| 58 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 60 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 61 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 62 | /* Top-level Connection ID API */ |
| 63 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 64 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 65 | size_t len, |
| 66 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 67 | { |
| 68 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 69 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 70 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 71 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 72 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 73 | { |
| 74 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 75 | } |
| 76 | |
| 77 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 78 | conf->cid_len = len; |
| 79 | return( 0 ); |
| 80 | } |
| 81 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 82 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 83 | int enable, |
| 84 | unsigned char const *own_cid, |
| 85 | size_t own_cid_len ) |
| 86 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 87 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 88 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 89 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 90 | ssl->negotiate_cid = enable; |
| 91 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 92 | { |
| 93 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 94 | return( 0 ); |
| 95 | } |
| 96 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 97 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 98 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 99 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 100 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 101 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 102 | (unsigned) own_cid_len, |
| 103 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 104 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 105 | } |
| 106 | |
| 107 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 108 | /* Truncation is not an issue here because |
| 109 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 110 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 111 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 112 | return( 0 ); |
| 113 | } |
| 114 | |
| 115 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 116 | int *enabled, |
| 117 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 118 | size_t *peer_cid_len ) |
| 119 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 120 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 121 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 122 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 123 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 124 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 125 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 126 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 127 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 128 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 129 | * were used, but client and server requested the empty CID. |
| 130 | * This is indistinguishable from not using the CID extension |
| 131 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 132 | if( ssl->transform_in->in_cid_len == 0 && |
| 133 | ssl->transform_in->out_cid_len == 0 ) |
| 134 | { |
| 135 | return( 0 ); |
| 136 | } |
| 137 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 138 | if( peer_cid_len != NULL ) |
| 139 | { |
| 140 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 141 | if( peer_cid != NULL ) |
| 142 | { |
| 143 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 144 | ssl->transform_in->out_cid_len ); |
| 145 | } |
| 146 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 147 | |
| 148 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 149 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 150 | return( 0 ); |
| 151 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 152 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 157 | /* |
| 158 | * Convert max_fragment_length codes to length. |
| 159 | * RFC 6066 says: |
| 160 | * enum{ |
| 161 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 162 | * } MaxFragmentLength; |
| 163 | * and we add 0 -> extension unused |
| 164 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 165 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 166 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 167 | switch( mfl ) |
| 168 | { |
| 169 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 170 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 171 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 172 | return 512; |
| 173 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 174 | return 1024; |
| 175 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 176 | return 2048; |
| 177 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 178 | return 4096; |
| 179 | default: |
| 180 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 181 | } |
| 182 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 184 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 185 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 186 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 187 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | mbedtls_ssl_session_free( dst ); |
| 189 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 190 | |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 191 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 192 | dst->ticket = NULL; |
| 193 | #endif |
| 194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 196 | |
| 197 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 198 | if( src->peer_cert != NULL ) |
| 199 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 200 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 202 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 203 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 204 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p, |
Manuel Pégourié-Gonnard | 4d2a8eb | 2014-06-13 20:33:27 +0200 | [diff] [blame] | 209 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 212 | dst->peer_cert = NULL; |
| 213 | return( ret ); |
| 214 | } |
| 215 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 216 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 217 | if( src->peer_cert_digest != NULL ) |
| 218 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 219 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 220 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 221 | if( dst->peer_cert_digest == NULL ) |
| 222 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 223 | |
| 224 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 225 | src->peer_cert_digest_len ); |
| 226 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 227 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 228 | } |
| 229 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 232 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 233 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 234 | if( src->ticket != NULL ) |
| 235 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 236 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 237 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 238 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 239 | |
| 240 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 241 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 242 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 243 | |
| 244 | return( 0 ); |
| 245 | } |
| 246 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 247 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 248 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
| 249 | { |
| 250 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); |
| 251 | if( resized_buffer == NULL ) |
| 252 | return -1; |
| 253 | |
| 254 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 255 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 256 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 257 | * lost, are done outside of this function. */ |
| 258 | memcpy( resized_buffer, *buffer, |
| 259 | ( len_new < *len_old ) ? len_new : *len_old ); |
| 260 | mbedtls_platform_zeroize( *buffer, *len_old ); |
| 261 | mbedtls_free( *buffer ); |
| 262 | |
| 263 | *buffer = resized_buffer; |
| 264 | *len_old = len_new; |
| 265 | |
| 266 | return 0; |
| 267 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 268 | |
| 269 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, |
Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 270 | size_t in_buf_new_len, |
| 271 | size_t out_buf_new_len ) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 272 | { |
| 273 | int modified = 0; |
| 274 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 275 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
| 276 | if( ssl->in_buf != NULL ) |
| 277 | { |
| 278 | written_in = ssl->in_msg - ssl->in_buf; |
| 279 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 280 | len_offset_in = ssl->in_len - ssl->in_buf; |
| 281 | if( downsizing ? |
| 282 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : |
| 283 | ssl->in_buf_len < in_buf_new_len ) |
| 284 | { |
| 285 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) |
| 286 | { |
| 287 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 288 | } |
| 289 | else |
| 290 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 291 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 292 | in_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 293 | modified = 1; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if( ssl->out_buf != NULL ) |
| 299 | { |
| 300 | written_out = ssl->out_msg - ssl->out_buf; |
| 301 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 302 | len_offset_out = ssl->out_len - ssl->out_buf; |
| 303 | if( downsizing ? |
| 304 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : |
| 305 | ssl->out_buf_len < out_buf_new_len ) |
| 306 | { |
| 307 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) |
| 308 | { |
| 309 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 310 | } |
| 311 | else |
| 312 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 313 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 314 | out_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 315 | modified = 1; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | if( modified ) |
| 320 | { |
| 321 | /* Update pointers here to avoid doing it twice. */ |
| 322 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 323 | /* Fields below might not be properly updated with record |
| 324 | * splitting or with CID, so they are manually updated here. */ |
| 325 | ssl->out_msg = ssl->out_buf + written_out; |
| 326 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 327 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 328 | |
| 329 | ssl->in_msg = ssl->in_buf + written_in; |
| 330 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 331 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 332 | } |
| 333 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 334 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 335 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 336 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 337 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 338 | |
| 339 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 340 | mbedtls_svc_key_id_t key, |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 341 | psa_algorithm_t alg, |
| 342 | const unsigned char* seed, size_t seed_length, |
| 343 | const unsigned char* label, size_t label_length, |
| 344 | size_t capacity ) |
| 345 | { |
| 346 | psa_status_t status; |
| 347 | |
| 348 | status = psa_key_derivation_setup( derivation, alg ); |
| 349 | if( status != PSA_SUCCESS ) |
| 350 | return( status ); |
| 351 | |
| 352 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 353 | { |
| 354 | status = psa_key_derivation_input_bytes( derivation, |
| 355 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 356 | seed, seed_length ); |
| 357 | if( status != PSA_SUCCESS ) |
| 358 | return( status ); |
| 359 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 360 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 361 | { |
| 362 | status = psa_key_derivation_input_bytes( |
| 363 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 364 | NULL, 0 ); |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | status = psa_key_derivation_input_key( |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 369 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 370 | } |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 371 | if( status != PSA_SUCCESS ) |
| 372 | return( status ); |
| 373 | |
| 374 | status = psa_key_derivation_input_bytes( derivation, |
| 375 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 376 | label, label_length ); |
| 377 | if( status != PSA_SUCCESS ) |
| 378 | return( status ); |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 383 | } |
| 384 | |
| 385 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 386 | if( status != PSA_SUCCESS ) |
| 387 | return( status ); |
| 388 | |
| 389 | return( PSA_SUCCESS ); |
| 390 | } |
| 391 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 392 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 393 | const unsigned char *secret, size_t slen, |
| 394 | const char *label, |
| 395 | const unsigned char *random, size_t rlen, |
| 396 | unsigned char *dstbuf, size_t dlen ) |
| 397 | { |
| 398 | psa_status_t status; |
| 399 | psa_algorithm_t alg; |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 400 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 401 | psa_key_derivation_operation_t derivation = |
Janos Follath | 8dee877 | 2019-07-30 12:53:32 +0100 | [diff] [blame] | 402 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 403 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 404 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 405 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 406 | else |
| 407 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 408 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 409 | /* Normally a "secret" should be long enough to be impossible to |
| 410 | * find by brute force, and in particular should not be empty. But |
| 411 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 412 | * and for this use case it makes sense to have a 0-length "secret". |
| 413 | * Since the key API doesn't allow importing a key of length 0, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 414 | * keep master_key=0, which setup_psa_key_derivation() understands |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 415 | * to mean a 0-length "secret" input. */ |
| 416 | if( slen != 0 ) |
| 417 | { |
| 418 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 419 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 420 | psa_set_key_algorithm( &key_attributes, alg ); |
| 421 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 422 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 423 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 424 | if( status != PSA_SUCCESS ) |
| 425 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 426 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 427 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 428 | status = setup_psa_key_derivation( &derivation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 429 | master_key, alg, |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 430 | random, rlen, |
| 431 | (unsigned char const *) label, |
| 432 | (size_t) strlen( label ), |
| 433 | dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 434 | if( status != PSA_SUCCESS ) |
| 435 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 436 | psa_key_derivation_abort( &derivation ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 437 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 438 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 439 | } |
| 440 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 441 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 442 | if( status != PSA_SUCCESS ) |
| 443 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 444 | psa_key_derivation_abort( &derivation ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 445 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 446 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 447 | } |
| 448 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 449 | status = psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 450 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 451 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 452 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 453 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 454 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 455 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 456 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 457 | status = psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 458 | if( status != PSA_SUCCESS ) |
| 459 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 460 | |
Andrzej Kurek | 3317126 | 2019-01-15 03:25:18 -0500 | [diff] [blame] | 461 | return( 0 ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 465 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 466 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 467 | const unsigned char *secret, size_t slen, |
| 468 | const char *label, |
| 469 | const unsigned char *random, size_t rlen, |
| 470 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 471 | { |
| 472 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 473 | size_t i, j, k, md_len; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 474 | unsigned char *tmp; |
| 475 | size_t tmp_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 477 | const mbedtls_md_info_t *md_info; |
| 478 | mbedtls_md_context_t md_ctx; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 479 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 481 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 483 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 484 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 486 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 487 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 488 | tmp_len = md_len + strlen( label ) + rlen; |
| 489 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 490 | if( tmp == NULL ) |
| 491 | { |
| 492 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 493 | goto exit; |
| 494 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 495 | |
| 496 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 497 | memcpy( tmp + md_len, label, nb ); |
| 498 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 499 | nb += rlen; |
| 500 | |
| 501 | /* |
| 502 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 503 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 504 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 505 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 506 | |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 507 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 508 | if( ret != 0 ) |
| 509 | goto exit; |
| 510 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 511 | if( ret != 0 ) |
| 512 | goto exit; |
| 513 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 514 | if( ret != 0 ) |
| 515 | goto exit; |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 516 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 517 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 518 | { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 519 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 520 | if( ret != 0 ) |
| 521 | goto exit; |
| 522 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 523 | if( ret != 0 ) |
| 524 | goto exit; |
| 525 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 526 | if( ret != 0 ) |
| 527 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 528 | |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 529 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 530 | if( ret != 0 ) |
| 531 | goto exit; |
| 532 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 533 | if( ret != 0 ) |
| 534 | goto exit; |
| 535 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 536 | if( ret != 0 ) |
| 537 | goto exit; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 538 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 539 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 540 | |
| 541 | for( j = 0; j < k; j++ ) |
| 542 | dstbuf[i + j] = h_i[j]; |
| 543 | } |
| 544 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 545 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 547 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 548 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 549 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 550 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 551 | mbedtls_free( tmp ); |
| 552 | |
| 553 | return( ret ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 554 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 555 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 557 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 558 | const char *label, |
| 559 | const unsigned char *random, size_t rlen, |
| 560 | unsigned char *dstbuf, size_t dlen ) |
| 561 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 563 | label, random, rlen, dstbuf, dlen ) ); |
| 564 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 566 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 567 | #if defined(MBEDTLS_SHA384_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 568 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 569 | const char *label, |
| 570 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 571 | unsigned char *dstbuf, size_t dlen ) |
| 572 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 574 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 575 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 576 | #endif /* MBEDTLS_SHA384_C */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 577 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 578 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 579 | 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] | 580 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 581 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 582 | #if defined(MBEDTLS_SHA256_C) |
| 583 | 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] | 584 | 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] | 585 | 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] | 586 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 587 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 588 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | 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] | 590 | 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] | 591 | 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] | 592 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 593 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 594 | |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 595 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 596 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 597 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 598 | { |
| 599 | if( ssl->conf->f_psk != NULL ) |
| 600 | { |
| 601 | /* If we've used a callback to select the PSK, |
| 602 | * the static configuration is irrelevant. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 603 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 604 | return( 1 ); |
| 605 | |
| 606 | return( 0 ); |
| 607 | } |
| 608 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 609 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 610 | return( 1 ); |
| 611 | |
| 612 | return( 0 ); |
| 613 | } |
| 614 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 615 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 616 | |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 617 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 618 | { |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 619 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 620 | #if defined(MBEDTLS_SHA384_C) |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 621 | if( tls_prf == tls_prf_sha384 ) |
| 622 | { |
| 623 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 624 | } |
| 625 | else |
| 626 | #endif |
| 627 | #if defined(MBEDTLS_SHA256_C) |
| 628 | if( tls_prf == tls_prf_sha256 ) |
| 629 | { |
| 630 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 631 | } |
| 632 | else |
| 633 | #endif |
| 634 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 635 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 636 | } |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 637 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 638 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 639 | const unsigned char *secret, size_t slen, |
| 640 | const char *label, |
| 641 | const unsigned char *random, size_t rlen, |
| 642 | unsigned char *dstbuf, size_t dlen ) |
| 643 | { |
| 644 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 645 | |
| 646 | switch( prf ) |
| 647 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 648 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 649 | #if defined(MBEDTLS_SHA384_C) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 650 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 651 | tls_prf = tls_prf_sha384; |
| 652 | break; |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 653 | #endif /* MBEDTLS_SHA384_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 654 | #if defined(MBEDTLS_SHA256_C) |
| 655 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 656 | tls_prf = tls_prf_sha256; |
| 657 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 658 | #endif /* MBEDTLS_SHA256_C */ |
| 659 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 660 | default: |
| 661 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 662 | } |
| 663 | |
| 664 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 665 | } |
| 666 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 667 | /* Type for the TLS PRF */ |
| 668 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 669 | const unsigned char *, size_t, |
| 670 | unsigned char *, size_t); |
| 671 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 672 | /* |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 673 | * Populate a transform structure with session keys and all the other |
| 674 | * necessary information. |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 675 | * |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 676 | * Parameters: |
| 677 | * - [in/out]: transform: structure to populate |
| 678 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 679 | * [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] | 680 | * - [in] ciphersuite |
| 681 | * - [in] master |
| 682 | * - [in] encrypt_then_mac |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 683 | * - [in] compression |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 684 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 685 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 686 | * - [in] minor_ver: SSL/TLS minor version |
| 687 | * - [in] endpoint: client or server |
Andrzej Kurek | 5902cd6 | 2021-09-28 10:00:32 -0400 | [diff] [blame] | 688 | * - [in] ssl: used for: |
| 689 | * - ssl->conf->{f,p}_export_keys |
| 690 | * [in] optionally used for: |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 691 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 692 | */ |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 693 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 694 | int ciphersuite, |
| 695 | const unsigned char master[48], |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 696 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 697 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 698 | int encrypt_then_mac, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 699 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 700 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 701 | ssl_tls_prf_t tls_prf, |
| 702 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 703 | int minor_ver, |
| 704 | unsigned endpoint, |
Mateusz Starzyk | e204dbf | 2021-03-15 17:57:20 +0100 | [diff] [blame] | 705 | const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 706 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 707 | int ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 708 | unsigned char keyblk[256]; |
| 709 | unsigned char *key1; |
| 710 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 711 | unsigned char *mac_enc; |
| 712 | unsigned char *mac_dec; |
sander-visser | 3888b03 | 2020-05-06 21:49:46 +0200 | [diff] [blame] | 713 | size_t mac_key_len = 0; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 714 | size_t iv_copy_len; |
Gilles Peskine | 88d681c | 2021-09-01 11:19:33 +0200 | [diff] [blame] | 715 | size_t keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 716 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | const mbedtls_cipher_info_t *cipher_info; |
| 718 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 719 | |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 720 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 721 | psa_key_type_t key_type; |
| 722 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 723 | psa_algorithm_t alg; |
| 724 | size_t key_bits; |
| 725 | psa_status_t status; |
| 726 | #endif |
| 727 | |
Andrzej Kurek | 324f72e | 2021-09-29 04:21:21 -0400 | [diff] [blame] | 728 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 729 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Andrzej Kurek | a72fe64 | 2021-09-29 15:57:30 -0400 | [diff] [blame] | 730 | if( ssl->f_export_keys == NULL ) |
| 731 | { |
| 732 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 733 | (void) ssl; |
| 734 | } |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 735 | #endif |
| 736 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 737 | /* |
| 738 | * Some data just needs copying into the structure |
| 739 | */ |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 740 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 741 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 742 | transform->encrypt_then_mac = encrypt_then_mac; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 743 | #endif |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 744 | transform->minor_ver = minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 745 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 746 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 747 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 748 | #endif |
| 749 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 750 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 751 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 752 | { |
| 753 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 754 | * generation separate. This should never happen. */ |
| 755 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 756 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 757 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 758 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 759 | /* |
| 760 | * Get various info structures |
| 761 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 762 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 763 | if( ciphersuite_info == NULL ) |
| 764 | { |
| 765 | 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] | 766 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 767 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 768 | } |
| 769 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 770 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 771 | if( cipher_info == NULL ) |
| 772 | { |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 773 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 774 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 775 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 778 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 779 | if( md_info == NULL ) |
| 780 | { |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 781 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 782 | (unsigned) ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 783 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 784 | } |
| 785 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 786 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 787 | /* Copy own and peer's CID if the use of the CID |
| 788 | * extension has been negotiated. */ |
| 789 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 790 | { |
| 791 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 792 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 793 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 794 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 795 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 796 | transform->in_cid_len ); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 797 | |
| 798 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 799 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 800 | ssl->handshake->peer_cid_len ); |
| 801 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 802 | transform->out_cid_len ); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 803 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 804 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 805 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 806 | /* |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 807 | * Compute key block using the PRF |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 808 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 809 | 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] | 810 | if( ret != 0 ) |
| 811 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 812 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 813 | return( ret ); |
| 814 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 815 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 817 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 818 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 819 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 820 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 821 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 822 | /* |
| 823 | * Determine the appropriate key, IV and MAC length. |
| 824 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 825 | |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 826 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 827 | |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 828 | #if defined(MBEDTLS_GCM_C) || \ |
| 829 | defined(MBEDTLS_CCM_C) || \ |
| 830 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 831 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM || |
| 832 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM || |
| 833 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 834 | { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 835 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 836 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 837 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 838 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 839 | transform->taglen = |
| 840 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 841 | |
Hanno Becker | 447558d | 2020-05-28 07:36:33 +0100 | [diff] [blame] | 842 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 843 | * with mode and version: |
| 844 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 845 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 846 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 847 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 848 | * sequence number). |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 849 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 850 | transform->ivlen = 12; |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 851 | 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] | 852 | transform->fixed_ivlen = 12; |
| 853 | else |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 854 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 855 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 856 | /* Minimum length of encrypted record */ |
| 857 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 858 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 859 | } |
| 860 | else |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 861 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 862 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 863 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM || |
| 864 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 865 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 866 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 867 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 868 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 869 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 870 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 871 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 872 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 873 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 874 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 875 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 876 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 877 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 878 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 879 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 880 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 881 | /* Minimum length */ |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 882 | 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] | 883 | transform->minlen = transform->maclen; |
| 884 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 885 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 886 | /* |
| 887 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 888 | * 1. if EtM is in use: one block plus MAC |
| 889 | * otherwise: * first multiple of blocklen greater than maclen |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 890 | * 2. IV |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 891 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 892 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 893 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 894 | { |
| 895 | transform->minlen = transform->maclen |
| 896 | + cipher_info->block_size; |
| 897 | } |
| 898 | else |
| 899 | #endif |
| 900 | { |
| 901 | transform->minlen = transform->maclen |
| 902 | + cipher_info->block_size |
| 903 | - transform->maclen % cipher_info->block_size; |
| 904 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 905 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 906 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 907 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 908 | { |
| 909 | transform->minlen += transform->ivlen; |
| 910 | } |
| 911 | else |
| 912 | #endif |
| 913 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 914 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 915 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 916 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 917 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 918 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 919 | } |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 920 | else |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 921 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 922 | { |
| 923 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 924 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 925 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 926 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 927 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 928 | (unsigned) keylen, |
| 929 | (unsigned) transform->minlen, |
| 930 | (unsigned) transform->ivlen, |
| 931 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 932 | |
| 933 | /* |
| 934 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 935 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 936 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 937 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 938 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 939 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 940 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 941 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 942 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 943 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 944 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 945 | /* |
| 946 | * This is not used in TLS v1.1. |
| 947 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 948 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 949 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 950 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 951 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 952 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 953 | } |
| 954 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 955 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 956 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 957 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 958 | { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 959 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 960 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 961 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 962 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 963 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 964 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 965 | /* |
| 966 | * This is not used in TLS v1.1. |
| 967 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 968 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 969 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 970 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 971 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 972 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 973 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 974 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 975 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 976 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 977 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 978 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 979 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 980 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 981 | |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 982 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 983 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 984 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 985 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 986 | if( mac_key_len != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 987 | { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 988 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 989 | if( ret != 0 ) |
| 990 | goto end; |
| 991 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 992 | if( ret != 0 ) |
| 993 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 994 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 995 | #endif |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 996 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 997 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 998 | ((void) mac_dec); |
| 999 | ((void) mac_enc); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1000 | |
Andrzej Kurek | a72fe64 | 2021-09-29 15:57:30 -0400 | [diff] [blame] | 1001 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1002 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 1003 | ssl->f_export_keys( ssl->p_export_keys, |
| 1004 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 1005 | master, 48, |
| 1006 | randbytes + 32, |
| 1007 | randbytes, |
| 1008 | tls_prf_get_type( tls_prf ) ); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1009 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1010 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1011 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame^] | 1012 | if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type, |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1013 | transform->taglen, |
| 1014 | &alg, |
| 1015 | &key_type, |
| 1016 | &key_bits ) ) != PSA_SUCCESS ) |
| 1017 | { |
Przemyslaw Stekiel | 76e1583 | 2022-01-12 11:54:49 +0100 | [diff] [blame] | 1018 | ret = psa_status_to_mbedtls( status ); |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame^] | 1019 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1020 | goto end; |
| 1021 | } |
| 1022 | |
Przemyslaw Stekiel | fe7397d | 2022-01-17 15:47:07 +0100 | [diff] [blame] | 1023 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1024 | psa_set_key_algorithm( &attributes, alg ); |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1025 | psa_set_key_type( &attributes, key_type ); |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1026 | |
| 1027 | transform->psa_alg = alg; |
| 1028 | |
| 1029 | if( ( status = psa_import_key( &attributes, |
| 1030 | key1, |
Przemyslaw Stekiel | 11a33e6 | 2022-01-12 10:42:58 +0100 | [diff] [blame] | 1031 | PSA_BITS_TO_BYTES( key_bits ), |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1032 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 1033 | { |
Przemyslaw Stekiel | 76e1583 | 2022-01-12 11:54:49 +0100 | [diff] [blame] | 1034 | ret = psa_status_to_mbedtls( status ); |
Przemyslaw Stekiel | ce09e7d | 2022-01-17 16:03:22 +0100 | [diff] [blame] | 1035 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1036 | goto end; |
| 1037 | } |
Przemyslaw Stekiel | fe7397d | 2022-01-17 15:47:07 +0100 | [diff] [blame] | 1038 | |
| 1039 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 1040 | |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1041 | if( ( status = psa_import_key( &attributes, |
| 1042 | key2, |
Przemyslaw Stekiel | 11a33e6 | 2022-01-12 10:42:58 +0100 | [diff] [blame] | 1043 | PSA_BITS_TO_BYTES( key_bits ), |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1044 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 1045 | { |
Przemyslaw Stekiel | 76e1583 | 2022-01-12 11:54:49 +0100 | [diff] [blame] | 1046 | ret = psa_status_to_mbedtls( status ); |
Przemyslaw Stekiel | ce09e7d | 2022-01-17 16:03:22 +0100 | [diff] [blame] | 1047 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1048 | goto end; |
| 1049 | } |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1050 | #else |
| 1051 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 1052 | cipher_info ) ) != 0 ) |
| 1053 | { |
| 1054 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 1055 | goto end; |
| 1056 | } |
| 1057 | |
| 1058 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 1059 | cipher_info ) ) != 0 ) |
| 1060 | { |
| 1061 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 1062 | goto end; |
| 1063 | } |
| 1064 | |
| 1065 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 1066 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 1067 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 1068 | { |
| 1069 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 1070 | goto end; |
| 1071 | } |
| 1072 | |
| 1073 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 1074 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 1075 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 1076 | { |
| 1077 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 1078 | goto end; |
| 1079 | } |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1080 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1081 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 1082 | 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] | 1083 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1084 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1085 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1086 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1087 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1088 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1089 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1092 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1093 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1094 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1095 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1096 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1097 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1098 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1099 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1100 | |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1101 | end: |
Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 1102 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1103 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1106 | /* |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1107 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1108 | * |
| 1109 | * Inputs: |
| 1110 | * - SSL/TLS minor version |
| 1111 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1112 | * |
Manuel Pégourié-Gonnard | 31d3ef1 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1113 | * Outputs: |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1114 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1115 | */ |
| 1116 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1117 | int minor_ver, |
| 1118 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1119 | { |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1120 | #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] | 1121 | (void) hash; |
| 1122 | #endif |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1123 | |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1124 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1125 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1126 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1127 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1128 | { |
| 1129 | handshake->tls_prf = tls_prf_sha384; |
| 1130 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1131 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1132 | } |
| 1133 | else |
| 1134 | #endif |
| 1135 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1136 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1137 | { |
| 1138 | handshake->tls_prf = tls_prf_sha256; |
| 1139 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1140 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1141 | } |
| 1142 | else |
| 1143 | #endif |
| 1144 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1145 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1146 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1147 | } |
| 1148 | |
| 1149 | return( 0 ); |
| 1150 | } |
| 1151 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1152 | /* |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1153 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1154 | * |
| 1155 | * Parameters: |
| 1156 | * [in/out] handshake |
| 1157 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1158 | * (PSA-PSK) ciphersuite_info, psk_opaque |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1159 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1160 | * [out] master |
| 1161 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 1162 | * debug: conf->f_dbg, conf->p_dbg |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1163 | * EMS: passed to calc_verify (debug + session_negotiate) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1164 | * PSA-PSA: minor_ver, conf |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1165 | */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1166 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1167 | unsigned char *master, |
Manuel Pégourié-Gonnard | 0d56aaa | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1168 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1169 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1170 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1171 | |
| 1172 | /* cf. RFC 5246, Section 8.1: |
| 1173 | * "The master secret is always exactly 48 bytes in length." */ |
| 1174 | size_t const master_secret_len = 48; |
| 1175 | |
| 1176 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1177 | unsigned char session_hash[48]; |
| 1178 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 1179 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1180 | /* The label for the KDF used for key expansion. |
| 1181 | * This is either "master secret" or "extended master secret" |
| 1182 | * depending on whether the Extended Master Secret extension |
| 1183 | * is used. */ |
| 1184 | char const *lbl = "master secret"; |
| 1185 | |
| 1186 | /* The salt for the KDF used for key expansion. |
| 1187 | * - If the Extended Master Secret extension is not used, |
| 1188 | * this is ClientHello.Random + ServerHello.Random |
| 1189 | * (see Sect. 8.1 in RFC 5246). |
| 1190 | * - If the Extended Master Secret extension is used, |
| 1191 | * this is the transcript of the handshake so far. |
| 1192 | * (see Sect. 4 in RFC 7627). */ |
| 1193 | unsigned char const *salt = handshake->randbytes; |
| 1194 | size_t salt_len = 64; |
| 1195 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1196 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 1197 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 1198 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1199 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1200 | 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] | 1201 | (void) ssl; |
| 1202 | #endif |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1203 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1204 | if( handshake->resume != 0 ) |
| 1205 | { |
| 1206 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1207 | return( 0 ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1208 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1209 | |
| 1210 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1211 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1212 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1213 | lbl = "extended master secret"; |
| 1214 | salt = session_hash; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1215 | handshake->calc_verify( ssl, session_hash, &salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1216 | |
Manuel Pégourié-Gonnard | 8faa70e | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1217 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1218 | session_hash, salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1219 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1220 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 1221 | |
| 1222 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1223 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1224 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1225 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1226 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 1227 | { |
| 1228 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1229 | psa_status_t status; |
| 1230 | psa_algorithm_t alg; |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1231 | mbedtls_svc_key_id_t psk; |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1232 | psa_key_derivation_operation_t derivation = |
| 1233 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1234 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1235 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1236 | 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] | 1237 | |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1238 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1239 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1240 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1241 | 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] | 1242 | else |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1243 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1244 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 1245 | status = setup_psa_key_derivation( &derivation, psk, alg, |
| 1246 | salt, salt_len, |
| 1247 | (unsigned char const *) lbl, |
| 1248 | (size_t) strlen( lbl ), |
| 1249 | master_secret_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1250 | if( status != PSA_SUCCESS ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1251 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1252 | psa_key_derivation_abort( &derivation ); |
| 1253 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1254 | } |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1255 | |
| 1256 | status = psa_key_derivation_output_bytes( &derivation, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1257 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1258 | master_secret_len ); |
| 1259 | if( status != PSA_SUCCESS ) |
| 1260 | { |
| 1261 | psa_key_derivation_abort( &derivation ); |
| 1262 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1263 | } |
| 1264 | |
| 1265 | status = psa_key_derivation_abort( &derivation ); |
| 1266 | if( status != PSA_SUCCESS ) |
| 1267 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1268 | } |
| 1269 | else |
| 1270 | #endif |
| 1271 | { |
| 1272 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1273 | lbl, salt, salt_len, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1274 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1275 | master_secret_len ); |
| 1276 | if( ret != 0 ) |
| 1277 | { |
| 1278 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1279 | return( ret ); |
| 1280 | } |
| 1281 | |
| 1282 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 1283 | handshake->premaster, |
| 1284 | handshake->pmslen ); |
| 1285 | |
| 1286 | mbedtls_platform_zeroize( handshake->premaster, |
| 1287 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | return( 0 ); |
| 1291 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1292 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1293 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1294 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1295 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1296 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1297 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1298 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1299 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1300 | |
| 1301 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1302 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1303 | ssl->minor_ver, |
| 1304 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1305 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1306 | { |
| 1307 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1308 | return( ret ); |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1309 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1310 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1311 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1312 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1313 | ssl->session_negotiate->master, |
| 1314 | ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1315 | if( ret != 0 ) |
| 1316 | { |
| 1317 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1318 | return( ret ); |
| 1319 | } |
| 1320 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1321 | /* Swap the client and server random values: |
| 1322 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1323 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1324 | { |
| 1325 | unsigned char tmp[64]; |
| 1326 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1327 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1328 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1329 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1330 | } |
| 1331 | |
| 1332 | /* Populate transform structure */ |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 1333 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 1334 | ssl->session_negotiate->ciphersuite, |
| 1335 | ssl->session_negotiate->master, |
| 1336 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 1337 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1338 | ssl->session_negotiate->encrypt_then_mac, |
| 1339 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 1340 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1341 | ssl->handshake->tls_prf, |
| 1342 | ssl->handshake->randbytes, |
| 1343 | ssl->minor_ver, |
| 1344 | ssl->conf->endpoint, |
| 1345 | ssl ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1346 | if( ret != 0 ) |
| 1347 | { |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 1348 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1349 | return( ret ); |
| 1350 | } |
| 1351 | |
| 1352 | /* We no longer need Server/ClientHello.random values */ |
| 1353 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1354 | sizeof( ssl->handshake->randbytes ) ); |
| 1355 | |
| 1356 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1357 | |
| 1358 | return( 0 ); |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1359 | } |
| 1360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1361 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1362 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1363 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1364 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1365 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1366 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1367 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1368 | size_t hash_size; |
| 1369 | psa_status_t status; |
| 1370 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1371 | |
| 1372 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 1373 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 1374 | if( status != PSA_SUCCESS ) |
| 1375 | { |
| 1376 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1377 | return; |
| 1378 | } |
| 1379 | |
| 1380 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 1381 | if( status != PSA_SUCCESS ) |
| 1382 | { |
| 1383 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1384 | return; |
| 1385 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1386 | |
| 1387 | *hlen = 32; |
| 1388 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1389 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1390 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1391 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1392 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1393 | mbedtls_sha256_init( &sha256 ); |
| 1394 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1395 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1396 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1397 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 1398 | mbedtls_sha256_finish( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1399 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1400 | *hlen = 32; |
| 1401 | |
| 1402 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1403 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1404 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1405 | mbedtls_sha256_free( &sha256 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1406 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1407 | return; |
| 1408 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1409 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1410 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1411 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1412 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1413 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1414 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1415 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1416 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1417 | size_t hash_size; |
| 1418 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1419 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1420 | |
| 1421 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1422 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1423 | if( status != PSA_SUCCESS ) |
| 1424 | { |
| 1425 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1426 | return; |
| 1427 | } |
| 1428 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1429 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1430 | if( status != PSA_SUCCESS ) |
| 1431 | { |
| 1432 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1433 | return; |
| 1434 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1435 | |
| 1436 | *hlen = 48; |
| 1437 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1438 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1439 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1440 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1441 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1442 | mbedtls_sha512_init( &sha512 ); |
| 1443 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1444 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1445 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1446 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 1447 | mbedtls_sha512_finish( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1448 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1449 | *hlen = 48; |
| 1450 | |
| 1451 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1452 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1453 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1454 | mbedtls_sha512_free( &sha512 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1455 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1456 | return; |
| 1457 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1458 | #endif /* MBEDTLS_SHA384_C */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1459 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1460 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1461 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1462 | 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] | 1463 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1464 | unsigned char *p = ssl->handshake->premaster; |
| 1465 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Guilhem Bryant | b5f04e4 | 2020-04-01 11:23:58 +0100 | [diff] [blame] | 1466 | const unsigned char *psk = NULL; |
Guilhem Bryant | 61b0fe6 | 2020-03-27 11:12:12 +0000 | [diff] [blame] | 1467 | size_t psk_len = 0; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1468 | |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1469 | if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) |
| 1470 | == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1471 | { |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1472 | /* |
| 1473 | * This should never happen because the existence of a PSK is always |
| 1474 | * checked before calling this function |
| 1475 | */ |
Guilhem Bryant | 82194c8 | 2020-03-26 17:04:31 +0000 | [diff] [blame] | 1476 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Guilhem Bryant | 0c9b195 | 2020-04-08 11:02:38 +0100 | [diff] [blame] | 1477 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1478 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1479 | |
| 1480 | /* |
| 1481 | * PMS = struct { |
| 1482 | * opaque other_secret<0..2^16-1>; |
| 1483 | * opaque psk<0..2^16-1>; |
| 1484 | * }; |
| 1485 | * with "other_secret" depending on the particular key exchange |
| 1486 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1487 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1488 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1489 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1490 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1491 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1492 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1493 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 1494 | p += 2; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1495 | |
| 1496 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1497 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1498 | |
| 1499 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1500 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1501 | } |
| 1502 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1503 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1504 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1505 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1506 | { |
| 1507 | /* |
| 1508 | * other_secret already set by the ClientKeyExchange message, |
| 1509 | * and is 48 bytes long |
| 1510 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1511 | if( end - p < 2 ) |
| 1512 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1513 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1514 | *p++ = 0; |
| 1515 | *p++ = 48; |
| 1516 | p += 48; |
| 1517 | } |
| 1518 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1519 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1520 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1521 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1522 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1523 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1524 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1525 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1526 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1527 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1528 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1529 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1531 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1532 | return( ret ); |
| 1533 | } |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1534 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 1535 | p += 2 + len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1536 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1537 | 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] | 1538 | } |
| 1539 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1540 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1541 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1542 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1543 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1544 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1545 | size_t zlen; |
| 1546 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1547 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1548 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1549 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1550 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1551 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1552 | return( ret ); |
| 1553 | } |
| 1554 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1555 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 1556 | p += 2 + zlen; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1557 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1558 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1559 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1560 | } |
| 1561 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1562 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1563 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1564 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1565 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1569 | if( end - p < 2 ) |
| 1570 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1571 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1572 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 1573 | p += 2; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1574 | |
| 1575 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1576 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1577 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1578 | memcpy( p, psk, psk_len ); |
| 1579 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1580 | |
| 1581 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1582 | |
| 1583 | return( 0 ); |
| 1584 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1585 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1586 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1587 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1588 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1590 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 1591 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1592 | { |
| 1593 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 1594 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1595 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1596 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1597 | 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] | 1598 | unsigned char doublings = 1; |
| 1599 | |
| 1600 | while( ratio != 0 ) |
| 1601 | { |
| 1602 | ++doublings; |
| 1603 | ratio >>= 1; |
| 1604 | } |
| 1605 | |
| 1606 | if( ++ssl->renego_records_seen > doublings ) |
| 1607 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 1608 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1609 | return( 0 ); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | return( ssl_write_hello_request( ssl ) ); |
| 1614 | } |
| 1615 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1616 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1617 | |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 1618 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1619 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 1620 | { |
| 1621 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1622 | if( session->peer_cert != NULL ) |
| 1623 | { |
| 1624 | mbedtls_x509_crt_free( session->peer_cert ); |
| 1625 | mbedtls_free( session->peer_cert ); |
| 1626 | session->peer_cert = NULL; |
| 1627 | } |
| 1628 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1629 | if( session->peer_cert_digest != NULL ) |
| 1630 | { |
| 1631 | /* Zeroization is not necessary. */ |
| 1632 | mbedtls_free( session->peer_cert_digest ); |
| 1633 | session->peer_cert_digest = NULL; |
| 1634 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 1635 | session->peer_cert_digest_len = 0; |
| 1636 | } |
| 1637 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1638 | } |
| 1639 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 1640 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1641 | /* |
| 1642 | * Handshake functions |
| 1643 | */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1644 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1645 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1646 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1647 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1648 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1649 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1651 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1652 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1653 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1654 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1655 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1656 | ssl->state++; |
| 1657 | return( 0 ); |
| 1658 | } |
| 1659 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1660 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1661 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1662 | } |
| 1663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1665 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1666 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1667 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1669 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1670 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1671 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1672 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1673 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1674 | ssl->state++; |
| 1675 | return( 0 ); |
| 1676 | } |
| 1677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1678 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1679 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1680 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1681 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1682 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1683 | /* Some certificate support -> implement write and parse */ |
| 1684 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1685 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1686 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1687 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1688 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1689 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1690 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1691 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1692 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1693 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1694 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1695 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1696 | { |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 1697 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 1698 | ssl->state++; |
| 1699 | return( 0 ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1700 | } |
| 1701 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1702 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1703 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1704 | { |
| 1705 | if( ssl->client_auth == 0 ) |
| 1706 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1707 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1708 | ssl->state++; |
| 1709 | return( 0 ); |
| 1710 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1711 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1712 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1713 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1714 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1715 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1716 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1717 | { |
Hanno Becker | 9cfe6e9 | 2021-04-30 05:38:24 +0100 | [diff] [blame] | 1718 | /* Should never happen because we shouldn't have picked the |
| 1719 | * ciphersuite if we don't have a certificate. */ |
| 1720 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1721 | } |
| 1722 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1723 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1725 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1726 | |
| 1727 | /* |
| 1728 | * 0 . 0 handshake type |
| 1729 | * 1 . 3 handshake length |
| 1730 | * 4 . 6 length of all certs |
| 1731 | * 7 . 9 length of cert. 1 |
| 1732 | * 10 . n-1 peer certificate |
| 1733 | * n . n+2 length of cert. 2 |
| 1734 | * n+3 . ... upper level cert, etc. |
| 1735 | */ |
| 1736 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1737 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1738 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 1739 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1740 | { |
| 1741 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1742 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1743 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1744 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 1745 | " > %" MBEDTLS_PRINTF_SIZET, |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 1746 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Hanno Becker | 91e1cc3 | 2021-04-30 05:28:49 +0100 | [diff] [blame] | 1747 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Joe Subbiani | fbeb692 | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 1750 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 1751 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 1752 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1753 | |
| 1754 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 1755 | i += n; crt = crt->next; |
| 1756 | } |
| 1757 | |
Joe Subbiani | fbeb692 | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 1758 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 1759 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 1760 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1761 | |
| 1762 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1763 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 1764 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1765 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1766 | ssl->state++; |
| 1767 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1768 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1769 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1770 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1771 | return( ret ); |
| 1772 | } |
| 1773 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1774 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1775 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1776 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 1779 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1780 | |
| 1781 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1782 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 1783 | unsigned char *crt_buf, |
| 1784 | size_t crt_buf_len ) |
| 1785 | { |
| 1786 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 1787 | |
| 1788 | if( peer_crt == NULL ) |
| 1789 | return( -1 ); |
| 1790 | |
| 1791 | if( peer_crt->raw.len != crt_buf_len ) |
| 1792 | return( -1 ); |
| 1793 | |
k-stachowiak | 95b68ef | 2019-09-16 12:21:00 +0200 | [diff] [blame] | 1794 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1795 | } |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1796 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1797 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 1798 | unsigned char *crt_buf, |
| 1799 | size_t crt_buf_len ) |
| 1800 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1801 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1802 | unsigned char const * const peer_cert_digest = |
| 1803 | ssl->session->peer_cert_digest; |
| 1804 | mbedtls_md_type_t const peer_cert_digest_type = |
| 1805 | ssl->session->peer_cert_digest_type; |
| 1806 | mbedtls_md_info_t const * const digest_info = |
| 1807 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 1808 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 1809 | size_t digest_len; |
| 1810 | |
| 1811 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 1812 | return( -1 ); |
| 1813 | |
| 1814 | digest_len = mbedtls_md_get_size( digest_info ); |
| 1815 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 1816 | return( -1 ); |
| 1817 | |
| 1818 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 1819 | if( ret != 0 ) |
| 1820 | return( -1 ); |
| 1821 | |
| 1822 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 1823 | } |
| 1824 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 1825 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1826 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1827 | /* |
| 1828 | * Once the certificate message is read, parse it into a cert chain and |
| 1829 | * perform basic checks, but leave actual verification to the caller |
| 1830 | */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1831 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 1832 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1833 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1834 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1835 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 1836 | int crt_cnt=0; |
| 1837 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1838 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 1839 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1840 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1841 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1842 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1843 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1844 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1845 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1846 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1849 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 1850 | { |
| 1851 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1852 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 1853 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 1854 | } |
| 1855 | |
| 1856 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1857 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1858 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1859 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1860 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1861 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1864 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1865 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1866 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1867 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1868 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1869 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1870 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1871 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1872 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1873 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1874 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1875 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1876 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1877 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1880 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 1881 | i += 3; |
| 1882 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1883 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1884 | while( i < ssl->in_hslen ) |
| 1885 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1886 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1887 | if ( i + 3 > ssl->in_hslen ) { |
| 1888 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1889 | mbedtls_ssl_send_alert_message( ssl, |
| 1890 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1891 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1892 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1893 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1894 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 1895 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1896 | if( ssl->in_msg[i] != 0 ) |
| 1897 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1898 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1899 | mbedtls_ssl_send_alert_message( ssl, |
| 1900 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1901 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 1902 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1905 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1906 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 1907 | | (unsigned int) ssl->in_msg[i + 2]; |
| 1908 | i += 3; |
| 1909 | |
| 1910 | if( n < 128 || i + n > ssl->in_hslen ) |
| 1911 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1912 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1913 | mbedtls_ssl_send_alert_message( ssl, |
| 1914 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1915 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1916 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1919 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1920 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 1921 | if( crt_cnt++ == 0 && |
| 1922 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 1923 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1924 | { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 1925 | /* During client-side renegotiation, check that the server's |
| 1926 | * end-CRTs hasn't changed compared to the initial handshake, |
| 1927 | * mitigating the triple handshake attack. On success, reuse |
| 1928 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1929 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 1930 | if( ssl_check_peer_crt_unchanged( ssl, |
| 1931 | &ssl->in_msg[i], |
| 1932 | n ) != 0 ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1933 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1934 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 1935 | mbedtls_ssl_send_alert_message( ssl, |
| 1936 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1937 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 1938 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1939 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1940 | |
| 1941 | /* Now we can safely free the original chain. */ |
| 1942 | ssl_clear_peer_cert( ssl->session ); |
| 1943 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1944 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 1945 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1946 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 1947 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1948 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 1949 | #else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 1950 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 1951 | * it in-place from the input buffer instead of making a copy. */ |
| 1952 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 1953 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1954 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1955 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1956 | case 0: /*ok*/ |
| 1957 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 1958 | /* Ignore certificate with an unknown algorithm: maybe a |
| 1959 | prior certificate was already trusted. */ |
| 1960 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1961 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1962 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 1963 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 1964 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1965 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1966 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 1967 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 1968 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1969 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1970 | default: |
| 1971 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 1972 | crt_parse_der_failed: |
| 1973 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 1974 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 1975 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | i += n; |
| 1979 | } |
| 1980 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1981 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1982 | return( 0 ); |
| 1983 | } |
| 1984 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1985 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1986 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 1987 | { |
| 1988 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1989 | return( -1 ); |
| 1990 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1991 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1992 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 1993 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 1994 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 1995 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 1996 | { |
| 1997 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 1998 | return( 0 ); |
| 1999 | } |
| 2000 | |
| 2001 | return( -1 ); |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 2002 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2003 | } |
| 2004 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2005 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2006 | /* Check if a certificate message is expected. |
| 2007 | * Return either |
| 2008 | * - SSL_CERTIFICATE_EXPECTED, or |
| 2009 | * - SSL_CERTIFICATE_SKIP |
| 2010 | * indicating whether a Certificate message is expected or not. |
| 2011 | */ |
| 2012 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 2013 | #define SSL_CERTIFICATE_SKIP 1 |
| 2014 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 2015 | int authmode ) |
| 2016 | { |
| 2017 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2018 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2019 | |
| 2020 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 2021 | return( SSL_CERTIFICATE_SKIP ); |
| 2022 | |
| 2023 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2024 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2025 | { |
| 2026 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 2027 | return( SSL_CERTIFICATE_SKIP ); |
| 2028 | |
| 2029 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 2030 | { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2031 | ssl->session_negotiate->verify_result = |
| 2032 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 2033 | return( SSL_CERTIFICATE_SKIP ); |
| 2034 | } |
| 2035 | } |
Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 2036 | #else |
| 2037 | ((void) authmode); |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2038 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2039 | |
| 2040 | return( SSL_CERTIFICATE_EXPECTED ); |
| 2041 | } |
| 2042 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2043 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 2044 | int authmode, |
| 2045 | mbedtls_x509_crt *chain, |
| 2046 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2047 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2048 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2049 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2050 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2051 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2052 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2053 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 2054 | void *p_vrfy; |
| 2055 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2056 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 2057 | return( 0 ); |
| 2058 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2059 | if( ssl->f_vrfy != NULL ) |
| 2060 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2061 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2062 | f_vrfy = ssl->f_vrfy; |
| 2063 | p_vrfy = ssl->p_vrfy; |
| 2064 | } |
| 2065 | else |
| 2066 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2067 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2068 | f_vrfy = ssl->conf->f_vrfy; |
| 2069 | p_vrfy = ssl->conf->p_vrfy; |
| 2070 | } |
| 2071 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2072 | /* |
| 2073 | * Main check: verify certificate |
| 2074 | */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2075 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 2076 | if( ssl->conf->f_ca_cb != NULL ) |
| 2077 | { |
| 2078 | ((void) rs_ctx); |
| 2079 | have_ca_chain = 1; |
| 2080 | |
| 2081 | 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] | 2082 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2083 | chain, |
| 2084 | ssl->conf->f_ca_cb, |
| 2085 | ssl->conf->p_ca_cb, |
| 2086 | ssl->conf->cert_profile, |
| 2087 | ssl->hostname, |
| 2088 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2089 | f_vrfy, p_vrfy ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2090 | } |
| 2091 | else |
| 2092 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 2093 | { |
| 2094 | mbedtls_x509_crt *ca_chain; |
| 2095 | mbedtls_x509_crl *ca_crl; |
| 2096 | |
| 2097 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2098 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 2099 | { |
| 2100 | ca_chain = ssl->handshake->sni_ca_chain; |
| 2101 | ca_crl = ssl->handshake->sni_ca_crl; |
| 2102 | } |
| 2103 | else |
| 2104 | #endif |
| 2105 | { |
| 2106 | ca_chain = ssl->conf->ca_chain; |
| 2107 | ca_crl = ssl->conf->ca_crl; |
| 2108 | } |
| 2109 | |
| 2110 | if( ca_chain != NULL ) |
| 2111 | have_ca_chain = 1; |
| 2112 | |
| 2113 | ret = mbedtls_x509_crt_verify_restartable( |
| 2114 | chain, |
| 2115 | ca_chain, ca_crl, |
| 2116 | ssl->conf->cert_profile, |
| 2117 | ssl->hostname, |
| 2118 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2119 | f_vrfy, p_vrfy, rs_ctx ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2120 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2121 | |
| 2122 | if( ret != 0 ) |
| 2123 | { |
| 2124 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 2125 | } |
| 2126 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2127 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2128 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2129 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 2130 | #endif |
| 2131 | |
| 2132 | /* |
| 2133 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 2134 | */ |
| 2135 | |
| 2136 | #if defined(MBEDTLS_ECP_C) |
| 2137 | { |
| 2138 | const mbedtls_pk_context *pk = &chain->pk; |
| 2139 | |
| 2140 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 2141 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 2142 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 2143 | { |
| 2144 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 2145 | |
| 2146 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 2147 | if( ret == 0 ) |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2148 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2149 | } |
| 2150 | } |
| 2151 | #endif /* MBEDTLS_ECP_C */ |
| 2152 | |
| 2153 | if( mbedtls_ssl_check_cert_usage( chain, |
| 2154 | ciphersuite_info, |
| 2155 | ! ssl->conf->endpoint, |
| 2156 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 2157 | { |
| 2158 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 2159 | if( ret == 0 ) |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2160 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 2164 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 2165 | * with details encoded in the verification flags. All other kinds |
| 2166 | * of error codes, including those from the user provided f_vrfy |
| 2167 | * functions, are treated as fatal and lead to a failure of |
| 2168 | * ssl_parse_certificate even if verification was optional. */ |
| 2169 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 2170 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2171 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2172 | { |
| 2173 | ret = 0; |
| 2174 | } |
| 2175 | |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2176 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2177 | { |
| 2178 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 2179 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 2180 | } |
| 2181 | |
| 2182 | if( ret != 0 ) |
| 2183 | { |
| 2184 | uint8_t alert; |
| 2185 | |
| 2186 | /* The certificate may have been rejected for several reasons. |
| 2187 | Pick one and send the corresponding alert. Which alert to send |
| 2188 | may be a subject of debate in some cases. */ |
| 2189 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 2190 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 2191 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 2192 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 2193 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 2194 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2195 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 2196 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2197 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 2198 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2199 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 2200 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2201 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 2202 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2203 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 2204 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 2205 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 2206 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 2207 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 2208 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 2209 | else |
| 2210 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 2211 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2212 | alert ); |
| 2213 | } |
| 2214 | |
| 2215 | #if defined(MBEDTLS_DEBUG_C) |
| 2216 | if( ssl->session_negotiate->verify_result != 0 ) |
| 2217 | { |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2218 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2219 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2220 | } |
| 2221 | else |
| 2222 | { |
| 2223 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 2224 | } |
| 2225 | #endif /* MBEDTLS_DEBUG_C */ |
| 2226 | |
| 2227 | return( ret ); |
| 2228 | } |
| 2229 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2230 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2231 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 2232 | unsigned char *start, size_t len ) |
| 2233 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2234 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2235 | /* Remember digest of the peer's end-CRT. */ |
| 2236 | ssl->session_negotiate->peer_cert_digest = |
| 2237 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 2238 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 2239 | { |
| 2240 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
irwir | 40883e9 | 2019-09-21 17:55:33 +0300 | [diff] [blame] | 2241 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2242 | mbedtls_ssl_send_alert_message( ssl, |
| 2243 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2244 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 2245 | |
| 2246 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2247 | } |
| 2248 | |
| 2249 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 2250 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 2251 | start, len, |
| 2252 | ssl->session_negotiate->peer_cert_digest ); |
| 2253 | |
| 2254 | ssl->session_negotiate->peer_cert_digest_type = |
| 2255 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 2256 | ssl->session_negotiate->peer_cert_digest_len = |
| 2257 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 2258 | |
| 2259 | return( ret ); |
| 2260 | } |
| 2261 | |
| 2262 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 2263 | unsigned char *start, size_t len ) |
| 2264 | { |
| 2265 | unsigned char *end = start + len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2266 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2267 | |
| 2268 | /* Make a copy of the peer's raw public key. */ |
| 2269 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 2270 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 2271 | &ssl->handshake->peer_pubkey ); |
| 2272 | if( ret != 0 ) |
| 2273 | { |
| 2274 | /* We should have parsed the public key before. */ |
| 2275 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2276 | } |
| 2277 | |
| 2278 | return( 0 ); |
| 2279 | } |
| 2280 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2281 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2282 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 2283 | { |
| 2284 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2285 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2286 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2287 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 2288 | ? ssl->handshake->sni_authmode |
| 2289 | : ssl->conf->authmode; |
| 2290 | #else |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 2291 | const int authmode = ssl->conf->authmode; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2292 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2293 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2294 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2295 | |
| 2296 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 2297 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2298 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 2299 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2300 | { |
| 2301 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2302 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2303 | } |
| 2304 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2305 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2306 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2307 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2308 | { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2309 | chain = ssl->handshake->ecrs_peer_cert; |
| 2310 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2311 | goto crt_verify; |
| 2312 | } |
| 2313 | #endif |
| 2314 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 2315 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2316 | { |
| 2317 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 2318 | let it decide whether to alert. */ |
| 2319 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2320 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2321 | } |
| 2322 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2323 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2324 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 2325 | { |
| 2326 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2327 | |
irwir | d3085ab | 2020-04-21 22:26:05 +0300 | [diff] [blame] | 2328 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2329 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2330 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2331 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2332 | } |
| 2333 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2334 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2335 | /* Clear existing peer CRT structure in case we tried to |
| 2336 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2337 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2338 | |
| 2339 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 2340 | if( chain == NULL ) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2341 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2342 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2343 | sizeof( mbedtls_x509_crt ) ) ); |
| 2344 | mbedtls_ssl_send_alert_message( ssl, |
| 2345 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2346 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2347 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2348 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2349 | goto exit; |
| 2350 | } |
| 2351 | mbedtls_x509_crt_init( chain ); |
| 2352 | |
| 2353 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2354 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2355 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2356 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2357 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2358 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2359 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2360 | |
| 2361 | crt_verify: |
| 2362 | if( ssl->handshake->ecrs_enabled) |
| 2363 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 2364 | #endif |
| 2365 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2366 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2367 | chain, rs_ctx ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2368 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2369 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2370 | |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2371 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2372 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2373 | unsigned char *crt_start, *pk_start; |
| 2374 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2375 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2376 | /* We parse the CRT chain without copying, so |
| 2377 | * these pointers point into the input buffer, |
| 2378 | * and are hence still valid after freeing the |
| 2379 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2380 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2381 | crt_start = chain->raw.p; |
| 2382 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2383 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2384 | pk_start = chain->pk_raw.p; |
| 2385 | pk_len = chain->pk_raw.len; |
| 2386 | |
| 2387 | /* Free the CRT structures before computing |
| 2388 | * digest and copying the peer's public key. */ |
| 2389 | mbedtls_x509_crt_free( chain ); |
| 2390 | mbedtls_free( chain ); |
| 2391 | chain = NULL; |
| 2392 | |
| 2393 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2394 | if( ret != 0 ) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2395 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2396 | |
| 2397 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 2398 | if( ret != 0 ) |
| 2399 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2400 | } |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2401 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2402 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2403 | ssl->session_negotiate->peer_cert = chain; |
| 2404 | chain = NULL; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2405 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2406 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2407 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2408 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2409 | exit: |
| 2410 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2411 | if( ret == 0 ) |
| 2412 | ssl->state++; |
| 2413 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2414 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2415 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 2416 | { |
| 2417 | ssl->handshake->ecrs_peer_cert = chain; |
| 2418 | chain = NULL; |
| 2419 | } |
| 2420 | #endif |
| 2421 | |
| 2422 | if( chain != NULL ) |
| 2423 | { |
| 2424 | mbedtls_x509_crt_free( chain ); |
| 2425 | mbedtls_free( chain ); |
| 2426 | } |
| 2427 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2428 | return( ret ); |
| 2429 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2430 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2432 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 2433 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2434 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 2435 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2436 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2437 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2438 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2439 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2440 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 2441 | else |
| 2442 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2443 | #if defined(MBEDTLS_SHA256_C) |
| 2444 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2445 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2446 | else |
| 2447 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2448 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2449 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2450 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2451 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2452 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2453 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2455 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2456 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2457 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2458 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2459 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 2460 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2461 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 2462 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2463 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2464 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2465 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2466 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2467 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 2468 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2469 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2470 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2471 | mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2472 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2473 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2474 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2475 | } |
| 2476 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2477 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2478 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2479 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2480 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2481 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2482 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2483 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 2484 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2485 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2486 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2487 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2488 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2489 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2490 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2491 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2492 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2493 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2494 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2495 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2498 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2499 | #if defined(MBEDTLS_SHA256_C) |
| 2500 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2501 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2502 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2503 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2504 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 2505 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2506 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2507 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2508 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2509 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2510 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2511 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2512 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2513 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2514 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2515 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2516 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2517 | #else |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2518 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2519 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2520 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2521 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2522 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2523 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2524 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2525 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2526 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2527 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2528 | { |
| 2529 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2530 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2531 | unsigned char padbuf[32]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2532 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2533 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 2534 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2535 | psa_status_t status; |
| 2536 | #else |
| 2537 | mbedtls_sha256_context sha256; |
| 2538 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2539 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2540 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2541 | if( !session ) |
| 2542 | session = ssl->session; |
| 2543 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2544 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 2545 | ? "client finished" |
| 2546 | : "server finished"; |
| 2547 | |
| 2548 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2549 | sha256_psa = psa_hash_operation_init(); |
| 2550 | |
| 2551 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 2552 | |
| 2553 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 2554 | if( status != PSA_SUCCESS ) |
| 2555 | { |
| 2556 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 2557 | return; |
| 2558 | } |
| 2559 | |
| 2560 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 2561 | if( status != PSA_SUCCESS ) |
| 2562 | { |
| 2563 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 2564 | return; |
| 2565 | } |
| 2566 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 2567 | #else |
| 2568 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2569 | mbedtls_sha256_init( &sha256 ); |
| 2570 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2571 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2572 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2573 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2574 | |
| 2575 | /* |
| 2576 | * TLSv1.2: |
| 2577 | * hash = PRF( master, finished_label, |
| 2578 | * Hash( handshake ) )[0.11] |
| 2579 | */ |
| 2580 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2581 | #if !defined(MBEDTLS_SHA256_ALT) |
| 2582 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2583 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 2584 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2585 | |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2586 | mbedtls_sha256_finish( &sha256, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2587 | mbedtls_sha256_free( &sha256 ); |
| 2588 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2589 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2590 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2591 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2592 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2593 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2594 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2595 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2597 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2598 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2599 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2600 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2601 | #if defined(MBEDTLS_SHA384_C) |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 2602 | |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2603 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2604 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2605 | { |
| 2606 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2607 | const char *sender; |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 2608 | unsigned char padbuf[48]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2609 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2610 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 2611 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2612 | psa_status_t status; |
| 2613 | #else |
| 2614 | mbedtls_sha512_context sha512; |
| 2615 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2617 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2618 | if( !session ) |
| 2619 | session = ssl->session; |
| 2620 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2621 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 2622 | ? "client finished" |
| 2623 | : "server finished"; |
| 2624 | |
| 2625 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2626 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2627 | |
| 2628 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 2629 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2630 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2631 | if( status != PSA_SUCCESS ) |
| 2632 | { |
| 2633 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 2634 | return; |
| 2635 | } |
| 2636 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2637 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2638 | if( status != PSA_SUCCESS ) |
| 2639 | { |
| 2640 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 2641 | return; |
| 2642 | } |
| 2643 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 2644 | #else |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2645 | mbedtls_sha512_init( &sha512 ); |
| 2646 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2647 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2648 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2649 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2650 | |
| 2651 | /* |
| 2652 | * TLSv1.2: |
| 2653 | * hash = PRF( master, finished_label, |
| 2654 | * Hash( handshake ) )[0.11] |
| 2655 | */ |
| 2656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2657 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2658 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 2659 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 2660 | #endif |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2661 | mbedtls_sha512_finish( &sha512, padbuf ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2662 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2663 | mbedtls_sha512_free( &sha512 ); |
| 2664 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2665 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2666 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2667 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2669 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2670 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2671 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2672 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2673 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2674 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2675 | #endif /* MBEDTLS_SHA384_C */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2676 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2677 | |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 2678 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2679 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2680 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2681 | |
| 2682 | /* |
| 2683 | * Free our handshake params |
| 2684 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 2685 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2686 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2687 | ssl->handshake = NULL; |
| 2688 | |
| 2689 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2690 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2691 | */ |
| 2692 | if( ssl->transform ) |
| 2693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2694 | mbedtls_ssl_transform_free( ssl->transform ); |
| 2695 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2696 | } |
| 2697 | ssl->transform = ssl->transform_negotiate; |
| 2698 | ssl->transform_negotiate = NULL; |
| 2699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2700 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2701 | } |
| 2702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2703 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2704 | { |
| 2705 | int resume = ssl->handshake->resume; |
| 2706 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2707 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2708 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2709 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2710 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2711 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2712 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2713 | ssl->renego_records_seen = 0; |
| 2714 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2715 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2716 | |
| 2717 | /* |
| 2718 | * Free the previous session and switch in the current one |
| 2719 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2720 | if( ssl->session ) |
| 2721 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2722 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 2723 | /* RFC 7366 3.1: keep the EtM state */ |
| 2724 | ssl->session_negotiate->encrypt_then_mac = |
| 2725 | ssl->session->encrypt_then_mac; |
| 2726 | #endif |
| 2727 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2728 | mbedtls_ssl_session_free( ssl->session ); |
| 2729 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2730 | } |
| 2731 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2732 | ssl->session_negotiate = NULL; |
| 2733 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2734 | /* |
| 2735 | * Add cache entry |
| 2736 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2737 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2738 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 2739 | resume == 0 ) |
| 2740 | { |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 2741 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 2742 | ssl->session->id, |
| 2743 | ssl->session->id_len, |
| 2744 | ssl->session ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2745 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 2746 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2748 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2749 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2750 | ssl->handshake->flight != NULL ) |
| 2751 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2752 | /* Cancel handshake timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 2753 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2754 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2755 | /* Keep last flight around in case we need to resend it: |
| 2756 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2757 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2758 | } |
| 2759 | else |
| 2760 | #endif |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 2761 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2762 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2763 | ssl->state++; |
| 2764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2765 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2768 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2769 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2770 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2772 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2773 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 2774 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 2775 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2776 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2777 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 2778 | /* |
| 2779 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 2780 | * may define some other value. Currently (early 2016), no defined |
| 2781 | * ciphersuite does this (and this is unlikely to change as activity has |
| 2782 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 2783 | */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2784 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2785 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2786 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2787 | ssl->verify_data_len = hash_len; |
| 2788 | 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] | 2789 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2790 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2791 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2792 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2793 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2794 | |
| 2795 | /* |
| 2796 | * In case of session resuming, invert the client and server |
| 2797 | * ChangeCipherSpec messages order. |
| 2798 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2799 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2800 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2801 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2802 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2803 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2804 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2805 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2806 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2807 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2808 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2809 | } |
| 2810 | else |
| 2811 | ssl->state++; |
| 2812 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2813 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2814 | * Switch to our negotiated transform and session parameters for outbound |
| 2815 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2816 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2817 | 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] | 2818 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2819 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2820 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2821 | { |
| 2822 | unsigned char i; |
| 2823 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2824 | /* Remember current epoch settings for resending */ |
| 2825 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 2826 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
Jerry Yu | d96a5c2 | 2021-09-29 17:46:51 +0800 | [diff] [blame] | 2827 | sizeof( ssl->handshake->alt_out_ctr ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2828 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2829 | /* Set sequence_number to zero */ |
Jerry Yu | fd320e9 | 2021-10-08 21:52:41 +0800 | [diff] [blame] | 2830 | 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] | 2831 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2832 | |
| 2833 | /* Increment epoch */ |
| 2834 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 2835 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2836 | break; |
| 2837 | |
| 2838 | /* The loop goes to its end iff the counter is wrapping */ |
| 2839 | if( i == 0 ) |
| 2840 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2841 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 2842 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2846 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Jerry Yu | fd320e9 | 2021-10-08 21:52:41 +0800 | [diff] [blame] | 2847 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2848 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2849 | ssl->transform_out = ssl->transform_negotiate; |
| 2850 | ssl->session_out = ssl->session_negotiate; |
| 2851 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2852 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2853 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2854 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2855 | #endif |
| 2856 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2857 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2858 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2859 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2860 | return( ret ); |
| 2861 | } |
| 2862 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2863 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2864 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2865 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2866 | { |
| 2867 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 2868 | return( ret ); |
| 2869 | } |
| 2870 | #endif |
| 2871 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2872 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2873 | |
| 2874 | return( 0 ); |
| 2875 | } |
| 2876 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2877 | #define SSL_MAX_HASH_LEN 12 |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2878 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2879 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2880 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2881 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a417431 | 2021-12-13 14:38:40 +0100 | [diff] [blame] | 2882 | unsigned int hash_len = 12; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2883 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2884 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2885 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2886 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2887 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2888 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2889 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2890 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2891 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2892 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2895 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2896 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2897 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2898 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2899 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2900 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 2901 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Hanno Becker | a0ca87e | 2021-06-24 10:27:37 +0100 | [diff] [blame] | 2904 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 2905 | { |
| 2906 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2907 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2908 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 2909 | goto exit; |
Hanno Becker | a0ca87e | 2021-06-24 10:27:37 +0100 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2913 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2914 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2915 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2916 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2917 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 2918 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 2921 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
Manuel Pégourié-Gonnard | 4abc327 | 2014-09-10 12:02:46 +0000 | [diff] [blame] | 2922 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2923 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2924 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2925 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 2926 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2927 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 2928 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2931 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2932 | ssl->verify_data_len = hash_len; |
| 2933 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2934 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2935 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2936 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2937 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2938 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2939 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2940 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2941 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2942 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2943 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2944 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2945 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2946 | } |
| 2947 | else |
| 2948 | ssl->state++; |
| 2949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2950 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2951 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2952 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2953 | #endif |
| 2954 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2955 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2956 | |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2957 | exit: |
| 2958 | mbedtls_platform_zeroize( buf, hash_len ); |
| 2959 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2962 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2963 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2964 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2965 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2966 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2967 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2968 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2969 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 2970 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 2971 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2972 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2973 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2974 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2975 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2976 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2977 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2978 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 2979 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2980 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2981 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2982 | mbedtls_sha512_starts( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2983 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2984 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2985 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2986 | |
| 2987 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2988 | |
| 2989 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2990 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2991 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 2992 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2993 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2994 | #if defined(MBEDTLS_DHM_C) |
| 2995 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2996 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2997 | #if defined(MBEDTLS_ECDH_C) |
| 2998 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2999 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3000 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3001 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3002 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3003 | handshake->ecjpake_cache = NULL; |
| 3004 | handshake->ecjpake_cache_len = 0; |
| 3005 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3006 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3007 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3008 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3009 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3010 | #endif |
| 3011 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3012 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3013 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 3014 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3015 | |
| 3016 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3017 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3018 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 3019 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3020 | } |
| 3021 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3022 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3023 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3024 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 3025 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 3026 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3027 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 3028 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 3029 | #else |
| 3030 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 3031 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 3032 | #endif |
| 3033 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 3034 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3035 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 3036 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 3037 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3038 | } |
| 3039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3040 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3041 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3042 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3043 | } |
| 3044 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3045 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3046 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3047 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3048 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3049 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3050 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3051 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3052 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3053 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3054 | |
| 3055 | /* |
| 3056 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 3057 | * Now allocate missing structures. |
| 3058 | */ |
| 3059 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3060 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3061 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3062 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3063 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3064 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3065 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3066 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3067 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3068 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 3069 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3070 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3071 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3072 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3073 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3074 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 3075 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3076 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 3077 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3078 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3079 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3080 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3081 | if( ssl->handshake == NULL || |
| 3082 | ssl->transform_negotiate == NULL || |
| 3083 | ssl->session_negotiate == NULL ) |
| 3084 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3085 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3086 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3087 | mbedtls_free( ssl->handshake ); |
| 3088 | mbedtls_free( ssl->transform_negotiate ); |
| 3089 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3090 | |
| 3091 | ssl->handshake = NULL; |
| 3092 | ssl->transform_negotiate = NULL; |
| 3093 | ssl->session_negotiate = NULL; |
| 3094 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3095 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3098 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3099 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3100 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 3101 | ssl_handshake_params_init( ssl->handshake ); |
| 3102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3103 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3104 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3105 | { |
| 3106 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3107 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3108 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 3109 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 3110 | else |
| 3111 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3112 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3113 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3114 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3115 | #endif |
| 3116 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3117 | /* |
| 3118 | * curve_list is translated to IANA TLS group identifiers here because |
| 3119 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 3120 | * any error codes. |
| 3121 | */ |
| 3122 | #if defined(MBEDTLS_ECP_C) |
| 3123 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3124 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 3125 | if ( ssl->conf->curve_list != NULL ) |
| 3126 | { |
| 3127 | size_t length; |
| 3128 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 3129 | |
| 3130 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 3131 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 3132 | |
| 3133 | /* Leave room for zero termination */ |
| 3134 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 3135 | if ( group_list == NULL ) |
| 3136 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 3137 | |
| 3138 | for( size_t i = 0; i < length; i++ ) |
| 3139 | { |
| 3140 | const mbedtls_ecp_curve_info *info = |
| 3141 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 3142 | if ( info == NULL ) |
| 3143 | { |
| 3144 | mbedtls_free( group_list ); |
| 3145 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 3146 | } |
| 3147 | group_list[i] = info->tls_id; |
| 3148 | } |
| 3149 | |
| 3150 | group_list[length] = 0; |
| 3151 | |
| 3152 | ssl->handshake->group_list = group_list; |
| 3153 | ssl->handshake->group_list_heap_allocated = 1; |
| 3154 | } |
| 3155 | else |
| 3156 | { |
| 3157 | ssl->handshake->group_list = ssl->conf->group_list; |
| 3158 | ssl->handshake->group_list_heap_allocated = 0; |
| 3159 | } |
| 3160 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3161 | #endif /* MBEDTLS_ECP_C */ |
| 3162 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3163 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 3164 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 3165 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3166 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 3167 | signature algorithms IANA identifiers. */ |
| 3168 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3169 | ssl->conf->sig_hashes != NULL ) |
| 3170 | { |
| 3171 | const int *md; |
| 3172 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3173 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3174 | uint16_t *p; |
| 3175 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3176 | #if defined(static_assert) |
| 3177 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 3178 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 3179 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 3180 | #endif |
| 3181 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3182 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 3183 | { |
| 3184 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 3185 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3186 | #if defined(MBEDTLS_ECDSA_C) |
| 3187 | sig_algs_len += sizeof( uint16_t ); |
| 3188 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 3189 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3190 | #if defined(MBEDTLS_RSA_C) |
| 3191 | sig_algs_len += sizeof( uint16_t ); |
| 3192 | #endif |
| 3193 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 3194 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3195 | } |
| 3196 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3197 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3198 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 3199 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3200 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 3201 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3202 | if( ssl->handshake->sig_algs == NULL ) |
| 3203 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 3204 | |
| 3205 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 3206 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 3207 | { |
| 3208 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 3209 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 3210 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3211 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3212 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 3213 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3214 | #endif |
| 3215 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3216 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 3217 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3218 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3219 | } |
| 3220 | *p = MBEDTLS_TLS1_3_SIG_NONE; |
| 3221 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 3222 | } |
| 3223 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 3224 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3225 | { |
| 3226 | ssl->handshake->sig_algs = ssl->conf->sig_algs; |
| 3227 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 3228 | } |
| 3229 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3230 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3231 | return( 0 ); |
| 3232 | } |
| 3233 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3234 | #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] | 3235 | /* Dummy cookie callbacks for defaults */ |
| 3236 | static int ssl_cookie_write_dummy( void *ctx, |
| 3237 | unsigned char **p, unsigned char *end, |
| 3238 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3239 | { |
| 3240 | ((void) ctx); |
| 3241 | ((void) p); |
| 3242 | ((void) end); |
| 3243 | ((void) cli_id); |
| 3244 | ((void) cli_id_len); |
| 3245 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3246 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | static int ssl_cookie_check_dummy( void *ctx, |
| 3250 | const unsigned char *cookie, size_t cookie_len, |
| 3251 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3252 | { |
| 3253 | ((void) ctx); |
| 3254 | ((void) cookie); |
| 3255 | ((void) cookie_len); |
| 3256 | ((void) cli_id); |
| 3257 | ((void) cli_id_len); |
| 3258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3259 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3260 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3261 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3262 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3263 | /* |
| 3264 | * Initialize an SSL context |
| 3265 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3266 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 3267 | { |
| 3268 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 3269 | } |
| 3270 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3271 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 3272 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3273 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3274 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
| 3275 | { |
| 3276 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3277 | { |
| 3278 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported" ) ); |
| 3279 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3280 | } |
| 3281 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 3282 | return( 0 ); |
| 3283 | } |
| 3284 | #endif |
| 3285 | |
| 3286 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3287 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3288 | { |
| 3289 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 3290 | return( 0 ); |
| 3291 | } |
| 3292 | #endif |
| 3293 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3294 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3295 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( ssl->conf ) ) |
| 3296 | { |
| 3297 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) ); |
| 3298 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3299 | } |
| 3300 | #endif |
| 3301 | |
| 3302 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 3303 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 3304 | } |
| 3305 | |
| 3306 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 3307 | { |
| 3308 | int ret; |
| 3309 | ret = ssl_conf_version_check( ssl ); |
| 3310 | if( ret != 0 ) |
| 3311 | return( ret ); |
| 3312 | |
| 3313 | /* Space for further checks */ |
| 3314 | |
| 3315 | return( 0 ); |
| 3316 | } |
| 3317 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3318 | /* |
| 3319 | * Setup an SSL context |
| 3320 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 3321 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3322 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 3323 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3324 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3325 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3326 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3327 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3328 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3329 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3330 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3331 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 3332 | return( ret ); |
| 3333 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3334 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3335 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3336 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 3337 | |
| 3338 | /* Set to NULL in case of an error condition */ |
| 3339 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3340 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3341 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3342 | ssl->in_buf_len = in_buf_len; |
| 3343 | #endif |
| 3344 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3345 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3346 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3347 | 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] | 3348 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3349 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3350 | } |
| 3351 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3352 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3353 | ssl->out_buf_len = out_buf_len; |
| 3354 | #endif |
| 3355 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3356 | if( ssl->out_buf == NULL ) |
| 3357 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3358 | 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] | 3359 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3360 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3363 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3364 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3365 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 3366 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3367 | #endif |
| 3368 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3369 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3370 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3371 | |
| 3372 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3373 | |
| 3374 | error: |
| 3375 | mbedtls_free( ssl->in_buf ); |
| 3376 | mbedtls_free( ssl->out_buf ); |
| 3377 | |
| 3378 | ssl->conf = NULL; |
| 3379 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3380 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3381 | ssl->in_buf_len = 0; |
| 3382 | ssl->out_buf_len = 0; |
| 3383 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3384 | ssl->in_buf = NULL; |
| 3385 | ssl->out_buf = NULL; |
| 3386 | |
| 3387 | ssl->in_hdr = NULL; |
| 3388 | ssl->in_ctr = NULL; |
| 3389 | ssl->in_len = NULL; |
| 3390 | ssl->in_iv = NULL; |
| 3391 | ssl->in_msg = NULL; |
| 3392 | |
| 3393 | ssl->out_hdr = NULL; |
| 3394 | ssl->out_ctr = NULL; |
| 3395 | ssl->out_len = NULL; |
| 3396 | ssl->out_iv = NULL; |
| 3397 | ssl->out_msg = NULL; |
| 3398 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3399 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3403 | * Reset an initialized and used SSL context for re-use while retaining |
| 3404 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3405 | * |
| 3406 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 3407 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3408 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 3409 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 3410 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3411 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3412 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3413 | size_t in_buf_len = ssl->in_buf_len; |
| 3414 | size_t out_buf_len = ssl->out_buf_len; |
| 3415 | #else |
| 3416 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3417 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 3418 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3419 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3420 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 3421 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 3422 | #endif |
| 3423 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3424 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3425 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3426 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3427 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 3428 | |
| 3429 | /* Reset incoming message parsing */ |
| 3430 | ssl->in_offt = NULL; |
| 3431 | ssl->nb_zero = 0; |
| 3432 | ssl->in_msgtype = 0; |
| 3433 | ssl->in_msglen = 0; |
| 3434 | ssl->in_hslen = 0; |
| 3435 | ssl->keep_current_message = 0; |
| 3436 | ssl->transform_in = NULL; |
| 3437 | |
| 3438 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3439 | ssl->next_record_offset = 0; |
| 3440 | ssl->in_epoch = 0; |
| 3441 | #endif |
| 3442 | |
| 3443 | /* Keep current datagram if partial == 1 */ |
| 3444 | if( partial == 0 ) |
| 3445 | { |
| 3446 | ssl->in_left = 0; |
| 3447 | memset( ssl->in_buf, 0, in_buf_len ); |
| 3448 | } |
| 3449 | |
| 3450 | /* Reset outgoing message writing */ |
| 3451 | ssl->out_msgtype = 0; |
| 3452 | ssl->out_msglen = 0; |
| 3453 | ssl->out_left = 0; |
| 3454 | memset( ssl->out_buf, 0, out_buf_len ); |
| 3455 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 3456 | ssl->transform_out = NULL; |
| 3457 | |
| 3458 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3459 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 3460 | #endif |
| 3461 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3462 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3463 | if( ssl->transform ) |
| 3464 | { |
| 3465 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3466 | mbedtls_free( ssl->transform ); |
| 3467 | ssl->transform = NULL; |
| 3468 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3469 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3470 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3471 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3472 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 3473 | mbedtls_free( ssl->transform_application ); |
| 3474 | ssl->transform_application = NULL; |
| 3475 | |
| 3476 | if( ssl->handshake != NULL ) |
| 3477 | { |
| 3478 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 3479 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 3480 | ssl->handshake->transform_earlydata = NULL; |
| 3481 | |
| 3482 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 3483 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 3484 | ssl->handshake->transform_handshake = NULL; |
| 3485 | } |
| 3486 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3487 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 3491 | { |
| 3492 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3493 | |
| 3494 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3495 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 3496 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3497 | |
| 3498 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3499 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3500 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3501 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3502 | |
| 3503 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3504 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 3505 | 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] | 3506 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3507 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3508 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3509 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 3510 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3511 | if( ssl->session ) |
| 3512 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3513 | mbedtls_ssl_session_free( ssl->session ); |
| 3514 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3515 | ssl->session = NULL; |
| 3516 | } |
| 3517 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3518 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3519 | ssl->alpn_chosen = NULL; |
| 3520 | #endif |
| 3521 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3522 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3523 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3524 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3525 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3526 | { |
| 3527 | mbedtls_free( ssl->cli_id ); |
| 3528 | ssl->cli_id = NULL; |
| 3529 | ssl->cli_id_len = 0; |
| 3530 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 3531 | #endif |
| 3532 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3533 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3534 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3535 | |
| 3536 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 3539 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3540 | * Reset an initialized and used SSL context for re-use while retaining |
| 3541 | * all application-set variables, function pointers and data. |
| 3542 | */ |
| 3543 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 3544 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3545 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3549 | * SSL set accessors |
| 3550 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3551 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3552 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3553 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 3556 | 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] | 3557 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3558 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 3559 | } |
| 3560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3561 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3562 | 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] | 3563 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3564 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3565 | } |
| 3566 | #endif |
| 3567 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3568 | 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] | 3569 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3570 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3571 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3572 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3573 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3574 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 3575 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 3576 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3577 | { |
| 3578 | ssl->disable_datagram_packing = !allow_packing; |
| 3579 | } |
| 3580 | |
| 3581 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 3582 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3583 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3584 | conf->hs_timeout_min = min; |
| 3585 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3586 | } |
| 3587 | #endif |
| 3588 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3589 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3590 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3591 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3594 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3595 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 3596 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3597 | void *p_vrfy ) |
| 3598 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3599 | conf->f_vrfy = f_vrfy; |
| 3600 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3601 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3602 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3603 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3604 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 3605 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3606 | void *p_rng ) |
| 3607 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3608 | conf->f_rng = f_rng; |
| 3609 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3612 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 3613 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3614 | void *p_dbg ) |
| 3615 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3616 | conf->f_dbg = f_dbg; |
| 3617 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3620 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3621 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 3622 | mbedtls_ssl_send_t *f_send, |
| 3623 | mbedtls_ssl_recv_t *f_recv, |
| 3624 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3625 | { |
| 3626 | ssl->p_bio = p_bio; |
| 3627 | ssl->f_send = f_send; |
| 3628 | ssl->f_recv = f_recv; |
| 3629 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 3630 | } |
| 3631 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 3632 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3633 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 3634 | { |
| 3635 | ssl->mtu = mtu; |
| 3636 | } |
| 3637 | #endif |
| 3638 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3639 | 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] | 3640 | { |
| 3641 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3642 | } |
| 3643 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3644 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 3645 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 3646 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 3647 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3648 | { |
| 3649 | ssl->p_timer = p_timer; |
| 3650 | ssl->f_set_timer = f_set_timer; |
| 3651 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3652 | |
| 3653 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3654 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3655 | } |
| 3656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3657 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3658 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 3659 | void *p_cache, |
| 3660 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 3661 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3662 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 3663 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3664 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3665 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3666 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3667 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3669 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3670 | 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] | 3671 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3672 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3673 | |
| 3674 | if( ssl == NULL || |
| 3675 | session == NULL || |
| 3676 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3677 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3678 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3679 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3680 | } |
| 3681 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3682 | if( ssl->handshake->resume == 1 ) |
| 3683 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3684 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 3685 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 3686 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3687 | return( ret ); |
| 3688 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3689 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3690 | |
| 3691 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3692 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3693 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3694 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 3695 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 3696 | const int *ciphersuites ) |
| 3697 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 3698 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3701 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 3702 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 3703 | const int kex_modes ) |
| 3704 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3705 | conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 3706 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3707 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 3708 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3709 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 3710 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 3711 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 3712 | { |
| 3713 | conf->cert_profile = profile; |
| 3714 | } |
| 3715 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3716 | /* Append a new keycert entry to a (possibly empty) list */ |
| 3717 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 3718 | mbedtls_x509_crt *cert, |
| 3719 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3720 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3721 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3722 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3723 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 3724 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3725 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3726 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3727 | new_cert->cert = cert; |
| 3728 | new_cert->key = key; |
| 3729 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3730 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3731 | /* Update head is the list was null, else add to the end */ |
| 3732 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 3733 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3734 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 3735 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3736 | else |
| 3737 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3738 | mbedtls_ssl_key_cert *cur = *head; |
| 3739 | while( cur->next != NULL ) |
| 3740 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3741 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3742 | } |
| 3743 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3744 | return( 0 ); |
| 3745 | } |
| 3746 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3747 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3748 | mbedtls_x509_crt *own_cert, |
| 3749 | mbedtls_pk_context *pk_key ) |
| 3750 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 3751 | 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] | 3752 | } |
| 3753 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3754 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3755 | mbedtls_x509_crt *ca_chain, |
| 3756 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3757 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3758 | conf->ca_chain = ca_chain; |
| 3759 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3760 | |
| 3761 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3762 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 3763 | * cannot be used together. */ |
| 3764 | conf->f_ca_cb = NULL; |
| 3765 | conf->p_ca_cb = NULL; |
| 3766 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3767 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3768 | |
| 3769 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3770 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 3771 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3772 | void *p_ca_cb ) |
| 3773 | { |
| 3774 | conf->f_ca_cb = f_ca_cb; |
| 3775 | conf->p_ca_cb = p_ca_cb; |
| 3776 | |
| 3777 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 3778 | * cannot be used together. */ |
| 3779 | conf->ca_chain = NULL; |
| 3780 | conf->ca_crl = NULL; |
| 3781 | } |
| 3782 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3783 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 3784 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3785 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3786 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 3787 | mbedtls_x509_crt *own_cert, |
| 3788 | mbedtls_pk_context *pk_key ) |
| 3789 | { |
| 3790 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 3791 | own_cert, pk_key ) ); |
| 3792 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 3793 | |
| 3794 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 3795 | mbedtls_x509_crt *ca_chain, |
| 3796 | mbedtls_x509_crl *ca_crl ) |
| 3797 | { |
| 3798 | ssl->handshake->sni_ca_chain = ca_chain; |
| 3799 | ssl->handshake->sni_ca_crl = ca_crl; |
| 3800 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3801 | |
| 3802 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 3803 | int authmode ) |
| 3804 | { |
| 3805 | ssl->handshake->sni_authmode = authmode; |
| 3806 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3807 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 3808 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 3809 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3810 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 3811 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3812 | void *p_vrfy ) |
| 3813 | { |
| 3814 | ssl->f_vrfy = f_vrfy; |
| 3815 | ssl->p_vrfy = p_vrfy; |
| 3816 | } |
| 3817 | #endif |
| 3818 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3819 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3820 | /* |
| 3821 | * Set EC J-PAKE password for current handshake |
| 3822 | */ |
| 3823 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 3824 | const unsigned char *pw, |
| 3825 | size_t pw_len ) |
| 3826 | { |
| 3827 | mbedtls_ecjpake_role role; |
| 3828 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 3829 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3830 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3831 | |
| 3832 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 3833 | role = MBEDTLS_ECJPAKE_SERVER; |
| 3834 | else |
| 3835 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 3836 | |
| 3837 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 3838 | role, |
| 3839 | MBEDTLS_MD_SHA256, |
| 3840 | MBEDTLS_ECP_DP_SECP256R1, |
| 3841 | pw, pw_len ) ); |
| 3842 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3843 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3844 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3845 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3846 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3847 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
| 3848 | { |
| 3849 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3850 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 3851 | return( 1 ); |
| 3852 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3853 | |
| 3854 | if( conf->psk != NULL ) |
| 3855 | return( 1 ); |
| 3856 | |
| 3857 | return( 0 ); |
| 3858 | } |
| 3859 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3860 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 3861 | { |
| 3862 | /* Remove reference to existing PSK, if any. */ |
| 3863 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3864 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3865 | { |
| 3866 | /* The maintenance of the PSK key slot is the |
| 3867 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3868 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3869 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 3870 | /* This and the following branch should never |
| 3871 | * be taken simultaenously as we maintain the |
| 3872 | * invariant that raw and opaque PSKs are never |
| 3873 | * configured simultaneously. As a safeguard, |
| 3874 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3875 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3876 | if( conf->psk != NULL ) |
| 3877 | { |
| 3878 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 3879 | |
| 3880 | mbedtls_free( conf->psk ); |
| 3881 | conf->psk = NULL; |
| 3882 | conf->psk_len = 0; |
| 3883 | } |
| 3884 | |
| 3885 | /* Remove reference to PSK identity, if any. */ |
| 3886 | if( conf->psk_identity != NULL ) |
| 3887 | { |
| 3888 | mbedtls_free( conf->psk_identity ); |
| 3889 | conf->psk_identity = NULL; |
| 3890 | conf->psk_identity_len = 0; |
| 3891 | } |
| 3892 | } |
| 3893 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3894 | /* This function assumes that PSK identity in the SSL config is unset. |
| 3895 | * It checks that the provided identity is well-formed and attempts |
| 3896 | * to make a copy of it in the SSL config. |
| 3897 | * On failure, the PSK identity in the config remains unset. */ |
| 3898 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 3899 | unsigned char const *psk_identity, |
| 3900 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3901 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3902 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3903 | if( psk_identity == NULL || |
| 3904 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3905 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3906 | { |
| 3907 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3908 | } |
| 3909 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3910 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 3911 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3912 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3913 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 3914 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 3915 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 3916 | |
| 3917 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3918 | } |
| 3919 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3920 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 3921 | const unsigned char *psk, size_t psk_len, |
| 3922 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 3923 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3924 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3925 | |
| 3926 | /* We currently only support one PSK, raw or opaque. */ |
| 3927 | if( ssl_conf_psk_is_configured( conf ) ) |
| 3928 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3929 | |
| 3930 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 3931 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3932 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 3933 | if( psk_len == 0 ) |
| 3934 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3935 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 3936 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3937 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3938 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 3939 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 3940 | conf->psk_len = psk_len; |
| 3941 | memcpy( conf->psk, psk, conf->psk_len ); |
| 3942 | |
| 3943 | /* Check and set PSK Identity */ |
| 3944 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 3945 | if( ret != 0 ) |
| 3946 | ssl_conf_remove_psk( conf ); |
| 3947 | |
| 3948 | return( ret ); |
| 3949 | } |
| 3950 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3951 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 3952 | { |
| 3953 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3954 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3955 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3956 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3957 | } |
| 3958 | else |
| 3959 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3960 | if( ssl->handshake->psk != NULL ) |
| 3961 | { |
| 3962 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 3963 | ssl->handshake->psk_len ); |
| 3964 | mbedtls_free( ssl->handshake->psk ); |
| 3965 | ssl->handshake->psk_len = 0; |
| 3966 | } |
| 3967 | } |
| 3968 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3969 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 3970 | const unsigned char *psk, size_t psk_len ) |
| 3971 | { |
| 3972 | if( psk == NULL || ssl->handshake == NULL ) |
| 3973 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3974 | |
| 3975 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 3976 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3977 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3978 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3979 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3980 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3981 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3982 | |
| 3983 | ssl->handshake->psk_len = psk_len; |
| 3984 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 3985 | |
| 3986 | return( 0 ); |
| 3987 | } |
| 3988 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3989 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3990 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 3991 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3992 | const unsigned char *psk_identity, |
| 3993 | size_t psk_identity_len ) |
| 3994 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3995 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3996 | |
| 3997 | /* We currently only support one PSK, raw or opaque. */ |
| 3998 | if( ssl_conf_psk_is_configured( conf ) ) |
| 3999 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4000 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4001 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4002 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4003 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4004 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4005 | |
| 4006 | /* Check and set PSK Identity */ |
| 4007 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 4008 | psk_identity_len ); |
| 4009 | if( ret != 0 ) |
| 4010 | ssl_conf_remove_psk( conf ); |
| 4011 | |
| 4012 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4013 | } |
| 4014 | |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame^] | 4015 | psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type, |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 4016 | size_t taglen, |
| 4017 | psa_algorithm_t *alg, |
| 4018 | psa_key_type_t *key_type, |
| 4019 | size_t *key_size ) |
| 4020 | { |
| 4021 | switch ( mbedtls_cipher_type ) |
| 4022 | { |
| 4023 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 4024 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4025 | *key_type = PSA_KEY_TYPE_AES; |
| 4026 | *key_size = 128; |
| 4027 | break; |
| 4028 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 4029 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4030 | *key_type = PSA_KEY_TYPE_AES; |
| 4031 | *key_size = 128; |
| 4032 | break; |
| 4033 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 4034 | *alg = PSA_ALG_GCM; |
| 4035 | *key_type = PSA_KEY_TYPE_AES; |
| 4036 | *key_size = 128; |
| 4037 | break; |
| 4038 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 4039 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4040 | *key_type = PSA_KEY_TYPE_AES; |
| 4041 | *key_size = 192; |
| 4042 | break; |
| 4043 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 4044 | *alg = PSA_ALG_GCM; |
| 4045 | *key_type = PSA_KEY_TYPE_AES; |
| 4046 | *key_size = 192; |
| 4047 | break; |
| 4048 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 4049 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4050 | *key_type = PSA_KEY_TYPE_AES; |
| 4051 | *key_size = 256; |
| 4052 | break; |
| 4053 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 4054 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4055 | *key_type = PSA_KEY_TYPE_AES; |
| 4056 | *key_size = 256; |
| 4057 | break; |
| 4058 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 4059 | *alg = PSA_ALG_GCM; |
| 4060 | *key_type = PSA_KEY_TYPE_AES; |
| 4061 | *key_size = 256; |
| 4062 | break; |
| 4063 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 4064 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4065 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4066 | *key_size = 128; |
| 4067 | break; |
| 4068 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 4069 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4070 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4071 | *key_size = 128; |
| 4072 | break; |
| 4073 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 4074 | *alg = PSA_ALG_GCM; |
| 4075 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4076 | *key_size = 128; |
| 4077 | break; |
| 4078 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 4079 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4080 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4081 | *key_size = 192; |
| 4082 | break; |
| 4083 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 4084 | *alg = PSA_ALG_GCM; |
| 4085 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4086 | *key_size = 192; |
| 4087 | break; |
| 4088 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 4089 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4090 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4091 | *key_size = 256; |
| 4092 | break; |
| 4093 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 4094 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4095 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4096 | *key_size = 256; |
| 4097 | break; |
| 4098 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 4099 | *alg = PSA_ALG_GCM; |
| 4100 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4101 | *key_size = 256; |
| 4102 | break; |
| 4103 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 4104 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4105 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4106 | *key_size = 128; |
| 4107 | break; |
| 4108 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 4109 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4110 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4111 | *key_size = 128; |
| 4112 | break; |
| 4113 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 4114 | *alg = PSA_ALG_GCM; |
| 4115 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4116 | *key_size = 128; |
| 4117 | break; |
| 4118 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 4119 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4120 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4121 | *key_size = 192; |
| 4122 | break; |
| 4123 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 4124 | *alg = PSA_ALG_GCM; |
| 4125 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4126 | *key_size = 192; |
| 4127 | break; |
| 4128 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 4129 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4130 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4131 | *key_size = 256; |
| 4132 | break; |
| 4133 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 4134 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4135 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4136 | *key_size = 256; |
| 4137 | break; |
| 4138 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 4139 | *alg = PSA_ALG_GCM; |
| 4140 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4141 | *key_size = 256; |
| 4142 | break; |
| 4143 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 4144 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 4145 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 4146 | *key_size = 256; |
| 4147 | break; |
| 4148 | case MBEDTLS_CIPHER_NULL: |
| 4149 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 4150 | *key_type = 0; |
| 4151 | *key_size = 0; |
| 4152 | break; |
| 4153 | default: |
| 4154 | return PSA_ERROR_NOT_SUPPORTED; |
| 4155 | } |
| 4156 | |
| 4157 | return PSA_SUCCESS; |
| 4158 | } |
| 4159 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4160 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 4161 | mbedtls_svc_key_id_t psk ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4162 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4163 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
Ronald Cron | c26f8d4 | 2020-09-01 10:51:51 +0200 | [diff] [blame] | 4164 | ( ssl->handshake == NULL ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4165 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4166 | |
| 4167 | ssl_remove_psk( ssl ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4168 | ssl->handshake->psk_opaque = psk; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4169 | return( 0 ); |
| 4170 | } |
| 4171 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4172 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4173 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4174 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4175 | size_t), |
| 4176 | void *p_psk ) |
| 4177 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4178 | conf->f_psk = f_psk; |
| 4179 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 4180 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4181 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 4182 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4183 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4184 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 4185 | const unsigned char *dhm_P, size_t P_len, |
| 4186 | const unsigned char *dhm_G, size_t G_len ) |
| 4187 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4188 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4189 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 4190 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4191 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4192 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4193 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 4194 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 4195 | { |
| 4196 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4197 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4198 | return( ret ); |
| 4199 | } |
| 4200 | |
| 4201 | return( 0 ); |
| 4202 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4203 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4204 | 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] | 4205 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4206 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4207 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 4208 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4209 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4210 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 4211 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 4212 | &conf->dhm_P ) ) != 0 || |
| 4213 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 4214 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4215 | { |
| 4216 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4217 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4218 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4219 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4220 | |
| 4221 | return( 0 ); |
| 4222 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4223 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4224 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4225 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4226 | /* |
| 4227 | * Set the minimum length for Diffie-Hellman parameters |
| 4228 | */ |
| 4229 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 4230 | unsigned int bitlen ) |
| 4231 | { |
| 4232 | conf->dhm_min_bitlen = bitlen; |
| 4233 | } |
| 4234 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 4235 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4236 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4237 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 4238 | /* |
| 4239 | * Set allowed/preferred hashes for handshake signatures |
| 4240 | */ |
| 4241 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 4242 | const int *hashes ) |
| 4243 | { |
| 4244 | conf->sig_hashes = hashes; |
| 4245 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4246 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 4247 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4248 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 4249 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 4250 | const uint16_t* sig_algs ) |
| 4251 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4252 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4253 | conf->sig_hashes = NULL; |
| 4254 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 4255 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 4256 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4257 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 4258 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4259 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4260 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4261 | /* |
| 4262 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4263 | * |
| 4264 | * mbedtls_ssl_setup() takes the provided list |
| 4265 | * and translates it to a list of IANA TLS group identifiers, |
| 4266 | * stored in ssl->handshake->group_list. |
| 4267 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4268 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4269 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4270 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4271 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4272 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4273 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4274 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4275 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4276 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4277 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4278 | /* |
| 4279 | * Set the allowed groups |
| 4280 | */ |
| 4281 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 4282 | const uint16_t *group_list ) |
| 4283 | { |
| 4284 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4285 | conf->curve_list = NULL; |
| 4286 | #endif |
| 4287 | conf->group_list = group_list; |
| 4288 | } |
| 4289 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4290 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4291 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4292 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4293 | /* Initialize to suppress unnecessary compiler warning */ |
| 4294 | size_t hostname_len = 0; |
| 4295 | |
| 4296 | /* Check if new hostname is valid before |
| 4297 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4298 | if( hostname != NULL ) |
| 4299 | { |
| 4300 | hostname_len = strlen( hostname ); |
| 4301 | |
| 4302 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 4303 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4304 | } |
| 4305 | |
| 4306 | /* Now it's clear that we will overwrite the old hostname, |
| 4307 | * so we can free it safely */ |
| 4308 | |
| 4309 | if( ssl->hostname != NULL ) |
| 4310 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4311 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4312 | mbedtls_free( ssl->hostname ); |
| 4313 | } |
| 4314 | |
| 4315 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 4316 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4317 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4318 | { |
| 4319 | ssl->hostname = NULL; |
| 4320 | } |
| 4321 | else |
| 4322 | { |
| 4323 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4324 | if( ssl->hostname == NULL ) |
| 4325 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4326 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4327 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4328 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4329 | ssl->hostname[hostname_len] = '\0'; |
| 4330 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4331 | |
| 4332 | return( 0 ); |
| 4333 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 4334 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4335 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4336 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4337 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4338 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4339 | const unsigned char *, size_t), |
| 4340 | void *p_sni ) |
| 4341 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4342 | conf->f_sni = f_sni; |
| 4343 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4344 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4345 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4346 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4347 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4348 | 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] | 4349 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4350 | size_t cur_len, tot_len; |
| 4351 | const char **p; |
| 4352 | |
| 4353 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4354 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 4355 | * MUST NOT be truncated." |
| 4356 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4357 | */ |
| 4358 | tot_len = 0; |
| 4359 | for( p = protos; *p != NULL; p++ ) |
| 4360 | { |
| 4361 | cur_len = strlen( *p ); |
| 4362 | tot_len += cur_len; |
| 4363 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 4364 | if( ( cur_len == 0 ) || |
| 4365 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 4366 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4367 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4368 | } |
| 4369 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4370 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4371 | |
| 4372 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4373 | } |
| 4374 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4375 | 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] | 4376 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4377 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4378 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4379 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4380 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4381 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4382 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 4383 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4384 | { |
| 4385 | conf->dtls_srtp_mki_support = support_mki_value; |
| 4386 | } |
| 4387 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4388 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 4389 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 4390 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4391 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4392 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4393 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4394 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4395 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4396 | |
| 4397 | 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] | 4398 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4399 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4400 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4401 | |
| 4402 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 4403 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4404 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4405 | } |
| 4406 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4407 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4408 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4409 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4410 | const mbedtls_ssl_srtp_profile *p; |
| 4411 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4412 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4413 | /* check the profiles list: all entry must be valid, |
| 4414 | * 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] | 4415 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 4416 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 4417 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4418 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4419 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4420 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 4421 | list_size++; |
| 4422 | } |
| 4423 | else |
| 4424 | { |
| 4425 | /* unsupported value, stop parsing and set the size to an error value */ |
| 4426 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4427 | } |
| 4428 | } |
| 4429 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4430 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 4431 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4432 | conf->dtls_srtp_profile_list = NULL; |
| 4433 | conf->dtls_srtp_profile_list_len = 0; |
| 4434 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4435 | } |
| 4436 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 4437 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4438 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4439 | |
| 4440 | return( 0 ); |
| 4441 | } |
| 4442 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4443 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 4444 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4445 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4446 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 4447 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4448 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4449 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4450 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4451 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4452 | else |
| 4453 | { |
| 4454 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4455 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 4456 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4457 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4458 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4459 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 4460 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4461 | 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] | 4462 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4463 | conf->max_major_ver = major; |
| 4464 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4467 | 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] | 4468 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4469 | conf->min_major_ver = major; |
| 4470 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4473 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4474 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 4475 | char cert_req_ca_list ) |
| 4476 | { |
| 4477 | conf->cert_req_ca_list = cert_req_ca_list; |
| 4478 | } |
| 4479 | #endif |
| 4480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4481 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4482 | 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] | 4483 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4484 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 4485 | } |
| 4486 | #endif |
| 4487 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4488 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4489 | 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] | 4490 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4491 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 4492 | } |
| 4493 | #endif |
| 4494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4495 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4496 | 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] | 4497 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4498 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4499 | 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] | 4500 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4501 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4502 | } |
| 4503 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 4504 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4505 | |
| 4506 | return( 0 ); |
| 4507 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4508 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4509 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4510 | 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] | 4511 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4512 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4515 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4516 | 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] | 4517 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4518 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 4519 | } |
| 4520 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4521 | 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] | 4522 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4523 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 4524 | } |
| 4525 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4526 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4527 | const unsigned char period[8] ) |
| 4528 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4529 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4530 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4531 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4532 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4533 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4534 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4535 | 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] | 4536 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 4537 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4538 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4539 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4540 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4541 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4542 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 4543 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 4544 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 4545 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4546 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4547 | conf->f_ticket_write = f_ticket_write; |
| 4548 | conf->f_ticket_parse = f_ticket_parse; |
| 4549 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4550 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4551 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4552 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4553 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 4554 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 4555 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 4556 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4557 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 4558 | ssl->f_export_keys = f_export_keys; |
| 4559 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 4560 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4561 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4562 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4563 | void mbedtls_ssl_conf_async_private_cb( |
| 4564 | mbedtls_ssl_config *conf, |
| 4565 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 4566 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 4567 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 4568 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4569 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4570 | { |
| 4571 | conf->f_async_sign_start = f_async_sign; |
| 4572 | conf->f_async_decrypt_start = f_async_decrypt; |
| 4573 | conf->f_async_resume = f_async_resume; |
| 4574 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4575 | conf->p_async_config_data = async_config_data; |
| 4576 | } |
| 4577 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 4578 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 4579 | { |
| 4580 | return( conf->p_async_config_data ); |
| 4581 | } |
| 4582 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4583 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4584 | { |
| 4585 | if( ssl->handshake == NULL ) |
| 4586 | return( NULL ); |
| 4587 | else |
| 4588 | return( ssl->handshake->user_async_ctx ); |
| 4589 | } |
| 4590 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4591 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4592 | void *ctx ) |
| 4593 | { |
| 4594 | if( ssl->handshake != NULL ) |
| 4595 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4596 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4597 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4598 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4599 | /* |
| 4600 | * SSL get accessors |
| 4601 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4602 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4603 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 4604 | if( ssl->session != NULL ) |
| 4605 | return( ssl->session->verify_result ); |
| 4606 | |
| 4607 | if( ssl->session_negotiate != NULL ) |
| 4608 | return( ssl->session_negotiate->verify_result ); |
| 4609 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 4610 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 4613 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 4614 | { |
| 4615 | if( ssl == NULL || ssl->session == NULL ) |
| 4616 | return( 0 ); |
| 4617 | |
| 4618 | return( ssl->session->ciphersuite ); |
| 4619 | } |
| 4620 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4621 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4622 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4623 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4624 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4625 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4626 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4627 | } |
| 4628 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4629 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4630 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4631 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4632 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4633 | { |
| 4634 | switch( ssl->minor_ver ) |
| 4635 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4636 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4637 | return( "DTLSv1.2" ); |
| 4638 | |
| 4639 | default: |
| 4640 | return( "unknown (DTLS)" ); |
| 4641 | } |
| 4642 | } |
| 4643 | #endif |
| 4644 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4645 | switch( ssl->minor_ver ) |
| 4646 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4647 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4648 | return( "TLSv1.2" ); |
| 4649 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4650 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4651 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4652 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4653 | } |
| 4654 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4655 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4656 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 4657 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 4658 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4659 | size_t read_mfl; |
| 4660 | |
| 4661 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 4662 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4663 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 4664 | { |
| 4665 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 4666 | } |
| 4667 | |
| 4668 | /* Check if a smaller max length was negotiated */ |
| 4669 | if( ssl->session_out != NULL ) |
| 4670 | { |
| 4671 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 4672 | if( read_mfl < max_len ) |
| 4673 | { |
| 4674 | max_len = read_mfl; |
| 4675 | } |
| 4676 | } |
| 4677 | |
| 4678 | // During a handshake, use the value being negotiated |
| 4679 | if( ssl->session_negotiate != NULL ) |
| 4680 | { |
| 4681 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 4682 | if( read_mfl < max_len ) |
| 4683 | { |
| 4684 | max_len = read_mfl; |
| 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | return( max_len ); |
| 4689 | } |
| 4690 | |
| 4691 | 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] | 4692 | { |
| 4693 | size_t max_len; |
| 4694 | |
| 4695 | /* |
| 4696 | * Assume mfl_code is correct since it was checked when set |
| 4697 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4698 | 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] | 4699 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4700 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4701 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4702 | 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] | 4703 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4704 | 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] | 4705 | } |
| 4706 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4707 | /* During a handshake, use the value being negotiated */ |
| 4708 | if( ssl->session_negotiate != NULL && |
| 4709 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 4710 | { |
| 4711 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 4712 | } |
| 4713 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4714 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4715 | } |
| 4716 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4717 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4718 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4719 | 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] | 4720 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 4721 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 4722 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4723 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 4724 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 4725 | return ( 0 ); |
| 4726 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4727 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 4728 | return( ssl->mtu ); |
| 4729 | |
| 4730 | if( ssl->mtu == 0 ) |
| 4731 | return( ssl->handshake->mtu ); |
| 4732 | |
| 4733 | return( ssl->mtu < ssl->handshake->mtu ? |
| 4734 | ssl->mtu : ssl->handshake->mtu ); |
| 4735 | } |
| 4736 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4737 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4738 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 4739 | { |
| 4740 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 4741 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 4742 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4743 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4744 | (void) ssl; |
| 4745 | #endif |
| 4746 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4747 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4748 | 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] | 4749 | |
| 4750 | if( max_len > mfl ) |
| 4751 | max_len = mfl; |
| 4752 | #endif |
| 4753 | |
| 4754 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4755 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4756 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4757 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4758 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 4759 | const size_t overhead = (size_t) ret; |
| 4760 | |
| 4761 | if( ret < 0 ) |
| 4762 | return( ret ); |
| 4763 | |
| 4764 | if( mtu <= overhead ) |
| 4765 | { |
| 4766 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 4767 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4768 | } |
| 4769 | |
| 4770 | if( max_len > mtu - overhead ) |
| 4771 | max_len = mtu - overhead; |
| 4772 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4773 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4774 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 4775 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4776 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4777 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4778 | #endif |
| 4779 | |
| 4780 | return( (int) max_len ); |
| 4781 | } |
| 4782 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 4783 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 4784 | { |
| 4785 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 4786 | |
| 4787 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4788 | (void) ssl; |
| 4789 | #endif |
| 4790 | |
| 4791 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4792 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 4793 | |
| 4794 | if( max_len > mfl ) |
| 4795 | max_len = mfl; |
| 4796 | #endif |
| 4797 | |
| 4798 | return( (int) max_len ); |
| 4799 | } |
| 4800 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4801 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4802 | 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] | 4803 | { |
| 4804 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4805 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4806 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 4807 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4808 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 4809 | #else |
| 4810 | return( NULL ); |
| 4811 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4812 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4813 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4814 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4815 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 4816 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 4817 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4818 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4819 | int ret; |
| 4820 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4821 | if( ssl == NULL || |
| 4822 | dst == NULL || |
| 4823 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4824 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4825 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4826 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4827 | } |
| 4828 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4829 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 4830 | * idempotent: Each session can only be exported once. |
| 4831 | * |
| 4832 | * (This is in preparation for TLS 1.3 support where we will |
| 4833 | * need the ability to export multiple sessions (aka tickets), |
| 4834 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 4835 | * multiple times until it fails.) |
| 4836 | * |
| 4837 | * Check whether we have already exported the current session, |
| 4838 | * and fail if so. |
| 4839 | */ |
| 4840 | if( ssl->session->exported == 1 ) |
| 4841 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4842 | |
| 4843 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 4844 | if( ret != 0 ) |
| 4845 | return( ret ); |
| 4846 | |
| 4847 | /* Remember that we've exported the session. */ |
| 4848 | ssl->session->exported = 1; |
| 4849 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4850 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4851 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4852 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4853 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4854 | * Define ticket header determining Mbed TLS version |
| 4855 | * and structure of the ticket. |
| 4856 | */ |
| 4857 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4858 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4859 | * Define bitflag determining compile-time settings influencing |
| 4860 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4861 | */ |
| 4862 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4863 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4864 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4865 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4866 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4867 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4868 | |
| 4869 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4870 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4871 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4872 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4873 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4874 | |
| 4875 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4876 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4877 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4878 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4879 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 4880 | |
| 4881 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4882 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4883 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4884 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4885 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4886 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4887 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4888 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4889 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4890 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4891 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 4892 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4893 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4894 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 4895 | #else |
| 4896 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 4897 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4898 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4899 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 4900 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 4901 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 4902 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 4903 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 4904 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4905 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4906 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4907 | ( (uint16_t) ( \ |
| 4908 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 4909 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 4910 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 4911 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4912 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 4913 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4914 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4915 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4916 | MBEDTLS_VERSION_MAJOR, |
| 4917 | MBEDTLS_VERSION_MINOR, |
| 4918 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4919 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 4920 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4921 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4922 | |
| 4923 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4924 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4925 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4926 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4927 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4928 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 4929 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 4930 | * opaque session_format[2]; // library-version specific 16-bit field |
| 4931 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4932 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4933 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4934 | * Note: When updating the format, remember to keep |
| 4935 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4936 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4937 | * // In this version, `session_format` determines |
| 4938 | * // the setting of those compile-time |
| 4939 | * // configuration options which influence |
| 4940 | * // the structure of mbedtls_ssl_session. |
| 4941 | * |
Hanno Becker | fa0d61e | 2021-08-02 08:56:14 +0100 | [diff] [blame] | 4942 | * uint8_t minor_ver; // Protocol-version. Possible values: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4943 | * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3) |
| 4944 | * |
| 4945 | * select (serialized_session.minor_ver) { |
| 4946 | * |
| 4947 | * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2 |
| 4948 | * serialized_session_tls12 data; |
| 4949 | * |
| 4950 | * }; |
| 4951 | * |
| 4952 | * } serialized_session; |
| 4953 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4954 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4955 | |
| 4956 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4957 | /* Serialization of TLS 1.2 sessions: |
| 4958 | * |
| 4959 | * struct { |
| 4960 | * uint64 start_time; |
| 4961 | * uint8 ciphersuite[2]; // defined by the standard |
| 4962 | * uint8 compression; // 0 or 1 |
| 4963 | * uint8 session_id_len; // at most 32 |
| 4964 | * opaque session_id[32]; |
| 4965 | * opaque master[48]; // fixed length in the standard |
| 4966 | * uint32 verify_result; |
| 4967 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 4968 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 4969 | * uint32 ticket_lifetime; |
| 4970 | * uint8 mfl_code; // up to 255 according to standard |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4971 | * uint8 encrypt_then_mac; // 0 or 1 |
| 4972 | * } serialized_session_tls12; |
| 4973 | * |
| 4974 | */ |
| 4975 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 4976 | unsigned char *buf, |
| 4977 | size_t buf_len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4978 | { |
| 4979 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4980 | size_t used = 0; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4981 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4982 | #if defined(MBEDTLS_HAVE_TIME) |
| 4983 | uint64_t start; |
| 4984 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4985 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4986 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4987 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4988 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4989 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4990 | |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4991 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4992 | * Time |
| 4993 | */ |
| 4994 | #if defined(MBEDTLS_HAVE_TIME) |
| 4995 | used += 8; |
| 4996 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4997 | if( used <= buf_len ) |
| 4998 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4999 | start = (uint64_t) session->start; |
| 5000 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5001 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 5002 | p += 8; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5003 | } |
| 5004 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5005 | |
| 5006 | /* |
| 5007 | * Basic mandatory fields |
| 5008 | */ |
| 5009 | used += 2 /* ciphersuite */ |
| 5010 | + 1 /* compression */ |
| 5011 | + 1 /* id_len */ |
| 5012 | + sizeof( session->id ) |
| 5013 | + sizeof( session->master ) |
| 5014 | + 4; /* verify_result */ |
| 5015 | |
| 5016 | if( used <= buf_len ) |
| 5017 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5018 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 5019 | p += 2; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5020 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5021 | *p++ = MBEDTLS_BYTE_0( session->compression ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5022 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5023 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5024 | memcpy( p, session->id, 32 ); |
| 5025 | p += 32; |
| 5026 | |
| 5027 | memcpy( p, session->master, 48 ); |
| 5028 | p += 48; |
| 5029 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5030 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 5031 | p += 4; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5032 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5033 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5034 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5035 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5036 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5037 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5038 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5039 | if( session->peer_cert == NULL ) |
| 5040 | cert_len = 0; |
| 5041 | else |
| 5042 | cert_len = session->peer_cert->raw.len; |
| 5043 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5044 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5045 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5046 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5047 | { |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5048 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 5049 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 5050 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5051 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5052 | if( session->peer_cert != NULL ) |
| 5053 | { |
| 5054 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 5055 | p += cert_len; |
| 5056 | } |
| 5057 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5058 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5059 | if( session->peer_cert_digest != NULL ) |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5060 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5061 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 5062 | if( used <= buf_len ) |
| 5063 | { |
| 5064 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 5065 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 5066 | memcpy( p, session->peer_cert_digest, |
| 5067 | session->peer_cert_digest_len ); |
| 5068 | p += session->peer_cert_digest_len; |
| 5069 | } |
| 5070 | } |
| 5071 | else |
| 5072 | { |
| 5073 | used += 2; |
| 5074 | if( used <= buf_len ) |
| 5075 | { |
| 5076 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 5077 | *p++ = 0; |
| 5078 | } |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5079 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5080 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5081 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5082 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5083 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5084 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5085 | */ |
| 5086 | #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] | 5087 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5088 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5089 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5090 | { |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5091 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 5092 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 5093 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5094 | |
| 5095 | if( session->ticket != NULL ) |
| 5096 | { |
| 5097 | memcpy( p, session->ticket, session->ticket_len ); |
| 5098 | p += session->ticket_len; |
| 5099 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5100 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5101 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 5102 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5103 | } |
| 5104 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5105 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5106 | /* |
| 5107 | * Misc extension-related info |
| 5108 | */ |
| 5109 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 5110 | used += 1; |
| 5111 | |
| 5112 | if( used <= buf_len ) |
| 5113 | *p++ = session->mfl_code; |
| 5114 | #endif |
| 5115 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5116 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5117 | used += 1; |
| 5118 | |
| 5119 | if( used <= buf_len ) |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5120 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5121 | #endif |
| 5122 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5123 | return( used ); |
| 5124 | } |
| 5125 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5126 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5127 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 5128 | unsigned char omit_header, |
| 5129 | unsigned char *buf, |
| 5130 | size_t buf_len, |
| 5131 | size_t *olen ) |
| 5132 | { |
| 5133 | unsigned char *p = buf; |
| 5134 | size_t used = 0; |
| 5135 | |
| 5136 | if( !omit_header ) |
| 5137 | { |
| 5138 | /* |
| 5139 | * Add Mbed TLS version identifier |
| 5140 | */ |
| 5141 | |
| 5142 | used += sizeof( ssl_serialized_session_header ); |
| 5143 | |
| 5144 | if( used <= buf_len ) |
| 5145 | { |
| 5146 | memcpy( p, ssl_serialized_session_header, |
| 5147 | sizeof( ssl_serialized_session_header ) ); |
| 5148 | p += sizeof( ssl_serialized_session_header ); |
| 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | /* |
| 5153 | * TLS version identifier |
| 5154 | */ |
| 5155 | used += 1; |
| 5156 | if( used <= buf_len ) |
| 5157 | { |
| 5158 | *p++ = session->minor_ver; |
| 5159 | } |
| 5160 | |
| 5161 | /* Forward to version-specific serialization routine. */ |
| 5162 | switch( session->minor_ver ) |
| 5163 | { |
| 5164 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5165 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 5166 | { |
| 5167 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; |
| 5168 | used += ssl_session_save_tls12( session, p, remaining_len ); |
| 5169 | break; |
| 5170 | } |
| 5171 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5172 | |
| 5173 | default: |
| 5174 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5175 | } |
| 5176 | |
| 5177 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5178 | if( used > buf_len ) |
| 5179 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5180 | |
| 5181 | return( 0 ); |
| 5182 | } |
| 5183 | |
| 5184 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5185 | * Public wrapper for ssl_session_save() |
| 5186 | */ |
| 5187 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 5188 | unsigned char *buf, |
| 5189 | size_t buf_len, |
| 5190 | size_t *olen ) |
| 5191 | { |
| 5192 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 5193 | } |
| 5194 | |
| 5195 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5196 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5197 | * |
| 5198 | * 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] | 5199 | * case of error, and has an extra option omit_header. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5200 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5201 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 5202 | const unsigned char *buf, |
| 5203 | size_t len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5204 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5205 | #if defined(MBEDTLS_HAVE_TIME) |
| 5206 | uint64_t start; |
| 5207 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5208 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5209 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5210 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5211 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5212 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5213 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5214 | const unsigned char *p = buf; |
| 5215 | const unsigned char * const end = buf + len; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5216 | |
| 5217 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5218 | * Time |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5219 | */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5220 | #if defined(MBEDTLS_HAVE_TIME) |
| 5221 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5222 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5223 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5224 | start = ( (uint64_t) p[0] << 56 ) | |
| 5225 | ( (uint64_t) p[1] << 48 ) | |
| 5226 | ( (uint64_t) p[2] << 40 ) | |
| 5227 | ( (uint64_t) p[3] << 32 ) | |
| 5228 | ( (uint64_t) p[4] << 24 ) | |
| 5229 | ( (uint64_t) p[5] << 16 ) | |
| 5230 | ( (uint64_t) p[6] << 8 ) | |
| 5231 | ( (uint64_t) p[7] ); |
| 5232 | p += 8; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5233 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5234 | session->start = (time_t) start; |
| 5235 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5236 | |
| 5237 | /* |
| 5238 | * Basic mandatory fields |
| 5239 | */ |
| 5240 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 5241 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5242 | |
| 5243 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 5244 | p += 2; |
| 5245 | |
| 5246 | session->compression = *p++; |
| 5247 | |
| 5248 | session->id_len = *p++; |
| 5249 | memcpy( session->id, p, 32 ); |
| 5250 | p += 32; |
| 5251 | |
| 5252 | memcpy( session->master, p, 48 ); |
| 5253 | p += 48; |
| 5254 | |
| 5255 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 5256 | ( (uint32_t) p[1] << 16 ) | |
| 5257 | ( (uint32_t) p[2] << 8 ) | |
| 5258 | ( (uint32_t) p[3] ); |
| 5259 | p += 4; |
| 5260 | |
| 5261 | /* Immediately clear invalid pointer values that have been read, in case |
| 5262 | * we exit early before we replaced them with valid ones. */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5263 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5264 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5265 | session->peer_cert = NULL; |
| 5266 | #else |
| 5267 | session->peer_cert_digest = NULL; |
| 5268 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5269 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5270 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5271 | session->ticket = NULL; |
| 5272 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5273 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5274 | /* |
| 5275 | * Peer certificate |
| 5276 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5277 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5278 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5279 | /* Deserialize CRT from the end of the ticket. */ |
| 5280 | if( 3 > (size_t)( end - p ) ) |
| 5281 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5282 | |
| 5283 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5284 | p += 3; |
| 5285 | |
| 5286 | if( cert_len != 0 ) |
| 5287 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5288 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5289 | |
| 5290 | if( cert_len > (size_t)( end - p ) ) |
| 5291 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5292 | |
| 5293 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 5294 | |
| 5295 | if( session->peer_cert == NULL ) |
| 5296 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5297 | |
| 5298 | mbedtls_x509_crt_init( session->peer_cert ); |
| 5299 | |
| 5300 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 5301 | p, cert_len ) ) != 0 ) |
| 5302 | { |
| 5303 | mbedtls_x509_crt_free( session->peer_cert ); |
| 5304 | mbedtls_free( session->peer_cert ); |
| 5305 | session->peer_cert = NULL; |
| 5306 | return( ret ); |
| 5307 | } |
| 5308 | |
| 5309 | p += cert_len; |
| 5310 | } |
| 5311 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5312 | /* Deserialize CRT digest from the end of the ticket. */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5313 | if( 2 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5314 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5315 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5316 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 5317 | session->peer_cert_digest_len = (size_t) *p++; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5318 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5319 | if( session->peer_cert_digest_len != 0 ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5320 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5321 | const mbedtls_md_info_t *md_info = |
| 5322 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 5323 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5324 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5325 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 5326 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5327 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5328 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 5329 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5330 | |
| 5331 | session->peer_cert_digest = |
| 5332 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5333 | if( session->peer_cert_digest == NULL ) |
| 5334 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5335 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5336 | memcpy( session->peer_cert_digest, p, |
| 5337 | session->peer_cert_digest_len ); |
| 5338 | p += session->peer_cert_digest_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5339 | } |
| 5340 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5341 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5342 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5343 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5344 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5345 | */ |
| 5346 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5347 | if( 3 > (size_t)( end - p ) ) |
| 5348 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5349 | |
| 5350 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5351 | p += 3; |
| 5352 | |
| 5353 | if( session->ticket_len != 0 ) |
| 5354 | { |
| 5355 | if( session->ticket_len > (size_t)( end - p ) ) |
| 5356 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5357 | |
| 5358 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 5359 | if( session->ticket == NULL ) |
| 5360 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5361 | |
| 5362 | memcpy( session->ticket, p, session->ticket_len ); |
| 5363 | p += session->ticket_len; |
| 5364 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5365 | |
| 5366 | if( 4 > (size_t)( end - p ) ) |
| 5367 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5368 | |
| 5369 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 5370 | ( (uint32_t) p[1] << 16 ) | |
| 5371 | ( (uint32_t) p[2] << 8 ) | |
| 5372 | ( (uint32_t) p[3] ); |
| 5373 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5374 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5375 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5376 | /* |
| 5377 | * Misc extension-related info |
| 5378 | */ |
| 5379 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 5380 | if( 1 > (size_t)( end - p ) ) |
| 5381 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5382 | |
| 5383 | session->mfl_code = *p++; |
| 5384 | #endif |
| 5385 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5386 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5387 | if( 1 > (size_t)( end - p ) ) |
| 5388 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5389 | |
| 5390 | session->encrypt_then_mac = *p++; |
| 5391 | #endif |
| 5392 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5393 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5394 | if( p != end ) |
| 5395 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5396 | |
| 5397 | return( 0 ); |
| 5398 | } |
| 5399 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5400 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 5401 | unsigned char omit_header, |
| 5402 | const unsigned char *buf, |
| 5403 | size_t len ) |
| 5404 | { |
| 5405 | const unsigned char *p = buf; |
| 5406 | const unsigned char * const end = buf + len; |
| 5407 | |
| 5408 | if( !omit_header ) |
| 5409 | { |
| 5410 | /* |
| 5411 | * Check Mbed TLS version identifier |
| 5412 | */ |
| 5413 | |
| 5414 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 5415 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5416 | |
| 5417 | if( memcmp( p, ssl_serialized_session_header, |
| 5418 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 5419 | { |
| 5420 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 5421 | } |
| 5422 | p += sizeof( ssl_serialized_session_header ); |
| 5423 | } |
| 5424 | |
| 5425 | /* |
| 5426 | * TLS version identifier |
| 5427 | */ |
| 5428 | if( 1 > (size_t)( end - p ) ) |
| 5429 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5430 | session->minor_ver = *p++; |
| 5431 | |
| 5432 | /* Dispatch according to TLS version. */ |
| 5433 | switch( session->minor_ver ) |
| 5434 | { |
| 5435 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5436 | case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */ |
| 5437 | { |
| 5438 | size_t remaining_len = ( end - p ); |
| 5439 | return( ssl_session_load_tls12( session, p, remaining_len ) ); |
| 5440 | } |
| 5441 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5442 | |
| 5443 | default: |
| 5444 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5445 | } |
| 5446 | } |
| 5447 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5448 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5449 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5450 | */ |
| 5451 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 5452 | const unsigned char *buf, |
| 5453 | size_t len ) |
| 5454 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5455 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5456 | |
| 5457 | if( ret != 0 ) |
| 5458 | mbedtls_ssl_session_free( session ); |
| 5459 | |
| 5460 | return( ret ); |
| 5461 | } |
| 5462 | |
| 5463 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5464 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5465 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5466 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 5467 | { |
| 5468 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5469 | |
| 5470 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 5471 | return( ret ); |
| 5472 | |
| 5473 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5474 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5475 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 5476 | { |
| 5477 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 5478 | return( ret ); |
| 5479 | } |
| 5480 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5481 | |
| 5482 | return( ret ); |
| 5483 | } |
| 5484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5485 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5486 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5487 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5488 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5489 | if( ssl == NULL || |
| 5490 | ssl->conf == NULL || |
| 5491 | ssl->handshake == NULL || |
| 5492 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5493 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5494 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5495 | } |
| 5496 | |
| 5497 | ret = ssl_prepare_handshake_step( ssl ); |
| 5498 | if( ret != 0 ) |
| 5499 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5500 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5501 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 5502 | if( ret != 0 ) |
| 5503 | goto cleanup; |
| 5504 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5505 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5506 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5507 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5508 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5509 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 5510 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5511 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5512 | |
| 5513 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5514 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 5515 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 5516 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5517 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5518 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5519 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5520 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5521 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5522 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5523 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 5524 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5525 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5526 | |
| 5527 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5528 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 5529 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 5530 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5531 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5532 | #endif |
| 5533 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5534 | if( ret != 0 ) |
| 5535 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 5536 | /* handshake_step return error. And it is same |
| 5537 | * with alert_reason. |
| 5538 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 5539 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5540 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 5541 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5542 | goto cleanup; |
| 5543 | } |
| 5544 | } |
| 5545 | |
| 5546 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5547 | return( ret ); |
| 5548 | } |
| 5549 | |
| 5550 | /* |
| 5551 | * Perform the SSL handshake |
| 5552 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5553 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5554 | { |
| 5555 | int ret = 0; |
| 5556 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5557 | /* Sanity checks */ |
| 5558 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5559 | if( ssl == NULL || ssl->conf == NULL ) |
| 5560 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5561 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5562 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5563 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5564 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 5565 | { |
| 5566 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 5567 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 5568 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5569 | } |
| 5570 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5571 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5572 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5573 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5574 | /* Main handshake loop */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5575 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5576 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5577 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5578 | |
| 5579 | if( ret != 0 ) |
| 5580 | break; |
| 5581 | } |
| 5582 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5583 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5584 | |
| 5585 | return( ret ); |
| 5586 | } |
| 5587 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5588 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5589 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5590 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5591 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5592 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5593 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5594 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5595 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5597 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5598 | |
| 5599 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5600 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5601 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5602 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5603 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5604 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5605 | 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] | 5606 | return( ret ); |
| 5607 | } |
| 5608 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5609 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5610 | |
| 5611 | return( 0 ); |
| 5612 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5613 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5614 | |
| 5615 | /* |
| 5616 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5617 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 5618 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 5619 | * - 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] | 5620 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5621 | * 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] | 5622 | * 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] | 5623 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5624 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5625 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5626 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5628 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5629 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5630 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 5631 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5632 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5633 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 5634 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5635 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5636 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5637 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5638 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5639 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 5640 | ssl->handshake->out_msg_seq = 1; |
| 5641 | else |
| 5642 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5643 | } |
| 5644 | #endif |
| 5645 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5646 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 5647 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5648 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5649 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5650 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5651 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5652 | return( ret ); |
| 5653 | } |
| 5654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5655 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5656 | |
| 5657 | return( 0 ); |
| 5658 | } |
| 5659 | |
| 5660 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5661 | * Renegotiate current connection on client, |
| 5662 | * or request renegotiation on server |
| 5663 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5664 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5665 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5666 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5667 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5668 | if( ssl == NULL || ssl->conf == NULL ) |
| 5669 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5671 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5672 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5673 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5674 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5675 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5676 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5678 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5679 | |
| 5680 | /* Did we already try/start sending HelloRequest? */ |
| 5681 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5682 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5683 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5684 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5685 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5686 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5688 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5689 | /* |
| 5690 | * On client, either start the renegotiation process or, |
| 5691 | * if already in progress, continue the handshake |
| 5692 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5693 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5694 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5695 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5696 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5697 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5698 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5699 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5700 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5701 | return( ret ); |
| 5702 | } |
| 5703 | } |
| 5704 | else |
| 5705 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5706 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5707 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5708 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5709 | return( ret ); |
| 5710 | } |
| 5711 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5712 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5713 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 5714 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5715 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5716 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5717 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5718 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5719 | 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] | 5720 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5721 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5722 | |
| 5723 | while( cur != NULL ) |
| 5724 | { |
| 5725 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5726 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5727 | cur = next; |
| 5728 | } |
| 5729 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5730 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5731 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5732 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5733 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5734 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 5735 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5736 | if( handshake == NULL ) |
| 5737 | return; |
| 5738 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5739 | #if defined(MBEDTLS_ECP_C) |
| 5740 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5741 | if ( ssl->handshake->group_list_heap_allocated ) |
| 5742 | mbedtls_free( (void*) handshake->group_list ); |
| 5743 | handshake->group_list = NULL; |
| 5744 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 5745 | #endif /* MBEDTLS_ECP_C */ |
| 5746 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 5747 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 5748 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5749 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 5750 | mbedtls_free( (void*) handshake->sig_algs ); |
| 5751 | handshake->sig_algs = NULL; |
| 5752 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 5753 | |
| 5754 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 5755 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5756 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 5757 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 5758 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 5759 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5760 | handshake->async_in_progress = 0; |
| 5761 | } |
| 5762 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 5763 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5764 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5765 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5766 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5767 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 5768 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5769 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 5770 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5771 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 5772 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5773 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 5774 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5775 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5776 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 5777 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5778 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5779 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5780 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5781 | #if defined(MBEDTLS_DHM_C) |
| 5782 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5783 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5784 | #if defined(MBEDTLS_ECDH_C) |
| 5785 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5786 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 5787 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 5788 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 5789 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5790 | mbedtls_free( handshake->ecjpake_cache ); |
| 5791 | handshake->ecjpake_cache = NULL; |
| 5792 | handshake->ecjpake_cache_len = 0; |
| 5793 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 5794 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5795 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 5796 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 5797 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 5798 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5799 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 5800 | #endif |
| 5801 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 5802 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5803 | if( handshake->psk != NULL ) |
| 5804 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5805 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5806 | mbedtls_free( handshake->psk ); |
| 5807 | } |
| 5808 | #endif |
| 5809 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5810 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5811 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5812 | /* |
| 5813 | * Free only the linked list wrapper, not the keys themselves |
| 5814 | * since the belong to the SNI callback |
| 5815 | */ |
| 5816 | if( handshake->sni_key_cert != NULL ) |
| 5817 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5818 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5819 | |
| 5820 | while( cur != NULL ) |
| 5821 | { |
| 5822 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5823 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5824 | cur = next; |
| 5825 | } |
| 5826 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5827 | #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] | 5828 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 5829 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 5830 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 5831 | if( handshake->ecrs_peer_cert != NULL ) |
| 5832 | { |
| 5833 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 5834 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 5835 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 5836 | #endif |
| 5837 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 5838 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5839 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5840 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 5841 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5842 | |
XiaokangQian | 3490974 | 2022-01-27 02:25:04 +0000 | [diff] [blame] | 5843 | #if defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5844 | mbedtls_free( handshake->verify_cookie ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 5845 | #endif /* MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 5846 | |
| 5847 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 5848 | mbedtls_ssl_flight_free( handshake->flight ); |
| 5849 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 5850 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 5851 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 5852 | #if defined(MBEDTLS_ECDH_C) && \ |
| 5853 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5854 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 5855 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 5856 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5857 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 5858 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 5859 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5860 | mbedtls_free( handshake->transform_earlydata ); |
| 5861 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5862 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5863 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5864 | |
| 5865 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5866 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 5867 | * processes datagrams and the fact that a datagram is allowed to have |
| 5868 | * several records in it, it is possible that the I/O buffers are not |
| 5869 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 5870 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 5871 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5872 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 5873 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5874 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 5875 | mbedtls_platform_zeroize( handshake, |
| 5876 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5877 | } |
| 5878 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5879 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5880 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5881 | if( session == NULL ) |
| 5882 | return; |
| 5883 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5884 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 5885 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 5886 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 5887 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 5888 | #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] | 5889 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 5890 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 5891 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5892 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5893 | } |
| 5894 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 5895 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5896 | |
| 5897 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5898 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 5899 | #else |
| 5900 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 5901 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5902 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5903 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5904 | |
| 5905 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5906 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 5907 | #else |
| 5908 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 5909 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5910 | |
| 5911 | #if defined(MBEDTLS_SSL_ALPN) |
| 5912 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 5913 | #else |
| 5914 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 5915 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5916 | |
| 5917 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 5918 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 5919 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 5920 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 5921 | |
| 5922 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 5923 | ( (uint32_t) ( \ |
| 5924 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 5925 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 5926 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 5927 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 5928 | 0u ) ) |
| 5929 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5930 | static unsigned char ssl_serialized_context_header[] = { |
| 5931 | MBEDTLS_VERSION_MAJOR, |
| 5932 | MBEDTLS_VERSION_MINOR, |
| 5933 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5934 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 5935 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 5936 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 5937 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 5938 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5939 | }; |
| 5940 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5941 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5942 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5943 | * |
| 5944 | * The format of the serialized data is: |
| 5945 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 5946 | * |
| 5947 | * // header |
| 5948 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5949 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5950 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5951 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5952 | * Note: When updating the format, remember to keep these |
| 5953 | * 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] | 5954 | * |
| 5955 | * // session sub-structure |
| 5956 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 5957 | * // transform sub-structure |
| 5958 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 5959 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 5960 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 5961 | * // fields from ssl_context |
| 5962 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 5963 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 5964 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 5965 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 5966 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 5967 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 5968 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 5969 | * |
| 5970 | * Note that many fields of the ssl_context or sub-structures are not |
| 5971 | * serialized, as they fall in one of the following categories: |
| 5972 | * |
| 5973 | * 1. forced value (eg in_left must be 0) |
| 5974 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 5975 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 5976 | * 4. value was temporary (eg content of input buffer) |
| 5977 | * 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] | 5978 | */ |
| 5979 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 5980 | unsigned char *buf, |
| 5981 | size_t buf_len, |
| 5982 | size_t *olen ) |
| 5983 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5984 | unsigned char *p = buf; |
| 5985 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5986 | size_t session_len; |
| 5987 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5988 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5989 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5990 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 5991 | * this function's documentation. |
| 5992 | * |
| 5993 | * These are due to assumptions/limitations in the implementation. Some of |
| 5994 | * them are likely to stay (no handshake in progress) some might go away |
| 5995 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5996 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5997 | /* The initial handshake must be over */ |
| 5998 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5999 | { |
| 6000 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6001 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6002 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6003 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6004 | { |
| 6005 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6006 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6007 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6008 | /* Double-check that sub-structures are indeed ready */ |
| 6009 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6010 | { |
| 6011 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6012 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6013 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6014 | /* There must be no pending incoming or outgoing data */ |
| 6015 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6016 | { |
| 6017 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6018 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6019 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6020 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6021 | { |
| 6022 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6023 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6024 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6025 | /* Protocol must be DLTS, not TLS */ |
| 6026 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6027 | { |
| 6028 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6029 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6030 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6031 | /* Version must be 1.2 */ |
| 6032 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6033 | { |
| 6034 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6035 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6036 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6037 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6038 | { |
| 6039 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6040 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6041 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6042 | /* We must be using an AEAD ciphersuite */ |
| 6043 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6044 | { |
| 6045 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6046 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6047 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6048 | /* Renegotiation must not be enabled */ |
| 6049 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6050 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6051 | { |
| 6052 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6053 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6054 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6055 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6056 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6057 | /* |
| 6058 | * Version and format identifier |
| 6059 | */ |
| 6060 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6061 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6062 | if( used <= buf_len ) |
| 6063 | { |
| 6064 | memcpy( p, ssl_serialized_context_header, |
| 6065 | sizeof( ssl_serialized_context_header ) ); |
| 6066 | p += sizeof( ssl_serialized_context_header ); |
| 6067 | } |
| 6068 | |
| 6069 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6070 | * Session (length + data) |
| 6071 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6072 | 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] | 6073 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 6074 | return( ret ); |
| 6075 | |
| 6076 | used += 4 + session_len; |
| 6077 | if( used <= buf_len ) |
| 6078 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6079 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 6080 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6081 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6082 | ret = ssl_session_save( ssl->session, 1, |
| 6083 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6084 | if( ret != 0 ) |
| 6085 | return( ret ); |
| 6086 | |
| 6087 | p += session_len; |
| 6088 | } |
| 6089 | |
| 6090 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6091 | * Transform |
| 6092 | */ |
| 6093 | used += sizeof( ssl->transform->randbytes ); |
| 6094 | if( used <= buf_len ) |
| 6095 | { |
| 6096 | memcpy( p, ssl->transform->randbytes, |
| 6097 | sizeof( ssl->transform->randbytes ) ); |
| 6098 | p += sizeof( ssl->transform->randbytes ); |
| 6099 | } |
| 6100 | |
| 6101 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6102 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 6103 | if( used <= buf_len ) |
| 6104 | { |
| 6105 | *p++ = ssl->transform->in_cid_len; |
| 6106 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 6107 | p += ssl->transform->in_cid_len; |
| 6108 | |
| 6109 | *p++ = ssl->transform->out_cid_len; |
| 6110 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 6111 | p += ssl->transform->out_cid_len; |
| 6112 | } |
| 6113 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6114 | |
| 6115 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6116 | * Saved fields from top-level ssl_context structure |
| 6117 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6118 | used += 4; |
| 6119 | if( used <= buf_len ) |
| 6120 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6121 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 6122 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6123 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6124 | |
| 6125 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6126 | used += 16; |
| 6127 | if( used <= buf_len ) |
| 6128 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6129 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 6130 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6131 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6132 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 6133 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6134 | } |
| 6135 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6136 | |
| 6137 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6138 | used += 1; |
| 6139 | if( used <= buf_len ) |
| 6140 | { |
| 6141 | *p++ = ssl->disable_datagram_packing; |
| 6142 | } |
| 6143 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6144 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 6145 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6146 | if( used <= buf_len ) |
| 6147 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 6148 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 6149 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6150 | } |
| 6151 | |
| 6152 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6153 | used += 2; |
| 6154 | if( used <= buf_len ) |
| 6155 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6156 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 6157 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6158 | } |
| 6159 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6160 | |
| 6161 | #if defined(MBEDTLS_SSL_ALPN) |
| 6162 | { |
| 6163 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 6164 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6165 | : 0; |
| 6166 | |
| 6167 | used += 1 + alpn_len; |
| 6168 | if( used <= buf_len ) |
| 6169 | { |
| 6170 | *p++ = alpn_len; |
| 6171 | |
| 6172 | if( ssl->alpn_chosen != NULL ) |
| 6173 | { |
| 6174 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 6175 | p += alpn_len; |
| 6176 | } |
| 6177 | } |
| 6178 | } |
| 6179 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6180 | |
| 6181 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6182 | * Done |
| 6183 | */ |
| 6184 | *olen = used; |
| 6185 | |
| 6186 | if( used > buf_len ) |
| 6187 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6188 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6189 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 6190 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6191 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6192 | } |
| 6193 | |
| 6194 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6195 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 6196 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 6197 | */ |
| 6198 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 6199 | const char *label, |
| 6200 | const unsigned char *random, size_t rlen, |
| 6201 | unsigned char *dstbuf, size_t dlen ); |
| 6202 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 6203 | { |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 6204 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6205 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6206 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 6207 | |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6208 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 6209 | return( tls_prf_sha384 ); |
Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 6210 | #else |
| 6211 | (void) ciphersuite_id; |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6212 | #endif |
| 6213 | return( tls_prf_sha256 ); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6214 | } |
| 6215 | |
| 6216 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6217 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6218 | * |
| 6219 | * This internal version is wrapped by a public function that cleans up in |
| 6220 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6221 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6222 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 6223 | const unsigned char *buf, |
| 6224 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6225 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6226 | const unsigned char *p = buf; |
| 6227 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6228 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6229 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6230 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6231 | /* |
| 6232 | * The context should have been freshly setup or reset. |
| 6233 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 6234 | * (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] | 6235 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 6236 | */ |
| 6237 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 6238 | ssl->session != NULL ) |
| 6239 | { |
| 6240 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6241 | } |
| 6242 | |
| 6243 | /* |
| 6244 | * We can't check that the config matches the initial one, but we can at |
| 6245 | * least check it matches the requirements for serializing. |
| 6246 | */ |
| 6247 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 6248 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 6249 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 6250 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || |
| 6251 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || |
| 6252 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6253 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6254 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6255 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6256 | { |
| 6257 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6258 | } |
| 6259 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6260 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 6261 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6262 | /* |
| 6263 | * Check version identifier |
| 6264 | */ |
| 6265 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 6266 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6267 | |
| 6268 | if( memcmp( p, ssl_serialized_context_header, |
| 6269 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 6270 | { |
| 6271 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 6272 | } |
| 6273 | p += sizeof( ssl_serialized_context_header ); |
| 6274 | |
| 6275 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6276 | * Session |
| 6277 | */ |
| 6278 | if( (size_t)( end - p ) < 4 ) |
| 6279 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6280 | |
| 6281 | session_len = ( (size_t) p[0] << 24 ) | |
| 6282 | ( (size_t) p[1] << 16 ) | |
| 6283 | ( (size_t) p[2] << 8 ) | |
| 6284 | ( (size_t) p[3] ); |
| 6285 | p += 4; |
| 6286 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6287 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6288 | * 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] | 6289 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6290 | ssl->session_in = ssl->session; |
| 6291 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6292 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6293 | |
| 6294 | if( (size_t)( end - p ) < session_len ) |
| 6295 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6296 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6297 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6298 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6299 | { |
| 6300 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6301 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6302 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6303 | |
| 6304 | p += session_len; |
| 6305 | |
| 6306 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6307 | * Transform |
| 6308 | */ |
| 6309 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6310 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6311 | * 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] | 6312 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6313 | ssl->transform_in = ssl->transform; |
| 6314 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6315 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6316 | |
| 6317 | /* Read random bytes and populate structure */ |
| 6318 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 6319 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6320 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6321 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6322 | ssl->session->ciphersuite, |
| 6323 | ssl->session->master, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6324 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 6325 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6326 | ssl->session->encrypt_then_mac, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6327 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 6328 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6329 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 6330 | p, /* currently pointing to randbytes */ |
| 6331 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 6332 | ssl->conf->endpoint, |
| 6333 | ssl ); |
| 6334 | if( ret != 0 ) |
| 6335 | return( ret ); |
| 6336 | |
| 6337 | p += sizeof( ssl->transform->randbytes ); |
| 6338 | |
| 6339 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6340 | /* Read connection IDs and store them */ |
| 6341 | if( (size_t)( end - p ) < 1 ) |
| 6342 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6343 | |
| 6344 | ssl->transform->in_cid_len = *p++; |
| 6345 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 6346 | 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] | 6347 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6348 | |
| 6349 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 6350 | p += ssl->transform->in_cid_len; |
| 6351 | |
| 6352 | ssl->transform->out_cid_len = *p++; |
| 6353 | |
| 6354 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 6355 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6356 | |
| 6357 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 6358 | p += ssl->transform->out_cid_len; |
| 6359 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6360 | |
| 6361 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6362 | * Saved fields from top-level ssl_context structure |
| 6363 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6364 | if( (size_t)( end - p ) < 4 ) |
| 6365 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6366 | |
| 6367 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 6368 | ( (uint32_t) p[1] << 16 ) | |
| 6369 | ( (uint32_t) p[2] << 8 ) | |
| 6370 | ( (uint32_t) p[3] ); |
| 6371 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6372 | |
| 6373 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6374 | if( (size_t)( end - p ) < 16 ) |
| 6375 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6376 | |
| 6377 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 6378 | ( (uint64_t) p[1] << 48 ) | |
| 6379 | ( (uint64_t) p[2] << 40 ) | |
| 6380 | ( (uint64_t) p[3] << 32 ) | |
| 6381 | ( (uint64_t) p[4] << 24 ) | |
| 6382 | ( (uint64_t) p[5] << 16 ) | |
| 6383 | ( (uint64_t) p[6] << 8 ) | |
| 6384 | ( (uint64_t) p[7] ); |
| 6385 | p += 8; |
| 6386 | |
| 6387 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 6388 | ( (uint64_t) p[1] << 48 ) | |
| 6389 | ( (uint64_t) p[2] << 40 ) | |
| 6390 | ( (uint64_t) p[3] << 32 ) | |
| 6391 | ( (uint64_t) p[4] << 24 ) | |
| 6392 | ( (uint64_t) p[5] << 16 ) | |
| 6393 | ( (uint64_t) p[6] << 8 ) | |
| 6394 | ( (uint64_t) p[7] ); |
| 6395 | p += 8; |
| 6396 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6397 | |
| 6398 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6399 | if( (size_t)( end - p ) < 1 ) |
| 6400 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6401 | |
| 6402 | ssl->disable_datagram_packing = *p++; |
| 6403 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6404 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 6405 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6406 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 6407 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 6408 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6409 | |
| 6410 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6411 | if( (size_t)( end - p ) < 2 ) |
| 6412 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6413 | |
| 6414 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 6415 | p += 2; |
| 6416 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6417 | |
| 6418 | #if defined(MBEDTLS_SSL_ALPN) |
| 6419 | { |
| 6420 | uint8_t alpn_len; |
| 6421 | const char **cur; |
| 6422 | |
| 6423 | if( (size_t)( end - p ) < 1 ) |
| 6424 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6425 | |
| 6426 | alpn_len = *p++; |
| 6427 | |
| 6428 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 6429 | { |
| 6430 | /* alpn_chosen should point to an item in the configured list */ |
| 6431 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 6432 | { |
| 6433 | if( strlen( *cur ) == alpn_len && |
| 6434 | memcmp( p, cur, alpn_len ) == 0 ) |
| 6435 | { |
| 6436 | ssl->alpn_chosen = *cur; |
| 6437 | break; |
| 6438 | } |
| 6439 | } |
| 6440 | } |
| 6441 | |
| 6442 | /* can only happen on conf mismatch */ |
| 6443 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 6444 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6445 | |
| 6446 | p += alpn_len; |
| 6447 | } |
| 6448 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6449 | |
| 6450 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6451 | * Forced fields from top-level ssl_context structure |
| 6452 | * |
| 6453 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 6454 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 6455 | */ |
| 6456 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 6457 | |
| 6458 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6459 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 6460 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6461 | /* Adjust pointers for header fields of outgoing records to |
| 6462 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 6463 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6464 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6465 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6466 | ssl->in_epoch = 1; |
| 6467 | #endif |
| 6468 | |
| 6469 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 6470 | * 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] | 6471 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 6472 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6473 | if( ssl->handshake != NULL ) |
| 6474 | { |
| 6475 | mbedtls_ssl_handshake_free( ssl ); |
| 6476 | mbedtls_free( ssl->handshake ); |
| 6477 | ssl->handshake = NULL; |
| 6478 | } |
| 6479 | |
| 6480 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6481 | * Done - should have consumed entire buffer |
| 6482 | */ |
| 6483 | if( p != end ) |
| 6484 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6485 | |
| 6486 | return( 0 ); |
| 6487 | } |
| 6488 | |
| 6489 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6490 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6491 | */ |
| 6492 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 6493 | const unsigned char *buf, |
| 6494 | size_t len ) |
| 6495 | { |
| 6496 | int ret = ssl_context_load( context, buf, len ); |
| 6497 | |
| 6498 | if( ret != 0 ) |
| 6499 | mbedtls_ssl_free( context ); |
| 6500 | |
| 6501 | return( ret ); |
| 6502 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 6503 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6504 | |
| 6505 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6506 | * Free an SSL context |
| 6507 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6508 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6509 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6510 | if( ssl == NULL ) |
| 6511 | return; |
| 6512 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6513 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6514 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6515 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6516 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6517 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6518 | size_t out_buf_len = ssl->out_buf_len; |
| 6519 | #else |
| 6520 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 6521 | #endif |
| 6522 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6523 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6524 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6525 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6526 | } |
| 6527 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6528 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6529 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6530 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6531 | size_t in_buf_len = ssl->in_buf_len; |
| 6532 | #else |
| 6533 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 6534 | #endif |
| 6535 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6536 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6537 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6538 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6539 | } |
| 6540 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6541 | if( ssl->transform ) |
| 6542 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6543 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6544 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6545 | } |
| 6546 | |
| 6547 | if( ssl->handshake ) |
| 6548 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6549 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6550 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 6551 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6553 | mbedtls_free( ssl->handshake ); |
| 6554 | mbedtls_free( ssl->transform_negotiate ); |
| 6555 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6558 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 6559 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 6560 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6561 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 6562 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6563 | if( ssl->session ) |
| 6564 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6565 | mbedtls_ssl_session_free( ssl->session ); |
| 6566 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6567 | } |
| 6568 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 6569 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 6570 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6571 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6572 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6573 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6574 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 6575 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6576 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6577 | #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] | 6578 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 6579 | #endif |
| 6580 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6581 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 6582 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 6583 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6584 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6585 | } |
| 6586 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6587 | /* |
| 6588 | * Initialze mbedtls_ssl_config |
| 6589 | */ |
| 6590 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 6591 | { |
| 6592 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 6593 | } |
| 6594 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6595 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6596 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6597 | /* 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] | 6598 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, |
| 6599 | * for no fundamental reason. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6600 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
| 6601 | * about this list. */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6602 | static int ssl_preset_default_hashes[] = { |
| 6603 | #if defined(MBEDTLS_SHA512_C) |
| 6604 | MBEDTLS_MD_SHA512, |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6605 | #endif |
| 6606 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6607 | MBEDTLS_MD_SHA384, |
| 6608 | #endif |
| 6609 | #if defined(MBEDTLS_SHA256_C) |
| 6610 | MBEDTLS_MD_SHA256, |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6611 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6612 | MBEDTLS_MD_NONE |
| 6613 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6614 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 6615 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6616 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6617 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 6618 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6619 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6620 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6621 | * about this list. |
| 6622 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6623 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6624 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6625 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6626 | #endif |
| 6627 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6628 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6629 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6630 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6631 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6632 | #endif |
| 6633 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6634 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6635 | #endif |
| 6636 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6637 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6638 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6639 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6640 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6641 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6642 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6643 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6644 | #endif |
| 6645 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6646 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6647 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6648 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6649 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6650 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6651 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 6652 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 6653 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 6654 | 0 |
| 6655 | }; |
| 6656 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6657 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6658 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6659 | static int ssl_preset_suiteb_hashes[] = { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6660 | #if defined(MBEDTLS_SHA256_C) |
| 6661 | MBEDTLS_MD_SHA256, |
| 6662 | #endif |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6663 | #if defined(MBEDTLS_SHA384_C) |
| 6664 | MBEDTLS_MD_SHA384, |
| 6665 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6666 | MBEDTLS_MD_NONE |
| 6667 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6668 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6669 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6670 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 6671 | * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 6672 | * rules SHOULD be upheld. |
| 6673 | * - No duplicate entries. |
| 6674 | * - But if there is a good reason, do not change the order of the algorithms. |
| 6675 | * - ssl_tls12_present* is for TLS 1.2 use only. |
| 6676 | * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes. |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6677 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6678 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6679 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 6680 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 6681 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 6682 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 6683 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 6684 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6685 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6686 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 6687 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 6688 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 6689 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 6690 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 6691 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 6692 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \ |
| 6693 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 6694 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
| 6695 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 6696 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6697 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6698 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 6699 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 6700 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
| 6701 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 6702 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 6703 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 6704 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 6705 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6706 | MBEDTLS_TLS1_3_SIG_NONE |
| 6707 | }; |
| 6708 | |
| 6709 | /* NOTICE: see above */ |
| 6710 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6711 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6712 | #if defined(MBEDTLS_SHA512_C) |
| 6713 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 ) |
| 6714 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 6715 | #if defined(MBEDTLS_SHA384_C) |
| 6716 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 6717 | #endif |
| 6718 | #if defined(MBEDTLS_SHA256_C) |
| 6719 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 6720 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6721 | MBEDTLS_TLS1_3_SIG_NONE |
| 6722 | }; |
| 6723 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6724 | /* NOTICE: see above */ |
| 6725 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 6726 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6727 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 6728 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 6729 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 6730 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 6731 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 6732 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 6733 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 6734 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 6735 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 6736 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 6737 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6738 | |
| 6739 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 6740 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 6741 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6742 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 6743 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 6744 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 6745 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 6746 | |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6747 | MBEDTLS_TLS1_3_SIG_NONE |
| 6748 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 6749 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6750 | /* NOTICE: see above */ |
| 6751 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6752 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6753 | #if defined(MBEDTLS_SHA256_C) |
| 6754 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 6755 | #endif |
Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 6756 | #if defined(MBEDTLS_SHA384_C) |
| 6757 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 6758 | #endif |
Xiaofei Bai | 9539501 | 2021-11-25 08:51:30 +0000 | [diff] [blame] | 6759 | MBEDTLS_TLS1_3_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6760 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6761 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6762 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6763 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6764 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6765 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6766 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6767 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6768 | #endif |
| 6769 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6770 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6771 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6772 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6773 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6774 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6775 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6776 | /* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs` |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 6777 | * to make sure there are no duplicated signature algorithm entries. */ |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6778 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6779 | { |
| 6780 | size_t i, j; |
| 6781 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6782 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6783 | for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6784 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6785 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6786 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6787 | if( sig_algs[i] != sig_algs[j] ) |
| 6788 | continue; |
| 6789 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 6790 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 6791 | sig_algs[i], j, i ); |
| 6792 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6793 | } |
| 6794 | } |
| 6795 | return( ret ); |
| 6796 | } |
| 6797 | |
| 6798 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6799 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6800 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 6801 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6802 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6803 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6804 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6805 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6806 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6807 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6808 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6809 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6810 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6811 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6812 | { |
| 6813 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 6814 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6815 | } |
| 6816 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6817 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6818 | { |
| 6819 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 6820 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6821 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6822 | |
| 6823 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6824 | if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) ) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6825 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6826 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6827 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6828 | } |
| 6829 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6830 | if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) ) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6831 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6832 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6833 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6834 | } |
| 6835 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6836 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6837 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 6838 | /* Use the functions here so that they are covered in tests, |
| 6839 | * but otherwise access member directly for efficiency */ |
| 6840 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 6841 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6842 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6843 | /* |
| 6844 | * Things that are common to all presets |
| 6845 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6846 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6847 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6848 | { |
| 6849 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 6850 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 6851 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 6852 | #endif |
| 6853 | } |
| 6854 | #endif |
| 6855 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6856 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6857 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 6858 | #endif |
| 6859 | |
| 6860 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6861 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 6862 | #endif |
| 6863 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6864 | #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] | 6865 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 6866 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 6867 | #endif |
| 6868 | |
| 6869 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6870 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 6871 | #endif |
| 6872 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6873 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6874 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 6875 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6876 | #endif |
| 6877 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6878 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6879 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 6880 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 6881 | #endif |
| 6882 | |
| 6883 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6884 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 6885 | memset( conf->renego_period, 0x00, 2 ); |
| 6886 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6887 | #endif |
| 6888 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6889 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 6890 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6891 | { |
| 6892 | const unsigned char dhm_p[] = |
| 6893 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 6894 | const unsigned char dhm_g[] = |
| 6895 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 6896 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 6897 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 6898 | dhm_p, sizeof( dhm_p ), |
| 6899 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 6900 | { |
| 6901 | return( ret ); |
| 6902 | } |
| 6903 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 6904 | #endif |
| 6905 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6906 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 6907 | /* |
| 6908 | * Allow all TLS 1.3 key exchange modes by default. |
| 6909 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 6910 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6911 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 6912 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6913 | /* |
| 6914 | * Preset-specific defaults |
| 6915 | */ |
| 6916 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6917 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6918 | /* |
| 6919 | * NSA Suite B |
| 6920 | */ |
| 6921 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 6922 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6923 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 6924 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6925 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6926 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6927 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6928 | |
| 6929 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6930 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6931 | #endif |
| 6932 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6933 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6934 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6935 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6936 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6937 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6938 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 6939 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 6940 | else |
| 6941 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6942 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6943 | #endif |
| 6944 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6945 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 6946 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6947 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6948 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 6949 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6950 | |
| 6951 | /* |
| 6952 | * Default |
| 6953 | */ |
| 6954 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 6955 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 6956 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 6957 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 6958 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 6959 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 6960 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 6961 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 6962 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6963 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6964 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6965 | |
| 6966 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6967 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
TRodziewicz | ef73f01 | 2021-05-13 14:53:36 +0200 | [diff] [blame] | 6968 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6969 | #endif |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6970 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6971 | |
| 6972 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6973 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 6974 | #endif |
| 6975 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6976 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6977 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6978 | conf->sig_hashes = ssl_preset_default_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6979 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6980 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6981 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 6982 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 6983 | else |
| 6984 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6985 | conf->sig_algs = ssl_preset_default_sig_algs; |
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 6986 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6987 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6988 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 6989 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6990 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6991 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6992 | |
| 6993 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 6994 | conf->dhm_min_bitlen = 1024; |
| 6995 | #endif |
| 6996 | } |
| 6997 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6998 | return( 0 ); |
| 6999 | } |
| 7000 | |
| 7001 | /* |
| 7002 | * Free mbedtls_ssl_config |
| 7003 | */ |
| 7004 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 7005 | { |
| 7006 | #if defined(MBEDTLS_DHM_C) |
| 7007 | mbedtls_mpi_free( &conf->dhm_P ); |
| 7008 | mbedtls_mpi_free( &conf->dhm_G ); |
| 7009 | #endif |
| 7010 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7011 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7012 | if( conf->psk != NULL ) |
| 7013 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7014 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7015 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 7016 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7017 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 7018 | } |
| 7019 | |
| 7020 | if( conf->psk_identity != NULL ) |
| 7021 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7022 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 7023 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 7024 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7025 | conf->psk_identity_len = 0; |
| 7026 | } |
| 7027 | #endif |
| 7028 | |
| 7029 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7030 | ssl_key_cert_free( conf->key_cert ); |
| 7031 | #endif |
| 7032 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7033 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7034 | } |
| 7035 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 7036 | #if defined(MBEDTLS_PK_C) && \ |
| 7037 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7038 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7039 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7040 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7041 | 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] | 7042 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7043 | #if defined(MBEDTLS_RSA_C) |
| 7044 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 7045 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7046 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7047 | #if defined(MBEDTLS_ECDSA_C) |
| 7048 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 7049 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7050 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7051 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7052 | } |
| 7053 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7054 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 7055 | { |
| 7056 | switch( type ) { |
| 7057 | case MBEDTLS_PK_RSA: |
| 7058 | return( MBEDTLS_SSL_SIG_RSA ); |
| 7059 | case MBEDTLS_PK_ECDSA: |
| 7060 | case MBEDTLS_PK_ECKEY: |
| 7061 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 7062 | default: |
| 7063 | return( MBEDTLS_SSL_SIG_ANON ); |
| 7064 | } |
| 7065 | } |
| 7066 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7067 | 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] | 7068 | { |
| 7069 | switch( sig ) |
| 7070 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7071 | #if defined(MBEDTLS_RSA_C) |
| 7072 | case MBEDTLS_SSL_SIG_RSA: |
| 7073 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7074 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7075 | #if defined(MBEDTLS_ECDSA_C) |
| 7076 | case MBEDTLS_SSL_SIG_ECDSA: |
| 7077 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7078 | #endif |
| 7079 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7080 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7081 | } |
| 7082 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 7083 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7084 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7085 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7086 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7087 | |
| 7088 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 7089 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 7090 | mbedtls_pk_type_t sig_alg ) |
| 7091 | { |
| 7092 | switch( sig_alg ) |
| 7093 | { |
| 7094 | case MBEDTLS_PK_RSA: |
| 7095 | return( set->rsa ); |
| 7096 | case MBEDTLS_PK_ECDSA: |
| 7097 | return( set->ecdsa ); |
| 7098 | default: |
| 7099 | return( MBEDTLS_MD_NONE ); |
| 7100 | } |
| 7101 | } |
| 7102 | |
| 7103 | /* Add a signature-hash-pair to a signature-hash set */ |
| 7104 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 7105 | mbedtls_pk_type_t sig_alg, |
| 7106 | mbedtls_md_type_t md_alg ) |
| 7107 | { |
| 7108 | switch( sig_alg ) |
| 7109 | { |
| 7110 | case MBEDTLS_PK_RSA: |
| 7111 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 7112 | set->rsa = md_alg; |
| 7113 | break; |
| 7114 | |
| 7115 | case MBEDTLS_PK_ECDSA: |
| 7116 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 7117 | set->ecdsa = md_alg; |
| 7118 | break; |
| 7119 | |
| 7120 | default: |
| 7121 | break; |
| 7122 | } |
| 7123 | } |
| 7124 | |
| 7125 | /* Allow exactly one hash algorithm for each signature. */ |
| 7126 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 7127 | mbedtls_md_type_t md_alg ) |
| 7128 | { |
| 7129 | set->rsa = md_alg; |
| 7130 | set->ecdsa = md_alg; |
| 7131 | } |
| 7132 | |
| 7133 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7134 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7135 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 7136 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7137 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 7138 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7139 | 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] | 7140 | { |
| 7141 | switch( hash ) |
| 7142 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7143 | #if defined(MBEDTLS_MD5_C) |
| 7144 | case MBEDTLS_SSL_HASH_MD5: |
| 7145 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7146 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7147 | #if defined(MBEDTLS_SHA1_C) |
| 7148 | case MBEDTLS_SSL_HASH_SHA1: |
| 7149 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7150 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 7151 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7152 | case MBEDTLS_SSL_HASH_SHA224: |
| 7153 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 7154 | #endif |
| 7155 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7156 | case MBEDTLS_SSL_HASH_SHA256: |
| 7157 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7158 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 7159 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7160 | case MBEDTLS_SSL_HASH_SHA384: |
| 7161 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 7162 | #endif |
| 7163 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7164 | case MBEDTLS_SSL_HASH_SHA512: |
| 7165 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7166 | #endif |
| 7167 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7168 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7169 | } |
| 7170 | } |
| 7171 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7172 | /* |
| 7173 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 7174 | */ |
| 7175 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 7176 | { |
| 7177 | switch( md ) |
| 7178 | { |
| 7179 | #if defined(MBEDTLS_MD5_C) |
| 7180 | case MBEDTLS_MD_MD5: |
| 7181 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 7182 | #endif |
| 7183 | #if defined(MBEDTLS_SHA1_C) |
| 7184 | case MBEDTLS_MD_SHA1: |
| 7185 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 7186 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 7187 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7188 | case MBEDTLS_MD_SHA224: |
| 7189 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 7190 | #endif |
| 7191 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7192 | case MBEDTLS_MD_SHA256: |
| 7193 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 7194 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 7195 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7196 | case MBEDTLS_MD_SHA384: |
| 7197 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 7198 | #endif |
| 7199 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7200 | case MBEDTLS_MD_SHA512: |
| 7201 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 7202 | #endif |
| 7203 | default: |
| 7204 | return( MBEDTLS_SSL_HASH_NONE ); |
| 7205 | } |
| 7206 | } |
| 7207 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7208 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7209 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7210 | * 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] | 7211 | * 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] | 7212 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7213 | 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] | 7214 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7215 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7216 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7217 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7218 | return( -1 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7219 | 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] | 7220 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7221 | for( ; *group_list != 0; group_list++ ) |
| 7222 | { |
| 7223 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7224 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7225 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7226 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7227 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7228 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7229 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7231 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7232 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 7233 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7234 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7235 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7236 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7237 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7238 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7239 | const char *ext_oid; |
| 7240 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7242 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7243 | { |
| 7244 | /* Server part of the key exchange */ |
| 7245 | switch( ciphersuite->key_exchange ) |
| 7246 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7247 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 7248 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7249 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7250 | break; |
| 7251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7252 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 7253 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 7254 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 7255 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7256 | break; |
| 7257 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7258 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 7259 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7260 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7261 | break; |
| 7262 | |
| 7263 | /* 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] | 7264 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 7265 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 7266 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 7267 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 7268 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7269 | usage = 0; |
| 7270 | } |
| 7271 | } |
| 7272 | else |
| 7273 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7274 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 7275 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7276 | } |
| 7277 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7278 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7279 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7280 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7281 | ret = -1; |
| 7282 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7284 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7285 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7286 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 7287 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7288 | } |
| 7289 | else |
| 7290 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7291 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 7292 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7293 | } |
| 7294 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7295 | 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] | 7296 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7297 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7298 | ret = -1; |
| 7299 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7300 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7301 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7302 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7303 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 7304 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7305 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 7306 | { |
| 7307 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7308 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 7309 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7310 | |
| 7311 | switch( md ) |
| 7312 | { |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 7313 | #if defined(MBEDTLS_SHA384_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7314 | case MBEDTLS_SSL_HASH_SHA384: |
| 7315 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 7316 | break; |
| 7317 | #endif |
| 7318 | #if defined(MBEDTLS_SHA256_C) |
| 7319 | case MBEDTLS_SSL_HASH_SHA256: |
| 7320 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7321 | break; |
| 7322 | #endif |
| 7323 | default: |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 7324 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7325 | } |
| 7326 | |
| 7327 | return 0; |
| 7328 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7329 | (void) ssl; |
| 7330 | (void) md; |
| 7331 | |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 7332 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7333 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7334 | } |
| 7335 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 7336 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7337 | |
| 7338 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7339 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7340 | unsigned char *hash, size_t *hashlen, |
| 7341 | unsigned char *data, size_t data_len, |
| 7342 | mbedtls_md_type_t md_alg ) |
| 7343 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7344 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7345 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7346 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 7347 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7348 | 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] | 7349 | |
| 7350 | if( ( status = psa_hash_setup( &hash_operation, |
| 7351 | hash_alg ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7352 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7353 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7354 | goto exit; |
| 7355 | } |
| 7356 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7357 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 7358 | 64 ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7359 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7360 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7361 | goto exit; |
| 7362 | } |
| 7363 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7364 | if( ( status = psa_hash_update( &hash_operation, |
| 7365 | data, data_len ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7366 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7367 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7368 | goto exit; |
| 7369 | } |
| 7370 | |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 7371 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7372 | hashlen ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7373 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7374 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7375 | goto exit; |
| 7376 | } |
| 7377 | |
| 7378 | exit: |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7379 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7380 | { |
| 7381 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7382 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7383 | switch( status ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7384 | { |
| 7385 | case PSA_ERROR_NOT_SUPPORTED: |
| 7386 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7387 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7388 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 7389 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 7390 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 7391 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 7392 | default: |
TRodziewicz | b579ccd | 2021-04-13 14:28:28 +0200 | [diff] [blame] | 7393 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7394 | } |
| 7395 | } |
| 7396 | return( 0 ); |
| 7397 | } |
| 7398 | |
| 7399 | #else |
| 7400 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7401 | 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] | 7402 | unsigned char *hash, size_t *hashlen, |
| 7403 | unsigned char *data, size_t data_len, |
| 7404 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7405 | { |
| 7406 | int ret = 0; |
| 7407 | mbedtls_md_context_t ctx; |
| 7408 | 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] | 7409 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7410 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7411 | 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] | 7412 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7413 | mbedtls_md_init( &ctx ); |
| 7414 | |
| 7415 | /* |
| 7416 | * digitally-signed struct { |
| 7417 | * opaque client_random[32]; |
| 7418 | * opaque server_random[32]; |
| 7419 | * ServerDHParams params; |
| 7420 | * }; |
| 7421 | */ |
| 7422 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7423 | { |
| 7424 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7425 | goto exit; |
| 7426 | } |
| 7427 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7428 | { |
| 7429 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7430 | goto exit; |
| 7431 | } |
| 7432 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7433 | { |
| 7434 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7435 | goto exit; |
| 7436 | } |
| 7437 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7438 | { |
| 7439 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7440 | goto exit; |
| 7441 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7442 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7443 | { |
| 7444 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7445 | goto exit; |
| 7446 | } |
| 7447 | |
| 7448 | exit: |
| 7449 | mbedtls_md_free( &ctx ); |
| 7450 | |
| 7451 | if( ret != 0 ) |
| 7452 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7453 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7454 | |
| 7455 | return( ret ); |
| 7456 | } |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7457 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7458 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 7459 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7460 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7461 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7462 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 7463 | const mbedtls_md_type_t md, |
| 7464 | unsigned char *dst, |
| 7465 | size_t dst_len, |
| 7466 | size_t *olen ) |
| 7467 | { |
| 7468 | ((void) ssl); |
| 7469 | ((void) md); |
| 7470 | ((void) dst); |
| 7471 | ((void) dst_len); |
| 7472 | *olen = 0; |
| 7473 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
| 7474 | } |
| 7475 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7476 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7477 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7478 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 7479 | unsigned char *dst, |
| 7480 | size_t dst_len, |
| 7481 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7482 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7483 | int ret; |
| 7484 | mbedtls_sha512_context sha512; |
| 7485 | |
| 7486 | if( dst_len < 48 ) |
| 7487 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7488 | |
| 7489 | mbedtls_sha512_init( &sha512 ); |
| 7490 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 7491 | |
| 7492 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 7493 | { |
| 7494 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 7495 | goto exit; |
| 7496 | } |
| 7497 | |
| 7498 | *olen = 48; |
| 7499 | |
| 7500 | exit: |
| 7501 | |
| 7502 | mbedtls_sha512_free( &sha512 ); |
| 7503 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7504 | } |
| 7505 | #endif /* MBEDTLS_SHA384_C */ |
| 7506 | |
| 7507 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7508 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 7509 | unsigned char *dst, |
| 7510 | size_t dst_len, |
| 7511 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7512 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7513 | int ret; |
| 7514 | mbedtls_sha256_context sha256; |
| 7515 | |
| 7516 | if( dst_len < 32 ) |
| 7517 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7518 | |
| 7519 | mbedtls_sha256_init( &sha256 ); |
| 7520 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 7521 | |
| 7522 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 7523 | { |
| 7524 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 7525 | goto exit; |
| 7526 | } |
| 7527 | |
| 7528 | *olen = 32; |
| 7529 | |
| 7530 | exit: |
| 7531 | |
| 7532 | mbedtls_sha256_free( &sha256 ); |
| 7533 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7534 | } |
| 7535 | #endif /* MBEDTLS_SHA256_C */ |
| 7536 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7537 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 7538 | const mbedtls_md_type_t md, |
| 7539 | unsigned char *dst, |
| 7540 | size_t dst_len, |
| 7541 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7542 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7543 | switch( md ) |
| 7544 | { |
| 7545 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7546 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7547 | case MBEDTLS_MD_SHA384: |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7548 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7549 | #endif /* MBEDTLS_SHA384_C */ |
| 7550 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7551 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7552 | case MBEDTLS_MD_SHA256: |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7553 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7554 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7555 | |
| 7556 | default: |
| 7557 | break; |
| 7558 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7559 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7560 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 7561 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7562 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7563 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 7564 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \ |
| 7565 | defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 7566 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7567 | /* |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7568 | * Function for writing a supported groups (TLS 1.3) or supported elliptic |
| 7569 | * curves (TLS 1.2) extension. |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7570 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7571 | * The "extension_data" field of a supported groups extension contains a |
| 7572 | * "NamedGroupList" value (TLS 1.3 RFC8446): |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7573 | * enum { |
| 7574 | * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019), |
| 7575 | * x25519(0x001D), x448(0x001E), |
| 7576 | * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102), |
| 7577 | * ffdhe6144(0x0103), ffdhe8192(0x0104), |
| 7578 | * ffdhe_private_use(0x01FC..0x01FF), |
| 7579 | * ecdhe_private_use(0xFE00..0xFEFF), |
| 7580 | * (0xFFFF) |
| 7581 | * } NamedGroup; |
| 7582 | * struct { |
| 7583 | * NamedGroup named_group_list<2..2^16-1>; |
| 7584 | * } NamedGroupList; |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7585 | * |
| 7586 | * The "extension_data" field of a supported elliptic curves extension contains |
| 7587 | * a "NamedCurveList" value (TLS 1.2 RFC 8422): |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7588 | * enum { |
| 7589 | * deprecated(1..22), |
| 7590 | * secp256r1 (23), secp384r1 (24), secp521r1 (25), |
| 7591 | * x25519(29), x448(30), |
| 7592 | * reserved (0xFE00..0xFEFF), |
| 7593 | * deprecated(0xFF01..0xFF02), |
| 7594 | * (0xFFFF) |
| 7595 | * } NamedCurve; |
| 7596 | * struct { |
| 7597 | * NamedCurve named_curve_list<2..2^16-1> |
| 7598 | * } NamedCurveList; |
| 7599 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7600 | * The TLS 1.3 supported groups extension was defined to be a compatible |
| 7601 | * generalization of the TLS 1.2 supported elliptic curves extension. They both |
| 7602 | * share the same extension identifier. |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7603 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7604 | * DHE groups are not supported yet. |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7605 | */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7606 | int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, |
| 7607 | unsigned char *buf, |
Jerry Yu | 1753261 | 2021-12-20 22:32:09 +0800 | [diff] [blame] | 7608 | const unsigned char *end, |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7609 | size_t *out_len ) |
| 7610 | { |
| 7611 | unsigned char *p = buf ; |
| 7612 | unsigned char *named_group_list; /* Start of named_group_list */ |
| 7613 | size_t named_group_list_len; /* Length of named_group_list */ |
| 7614 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
| 7615 | |
| 7616 | *out_len = 0; |
Jerry Yu | f46b016 | 2022-01-11 16:28:00 +0800 | [diff] [blame] | 7617 | |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7618 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) ); |
| 7619 | |
| 7620 | /* Check if we have space for header and length fields: |
| 7621 | * - extension_type (2 bytes) |
| 7622 | * - extension_data_length (2 bytes) |
| 7623 | * - named_group_list_length (2 bytes) |
| 7624 | */ |
| 7625 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 7626 | p += 6; |
| 7627 | |
| 7628 | named_group_list = p; |
| 7629 | |
| 7630 | if( group_list == NULL ) |
| 7631 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 7632 | |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7633 | for( ; *group_list != 0; group_list++ ) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7634 | { |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7635 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) ); |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7636 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 7637 | #if defined(MBEDTLS_ECP_C) |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7638 | if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) && |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7639 | mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) || |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7640 | ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) && |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7641 | mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) ) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7642 | { |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7643 | const mbedtls_ecp_curve_info *curve_info; |
| 7644 | curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list ); |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7645 | if( curve_info == NULL ) |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7646 | continue; |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7647 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 7648 | MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 ); |
| 7649 | p += 2; |
| 7650 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )", |
| 7651 | curve_info->name, *group_list ) ); |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7652 | } |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7653 | #endif /* MBEDTLS_ECP_C */ |
| 7654 | /* Add DHE groups here */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7655 | |
| 7656 | } |
| 7657 | |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7658 | /* Length of named_group_list */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7659 | named_group_list_len = p - named_group_list; |
| 7660 | if( named_group_list_len == 0 ) |
| 7661 | { |
| 7662 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) ); |
| 7663 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7664 | } |
| 7665 | |
| 7666 | /* Write extension_type */ |
| 7667 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 ); |
| 7668 | /* Write extension_data_length */ |
| 7669 | MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 ); |
| 7670 | /* Write length of named_group_list */ |
| 7671 | MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 ); |
| 7672 | |
Jerry Yu | 7f029d8 | 2022-01-11 11:08:53 +0800 | [diff] [blame] | 7673 | MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", |
| 7674 | buf + 4, named_group_list_len + 2 ); |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7675 | |
| 7676 | *out_len = p - buf; |
| 7677 | |
| 7678 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 7679 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; |
| 7680 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7681 | |
| 7682 | return( 0 ); |
| 7683 | } |
| 7684 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 7685 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED || |
| 7686 | MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 7687 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7688 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7689 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7690 | /* |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7691 | * Function for writing a signature algorithm extension. |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7692 | * |
Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 7693 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7694 | * value (TLS 1.3 RFC8446): |
| 7695 | * enum { |
| 7696 | * .... |
| 7697 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 7698 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 7699 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 7700 | * .... |
| 7701 | * } SignatureScheme; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7702 | * |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7703 | * struct { |
| 7704 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 7705 | * } SignatureSchemeList; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7706 | * |
Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 7707 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 7708 | * value (TLS 1.2 RFC5246): |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7709 | * enum { |
| 7710 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 7711 | * sha512(6), (255) |
| 7712 | * } HashAlgorithm; |
| 7713 | * |
| 7714 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 7715 | * SignatureAlgorithm; |
| 7716 | * |
| 7717 | * struct { |
| 7718 | * HashAlgorithm hash; |
| 7719 | * SignatureAlgorithm signature; |
| 7720 | * } SignatureAndHashAlgorithm; |
| 7721 | * |
| 7722 | * SignatureAndHashAlgorithm |
| 7723 | * supported_signature_algorithms<2..2^16-2>; |
| 7724 | * |
| 7725 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 7726 | * generalization of the TLS 1.2 signature algorithm extension. |
| 7727 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 7728 | * `SignatureScheme` field of TLS 1.3 |
| 7729 | * |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7730 | */ |
| 7731 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 7732 | const unsigned char *end, size_t *out_len ) |
| 7733 | { |
| 7734 | unsigned char *p = buf; |
| 7735 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 7736 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 7737 | |
| 7738 | *out_len = 0; |
| 7739 | |
| 7740 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 7741 | |
| 7742 | /* Check if we have space for header and length field: |
| 7743 | * - extension_type (2 bytes) |
| 7744 | * - extension_data_length (2 bytes) |
| 7745 | * - supported_signature_algorithms_length (2 bytes) |
| 7746 | */ |
| 7747 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 7748 | p += 6; |
| 7749 | |
| 7750 | /* |
| 7751 | * Write supported_signature_algorithms |
| 7752 | */ |
| 7753 | supported_sig_alg = p; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 7754 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 7755 | if( sig_alg == NULL ) |
| 7756 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 7757 | |
| 7758 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7759 | { |
Jerry Yu | 1bab301 | 2022-01-19 17:43:22 +0800 | [diff] [blame] | 7760 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 7761 | continue; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7762 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 7763 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 7764 | p += 2; |
| 7765 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) ); |
| 7766 | } |
| 7767 | |
| 7768 | /* Length of supported_signature_algorithms */ |
| 7769 | supported_sig_alg_len = p - supported_sig_alg; |
| 7770 | if( supported_sig_alg_len == 0 ) |
| 7771 | { |
| 7772 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 7773 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7774 | } |
| 7775 | |
| 7776 | /* Write extension_type */ |
| 7777 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
| 7778 | /* Write extension_data_length */ |
| 7779 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
| 7780 | /* Write length of supported_signature_algorithms */ |
| 7781 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 7782 | |
| 7783 | /* Output the total length of signature algorithms extension. */ |
| 7784 | *out_len = p - buf; |
| 7785 | |
| 7786 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 7787 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 7788 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7789 | return( 0 ); |
| 7790 | } |
| 7791 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7792 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7793 | #endif /* MBEDTLS_SSL_TLS_C */ |