| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
| Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2 | *  TLS shared functions | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ | 
|  | 19 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | *  http://www.ietf.org/rfc/rfc2246.txt | 
|  | 21 | *  http://www.ietf.org/rfc/rfc4346.txt | 
|  | 22 | */ | 
|  | 23 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_TLS_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 |  | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 28 | #include <assert.h> | 
|  | 29 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #if defined(MBEDTLS_PLATFORM_C) | 
|  | 31 | #include "mbedtls/platform.h" | 
|  | 32 | #else | 
|  | 33 | #include <stdlib.h> | 
| Jerry Yu | 6ade743 | 2022-01-25 10:39:33 +0800 | [diff] [blame] | 34 | #include <stdio.h> | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | #define mbedtls_calloc    calloc | 
|  | 36 | #define mbedtls_free      free | 
| Jerry Yu | 6ade743 | 2022-01-25 10:39:33 +0800 | [diff] [blame] | 37 | #define mbedtls_printf    printf | 
|  | 38 | #endif /* !MBEDTLS_PLATFORM_C */ | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 39 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/ssl.h" | 
| Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 41 | #include "ssl_misc.h" | 
| Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 42 | #include "mbedtls/debug.h" | 
|  | 43 | #include "mbedtls/error.h" | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 44 | #include "mbedtls/platform_util.h" | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 45 | #include "mbedtls/version.h" | 
| Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 46 | #include "mbedtls/constant_time.h" | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 47 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 48 | #include <string.h> | 
|  | 49 |  | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 50 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 51 | #include "mbedtls/psa_util.h" | 
|  | 52 | #include "psa/crypto.h" | 
|  | 53 | #endif | 
|  | 54 |  | 
| Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 55 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 56 | #include "mbedtls/oid.h" | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 57 | #endif | 
|  | 58 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 60 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 61 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 62 | /* Top-level Connection ID API */ | 
|  | 63 |  | 
| Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 64 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, | 
|  | 65 | size_t len, | 
|  | 66 | int ignore_other_cid ) | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 67 | { | 
|  | 68 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) | 
|  | 69 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 70 |  | 
| Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 71 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && | 
|  | 72 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) | 
|  | 73 | { | 
|  | 74 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | conf->ignore_unexpected_cid = ignore_other_cid; | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 78 | conf->cid_len = len; | 
|  | 79 | return( 0 ); | 
|  | 80 | } | 
|  | 81 |  | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 82 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, | 
|  | 83 | int enable, | 
|  | 84 | unsigned char const *own_cid, | 
|  | 85 | size_t own_cid_len ) | 
|  | 86 | { | 
| Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 87 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 88 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 89 |  | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 90 | ssl->negotiate_cid = enable; | 
|  | 91 | if( enable == MBEDTLS_SSL_CID_DISABLED ) | 
|  | 92 | { | 
|  | 93 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); | 
|  | 94 | return( 0 ); | 
|  | 95 | } | 
|  | 96 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 97 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 98 |  | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 99 | if( own_cid_len != ssl->conf->cid_len ) | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 100 | { | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 101 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", | 
|  | 102 | (unsigned) own_cid_len, | 
|  | 103 | (unsigned) ssl->conf->cid_len ) ); | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 104 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | memcpy( ssl->own_cid, own_cid, own_cid_len ); | 
| Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 108 | /* Truncation is not an issue here because | 
|  | 109 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ | 
|  | 110 | ssl->own_cid_len = (uint8_t) own_cid_len; | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 111 |  | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 112 | return( 0 ); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, | 
|  | 116 | int *enabled, | 
|  | 117 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], | 
|  | 118 | size_t *peer_cid_len ) | 
|  | 119 | { | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 120 | *enabled = MBEDTLS_SSL_CID_DISABLED; | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 121 |  | 
| Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 122 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | 
|  | 123 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 124 | { | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 125 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 126 | } | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 127 |  | 
| Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 128 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions | 
|  | 129 | * were used, but client and server requested the empty CID. | 
|  | 130 | * This is indistinguishable from not using the CID extension | 
|  | 131 | * in the first place. */ | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 132 | if( ssl->transform_in->in_cid_len  == 0 && | 
|  | 133 | ssl->transform_in->out_cid_len == 0 ) | 
|  | 134 | { | 
|  | 135 | return( 0 ); | 
|  | 136 | } | 
|  | 137 |  | 
| Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 138 | if( peer_cid_len != NULL ) | 
|  | 139 | { | 
|  | 140 | *peer_cid_len = ssl->transform_in->out_cid_len; | 
|  | 141 | if( peer_cid != NULL ) | 
|  | 142 | { | 
|  | 143 | memcpy( peer_cid, ssl->transform_in->out_cid, | 
|  | 144 | ssl->transform_in->out_cid_len ); | 
|  | 145 | } | 
|  | 146 | } | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 147 |  | 
|  | 148 | *enabled = MBEDTLS_SSL_CID_ENABLED; | 
|  | 149 |  | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 150 | return( 0 ); | 
|  | 151 | } | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 152 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 153 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 155 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 157 | /* | 
|  | 158 | * Convert max_fragment_length codes to length. | 
|  | 159 | * RFC 6066 says: | 
|  | 160 | *    enum{ | 
|  | 161 | *        2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) | 
|  | 162 | *    } MaxFragmentLength; | 
|  | 163 | * and we add 0 -> extension unused | 
|  | 164 | */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 165 | static unsigned int ssl_mfl_code_to_length( int mfl ) | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 166 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 167 | switch( mfl ) | 
|  | 168 | { | 
|  | 169 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: | 
|  | 170 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); | 
|  | 171 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: | 
|  | 172 | return 512; | 
|  | 173 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: | 
|  | 174 | return 1024; | 
|  | 175 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: | 
|  | 176 | return 2048; | 
|  | 177 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: | 
|  | 178 | return 4096; | 
|  | 179 | default: | 
|  | 180 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); | 
|  | 181 | } | 
|  | 182 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 184 |  | 
| Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 185 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, | 
|  | 186 | const mbedtls_ssl_session *src ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 187 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | mbedtls_ssl_session_free( dst ); | 
|  | 189 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 190 |  | 
| 吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 191 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 192 | dst->ticket = NULL; | 
|  | 193 | #endif | 
|  | 194 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 196 |  | 
|  | 197 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 198 | if( src->peer_cert != NULL ) | 
|  | 199 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 200 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 201 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 202 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 203 | if( dst->peer_cert == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 204 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 205 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | mbedtls_x509_crt_init( dst->peer_cert ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 207 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p, | 
| Manuel Pégourié-Gonnard | 4d2a8eb | 2014-06-13 20:33:27 +0200 | [diff] [blame] | 209 | src->peer_cert->raw.len ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 210 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | mbedtls_free( dst->peer_cert ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 212 | dst->peer_cert = NULL; | 
|  | 213 | return( ret ); | 
|  | 214 | } | 
|  | 215 | } | 
| Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 216 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 217 | if( src->peer_cert_digest != NULL ) | 
|  | 218 | { | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 219 | dst->peer_cert_digest = | 
| Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 220 | mbedtls_calloc( 1, src->peer_cert_digest_len ); | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 221 | if( dst->peer_cert_digest == NULL ) | 
|  | 222 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 223 |  | 
|  | 224 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, | 
|  | 225 | src->peer_cert_digest_len ); | 
|  | 226 | dst->peer_cert_digest_type = src->peer_cert_digest_type; | 
| Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 227 | dst->peer_cert_digest_len = src->peer_cert_digest_len; | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 228 | } | 
|  | 229 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 230 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 232 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 233 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 234 | if( src->ticket != NULL ) | 
|  | 235 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 236 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 237 | if( dst->ticket == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 238 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 239 |  | 
|  | 240 | memcpy( dst->ticket, src->ticket, src->ticket_len ); | 
|  | 241 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 242 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 243 |  | 
|  | 244 | return( 0 ); | 
|  | 245 | } | 
|  | 246 |  | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 247 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 248 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) | 
|  | 249 | { | 
|  | 250 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); | 
|  | 251 | if( resized_buffer == NULL ) | 
|  | 252 | return -1; | 
|  | 253 |  | 
|  | 254 | /* We want to copy len_new bytes when downsizing the buffer, and | 
|  | 255 | * len_old bytes when upsizing, so we choose the smaller of two sizes, | 
|  | 256 | * to fit one buffer into another. Size checks, ensuring that no data is | 
|  | 257 | * lost, are done outside of this function. */ | 
|  | 258 | memcpy( resized_buffer, *buffer, | 
|  | 259 | ( len_new < *len_old ) ? len_new : *len_old ); | 
|  | 260 | mbedtls_platform_zeroize( *buffer, *len_old ); | 
|  | 261 | mbedtls_free( *buffer ); | 
|  | 262 |  | 
|  | 263 | *buffer = resized_buffer; | 
|  | 264 | *len_old = len_new; | 
|  | 265 |  | 
|  | 266 | return 0; | 
|  | 267 | } | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 268 |  | 
|  | 269 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, | 
| Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 270 | size_t in_buf_new_len, | 
|  | 271 | size_t out_buf_new_len ) | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 272 | { | 
|  | 273 | int modified = 0; | 
|  | 274 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; | 
|  | 275 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; | 
|  | 276 | if( ssl->in_buf != NULL ) | 
|  | 277 | { | 
|  | 278 | written_in = ssl->in_msg - ssl->in_buf; | 
|  | 279 | iv_offset_in = ssl->in_iv - ssl->in_buf; | 
|  | 280 | len_offset_in = ssl->in_len - ssl->in_buf; | 
|  | 281 | if( downsizing ? | 
|  | 282 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : | 
|  | 283 | ssl->in_buf_len < in_buf_new_len ) | 
|  | 284 | { | 
|  | 285 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) | 
|  | 286 | { | 
|  | 287 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); | 
|  | 288 | } | 
|  | 289 | else | 
|  | 290 | { | 
| Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 291 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, | 
|  | 292 | in_buf_new_len ) ); | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 293 | modified = 1; | 
|  | 294 | } | 
|  | 295 | } | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | if( ssl->out_buf != NULL ) | 
|  | 299 | { | 
|  | 300 | written_out = ssl->out_msg - ssl->out_buf; | 
|  | 301 | iv_offset_out = ssl->out_iv - ssl->out_buf; | 
|  | 302 | len_offset_out = ssl->out_len - ssl->out_buf; | 
|  | 303 | if( downsizing ? | 
|  | 304 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : | 
|  | 305 | ssl->out_buf_len < out_buf_new_len ) | 
|  | 306 | { | 
|  | 307 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) | 
|  | 308 | { | 
|  | 309 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); | 
|  | 310 | } | 
|  | 311 | else | 
|  | 312 | { | 
| Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 313 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, | 
|  | 314 | out_buf_new_len ) ); | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 315 | modified = 1; | 
|  | 316 | } | 
|  | 317 | } | 
|  | 318 | } | 
|  | 319 | if( modified ) | 
|  | 320 | { | 
|  | 321 | /* Update pointers here to avoid doing it twice. */ | 
|  | 322 | mbedtls_ssl_reset_in_out_pointers( ssl ); | 
|  | 323 | /* Fields below might not be properly updated with record | 
|  | 324 | * splitting or with CID, so they are manually updated here. */ | 
|  | 325 | ssl->out_msg = ssl->out_buf + written_out; | 
|  | 326 | ssl->out_len = ssl->out_buf + len_offset_out; | 
|  | 327 | ssl->out_iv = ssl->out_buf + iv_offset_out; | 
|  | 328 |  | 
|  | 329 | ssl->in_msg = ssl->in_buf + written_in; | 
|  | 330 | ssl->in_len = ssl->in_buf + len_offset_in; | 
|  | 331 | ssl->in_iv = ssl->in_buf + iv_offset_in; | 
|  | 332 | } | 
|  | 333 | } | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 334 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ | 
|  | 335 |  | 
| Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 336 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 337 |  | 
|  | 338 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
|  | 339 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, | 
|  | 340 | const char *label, | 
|  | 341 | const unsigned char *random, size_t rlen, | 
|  | 342 | unsigned char *dstbuf, size_t dlen ); | 
|  | 343 |  | 
|  | 344 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ); | 
|  | 345 |  | 
|  | 346 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
|  | 347 |  | 
|  | 348 | /* Type for the TLS PRF */ | 
|  | 349 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, | 
|  | 350 | const unsigned char *, size_t, | 
|  | 351 | unsigned char *, size_t); | 
|  | 352 |  | 
|  | 353 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, | 
|  | 354 | int ciphersuite, | 
|  | 355 | const unsigned char master[48], | 
|  | 356 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ | 
|  | 357 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 358 | int encrypt_then_mac, | 
|  | 359 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && | 
|  | 360 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 361 | ssl_tls_prf_t tls_prf, | 
|  | 362 | const unsigned char randbytes[64], | 
|  | 363 | int minor_ver, | 
|  | 364 | unsigned endpoint, | 
|  | 365 | const mbedtls_ssl_context *ssl ); | 
|  | 366 |  | 
|  | 367 | #if defined(MBEDTLS_SHA256_C) | 
|  | 368 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, | 
|  | 369 | const char *label, | 
|  | 370 | const unsigned char *random, size_t rlen, | 
|  | 371 | unsigned char *dstbuf, size_t dlen ); | 
|  | 372 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); | 
|  | 373 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); | 
|  | 374 |  | 
|  | 375 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 376 |  | 
|  | 377 | #if defined(MBEDTLS_SHA384_C) | 
|  | 378 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, | 
|  | 379 | const char *label, | 
|  | 380 | const unsigned char *random, size_t rlen, | 
|  | 381 | unsigned char *dstbuf, size_t dlen ); | 
|  | 382 |  | 
|  | 383 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); | 
|  | 384 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); | 
|  | 385 | #endif /* MBEDTLS_SHA384_C */ | 
|  | 386 |  | 
|  | 387 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, | 
|  | 388 | unsigned char *buf, | 
|  | 389 | size_t buf_len ); | 
|  | 390 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, | 
|  | 391 | const unsigned char *buf, | 
|  | 392 | size_t len ); | 
|  | 393 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 394 |  | 
| Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 395 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); | 
|  | 396 |  | 
| Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 397 | #if defined(MBEDTLS_SHA256_C) | 
|  | 398 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); | 
| Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 399 | #endif /* MBEDTLS_SHA256_C */ | 
| Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 400 |  | 
|  | 401 | #if defined(MBEDTLS_SHA384_C) | 
|  | 402 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); | 
| Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 403 | #endif /* MBEDTLS_SHA384_C */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 404 |  | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 405 | int  mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, | 
|  | 406 | const unsigned char *secret, size_t slen, | 
|  | 407 | const char *label, | 
|  | 408 | const unsigned char *random, size_t rlen, | 
|  | 409 | unsigned char *dstbuf, size_t dlen ) | 
|  | 410 | { | 
|  | 411 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; | 
|  | 412 |  | 
|  | 413 | switch( prf ) | 
|  | 414 | { | 
| Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 415 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 416 | #if defined(MBEDTLS_SHA384_C) | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 417 | case MBEDTLS_SSL_TLS_PRF_SHA384: | 
|  | 418 | tls_prf = tls_prf_sha384; | 
|  | 419 | break; | 
| Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 420 | #endif /* MBEDTLS_SHA384_C */ | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 421 | #if defined(MBEDTLS_SHA256_C) | 
|  | 422 | case MBEDTLS_SSL_TLS_PRF_SHA256: | 
|  | 423 | tls_prf = tls_prf_sha256; | 
|  | 424 | break; | 
| Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 425 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 426 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 427 | default: | 
|  | 428 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 429 | } | 
|  | 430 |  | 
|  | 431 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); | 
|  | 432 | } | 
|  | 433 |  | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 434 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 435 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) | 
|  | 436 | { | 
|  | 437 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 438 | if( session->peer_cert != NULL ) | 
|  | 439 | { | 
|  | 440 | mbedtls_x509_crt_free( session->peer_cert ); | 
|  | 441 | mbedtls_free( session->peer_cert ); | 
|  | 442 | session->peer_cert = NULL; | 
|  | 443 | } | 
|  | 444 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 445 | if( session->peer_cert_digest != NULL ) | 
|  | 446 | { | 
|  | 447 | /* Zeroization is not necessary. */ | 
|  | 448 | mbedtls_free( session->peer_cert_digest ); | 
|  | 449 | session->peer_cert_digest      = NULL; | 
|  | 450 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; | 
|  | 451 | session->peer_cert_digest_len  = 0; | 
|  | 452 | } | 
|  | 453 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 454 | } | 
|  | 455 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 456 |  | 
|  | 457 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, | 
|  | 458 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) | 
|  | 459 | { | 
|  | 460 | ((void) ciphersuite_info); | 
|  | 461 |  | 
|  | 462 | #if defined(MBEDTLS_SHA384_C) | 
|  | 463 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) | 
|  | 464 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; | 
|  | 465 | else | 
|  | 466 | #endif | 
|  | 467 | #if defined(MBEDTLS_SHA256_C) | 
|  | 468 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) | 
|  | 469 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; | 
|  | 470 | else | 
|  | 471 | #endif | 
|  | 472 | { | 
|  | 473 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 474 | return; | 
|  | 475 | } | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) | 
|  | 479 | { | 
|  | 480 | ((void) ssl); | 
|  | 481 | #if defined(MBEDTLS_SHA256_C) | 
|  | 482 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 483 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); | 
|  | 484 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); | 
|  | 485 | #else | 
|  | 486 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); | 
|  | 487 | #endif | 
|  | 488 | #endif | 
|  | 489 | #if defined(MBEDTLS_SHA384_C) | 
|  | 490 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 491 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); | 
|  | 492 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); | 
|  | 493 | #else | 
|  | 494 | mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 ); | 
|  | 495 | #endif | 
|  | 496 | #endif | 
|  | 497 | } | 
|  | 498 |  | 
|  | 499 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, | 
|  | 500 | const unsigned char *buf, size_t len ) | 
|  | 501 | { | 
|  | 502 | #if defined(MBEDTLS_SHA256_C) | 
|  | 503 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 504 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); | 
|  | 505 | #else | 
|  | 506 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); | 
|  | 507 | #endif | 
|  | 508 | #endif | 
|  | 509 | #if defined(MBEDTLS_SHA384_C) | 
|  | 510 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 511 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); | 
|  | 512 | #else | 
|  | 513 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); | 
|  | 514 | #endif | 
|  | 515 | #endif | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | #if defined(MBEDTLS_SHA256_C) | 
|  | 519 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, | 
|  | 520 | const unsigned char *buf, size_t len ) | 
|  | 521 | { | 
|  | 522 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 523 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); | 
|  | 524 | #else | 
|  | 525 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); | 
|  | 526 | #endif | 
|  | 527 | } | 
|  | 528 | #endif | 
|  | 529 |  | 
|  | 530 | #if defined(MBEDTLS_SHA384_C) | 
|  | 531 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, | 
|  | 532 | const unsigned char *buf, size_t len ) | 
|  | 533 | { | 
|  | 534 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 535 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); | 
|  | 536 | #else | 
|  | 537 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); | 
|  | 538 | #endif | 
|  | 539 | } | 
|  | 540 | #endif | 
|  | 541 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 543 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 545 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | #if defined(MBEDTLS_SHA256_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 547 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 548 | handshake->fin_sha256_psa = psa_hash_operation_init(); | 
|  | 549 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); | 
|  | 550 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 551 | mbedtls_sha256_init(   &handshake->fin_sha256    ); | 
| TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 552 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 553 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 554 | #endif | 
| Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 555 | #if defined(MBEDTLS_SHA384_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 556 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 557 | handshake->fin_sha384_psa = psa_hash_operation_init(); | 
|  | 558 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 559 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 560 | mbedtls_sha512_init(   &handshake->fin_sha512    ); | 
| TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 561 | mbedtls_sha512_starts( &handshake->fin_sha512, 1 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 562 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 563 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 564 |  | 
|  | 565 | handshake->update_checksum = ssl_update_checksum_start; | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 566 |  | 
|  | 567 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 568 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 569 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); | 
|  | 570 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 571 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 572 | #if defined(MBEDTLS_DHM_C) | 
|  | 573 | mbedtls_dhm_init( &handshake->dhm_ctx ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 574 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | #if defined(MBEDTLS_ECDH_C) | 
|  | 576 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 577 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 578 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 579 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 580 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 581 | handshake->ecjpake_cache = NULL; | 
|  | 582 | handshake->ecjpake_cache_len = 0; | 
|  | 583 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 584 | #endif | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 585 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 586 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 587 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 588 | #endif | 
|  | 589 |  | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 590 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 591 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; | 
|  | 592 | #endif | 
| Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 593 |  | 
|  | 594 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 595 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 596 | mbedtls_pk_init( &handshake->peer_pubkey ); | 
|  | 597 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 598 | } | 
|  | 599 |  | 
| Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 600 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 601 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 603 |  | 
| Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 604 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 605 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 606 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; | 
| Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 607 | #else | 
|  | 608 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); | 
|  | 609 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); | 
| Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 610 | #endif | 
|  | 611 |  | 
| Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 612 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 613 | mbedtls_md_init( &transform->md_ctx_enc ); | 
|  | 614 | mbedtls_md_init( &transform->md_ctx_dec ); | 
| Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 615 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 616 | } | 
|  | 617 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 619 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 620 | memset( session, 0, sizeof(mbedtls_ssl_session) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 621 | } | 
|  | 622 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 624 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 625 | /* Clear old handshake information if present */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 626 | if( ssl->transform_negotiate ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 627 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 628 | if( ssl->session_negotiate ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 629 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 630 | if( ssl->handshake ) | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 631 | mbedtls_ssl_handshake_free( ssl ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 632 |  | 
|  | 633 | /* | 
|  | 634 | * Either the pointers are now NULL or cleared properly and can be freed. | 
|  | 635 | * Now allocate missing structures. | 
|  | 636 | */ | 
|  | 637 | if( ssl->transform_negotiate == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 638 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 639 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 640 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 641 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 642 | if( ssl->session_negotiate == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 643 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 644 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 645 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 646 |  | 
| Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 647 | if( ssl->handshake == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 648 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 649 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 650 | } | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 651 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 652 | /* If the buffers are too small - reallocate */ | 
| Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 653 |  | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 654 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, | 
|  | 655 | MBEDTLS_SSL_OUT_BUFFER_LEN ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 656 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 657 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 658 | /* All pointers should exist and can be directly freed without issue */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 659 | if( ssl->handshake == NULL || | 
|  | 660 | ssl->transform_negotiate == NULL || | 
|  | 661 | ssl->session_negotiate == NULL ) | 
|  | 662 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 663 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 664 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | mbedtls_free( ssl->handshake ); | 
|  | 666 | mbedtls_free( ssl->transform_negotiate ); | 
|  | 667 | mbedtls_free( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 668 |  | 
|  | 669 | ssl->handshake = NULL; | 
|  | 670 | ssl->transform_negotiate = NULL; | 
|  | 671 | ssl->session_negotiate = NULL; | 
|  | 672 |  | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 673 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 674 | } | 
|  | 675 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 676 | /* Initialize structures */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | mbedtls_ssl_session_init( ssl->session_negotiate ); | 
| Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 678 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); | 
| Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 679 | ssl_handshake_params_init( ssl->handshake ); | 
|  | 680 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 681 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 682 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 683 | { | 
|  | 684 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 685 |  | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 686 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 687 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; | 
|  | 688 | else | 
|  | 689 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 690 |  | 
| Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 691 | mbedtls_ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 692 | } | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 693 | #endif | 
|  | 694 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 695 | /* | 
|  | 696 | * curve_list is translated to IANA TLS group identifiers here because | 
|  | 697 | * mbedtls_ssl_conf_curves returns void and so can't return | 
|  | 698 | * any error codes. | 
|  | 699 | */ | 
|  | 700 | #if defined(MBEDTLS_ECP_C) | 
|  | 701 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 702 | /* Heap allocate and translate curve_list from internal to IANA group ids */ | 
|  | 703 | if ( ssl->conf->curve_list != NULL ) | 
|  | 704 | { | 
|  | 705 | size_t length; | 
|  | 706 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; | 
|  | 707 |  | 
|  | 708 | for( length = 0;  ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && | 
|  | 709 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} | 
|  | 710 |  | 
|  | 711 | /* Leave room for zero termination */ | 
|  | 712 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); | 
|  | 713 | if ( group_list == NULL ) | 
|  | 714 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 715 |  | 
|  | 716 | for( size_t i = 0; i < length; i++ ) | 
|  | 717 | { | 
|  | 718 | const mbedtls_ecp_curve_info *info = | 
|  | 719 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); | 
|  | 720 | if ( info == NULL ) | 
|  | 721 | { | 
|  | 722 | mbedtls_free( group_list ); | 
|  | 723 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 724 | } | 
|  | 725 | group_list[i] = info->tls_id; | 
|  | 726 | } | 
|  | 727 |  | 
|  | 728 | group_list[length] = 0; | 
|  | 729 |  | 
|  | 730 | ssl->handshake->group_list = group_list; | 
|  | 731 | ssl->handshake->group_list_heap_allocated = 1; | 
|  | 732 | } | 
|  | 733 | else | 
|  | 734 | { | 
|  | 735 | ssl->handshake->group_list = ssl->conf->group_list; | 
|  | 736 | ssl->handshake->group_list_heap_allocated = 0; | 
|  | 737 | } | 
|  | 738 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 739 | #endif /* MBEDTLS_ECP_C */ | 
|  | 740 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 741 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 742 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 743 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 744 | /* Heap allocate and translate sig_hashes from internal hash identifiers to | 
|  | 745 | signature algorithms IANA identifiers.  */ | 
|  | 746 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 747 | ssl->conf->sig_hashes != NULL ) | 
|  | 748 | { | 
|  | 749 | const int *md; | 
|  | 750 | const int *sig_hashes = ssl->conf->sig_hashes; | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 751 | size_t sig_algs_len = 0; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 752 | uint16_t *p; | 
|  | 753 |  | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 754 | #if defined(static_assert) | 
|  | 755 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN | 
|  | 756 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), | 
|  | 757 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); | 
| Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 758 | #endif | 
|  | 759 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 760 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) | 
|  | 761 | { | 
|  | 762 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) | 
|  | 763 | continue; | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 764 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 765 | sig_algs_len += sizeof( uint16_t ); | 
|  | 766 | #endif | 
| Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 767 |  | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 768 | #if defined(MBEDTLS_RSA_C) | 
|  | 769 | sig_algs_len += sizeof( uint16_t ); | 
|  | 770 | #endif | 
|  | 771 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) | 
|  | 772 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 773 | } | 
|  | 774 |  | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 775 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 776 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 777 |  | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 778 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + | 
|  | 779 | sizeof( uint16_t )); | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 780 | if( ssl->handshake->sig_algs == NULL ) | 
|  | 781 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 782 |  | 
|  | 783 | p = (uint16_t *)ssl->handshake->sig_algs; | 
|  | 784 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) | 
|  | 785 | { | 
|  | 786 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); | 
|  | 787 | if( hash == MBEDTLS_SSL_HASH_NONE ) | 
|  | 788 | continue; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 789 | #if defined(MBEDTLS_ECDSA_C) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 790 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); | 
|  | 791 | p++; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 792 | #endif | 
|  | 793 | #if defined(MBEDTLS_RSA_C) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 794 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); | 
|  | 795 | p++; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 796 | #endif | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 797 | } | 
|  | 798 | *p = MBEDTLS_TLS1_3_SIG_NONE; | 
|  | 799 | ssl->handshake->sig_algs_heap_allocated = 1; | 
|  | 800 | } | 
|  | 801 | else | 
| Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 802 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 803 | { | 
|  | 804 | ssl->handshake->sig_algs = ssl->conf->sig_algs; | 
|  | 805 | ssl->handshake->sig_algs_heap_allocated = 0; | 
|  | 806 | } | 
|  | 807 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 808 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 809 | return( 0 ); | 
|  | 810 | } | 
|  | 811 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 812 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 813 | /* Dummy cookie callbacks for defaults */ | 
|  | 814 | static int ssl_cookie_write_dummy( void *ctx, | 
|  | 815 | unsigned char **p, unsigned char *end, | 
|  | 816 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 817 | { | 
|  | 818 | ((void) ctx); | 
|  | 819 | ((void) p); | 
|  | 820 | ((void) end); | 
|  | 821 | ((void) cli_id); | 
|  | 822 | ((void) cli_id_len); | 
|  | 823 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 824 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 825 | } | 
|  | 826 |  | 
|  | 827 | static int ssl_cookie_check_dummy( void *ctx, | 
|  | 828 | const unsigned char *cookie, size_t cookie_len, | 
|  | 829 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 830 | { | 
|  | 831 | ((void) ctx); | 
|  | 832 | ((void) cookie); | 
|  | 833 | ((void) cookie_len); | 
|  | 834 | ((void) cli_id); | 
|  | 835 | ((void) cli_id_len); | 
|  | 836 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 838 | } | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 839 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 840 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 841 | /* | 
|  | 842 | * Initialize an SSL context | 
|  | 843 | */ | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 844 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) | 
|  | 845 | { | 
|  | 846 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); | 
|  | 847 | } | 
|  | 848 |  | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 849 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) | 
|  | 850 | { | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 851 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 852 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) | 
|  | 853 | { | 
|  | 854 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 855 | { | 
|  | 856 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported" ) ); | 
|  | 857 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 858 | } | 
|  | 859 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); | 
|  | 860 | return( 0 ); | 
|  | 861 | } | 
|  | 862 | #endif | 
|  | 863 |  | 
|  | 864 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 865 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) | 
|  | 866 | { | 
|  | 867 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); | 
|  | 868 | return( 0 ); | 
|  | 869 | } | 
|  | 870 | #endif | 
|  | 871 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 872 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 873 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( ssl->conf ) ) | 
|  | 874 | { | 
|  | 875 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) ); | 
|  | 876 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 877 | } | 
|  | 878 | #endif | 
|  | 879 |  | 
|  | 880 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); | 
|  | 881 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 882 | } | 
|  | 883 |  | 
|  | 884 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) | 
|  | 885 | { | 
|  | 886 | int ret; | 
|  | 887 | ret = ssl_conf_version_check( ssl ); | 
|  | 888 | if( ret != 0 ) | 
|  | 889 | return( ret ); | 
|  | 890 |  | 
|  | 891 | /* Space for further checks */ | 
|  | 892 |  | 
|  | 893 | return( 0 ); | 
|  | 894 | } | 
|  | 895 |  | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 896 | /* | 
|  | 897 | * Setup an SSL context | 
|  | 898 | */ | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 899 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 900 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 901 | const mbedtls_ssl_config *conf ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 902 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 903 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 904 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 905 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 906 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 907 | ssl->conf = conf; | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 908 |  | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 909 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) | 
|  | 910 | return( ret ); | 
|  | 911 |  | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 912 | /* | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 913 | * Prepare base structures | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 914 | */ | 
| k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 915 |  | 
|  | 916 | /* Set to NULL in case of an error condition */ | 
|  | 917 | ssl->out_buf = NULL; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 918 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 919 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 920 | ssl->in_buf_len = in_buf_len; | 
|  | 921 | #endif | 
|  | 922 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 923 | if( ssl->in_buf == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 924 | { | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 925 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) ); | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 926 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 927 | goto error; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 928 | } | 
|  | 929 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 930 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 931 | ssl->out_buf_len = out_buf_len; | 
|  | 932 | #endif | 
|  | 933 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 934 | if( ssl->out_buf == NULL ) | 
|  | 935 | { | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 936 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) ); | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 937 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 938 | goto error; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 939 | } | 
|  | 940 |  | 
| Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 941 | mbedtls_ssl_reset_in_out_pointers( ssl ); | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 942 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 943 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 944 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 945 | #endif | 
|  | 946 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 947 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 948 | goto error; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 949 |  | 
|  | 950 | return( 0 ); | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 951 |  | 
|  | 952 | error: | 
|  | 953 | mbedtls_free( ssl->in_buf ); | 
|  | 954 | mbedtls_free( ssl->out_buf ); | 
|  | 955 |  | 
|  | 956 | ssl->conf = NULL; | 
|  | 957 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 958 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 959 | ssl->in_buf_len = 0; | 
|  | 960 | ssl->out_buf_len = 0; | 
|  | 961 | #endif | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 962 | ssl->in_buf = NULL; | 
|  | 963 | ssl->out_buf = NULL; | 
|  | 964 |  | 
|  | 965 | ssl->in_hdr = NULL; | 
|  | 966 | ssl->in_ctr = NULL; | 
|  | 967 | ssl->in_len = NULL; | 
|  | 968 | ssl->in_iv = NULL; | 
|  | 969 | ssl->in_msg = NULL; | 
|  | 970 |  | 
|  | 971 | ssl->out_hdr = NULL; | 
|  | 972 | ssl->out_ctr = NULL; | 
|  | 973 | ssl->out_len = NULL; | 
|  | 974 | ssl->out_iv = NULL; | 
|  | 975 | ssl->out_msg = NULL; | 
|  | 976 |  | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 977 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 978 | } | 
|  | 979 |  | 
|  | 980 | /* | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 981 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 982 | * all application-set variables, function pointers and data. | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 983 | * | 
|  | 984 | * If partial is non-zero, keep data in the input buffer and client ID. | 
|  | 985 | * (Use when a DTLS client reconnects from the same port.) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 986 | */ | 
| XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 987 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, | 
|  | 988 | int partial ) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 989 | { | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 990 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 991 | size_t in_buf_len = ssl->in_buf_len; | 
|  | 992 | size_t out_buf_len = ssl->out_buf_len; | 
|  | 993 | #else | 
|  | 994 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 995 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
|  | 996 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 997 |  | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 998 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) | 
|  | 999 | partial = 0; | 
| Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1000 | #endif | 
|  | 1001 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1002 | /* Cancel any possibly running timer */ | 
| Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1003 | mbedtls_ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1004 |  | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1005 | mbedtls_ssl_reset_in_out_pointers( ssl ); | 
|  | 1006 |  | 
|  | 1007 | /* Reset incoming message parsing */ | 
|  | 1008 | ssl->in_offt    = NULL; | 
|  | 1009 | ssl->nb_zero    = 0; | 
|  | 1010 | ssl->in_msgtype = 0; | 
|  | 1011 | ssl->in_msglen  = 0; | 
|  | 1012 | ssl->in_hslen   = 0; | 
|  | 1013 | ssl->keep_current_message = 0; | 
|  | 1014 | ssl->transform_in  = NULL; | 
|  | 1015 |  | 
|  | 1016 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 1017 | ssl->next_record_offset = 0; | 
|  | 1018 | ssl->in_epoch = 0; | 
|  | 1019 | #endif | 
|  | 1020 |  | 
|  | 1021 | /* Keep current datagram if partial == 1 */ | 
|  | 1022 | if( partial == 0 ) | 
|  | 1023 | { | 
|  | 1024 | ssl->in_left = 0; | 
|  | 1025 | memset( ssl->in_buf, 0, in_buf_len ); | 
|  | 1026 | } | 
|  | 1027 |  | 
|  | 1028 | /* Reset outgoing message writing */ | 
|  | 1029 | ssl->out_msgtype = 0; | 
|  | 1030 | ssl->out_msglen  = 0; | 
|  | 1031 | ssl->out_left    = 0; | 
|  | 1032 | memset( ssl->out_buf, 0, out_buf_len ); | 
|  | 1033 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); | 
|  | 1034 | ssl->transform_out = NULL; | 
|  | 1035 |  | 
|  | 1036 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 1037 | mbedtls_ssl_dtls_replay_reset( ssl ); | 
|  | 1038 | #endif | 
|  | 1039 |  | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1040 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1041 | if( ssl->transform ) | 
|  | 1042 | { | 
|  | 1043 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 1044 | mbedtls_free( ssl->transform ); | 
|  | 1045 | ssl->transform = NULL; | 
|  | 1046 | } | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1047 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 1048 |  | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1049 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 1050 | mbedtls_ssl_transform_free( ssl->transform_application ); | 
|  | 1051 | mbedtls_free( ssl->transform_application ); | 
|  | 1052 | ssl->transform_application = NULL; | 
|  | 1053 |  | 
|  | 1054 | if( ssl->handshake != NULL ) | 
|  | 1055 | { | 
|  | 1056 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); | 
|  | 1057 | mbedtls_free( ssl->handshake->transform_earlydata ); | 
|  | 1058 | ssl->handshake->transform_earlydata = NULL; | 
|  | 1059 |  | 
|  | 1060 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); | 
|  | 1061 | mbedtls_free( ssl->handshake->transform_handshake ); | 
|  | 1062 | ssl->handshake->transform_handshake = NULL; | 
|  | 1063 | } | 
|  | 1064 |  | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1065 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1066 | } | 
|  | 1067 |  | 
|  | 1068 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) | 
|  | 1069 | { | 
|  | 1070 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 1071 |  | 
|  | 1072 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
|  | 1073 |  | 
| XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1074 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1075 |  | 
|  | 1076 | /* Reset renegotiation state */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1077 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1078 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1079 | ssl->renego_records_seen = 0; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1080 |  | 
|  | 1081 | ssl->verify_data_len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1082 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); | 
|  | 1083 | memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1084 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1085 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1086 |  | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1087 | ssl->session_in  = NULL; | 
| Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1088 | ssl->session_out = NULL; | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1089 | if( ssl->session ) | 
|  | 1090 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 1092 | mbedtls_free( ssl->session ); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1093 | ssl->session = NULL; | 
|  | 1094 | } | 
|  | 1095 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1096 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1097 | ssl->alpn_chosen = NULL; | 
|  | 1098 | #endif | 
|  | 1099 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1100 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1101 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1102 | if( partial == 0 ) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1103 | #endif | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1104 | { | 
|  | 1105 | mbedtls_free( ssl->cli_id ); | 
|  | 1106 | ssl->cli_id = NULL; | 
|  | 1107 | ssl->cli_id_len = 0; | 
|  | 1108 | } | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1109 | #endif | 
|  | 1110 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1111 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
|  | 1112 | return( ret ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1113 |  | 
|  | 1114 | return( 0 ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1115 | } | 
|  | 1116 |  | 
| Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1117 | /* | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1118 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 1119 | * all application-set variables, function pointers and data. | 
|  | 1120 | */ | 
|  | 1121 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) | 
|  | 1122 | { | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1123 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1124 | } | 
|  | 1125 |  | 
|  | 1126 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1127 | * SSL set accessors | 
|  | 1128 | */ | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1129 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1130 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1131 | conf->endpoint   = endpoint; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1132 | } | 
|  | 1133 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1134 | void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport ) | 
| Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1135 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1136 | conf->transport = transport; | 
| Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1137 | } | 
|  | 1138 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1140 | void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode ) | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1141 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1142 | conf->anti_replay = mode; | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1143 | } | 
|  | 1144 | #endif | 
|  | 1145 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1146 | void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit ) | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1147 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1148 | conf->badmac_limit = limit; | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1149 | } | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1150 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1151 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1152 |  | 
| Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1153 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, | 
|  | 1154 | unsigned allow_packing ) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1155 | { | 
|  | 1156 | ssl->disable_datagram_packing = !allow_packing; | 
|  | 1157 | } | 
|  | 1158 |  | 
|  | 1159 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, | 
|  | 1160 | uint32_t min, uint32_t max ) | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1161 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1162 | conf->hs_timeout_min = min; | 
|  | 1163 | conf->hs_timeout_max = max; | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1164 | } | 
|  | 1165 | #endif | 
|  | 1166 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1167 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1168 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1169 | conf->authmode   = authmode; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1170 | } | 
|  | 1171 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1172 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1173 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1174 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1175 | void *p_vrfy ) | 
|  | 1176 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1177 | conf->f_vrfy      = f_vrfy; | 
|  | 1178 | conf->p_vrfy      = p_vrfy; | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1179 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1180 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1181 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1182 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1183 | int (*f_rng)(void *, unsigned char *, size_t), | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1184 | void *p_rng ) | 
|  | 1185 | { | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1186 | conf->f_rng      = f_rng; | 
|  | 1187 | conf->p_rng      = p_rng; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1188 | } | 
|  | 1189 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1190 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1191 | void (*f_dbg)(void *, int, const char *, int, const char *), | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1192 | void  *p_dbg ) | 
|  | 1193 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1194 | conf->f_dbg      = f_dbg; | 
|  | 1195 | conf->p_dbg      = p_dbg; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1196 | } | 
|  | 1197 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1199 | void *p_bio, | 
| Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1200 | mbedtls_ssl_send_t *f_send, | 
|  | 1201 | mbedtls_ssl_recv_t *f_recv, | 
|  | 1202 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1203 | { | 
|  | 1204 | ssl->p_bio          = p_bio; | 
|  | 1205 | ssl->f_send         = f_send; | 
|  | 1206 | ssl->f_recv         = f_recv; | 
|  | 1207 | ssl->f_recv_timeout = f_recv_timeout; | 
| Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1208 | } | 
|  | 1209 |  | 
| Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1210 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 1211 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) | 
|  | 1212 | { | 
|  | 1213 | ssl->mtu = mtu; | 
|  | 1214 | } | 
|  | 1215 | #endif | 
|  | 1216 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1217 | void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout ) | 
| Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1218 | { | 
|  | 1219 | conf->read_timeout   = timeout; | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1220 | } | 
|  | 1221 |  | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1222 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, | 
|  | 1223 | void *p_timer, | 
| Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1224 | mbedtls_ssl_set_timer_t *f_set_timer, | 
|  | 1225 | mbedtls_ssl_get_timer_t *f_get_timer ) | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1226 | { | 
|  | 1227 | ssl->p_timer        = p_timer; | 
|  | 1228 | ssl->f_set_timer    = f_set_timer; | 
|  | 1229 | ssl->f_get_timer    = f_get_timer; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1230 |  | 
|  | 1231 | /* Make sure we start with no timer running */ | 
| Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1232 | mbedtls_ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1233 | } | 
|  | 1234 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1235 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1236 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, | 
| Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1237 | void *p_cache, | 
|  | 1238 | mbedtls_ssl_cache_get_t *f_get_cache, | 
|  | 1239 | mbedtls_ssl_cache_set_t *f_set_cache ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1240 | { | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1241 | conf->p_cache = p_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1242 | conf->f_get_cache = f_get_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1243 | conf->f_set_cache = f_set_cache; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1244 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1245 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1246 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1247 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 1248 | int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1249 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1250 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1251 |  | 
|  | 1252 | if( ssl == NULL || | 
|  | 1253 | session == NULL || | 
|  | 1254 | ssl->session_negotiate == NULL || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1255 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1256 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1257 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1258 | } | 
|  | 1259 |  | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1260 | if( ssl->handshake->resume == 1 ) | 
|  | 1261 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 1262 |  | 
| Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1263 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, | 
|  | 1264 | session ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1265 | return( ret ); | 
|  | 1266 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1267 | ssl->handshake->resume = 1; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1268 |  | 
|  | 1269 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1270 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1271 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1272 |  | 
| Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1273 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, | 
|  | 1274 | const int *ciphersuites ) | 
|  | 1275 | { | 
| Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1276 | conf->ciphersuite_list = ciphersuites; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1277 | } | 
|  | 1278 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1279 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1280 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1281 | const int kex_modes ) | 
|  | 1282 | { | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1283 | conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1284 | } | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1285 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1286 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1287 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1288 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, | 
| Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1289 | const mbedtls_x509_crt_profile *profile ) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1290 | { | 
|  | 1291 | conf->cert_profile = profile; | 
|  | 1292 | } | 
|  | 1293 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1294 | /* Append a new keycert entry to a (possibly empty) list */ | 
|  | 1295 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, | 
|  | 1296 | mbedtls_x509_crt *cert, | 
|  | 1297 | mbedtls_pk_context *key ) | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1298 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1299 | mbedtls_ssl_key_cert *new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1300 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1301 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); | 
|  | 1302 | if( new_cert == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1303 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1304 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1305 | new_cert->cert = cert; | 
|  | 1306 | new_cert->key  = key; | 
|  | 1307 | new_cert->next = NULL; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1308 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1309 | /* Update head is the list was null, else add to the end */ | 
|  | 1310 | if( *head == NULL ) | 
| Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1311 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1312 | *head = new_cert; | 
| Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1313 | } | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1314 | else | 
|  | 1315 | { | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1316 | mbedtls_ssl_key_cert *cur = *head; | 
|  | 1317 | while( cur->next != NULL ) | 
|  | 1318 | cur = cur->next; | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1319 | cur->next = new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1320 | } | 
|  | 1321 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1322 | return( 0 ); | 
|  | 1323 | } | 
|  | 1324 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1325 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1326 | mbedtls_x509_crt *own_cert, | 
|  | 1327 | mbedtls_pk_context *pk_key ) | 
|  | 1328 | { | 
| Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1329 | return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) ); | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1330 | } | 
|  | 1331 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1332 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1333 | mbedtls_x509_crt *ca_chain, | 
|  | 1334 | mbedtls_x509_crl *ca_crl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1335 | { | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1336 | conf->ca_chain   = ca_chain; | 
|  | 1337 | conf->ca_crl     = ca_crl; | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1338 |  | 
|  | 1339 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
|  | 1340 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() | 
|  | 1341 | * cannot be used together. */ | 
|  | 1342 | conf->f_ca_cb = NULL; | 
|  | 1343 | conf->p_ca_cb = NULL; | 
|  | 1344 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1345 | } | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1346 |  | 
|  | 1347 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
|  | 1348 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, | 
| Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1349 | mbedtls_x509_crt_ca_cb_t f_ca_cb, | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1350 | void *p_ca_cb ) | 
|  | 1351 | { | 
|  | 1352 | conf->f_ca_cb = f_ca_cb; | 
|  | 1353 | conf->p_ca_cb = p_ca_cb; | 
|  | 1354 |  | 
|  | 1355 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() | 
|  | 1356 | * cannot be used together. */ | 
|  | 1357 | conf->ca_chain   = NULL; | 
|  | 1358 | conf->ca_crl     = NULL; | 
|  | 1359 | } | 
|  | 1360 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1361 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1362 |  | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1363 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 1364 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, | 
|  | 1365 | mbedtls_x509_crt *own_cert, | 
|  | 1366 | mbedtls_pk_context *pk_key ) | 
|  | 1367 | { | 
|  | 1368 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, | 
|  | 1369 | own_cert, pk_key ) ); | 
|  | 1370 | } | 
| Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1371 |  | 
|  | 1372 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, | 
|  | 1373 | mbedtls_x509_crt *ca_chain, | 
|  | 1374 | mbedtls_x509_crl *ca_crl ) | 
|  | 1375 | { | 
|  | 1376 | ssl->handshake->sni_ca_chain   = ca_chain; | 
|  | 1377 | ssl->handshake->sni_ca_crl     = ca_crl; | 
|  | 1378 | } | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1379 |  | 
|  | 1380 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, | 
|  | 1381 | int authmode ) | 
|  | 1382 | { | 
|  | 1383 | ssl->handshake->sni_authmode = authmode; | 
|  | 1384 | } | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1385 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
|  | 1386 |  | 
| Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1387 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 1388 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, | 
|  | 1389 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | 
|  | 1390 | void *p_vrfy ) | 
|  | 1391 | { | 
|  | 1392 | ssl->f_vrfy = f_vrfy; | 
|  | 1393 | ssl->p_vrfy = p_vrfy; | 
|  | 1394 | } | 
|  | 1395 | #endif | 
|  | 1396 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1397 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1398 | /* | 
|  | 1399 | * Set EC J-PAKE password for current handshake | 
|  | 1400 | */ | 
|  | 1401 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, | 
|  | 1402 | const unsigned char *pw, | 
|  | 1403 | size_t pw_len ) | 
|  | 1404 | { | 
|  | 1405 | mbedtls_ecjpake_role role; | 
|  | 1406 |  | 
| Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1407 | if( ssl->handshake == NULL || ssl->conf == NULL ) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1408 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1409 |  | 
|  | 1410 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 1411 | role = MBEDTLS_ECJPAKE_SERVER; | 
|  | 1412 | else | 
|  | 1413 | role = MBEDTLS_ECJPAKE_CLIENT; | 
|  | 1414 |  | 
|  | 1415 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, | 
|  | 1416 | role, | 
|  | 1417 | MBEDTLS_MD_SHA256, | 
|  | 1418 | MBEDTLS_ECP_DP_SECP256R1, | 
|  | 1419 | pw, pw_len ) ); | 
|  | 1420 | } | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1421 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1422 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1423 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1424 |  | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1425 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) | 
|  | 1426 | { | 
|  | 1427 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 1428 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) | 
|  | 1429 | return( 1 ); | 
|  | 1430 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 1431 |  | 
|  | 1432 | if( conf->psk != NULL ) | 
|  | 1433 | return( 1 ); | 
|  | 1434 |  | 
|  | 1435 | return( 0 ); | 
|  | 1436 | } | 
|  | 1437 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1438 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) | 
|  | 1439 | { | 
|  | 1440 | /* Remove reference to existing PSK, if any. */ | 
|  | 1441 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1442 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1443 | { | 
|  | 1444 | /* The maintenance of the PSK key slot is the | 
|  | 1445 | * user's responsibility. */ | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1446 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1447 | } | 
| Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 1448 | /* This and the following branch should never | 
|  | 1449 | * be taken simultaenously as we maintain the | 
|  | 1450 | * invariant that raw and opaque PSKs are never | 
|  | 1451 | * configured simultaneously. As a safeguard, | 
|  | 1452 | * though, `else` is omitted here. */ | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1453 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 1454 | if( conf->psk != NULL ) | 
|  | 1455 | { | 
|  | 1456 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | 
|  | 1457 |  | 
|  | 1458 | mbedtls_free( conf->psk ); | 
|  | 1459 | conf->psk = NULL; | 
|  | 1460 | conf->psk_len = 0; | 
|  | 1461 | } | 
|  | 1462 |  | 
|  | 1463 | /* Remove reference to PSK identity, if any. */ | 
|  | 1464 | if( conf->psk_identity != NULL ) | 
|  | 1465 | { | 
|  | 1466 | mbedtls_free( conf->psk_identity ); | 
|  | 1467 | conf->psk_identity = NULL; | 
|  | 1468 | conf->psk_identity_len = 0; | 
|  | 1469 | } | 
|  | 1470 | } | 
|  | 1471 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1472 | /* This function assumes that PSK identity in the SSL config is unset. | 
|  | 1473 | * It checks that the provided identity is well-formed and attempts | 
|  | 1474 | * to make a copy of it in the SSL config. | 
|  | 1475 | * On failure, the PSK identity in the config remains unset. */ | 
|  | 1476 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, | 
|  | 1477 | unsigned char const *psk_identity, | 
|  | 1478 | size_t psk_identity_len ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1479 | { | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1480 | /* Identity len will be encoded on two bytes */ | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1481 | if( psk_identity               == NULL || | 
|  | 1482 | ( psk_identity_len >> 16 ) != 0    || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1483 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1484 | { | 
|  | 1485 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1486 | } | 
|  | 1487 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1488 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); | 
|  | 1489 | if( conf->psk_identity == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1490 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1491 |  | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1492 | conf->psk_identity_len = psk_identity_len; | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1493 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); | 
| Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1494 |  | 
|  | 1495 | return( 0 ); | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1496 | } | 
|  | 1497 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1498 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, | 
|  | 1499 | const unsigned char *psk, size_t psk_len, | 
|  | 1500 | const unsigned char *psk_identity, size_t psk_identity_len ) | 
|  | 1501 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1502 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1503 |  | 
|  | 1504 | /* We currently only support one PSK, raw or opaque. */ | 
|  | 1505 | if( ssl_conf_psk_is_configured( conf ) ) | 
|  | 1506 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1507 |  | 
|  | 1508 | /* Check and set raw PSK */ | 
| Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1509 | if( psk == NULL ) | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1510 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1511 | if( psk_len == 0 ) | 
|  | 1512 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1513 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | 
|  | 1514 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1515 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1516 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) | 
|  | 1517 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 1518 | conf->psk_len = psk_len; | 
|  | 1519 | memcpy( conf->psk, psk, conf->psk_len ); | 
|  | 1520 |  | 
|  | 1521 | /* Check and set PSK Identity */ | 
|  | 1522 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); | 
|  | 1523 | if( ret != 0 ) | 
|  | 1524 | ssl_conf_remove_psk( conf ); | 
|  | 1525 |  | 
|  | 1526 | return( ret ); | 
|  | 1527 | } | 
|  | 1528 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1529 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) | 
|  | 1530 | { | 
|  | 1531 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1532 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1533 | { | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1534 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1535 | } | 
|  | 1536 | else | 
|  | 1537 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 1538 | if( ssl->handshake->psk != NULL ) | 
|  | 1539 | { | 
|  | 1540 | mbedtls_platform_zeroize( ssl->handshake->psk, | 
|  | 1541 | ssl->handshake->psk_len ); | 
|  | 1542 | mbedtls_free( ssl->handshake->psk ); | 
|  | 1543 | ssl->handshake->psk_len = 0; | 
|  | 1544 | } | 
|  | 1545 | } | 
|  | 1546 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1547 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, | 
|  | 1548 | const unsigned char *psk, size_t psk_len ) | 
|  | 1549 | { | 
|  | 1550 | if( psk == NULL || ssl->handshake == NULL ) | 
|  | 1551 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1552 |  | 
|  | 1553 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | 
|  | 1554 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1555 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1556 | ssl_remove_psk( ssl ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1557 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1558 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1559 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1560 |  | 
|  | 1561 | ssl->handshake->psk_len = psk_len; | 
|  | 1562 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); | 
|  | 1563 |  | 
|  | 1564 | return( 0 ); | 
|  | 1565 | } | 
|  | 1566 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1567 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 1568 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, | 
| Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1569 | mbedtls_svc_key_id_t psk, | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1570 | const unsigned char *psk_identity, | 
|  | 1571 | size_t psk_identity_len ) | 
|  | 1572 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1573 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1574 |  | 
|  | 1575 | /* We currently only support one PSK, raw or opaque. */ | 
|  | 1576 | if( ssl_conf_psk_is_configured( conf ) ) | 
|  | 1577 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1578 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1579 | /* Check and set opaque PSK */ | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1580 | if( mbedtls_svc_key_id_is_null( psk ) ) | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1581 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1582 | conf->psk_opaque = psk; | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1583 |  | 
|  | 1584 | /* Check and set PSK Identity */ | 
|  | 1585 | ret = ssl_conf_set_psk_identity( conf, psk_identity, | 
|  | 1586 | psk_identity_len ); | 
|  | 1587 | if( ret != 0 ) | 
|  | 1588 | ssl_conf_remove_psk( conf ); | 
|  | 1589 |  | 
|  | 1590 | return( ret ); | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1591 | } | 
|  | 1592 |  | 
| Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1593 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, | 
|  | 1594 | mbedtls_svc_key_id_t psk ) | 
|  | 1595 | { | 
|  | 1596 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || | 
|  | 1597 | ( ssl->handshake == NULL ) ) | 
|  | 1598 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1599 |  | 
|  | 1600 | ssl_remove_psk( ssl ); | 
|  | 1601 | ssl->handshake->psk_opaque = psk; | 
|  | 1602 | return( 0 ); | 
|  | 1603 | } | 
|  | 1604 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 1605 |  | 
|  | 1606 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, | 
|  | 1607 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, | 
|  | 1608 | size_t), | 
|  | 1609 | void *p_psk ) | 
|  | 1610 | { | 
|  | 1611 | conf->f_psk = f_psk; | 
|  | 1612 | conf->p_psk = p_psk; | 
|  | 1613 | } | 
|  | 1614 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
|  | 1615 |  | 
|  | 1616 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1617 | psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type, | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 1618 | size_t taglen, | 
|  | 1619 | psa_algorithm_t *alg, | 
|  | 1620 | psa_key_type_t *key_type, | 
|  | 1621 | size_t *key_size ) | 
|  | 1622 | { | 
|  | 1623 | switch ( mbedtls_cipher_type ) | 
|  | 1624 | { | 
|  | 1625 | case MBEDTLS_CIPHER_AES_128_CBC: | 
|  | 1626 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1627 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1628 | *key_size = 128; | 
|  | 1629 | break; | 
|  | 1630 | case MBEDTLS_CIPHER_AES_128_CCM: | 
|  | 1631 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1632 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1633 | *key_size = 128; | 
|  | 1634 | break; | 
|  | 1635 | case MBEDTLS_CIPHER_AES_128_GCM: | 
|  | 1636 | *alg = PSA_ALG_GCM; | 
|  | 1637 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1638 | *key_size = 128; | 
|  | 1639 | break; | 
|  | 1640 | case MBEDTLS_CIPHER_AES_192_CCM: | 
|  | 1641 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1642 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1643 | *key_size = 192; | 
|  | 1644 | break; | 
|  | 1645 | case MBEDTLS_CIPHER_AES_192_GCM: | 
|  | 1646 | *alg = PSA_ALG_GCM; | 
|  | 1647 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1648 | *key_size = 192; | 
|  | 1649 | break; | 
|  | 1650 | case MBEDTLS_CIPHER_AES_256_CBC: | 
|  | 1651 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1652 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1653 | *key_size = 256; | 
|  | 1654 | break; | 
|  | 1655 | case MBEDTLS_CIPHER_AES_256_CCM: | 
|  | 1656 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1657 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1658 | *key_size = 256; | 
|  | 1659 | break; | 
|  | 1660 | case MBEDTLS_CIPHER_AES_256_GCM: | 
|  | 1661 | *alg = PSA_ALG_GCM; | 
|  | 1662 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1663 | *key_size = 256; | 
|  | 1664 | break; | 
|  | 1665 | case MBEDTLS_CIPHER_ARIA_128_CBC: | 
|  | 1666 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1667 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1668 | *key_size = 128; | 
|  | 1669 | break; | 
|  | 1670 | case MBEDTLS_CIPHER_ARIA_128_CCM: | 
|  | 1671 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1672 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1673 | *key_size = 128; | 
|  | 1674 | break; | 
|  | 1675 | case MBEDTLS_CIPHER_ARIA_128_GCM: | 
|  | 1676 | *alg = PSA_ALG_GCM; | 
|  | 1677 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1678 | *key_size = 128; | 
|  | 1679 | break; | 
|  | 1680 | case MBEDTLS_CIPHER_ARIA_192_CCM: | 
|  | 1681 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1682 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1683 | *key_size = 192; | 
|  | 1684 | break; | 
|  | 1685 | case MBEDTLS_CIPHER_ARIA_192_GCM: | 
|  | 1686 | *alg = PSA_ALG_GCM; | 
|  | 1687 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1688 | *key_size = 192; | 
|  | 1689 | break; | 
|  | 1690 | case MBEDTLS_CIPHER_ARIA_256_CBC: | 
|  | 1691 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1692 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1693 | *key_size = 256; | 
|  | 1694 | break; | 
|  | 1695 | case MBEDTLS_CIPHER_ARIA_256_CCM: | 
|  | 1696 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1697 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1698 | *key_size = 256; | 
|  | 1699 | break; | 
|  | 1700 | case MBEDTLS_CIPHER_ARIA_256_GCM: | 
|  | 1701 | *alg = PSA_ALG_GCM; | 
|  | 1702 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1703 | *key_size = 256; | 
|  | 1704 | break; | 
|  | 1705 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: | 
|  | 1706 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1707 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1708 | *key_size = 128; | 
|  | 1709 | break; | 
|  | 1710 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: | 
|  | 1711 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1712 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1713 | *key_size = 128; | 
|  | 1714 | break; | 
|  | 1715 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: | 
|  | 1716 | *alg = PSA_ALG_GCM; | 
|  | 1717 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1718 | *key_size = 128; | 
|  | 1719 | break; | 
|  | 1720 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: | 
|  | 1721 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1722 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1723 | *key_size = 192; | 
|  | 1724 | break; | 
|  | 1725 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: | 
|  | 1726 | *alg = PSA_ALG_GCM; | 
|  | 1727 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1728 | *key_size = 192; | 
|  | 1729 | break; | 
|  | 1730 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: | 
|  | 1731 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1732 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1733 | *key_size = 256; | 
|  | 1734 | break; | 
|  | 1735 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: | 
|  | 1736 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1737 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1738 | *key_size = 256; | 
|  | 1739 | break; | 
|  | 1740 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: | 
|  | 1741 | *alg = PSA_ALG_GCM; | 
|  | 1742 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1743 | *key_size = 256; | 
|  | 1744 | break; | 
|  | 1745 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: | 
|  | 1746 | *alg = PSA_ALG_CHACHA20_POLY1305; | 
|  | 1747 | *key_type = PSA_KEY_TYPE_CHACHA20; | 
|  | 1748 | *key_size = 256; | 
|  | 1749 | break; | 
|  | 1750 | case MBEDTLS_CIPHER_NULL: | 
|  | 1751 | *alg = MBEDTLS_SSL_NULL_CIPHER; | 
|  | 1752 | *key_type = 0; | 
|  | 1753 | *key_size = 0; | 
|  | 1754 | break; | 
|  | 1755 | default: | 
|  | 1756 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 1757 | } | 
|  | 1758 |  | 
|  | 1759 | return PSA_SUCCESS; | 
|  | 1760 | } | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1761 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 1762 |  | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 1763 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1764 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, | 
|  | 1765 | const unsigned char *dhm_P, size_t P_len, | 
|  | 1766 | const unsigned char *dhm_G, size_t G_len ) | 
|  | 1767 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1768 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1769 |  | 
| Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 1770 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 1771 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 1772 |  | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1773 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || | 
|  | 1774 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) | 
|  | 1775 | { | 
|  | 1776 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 1777 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 1778 | return( ret ); | 
|  | 1779 | } | 
|  | 1780 |  | 
|  | 1781 | return( 0 ); | 
|  | 1782 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1783 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1784 | int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx ) | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1785 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1786 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1787 |  | 
| Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 1788 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 1789 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 1790 |  | 
| Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 1791 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, | 
|  | 1792 | &conf->dhm_P ) ) != 0 || | 
|  | 1793 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, | 
|  | 1794 | &conf->dhm_G ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 1795 | { | 
|  | 1796 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 1797 | mbedtls_mpi_free( &conf->dhm_G ); | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1798 | return( ret ); | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 1799 | } | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1800 |  | 
|  | 1801 | return( 0 ); | 
|  | 1802 | } | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 1803 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1804 |  | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 1805 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 1806 | /* | 
|  | 1807 | * Set the minimum length for Diffie-Hellman parameters | 
|  | 1808 | */ | 
|  | 1809 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, | 
|  | 1810 | unsigned int bitlen ) | 
|  | 1811 | { | 
|  | 1812 | conf->dhm_min_bitlen = bitlen; | 
|  | 1813 | } | 
|  | 1814 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ | 
|  | 1815 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1816 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 1817 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 1818 | /* | 
|  | 1819 | * Set allowed/preferred hashes for handshake signatures | 
|  | 1820 | */ | 
|  | 1821 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, | 
|  | 1822 | const int *hashes ) | 
|  | 1823 | { | 
|  | 1824 | conf->sig_hashes = hashes; | 
|  | 1825 | } | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 1826 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1827 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1828 | /* Configure allowed signature algorithms for handshake */ | 
| Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1829 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, | 
|  | 1830 | const uint16_t* sig_algs ) | 
|  | 1831 | { | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1832 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 1833 | conf->sig_hashes = NULL; | 
|  | 1834 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 1835 | conf->sig_algs = sig_algs; | 
| Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1836 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1837 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 1838 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 1839 | #if defined(MBEDTLS_ECP_C) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1840 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1841 | /* | 
|  | 1842 | * Set the allowed elliptic curves | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1843 | * | 
|  | 1844 | * mbedtls_ssl_setup() takes the provided list | 
|  | 1845 | * and translates it to a list of IANA TLS group identifiers, | 
|  | 1846 | * stored in ssl->handshake->group_list. | 
|  | 1847 | * | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1848 | */ | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1849 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1850 | const mbedtls_ecp_group_id *curve_list ) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1851 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1852 | conf->curve_list = curve_list; | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1853 | conf->group_list = NULL; | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1854 | } | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1855 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1856 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1857 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1858 | /* | 
|  | 1859 | * Set the allowed groups | 
|  | 1860 | */ | 
|  | 1861 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, | 
|  | 1862 | const uint16_t *group_list ) | 
|  | 1863 | { | 
|  | 1864 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 1865 | conf->curve_list = NULL; | 
|  | 1866 | #endif | 
|  | 1867 | conf->group_list = group_list; | 
|  | 1868 | } | 
|  | 1869 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1870 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1871 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1872 | { | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1873 | /* Initialize to suppress unnecessary compiler warning */ | 
|  | 1874 | size_t hostname_len = 0; | 
|  | 1875 |  | 
|  | 1876 | /* Check if new hostname is valid before | 
|  | 1877 | * making any change to current one */ | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1878 | if( hostname != NULL ) | 
|  | 1879 | { | 
|  | 1880 | hostname_len = strlen( hostname ); | 
|  | 1881 |  | 
|  | 1882 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) | 
|  | 1883 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1884 | } | 
|  | 1885 |  | 
|  | 1886 | /* Now it's clear that we will overwrite the old hostname, | 
|  | 1887 | * so we can free it safely */ | 
|  | 1888 |  | 
|  | 1889 | if( ssl->hostname != NULL ) | 
|  | 1890 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1891 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1892 | mbedtls_free( ssl->hostname ); | 
|  | 1893 | } | 
|  | 1894 |  | 
|  | 1895 | /* Passing NULL as hostname shall clear the old one */ | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 1896 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1897 | if( hostname == NULL ) | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1898 | { | 
|  | 1899 | ssl->hostname = NULL; | 
|  | 1900 | } | 
|  | 1901 | else | 
|  | 1902 | { | 
|  | 1903 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1904 | if( ssl->hostname == NULL ) | 
|  | 1905 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 1906 |  | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1907 | memcpy( ssl->hostname, hostname, hostname_len ); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 1908 |  | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1909 | ssl->hostname[hostname_len] = '\0'; | 
|  | 1910 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1911 |  | 
|  | 1912 | return( 0 ); | 
|  | 1913 | } | 
| Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 1914 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1915 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1916 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1917 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1918 | int (*f_sni)(void *, mbedtls_ssl_context *, | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1919 | const unsigned char *, size_t), | 
|  | 1920 | void *p_sni ) | 
|  | 1921 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1922 | conf->f_sni = f_sni; | 
|  | 1923 | conf->p_sni = p_sni; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1924 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1925 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1926 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1927 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1928 | int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos ) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1929 | { | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1930 | size_t cur_len, tot_len; | 
|  | 1931 | const char **p; | 
|  | 1932 |  | 
|  | 1933 | /* | 
| Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 1934 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings | 
|  | 1935 | * MUST NOT be truncated." | 
|  | 1936 | * We check lengths now rather than later. | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1937 | */ | 
|  | 1938 | tot_len = 0; | 
|  | 1939 | for( p = protos; *p != NULL; p++ ) | 
|  | 1940 | { | 
|  | 1941 | cur_len = strlen( *p ); | 
|  | 1942 | tot_len += cur_len; | 
|  | 1943 |  | 
| Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 1944 | if( ( cur_len == 0 ) || | 
|  | 1945 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || | 
|  | 1946 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1947 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1948 | } | 
|  | 1949 |  | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1950 | conf->alpn_list = protos; | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1951 |  | 
|  | 1952 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1953 | } | 
|  | 1954 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1955 | const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1956 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1957 | return( ssl->alpn_chosen ); | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1958 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1959 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1960 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1961 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1962 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, | 
|  | 1963 | int support_mki_value ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1964 | { | 
|  | 1965 | conf->dtls_srtp_mki_support = support_mki_value; | 
|  | 1966 | } | 
|  | 1967 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1968 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, | 
|  | 1969 | unsigned char *mki_value, | 
| Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 1970 | uint16_t mki_len ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1971 | { | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 1972 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1973 | { | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 1974 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1975 | } | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1976 |  | 
|  | 1977 | if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED ) | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1978 | { | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 1979 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1980 | } | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1981 |  | 
|  | 1982 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); | 
|  | 1983 | ssl->dtls_srtp_info.mki_len = mki_len; | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1984 | return( 0 ); | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1985 | } | 
|  | 1986 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1987 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 1988 | const mbedtls_ssl_srtp_profile *profiles ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1989 | { | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 1990 | const mbedtls_ssl_srtp_profile *p; | 
|  | 1991 | size_t list_size = 0; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1992 |  | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 1993 | /* check the profiles list: all entry must be valid, | 
|  | 1994 | * its size cannot be more than the total number of supported profiles, currently 4 */ | 
| Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 1995 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && | 
|  | 1996 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; | 
|  | 1997 | p++ ) | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 1998 | { | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 1999 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2000 | { | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2001 | list_size++; | 
|  | 2002 | } | 
|  | 2003 | else | 
|  | 2004 | { | 
|  | 2005 | /* unsupported value, stop parsing and set the size to an error value */ | 
|  | 2006 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2007 | } | 
|  | 2008 | } | 
|  | 2009 |  | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2010 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) | 
| Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2011 | { | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2012 | conf->dtls_srtp_profile_list = NULL; | 
|  | 2013 | conf->dtls_srtp_profile_list_len = 0; | 
|  | 2014 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2015 | } | 
|  | 2016 |  | 
| Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2017 | conf->dtls_srtp_profile_list = profiles; | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2018 | conf->dtls_srtp_profile_list_len = list_size; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2019 |  | 
|  | 2020 | return( 0 ); | 
|  | 2021 | } | 
|  | 2022 |  | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2023 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, | 
|  | 2024 | mbedtls_dtls_srtp_info *dtls_srtp_info ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2025 | { | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2026 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; | 
|  | 2027 | /* do not copy the mki value if there is no chosen profile */ | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2028 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) | 
| Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2029 | { | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2030 | dtls_srtp_info->mki_len = 0; | 
| Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2031 | } | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2032 | else | 
|  | 2033 | { | 
|  | 2034 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2035 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, | 
|  | 2036 | ssl->dtls_srtp_info.mki_len ); | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2037 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2038 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2039 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 2040 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2041 | void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor ) | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2042 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2043 | conf->max_major_ver = major; | 
|  | 2044 | conf->max_minor_ver = minor; | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2045 | } | 
|  | 2046 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2047 | void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor ) | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2048 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2049 | conf->min_major_ver = major; | 
|  | 2050 | conf->min_minor_ver = minor; | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2051 | } | 
|  | 2052 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2053 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 2054 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, | 
|  | 2055 | char cert_req_ca_list ) | 
|  | 2056 | { | 
|  | 2057 | conf->cert_req_ca_list = cert_req_ca_list; | 
|  | 2058 | } | 
|  | 2059 | #endif | 
|  | 2060 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2061 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2062 | void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm ) | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2063 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2064 | conf->encrypt_then_mac = etm; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2065 | } | 
|  | 2066 | #endif | 
|  | 2067 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2068 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2069 | void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems ) | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2070 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2071 | conf->extended_ms = ems; | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2072 | } | 
|  | 2073 | #endif | 
|  | 2074 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2075 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2076 | int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code ) | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2077 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2078 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2079 | ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2080 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2081 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2082 | } | 
|  | 2083 |  | 
| Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2084 | conf->mfl_code = mfl_code; | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2085 |  | 
|  | 2086 | return( 0 ); | 
|  | 2087 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2088 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2089 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2090 | void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2091 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2092 | conf->allow_legacy_renegotiation = allow_legacy; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2093 | } | 
|  | 2094 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2095 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2096 | void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation ) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2097 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2098 | conf->disable_renegotiation = renegotiation; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2099 | } | 
|  | 2100 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2101 | void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records ) | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2102 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2103 | conf->renego_max_records = max_records; | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2104 | } | 
|  | 2105 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2106 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2107 | const unsigned char period[8] ) | 
|  | 2108 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2109 | memcpy( conf->renego_period, period, 8 ); | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2110 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2111 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2112 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2113 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2114 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 2115 | void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets ) | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2116 | { | 
| Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2117 | conf->session_tickets = use_tickets; | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2118 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2119 | #endif | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2120 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2121 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2122 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, | 
|  | 2123 | mbedtls_ssl_ticket_write_t *f_ticket_write, | 
|  | 2124 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, | 
|  | 2125 | void *p_ticket ) | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2126 | { | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2127 | conf->f_ticket_write = f_ticket_write; | 
|  | 2128 | conf->f_ticket_parse = f_ticket_parse; | 
|  | 2129 | conf->p_ticket       = p_ticket; | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2130 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2131 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2132 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2133 |  | 
| Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2134 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, | 
|  | 2135 | mbedtls_ssl_export_keys_t *f_export_keys, | 
|  | 2136 | void *p_export_keys ) | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2137 | { | 
| Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2138 | ssl->f_export_keys = f_export_keys; | 
|  | 2139 | ssl->p_export_keys = p_export_keys; | 
| Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2140 | } | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2141 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2142 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2143 | void mbedtls_ssl_conf_async_private_cb( | 
|  | 2144 | mbedtls_ssl_config *conf, | 
|  | 2145 | mbedtls_ssl_async_sign_t *f_async_sign, | 
|  | 2146 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, | 
|  | 2147 | mbedtls_ssl_async_resume_t *f_async_resume, | 
|  | 2148 | mbedtls_ssl_async_cancel_t *f_async_cancel, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2149 | void *async_config_data ) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2150 | { | 
|  | 2151 | conf->f_async_sign_start = f_async_sign; | 
|  | 2152 | conf->f_async_decrypt_start = f_async_decrypt; | 
|  | 2153 | conf->f_async_resume = f_async_resume; | 
|  | 2154 | conf->f_async_cancel = f_async_cancel; | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2155 | conf->p_async_config_data = async_config_data; | 
|  | 2156 | } | 
|  | 2157 |  | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2158 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) | 
|  | 2159 | { | 
|  | 2160 | return( conf->p_async_config_data ); | 
|  | 2161 | } | 
|  | 2162 |  | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2163 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2164 | { | 
|  | 2165 | if( ssl->handshake == NULL ) | 
|  | 2166 | return( NULL ); | 
|  | 2167 | else | 
|  | 2168 | return( ssl->handshake->user_async_ctx ); | 
|  | 2169 | } | 
|  | 2170 |  | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2171 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2172 | void *ctx ) | 
|  | 2173 | { | 
|  | 2174 | if( ssl->handshake != NULL ) | 
|  | 2175 | ssl->handshake->user_async_ctx = ctx; | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2176 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2177 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2178 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2179 | /* | 
|  | 2180 | * SSL get accessors | 
|  | 2181 | */ | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2182 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2183 | { | 
| Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2184 | if( ssl->session != NULL ) | 
|  | 2185 | return( ssl->session->verify_result ); | 
|  | 2186 |  | 
|  | 2187 | if( ssl->session_negotiate != NULL ) | 
|  | 2188 | return( ssl->session_negotiate->verify_result ); | 
|  | 2189 |  | 
| Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2190 | return( 0xFFFFFFFF ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2191 | } | 
|  | 2192 |  | 
| Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2193 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) | 
|  | 2194 | { | 
|  | 2195 | if( ssl == NULL || ssl->session == NULL ) | 
|  | 2196 | return( 0 ); | 
|  | 2197 |  | 
|  | 2198 | return( ssl->session->ciphersuite ); | 
|  | 2199 | } | 
|  | 2200 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2201 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2202 | { | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2203 | if( ssl == NULL || ssl->session == NULL ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2204 | return( NULL ); | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2205 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2206 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2207 | } | 
|  | 2208 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2209 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2210 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2211 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2212 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2213 | { | 
|  | 2214 | switch( ssl->minor_ver ) | 
|  | 2215 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2216 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2217 | return( "DTLSv1.2" ); | 
|  | 2218 |  | 
|  | 2219 | default: | 
|  | 2220 | return( "unknown (DTLS)" ); | 
|  | 2221 | } | 
|  | 2222 | } | 
|  | 2223 | #endif | 
|  | 2224 |  | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2225 | switch( ssl->minor_ver ) | 
|  | 2226 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2227 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2228 | return( "TLSv1.2" ); | 
|  | 2229 |  | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2230 | default: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2231 | return( "unknown" ); | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2232 | } | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2233 | } | 
|  | 2234 |  | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2235 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2236 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) | 
|  | 2237 | { | 
| David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2238 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2239 | size_t read_mfl; | 
|  | 2240 |  | 
|  | 2241 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ | 
|  | 2242 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 2243 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) | 
|  | 2244 | { | 
|  | 2245 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); | 
|  | 2246 | } | 
|  | 2247 |  | 
|  | 2248 | /* Check if a smaller max length was negotiated */ | 
|  | 2249 | if( ssl->session_out != NULL ) | 
|  | 2250 | { | 
|  | 2251 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); | 
|  | 2252 | if( read_mfl < max_len ) | 
|  | 2253 | { | 
|  | 2254 | max_len = read_mfl; | 
|  | 2255 | } | 
|  | 2256 | } | 
|  | 2257 |  | 
|  | 2258 | // During a handshake, use the value being negotiated | 
|  | 2259 | if( ssl->session_negotiate != NULL ) | 
|  | 2260 | { | 
|  | 2261 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); | 
|  | 2262 | if( read_mfl < max_len ) | 
|  | 2263 | { | 
|  | 2264 | max_len = read_mfl; | 
|  | 2265 | } | 
|  | 2266 | } | 
|  | 2267 |  | 
|  | 2268 | return( max_len ); | 
|  | 2269 | } | 
|  | 2270 |  | 
|  | 2271 | 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] | 2272 | { | 
|  | 2273 | size_t max_len; | 
|  | 2274 |  | 
|  | 2275 | /* | 
|  | 2276 | * Assume mfl_code is correct since it was checked when set | 
|  | 2277 | */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2278 | 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] | 2279 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2280 | /* Check if a smaller max length was negotiated */ | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2281 | if( ssl->session_out != NULL && | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2282 | 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] | 2283 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2284 | 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] | 2285 | } | 
|  | 2286 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2287 | /* During a handshake, use the value being negotiated */ | 
|  | 2288 | if( ssl->session_negotiate != NULL && | 
|  | 2289 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) | 
|  | 2290 | { | 
|  | 2291 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); | 
|  | 2292 | } | 
|  | 2293 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2294 | return( max_len ); | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2295 | } | 
|  | 2296 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 2297 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2298 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2299 | 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] | 2300 | { | 
| Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2301 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ | 
|  | 2302 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 2303 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || | 
|  | 2304 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) | 
|  | 2305 | return ( 0 ); | 
|  | 2306 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2307 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) | 
|  | 2308 | return( ssl->mtu ); | 
|  | 2309 |  | 
|  | 2310 | if( ssl->mtu == 0 ) | 
|  | 2311 | return( ssl->handshake->mtu ); | 
|  | 2312 |  | 
|  | 2313 | return( ssl->mtu < ssl->handshake->mtu ? | 
|  | 2314 | ssl->mtu : ssl->handshake->mtu ); | 
|  | 2315 | } | 
|  | 2316 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 2317 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2318 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) | 
|  | 2319 | { | 
|  | 2320 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; | 
|  | 2321 |  | 
| Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2322 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ | 
|  | 2323 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2324 | (void) ssl; | 
|  | 2325 | #endif | 
|  | 2326 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2327 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2328 | 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] | 2329 |  | 
|  | 2330 | if( max_len > mfl ) | 
|  | 2331 | max_len = mfl; | 
|  | 2332 | #endif | 
|  | 2333 |  | 
|  | 2334 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2335 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2336 | { | 
| Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2337 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2338 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); | 
|  | 2339 | const size_t overhead = (size_t) ret; | 
|  | 2340 |  | 
|  | 2341 | if( ret < 0 ) | 
|  | 2342 | return( ret ); | 
|  | 2343 |  | 
|  | 2344 | if( mtu <= overhead ) | 
|  | 2345 | { | 
|  | 2346 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); | 
|  | 2347 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 2348 | } | 
|  | 2349 |  | 
|  | 2350 | if( max_len > mtu - overhead ) | 
|  | 2351 | max_len = mtu - overhead; | 
|  | 2352 | } | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2353 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2354 |  | 
| Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2355 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) &&        \ | 
|  | 2356 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2357 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2358 | #endif | 
|  | 2359 |  | 
|  | 2360 | return( (int) max_len ); | 
|  | 2361 | } | 
|  | 2362 |  | 
| Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2363 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) | 
|  | 2364 | { | 
|  | 2365 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; | 
|  | 2366 |  | 
|  | 2367 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 2368 | (void) ssl; | 
|  | 2369 | #endif | 
|  | 2370 |  | 
|  | 2371 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 2372 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); | 
|  | 2373 |  | 
|  | 2374 | if( max_len > mfl ) | 
|  | 2375 | max_len = mfl; | 
|  | 2376 | #endif | 
|  | 2377 |  | 
|  | 2378 | return( (int) max_len ); | 
|  | 2379 | } | 
|  | 2380 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2381 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 2382 | 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] | 2383 | { | 
|  | 2384 | if( ssl == NULL || ssl->session == NULL ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2385 | return( NULL ); | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2386 |  | 
| Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2387 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2388 | return( ssl->session->peer_cert ); | 
| Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2389 | #else | 
|  | 2390 | return( NULL ); | 
|  | 2391 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2392 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2393 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2394 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2395 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2396 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, | 
|  | 2397 | mbedtls_ssl_session *dst ) | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2398 | { | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2399 | int ret; | 
|  | 2400 |  | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2401 | if( ssl == NULL || | 
|  | 2402 | dst == NULL || | 
|  | 2403 | ssl->session == NULL || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2404 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2405 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2406 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2407 | } | 
|  | 2408 |  | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2409 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer | 
|  | 2410 | * idempotent: Each session can only be exported once. | 
|  | 2411 | * | 
|  | 2412 | * (This is in preparation for TLS 1.3 support where we will | 
|  | 2413 | * need the ability to export multiple sessions (aka tickets), | 
|  | 2414 | * which will be achieved by calling mbedtls_ssl_get_session() | 
|  | 2415 | * multiple times until it fails.) | 
|  | 2416 | * | 
|  | 2417 | * Check whether we have already exported the current session, | 
|  | 2418 | * and fail if so. | 
|  | 2419 | */ | 
|  | 2420 | if( ssl->session->exported == 1 ) | 
|  | 2421 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 2422 |  | 
|  | 2423 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); | 
|  | 2424 | if( ret != 0 ) | 
|  | 2425 | return( ret ); | 
|  | 2426 |  | 
|  | 2427 | /* Remember that we've exported the session. */ | 
|  | 2428 | ssl->session->exported = 1; | 
|  | 2429 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2430 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2431 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2432 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2433 | /* | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2434 | * Define ticket header determining Mbed TLS version | 
|  | 2435 | * and structure of the ticket. | 
|  | 2436 | */ | 
|  | 2437 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2438 | /* | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2439 | * Define bitflag determining compile-time settings influencing | 
|  | 2440 | * structure of serialized SSL sessions. | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2441 | */ | 
|  | 2442 |  | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2443 | #if defined(MBEDTLS_HAVE_TIME) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2444 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2445 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2446 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2447 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 2448 |  | 
|  | 2449 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2450 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2451 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2452 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2453 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 2454 |  | 
|  | 2455 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2456 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2457 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2458 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2459 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 2460 |  | 
|  | 2461 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2462 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2463 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2464 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2465 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 2466 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2467 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2468 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2469 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2470 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2471 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
|  | 2472 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2473 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 2474 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 | 
|  | 2475 | #else | 
|  | 2476 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 | 
|  | 2477 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 2478 |  | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2479 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT          0 | 
|  | 2480 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT           1 | 
|  | 2481 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 | 
|  | 2482 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT           3 | 
| Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 2483 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT           4 | 
|  | 2484 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT        5 | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2485 |  | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2486 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG                           \ | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2487 | ( (uint16_t) (                                                      \ | 
|  | 2488 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME          << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT          ) | \ | 
|  | 2489 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT           << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT           ) | \ | 
|  | 2490 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ | 
|  | 2491 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL           << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT           ) | \ | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2492 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM           << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT           ) | \ | 
| Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 2493 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET        << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT        ) ) ) | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2494 |  | 
| Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2495 | static unsigned char ssl_serialized_session_header[] = { | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2496 | MBEDTLS_VERSION_MAJOR, | 
|  | 2497 | MBEDTLS_VERSION_MINOR, | 
|  | 2498 | MBEDTLS_VERSION_PATCH, | 
| Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 2499 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), | 
|  | 2500 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), | 
| Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2501 | }; | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2502 |  | 
|  | 2503 | /* | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2504 | * Serialize a session in the following format: | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 2505 | * (in the presentation language of TLS, RFC 8446 section 3) | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2506 | * | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2507 | *  struct { | 
| Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2508 | * | 
| Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 2509 | *    opaque mbedtls_version[3];   // library version: major, minor, patch | 
|  | 2510 | *    opaque session_format[2];    // library-version specific 16-bit field | 
|  | 2511 | *                                 // determining the format of the remaining | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2512 | *                                 // serialized data. | 
| Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2513 | * | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2514 | *          Note: When updating the format, remember to keep | 
|  | 2515 | *          these version+format bytes. | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2516 | * | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2517 | *                                 // In this version, `session_format` determines | 
|  | 2518 | *                                 // the setting of those compile-time | 
|  | 2519 | *                                 // configuration options which influence | 
|  | 2520 | *                                 // the structure of mbedtls_ssl_session. | 
|  | 2521 | * | 
| Hanno Becker | fa0d61e | 2021-08-02 08:56:14 +0100 | [diff] [blame] | 2522 | *    uint8_t minor_ver;           // Protocol-version. Possible values: | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2523 | *                                 // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3) | 
|  | 2524 | * | 
|  | 2525 | *    select (serialized_session.minor_ver) { | 
|  | 2526 | * | 
|  | 2527 | *      case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2 | 
|  | 2528 | *        serialized_session_tls12 data; | 
|  | 2529 | * | 
|  | 2530 | *   }; | 
|  | 2531 | * | 
|  | 2532 | * } serialized_session; | 
|  | 2533 | * | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2534 | */ | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2535 |  | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2536 | static int ssl_session_save( const mbedtls_ssl_session *session, | 
|  | 2537 | unsigned char omit_header, | 
|  | 2538 | unsigned char *buf, | 
|  | 2539 | size_t buf_len, | 
|  | 2540 | size_t *olen ) | 
|  | 2541 | { | 
|  | 2542 | unsigned char *p = buf; | 
|  | 2543 | size_t used = 0; | 
|  | 2544 |  | 
|  | 2545 | if( !omit_header ) | 
|  | 2546 | { | 
|  | 2547 | /* | 
|  | 2548 | * Add Mbed TLS version identifier | 
|  | 2549 | */ | 
|  | 2550 |  | 
|  | 2551 | used += sizeof( ssl_serialized_session_header ); | 
|  | 2552 |  | 
|  | 2553 | if( used <= buf_len ) | 
|  | 2554 | { | 
|  | 2555 | memcpy( p, ssl_serialized_session_header, | 
|  | 2556 | sizeof( ssl_serialized_session_header ) ); | 
|  | 2557 | p += sizeof( ssl_serialized_session_header ); | 
|  | 2558 | } | 
|  | 2559 | } | 
|  | 2560 |  | 
|  | 2561 | /* | 
|  | 2562 | * TLS version identifier | 
|  | 2563 | */ | 
|  | 2564 | used += 1; | 
|  | 2565 | if( used <= buf_len ) | 
|  | 2566 | { | 
|  | 2567 | *p++ = session->minor_ver; | 
|  | 2568 | } | 
|  | 2569 |  | 
|  | 2570 | /* Forward to version-specific serialization routine. */ | 
|  | 2571 | switch( session->minor_ver ) | 
|  | 2572 | { | 
|  | 2573 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2574 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
|  | 2575 | { | 
|  | 2576 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; | 
|  | 2577 | used += ssl_session_save_tls12( session, p, remaining_len ); | 
|  | 2578 | break; | 
|  | 2579 | } | 
|  | 2580 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 2581 |  | 
|  | 2582 | default: | 
|  | 2583 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 2584 | } | 
|  | 2585 |  | 
|  | 2586 | *olen = used; | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 2587 | if( used > buf_len ) | 
|  | 2588 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2589 |  | 
|  | 2590 | return( 0 ); | 
|  | 2591 | } | 
|  | 2592 |  | 
|  | 2593 | /* | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2594 | * Public wrapper for ssl_session_save() | 
|  | 2595 | */ | 
|  | 2596 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, | 
|  | 2597 | unsigned char *buf, | 
|  | 2598 | size_t buf_len, | 
|  | 2599 | size_t *olen ) | 
|  | 2600 | { | 
|  | 2601 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); | 
|  | 2602 | } | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2603 |  | 
| Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 2604 | /* | 
|  | 2605 | * Deserialize session, see mbedtls_ssl_session_save() for format. | 
|  | 2606 | * | 
|  | 2607 | * This internal version is wrapped by a public function that cleans up in | 
|  | 2608 | * case of error, and has an extra option omit_header. | 
|  | 2609 | */ | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2610 | static int ssl_session_load( mbedtls_ssl_session *session, | 
|  | 2611 | unsigned char omit_header, | 
|  | 2612 | const unsigned char *buf, | 
|  | 2613 | size_t len ) | 
|  | 2614 | { | 
|  | 2615 | const unsigned char *p = buf; | 
|  | 2616 | const unsigned char * const end = buf + len; | 
|  | 2617 |  | 
|  | 2618 | if( !omit_header ) | 
|  | 2619 | { | 
|  | 2620 | /* | 
|  | 2621 | * Check Mbed TLS version identifier | 
|  | 2622 | */ | 
|  | 2623 |  | 
|  | 2624 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) | 
|  | 2625 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2626 |  | 
|  | 2627 | if( memcmp( p, ssl_serialized_session_header, | 
|  | 2628 | sizeof( ssl_serialized_session_header ) ) != 0 ) | 
|  | 2629 | { | 
|  | 2630 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | 
|  | 2631 | } | 
|  | 2632 | p += sizeof( ssl_serialized_session_header ); | 
|  | 2633 | } | 
|  | 2634 |  | 
|  | 2635 | /* | 
|  | 2636 | * TLS version identifier | 
|  | 2637 | */ | 
|  | 2638 | if( 1 > (size_t)( end - p ) ) | 
|  | 2639 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2640 | session->minor_ver = *p++; | 
|  | 2641 |  | 
|  | 2642 | /* Dispatch according to TLS version. */ | 
|  | 2643 | switch( session->minor_ver ) | 
|  | 2644 | { | 
|  | 2645 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2646 | case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */ | 
|  | 2647 | { | 
|  | 2648 | size_t remaining_len = ( end - p ); | 
|  | 2649 | return( ssl_session_load_tls12( session, p, remaining_len ) ); | 
|  | 2650 | } | 
|  | 2651 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 2652 |  | 
|  | 2653 | default: | 
|  | 2654 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2655 | } | 
|  | 2656 | } | 
|  | 2657 |  | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2658 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 2659 | * Deserialize session: public wrapper for error cleaning | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2660 | */ | 
|  | 2661 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, | 
|  | 2662 | const unsigned char *buf, | 
|  | 2663 | size_t len ) | 
|  | 2664 | { | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2665 | int ret = ssl_session_load( session, 0, buf, len ); | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2666 |  | 
|  | 2667 | if( ret != 0 ) | 
|  | 2668 | mbedtls_ssl_session_free( session ); | 
|  | 2669 |  | 
|  | 2670 | return( ret ); | 
|  | 2671 | } | 
|  | 2672 |  | 
|  | 2673 | /* | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2674 | * Perform a single step of the SSL handshake | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2675 | */ | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2676 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) | 
|  | 2677 | { | 
|  | 2678 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 2679 |  | 
|  | 2680 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
|  | 2681 | return( ret ); | 
|  | 2682 |  | 
|  | 2683 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2684 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 2685 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | 
|  | 2686 | { | 
|  | 2687 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 2688 | return( ret ); | 
|  | 2689 | } | 
|  | 2690 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 2691 |  | 
|  | 2692 | return( ret ); | 
|  | 2693 | } | 
|  | 2694 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2695 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2696 | { | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2697 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2698 |  | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2699 | if( ssl            == NULL                       || | 
|  | 2700 | ssl->conf      == NULL                       || | 
|  | 2701 | ssl->handshake == NULL                       || | 
|  | 2702 | ssl->state     == MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 2703 | { | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2704 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2705 | } | 
|  | 2706 |  | 
|  | 2707 | ret = ssl_prepare_handshake_step( ssl ); | 
|  | 2708 | if( ret != 0 ) | 
|  | 2709 | return( ret ); | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2710 |  | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2711 | ret = mbedtls_ssl_handle_pending_alert( ssl ); | 
|  | 2712 | if( ret != 0 ) | 
|  | 2713 | goto cleanup; | 
|  | 2714 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2715 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2716 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2717 | { | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2718 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2719 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) | 
| Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 2720 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2721 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2722 |  | 
|  | 2723 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2724 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) | 
|  | 2725 | ret = mbedtls_ssl_handshake_client_step( ssl ); | 
|  | 2726 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 2727 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2728 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2729 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2730 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2731 | { | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2732 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2733 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) | 
| Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 2734 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2735 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2736 |  | 
|  | 2737 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2738 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) | 
|  | 2739 | ret = mbedtls_ssl_handshake_server_step( ssl ); | 
|  | 2740 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 2741 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2742 | #endif | 
|  | 2743 |  | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2744 | if( ret != 0 ) | 
|  | 2745 | { | 
| Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 2746 | /* handshake_step return error. And it is same | 
|  | 2747 | * with alert_reason. | 
|  | 2748 | */ | 
| Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 2749 | if( ssl->send_alert ) | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2750 | { | 
| Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 2751 | ret = mbedtls_ssl_handle_pending_alert( ssl ); | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2752 | goto cleanup; | 
|  | 2753 | } | 
|  | 2754 | } | 
|  | 2755 |  | 
|  | 2756 | cleanup: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2757 | return( ret ); | 
|  | 2758 | } | 
|  | 2759 |  | 
|  | 2760 | /* | 
|  | 2761 | * Perform the SSL handshake | 
|  | 2762 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2763 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2764 | { | 
|  | 2765 | int ret = 0; | 
|  | 2766 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2767 | /* Sanity checks */ | 
|  | 2768 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2769 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 2770 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2771 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2772 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2773 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 2774 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) | 
|  | 2775 | { | 
|  | 2776 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " | 
|  | 2777 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); | 
|  | 2778 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2779 | } | 
|  | 2780 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 2781 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2782 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2783 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2784 | /* Main handshake loop */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2785 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2786 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2787 | ret = mbedtls_ssl_handshake_step( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2788 |  | 
|  | 2789 | if( ret != 0 ) | 
|  | 2790 | break; | 
|  | 2791 | } | 
|  | 2792 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2793 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2794 |  | 
|  | 2795 | return( ret ); | 
|  | 2796 | } | 
|  | 2797 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2798 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2799 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2800 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2801 | * Write HelloRequest to request renegotiation on server | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2802 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2803 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2804 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2805 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2806 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2807 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2808 |  | 
|  | 2809 | ssl->out_msglen  = 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2810 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 2811 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_HELLO_REQUEST; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2812 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2813 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2814 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2815 | 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] | 2816 | return( ret ); | 
|  | 2817 | } | 
|  | 2818 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2819 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2820 |  | 
|  | 2821 | return( 0 ); | 
|  | 2822 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2823 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2824 |  | 
|  | 2825 | /* | 
|  | 2826 | * Actually renegotiate current connection, triggered by either: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2827 | * - any side: calling mbedtls_ssl_renegotiate(), | 
|  | 2828 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), | 
|  | 2829 | * - 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] | 2830 | *   the initial handshake is completed. | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2831 | * 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] | 2832 | * 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] | 2833 | */ | 
| Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 2834 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2835 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2836 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2837 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2838 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2839 |  | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2840 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
|  | 2841 | return( ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2842 |  | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2843 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and | 
|  | 2844 | * the ServerHello will have message_seq = 1" */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2845 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2846 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2847 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2848 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2849 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2850 | ssl->handshake->out_msg_seq = 1; | 
|  | 2851 | else | 
|  | 2852 | ssl->handshake->in_msg_seq = 1; | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2853 | } | 
|  | 2854 | #endif | 
|  | 2855 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2856 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
|  | 2857 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2858 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2859 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2860 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2861 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2862 | return( ret ); | 
|  | 2863 | } | 
|  | 2864 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2865 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2866 |  | 
|  | 2867 | return( 0 ); | 
|  | 2868 | } | 
|  | 2869 |  | 
|  | 2870 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2871 | * Renegotiate current connection on client, | 
|  | 2872 | * or request renegotiation on server | 
|  | 2873 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2874 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2875 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2876 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2877 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2878 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 2879 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2880 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2881 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2882 | /* On server, just send the request */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2883 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2884 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2885 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 2886 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2887 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2888 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 2889 |  | 
|  | 2890 | /* Did we already try/start sending HelloRequest? */ | 
|  | 2891 | if( ssl->out_left != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2892 | return( mbedtls_ssl_flush_output( ssl ) ); | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 2893 |  | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2894 | return( ssl_write_hello_request( ssl ) ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2895 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2896 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2897 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2898 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2899 | /* | 
|  | 2900 | * On client, either start the renegotiation process or, | 
|  | 2901 | * if already in progress, continue the handshake | 
|  | 2902 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2903 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2904 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2905 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 2906 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2907 |  | 
| Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 2908 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2909 | { | 
| Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 2910 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2911 | return( ret ); | 
|  | 2912 | } | 
|  | 2913 | } | 
|  | 2914 | else | 
|  | 2915 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2916 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2917 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2918 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2919 | return( ret ); | 
|  | 2920 | } | 
|  | 2921 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2922 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2923 |  | 
| Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 2924 | return( ret ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2925 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2926 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2927 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2928 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 2929 | 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] | 2930 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2931 | mbedtls_ssl_key_cert *cur = key_cert, *next; | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 2932 |  | 
|  | 2933 | while( cur != NULL ) | 
|  | 2934 | { | 
|  | 2935 | next = cur->next; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2936 | mbedtls_free( cur ); | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 2937 | cur = next; | 
|  | 2938 | } | 
|  | 2939 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2940 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 2941 |  | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 2942 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2943 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 2944 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
|  | 2945 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2946 | if( handshake == NULL ) | 
|  | 2947 | return; | 
|  | 2948 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2949 | #if defined(MBEDTLS_ECP_C) | 
|  | 2950 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 2951 | if ( ssl->handshake->group_list_heap_allocated ) | 
|  | 2952 | mbedtls_free( (void*) handshake->group_list ); | 
|  | 2953 | handshake->group_list = NULL; | 
|  | 2954 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 2955 | #endif /* MBEDTLS_ECP_C */ | 
|  | 2956 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2957 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 2958 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 2959 | if ( ssl->handshake->sig_algs_heap_allocated ) | 
|  | 2960 | mbedtls_free( (void*) handshake->sig_algs ); | 
|  | 2961 | handshake->sig_algs = NULL; | 
|  | 2962 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 2963 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 2964 | if( ssl->handshake->certificate_request_context ) | 
|  | 2965 | { | 
|  | 2966 | mbedtls_free( (void*) handshake->certificate_request_context ); | 
|  | 2967 | } | 
|  | 2968 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2969 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 2970 |  | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2971 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
|  | 2972 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) | 
|  | 2973 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2974 | ssl->conf->f_async_cancel( ssl ); | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2975 | handshake->async_in_progress = 0; | 
|  | 2976 | } | 
|  | 2977 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
|  | 2978 |  | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 2979 | #if defined(MBEDTLS_SHA256_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2980 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 2981 | psa_hash_abort( &handshake->fin_sha256_psa ); | 
|  | 2982 | #else | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 2983 | mbedtls_sha256_free(   &handshake->fin_sha256    ); | 
|  | 2984 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2985 | #endif | 
| Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2986 | #if defined(MBEDTLS_SHA384_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2987 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2988 | psa_hash_abort( &handshake->fin_sha384_psa ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2989 | #else | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 2990 | mbedtls_sha512_free(   &handshake->fin_sha512    ); | 
|  | 2991 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2992 | #endif | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +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_free( &handshake->dhm_ctx ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [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_free( &handshake->ecdh_ctx ); | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +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_free( &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 | mbedtls_free( handshake->ecjpake_cache ); | 
|  | 3004 | handshake->ecjpake_cache = NULL; | 
|  | 3005 | handshake->ecjpake_cache_len = 0; | 
|  | 3006 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3007 | #endif | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3008 |  | 
| Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3009 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
|  | 3010 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3011 | /* explicit void pointer cast for buggy MS compiler */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3012 | mbedtls_free( (void *) handshake->curves ); | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3013 | #endif | 
|  | 3014 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3015 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3016 | if( handshake->psk != NULL ) | 
|  | 3017 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3018 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3019 | mbedtls_free( handshake->psk ); | 
|  | 3020 | } | 
|  | 3021 | #endif | 
|  | 3022 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3023 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 3024 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3025 | /* | 
|  | 3026 | * Free only the linked list wrapper, not the keys themselves | 
|  | 3027 | * since the belong to the SNI callback | 
|  | 3028 | */ | 
|  | 3029 | if( handshake->sni_key_cert != NULL ) | 
|  | 3030 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3031 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3032 |  | 
|  | 3033 | while( cur != NULL ) | 
|  | 3034 | { | 
|  | 3035 | next = cur->next; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3036 | mbedtls_free( cur ); | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3037 | cur = next; | 
|  | 3038 | } | 
|  | 3039 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3040 | #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] | 3041 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3042 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3043 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3044 | if( handshake->ecrs_peer_cert != NULL ) | 
|  | 3045 | { | 
|  | 3046 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); | 
|  | 3047 | mbedtls_free( handshake->ecrs_peer_cert ); | 
|  | 3048 | } | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3049 | #endif | 
|  | 3050 |  | 
| Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3051 | #if defined(MBEDTLS_X509_CRT_PARSE_C) &&        \ | 
|  | 3052 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3053 | mbedtls_pk_free( &handshake->peer_pubkey ); | 
|  | 3054 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 3055 |  | 
| XiaokangQian | 3490974 | 2022-01-27 02:25:04 +0000 | [diff] [blame] | 3056 | #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] | 3057 | mbedtls_free( handshake->verify_cookie ); | 
| XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3058 | #endif /* MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 3059 |  | 
|  | 3060 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3061 | mbedtls_ssl_flight_free( handshake->flight ); | 
|  | 3062 | mbedtls_ssl_buffering_free( ssl ); | 
| XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3063 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3064 |  | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3065 | #if defined(MBEDTLS_ECDH_C) &&                  \ | 
|  | 3066 | defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3067 | psa_destroy_key( handshake->ecdh_psa_privkey ); | 
|  | 3068 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 3069 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3070 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3071 | mbedtls_ssl_transform_free( handshake->transform_handshake ); | 
|  | 3072 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); | 
| Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3073 | mbedtls_free( handshake->transform_earlydata ); | 
|  | 3074 | mbedtls_free( handshake->transform_handshake ); | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3075 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3076 |  | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3077 |  | 
|  | 3078 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3079 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS | 
|  | 3080 | * processes datagrams and the fact that a datagram is allowed to have | 
|  | 3081 | * several records in it, it is possible that the I/O buffers are not | 
|  | 3082 | * empty at this stage */ | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3083 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), | 
|  | 3084 | mbedtls_ssl_get_output_buflen( ssl ) ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3085 | #endif | 
| Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3086 |  | 
| Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3087 | /* mbedtls_platform_zeroize MUST be last one in this function */ | 
|  | 3088 | mbedtls_platform_zeroize( handshake, | 
|  | 3089 | sizeof( mbedtls_ssl_handshake_params ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3090 | } | 
|  | 3091 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3092 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3093 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3094 | if( session == NULL ) | 
|  | 3095 | return; | 
|  | 3096 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3097 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3098 | ssl_clear_peer_cert( session ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3099 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3100 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3101 | #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] | 3102 | mbedtls_free( session->ticket ); | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3103 | #endif | 
| Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3104 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3105 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3106 | } | 
|  | 3107 |  | 
| Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3108 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3109 |  | 
|  | 3110 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 3111 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u | 
|  | 3112 | #else | 
|  | 3113 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u | 
|  | 3114 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 3115 |  | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3116 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3117 |  | 
|  | 3118 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 3119 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u | 
|  | 3120 | #else | 
|  | 3121 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u | 
|  | 3122 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 3123 |  | 
|  | 3124 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 3125 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u | 
|  | 3126 | #else | 
|  | 3127 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u | 
|  | 3128 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 3129 |  | 
|  | 3130 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT    0 | 
|  | 3131 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT     1 | 
|  | 3132 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT      2 | 
|  | 3133 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT                  3 | 
|  | 3134 |  | 
|  | 3135 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG   \ | 
|  | 3136 | ( (uint32_t) (                              \ | 
|  | 3137 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID     << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT     ) | \ | 
|  | 3138 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT      << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT      ) | \ | 
|  | 3139 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY       << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT       ) | \ | 
|  | 3140 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN                   << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT                   ) | \ | 
|  | 3141 | 0u ) ) | 
|  | 3142 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3143 | static unsigned char ssl_serialized_context_header[] = { | 
|  | 3144 | MBEDTLS_VERSION_MAJOR, | 
|  | 3145 | MBEDTLS_VERSION_MINOR, | 
|  | 3146 | MBEDTLS_VERSION_PATCH, | 
| Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3147 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), | 
|  | 3148 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), | 
|  | 3149 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), | 
|  | 3150 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), | 
|  | 3151 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3152 | }; | 
|  | 3153 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3154 | /* | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3155 | * Serialize a full SSL context | 
| Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3156 | * | 
|  | 3157 | * The format of the serialized data is: | 
|  | 3158 | * (in the presentation language of TLS, RFC 8446 section 3) | 
|  | 3159 | * | 
|  | 3160 | *  // header | 
|  | 3161 | *  opaque mbedtls_version[3];   // major, minor, patch | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3162 | *  opaque context_format[5];    // version-specific field determining | 
| Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3163 | *                               // the format of the remaining | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3164 | *                               // serialized data. | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3165 | *  Note: When updating the format, remember to keep these | 
|  | 3166 | *        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] | 3167 | * | 
|  | 3168 | *  // session sub-structure | 
|  | 3169 | *  opaque session<1..2^32-1>;  // see mbedtls_ssl_session_save() | 
|  | 3170 | *  // transform sub-structure | 
|  | 3171 | *  uint8 random[64];           // ServerHello.random+ClientHello.random | 
|  | 3172 | *  uint8 in_cid<0..2^8-1>      // Connection ID: expected incoming value | 
|  | 3173 | *  uint8 out_cid<0..2^8-1>     // Connection ID: outgoing value to use | 
|  | 3174 | *  // fields from ssl_context | 
|  | 3175 | *  uint32 badmac_seen;         // DTLS: number of records with failing MAC | 
|  | 3176 | *  uint64 in_window_top;       // DTLS: last validated record seq_num | 
|  | 3177 | *  uint64 in_window;           // DTLS: bitmask for replay protection | 
|  | 3178 | *  uint8 disable_datagram_packing; // DTLS: only one record per datagram | 
|  | 3179 | *  uint64 cur_out_ctr;         // Record layer: outgoing sequence number | 
|  | 3180 | *  uint16 mtu;                 // DTLS: path mtu (max outgoing fragment size) | 
|  | 3181 | *  uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol | 
|  | 3182 | * | 
|  | 3183 | * Note that many fields of the ssl_context or sub-structures are not | 
|  | 3184 | * serialized, as they fall in one of the following categories: | 
|  | 3185 | * | 
|  | 3186 | *  1. forced value (eg in_left must be 0) | 
|  | 3187 | *  2. pointer to dynamically-allocated memory (eg session, transform) | 
|  | 3188 | *  3. value can be re-derived from other data (eg session keys from MS) | 
|  | 3189 | *  4. value was temporary (eg content of input buffer) | 
|  | 3190 | *  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] | 3191 | */ | 
|  | 3192 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, | 
|  | 3193 | unsigned char *buf, | 
|  | 3194 | size_t buf_len, | 
|  | 3195 | size_t *olen ) | 
|  | 3196 | { | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3197 | unsigned char *p = buf; | 
|  | 3198 | size_t used = 0; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3199 | size_t session_len; | 
|  | 3200 | int ret = 0; | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3201 |  | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3202 | /* | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3203 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in | 
|  | 3204 | * this function's documentation. | 
|  | 3205 | * | 
|  | 3206 | * These are due to assumptions/limitations in the implementation. Some of | 
|  | 3207 | * them are likely to stay (no handshake in progress) some might go away | 
|  | 3208 | * (only DTLS) but are currently used to simplify the implementation. | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3209 | */ | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3210 | /* The initial handshake must be over */ | 
|  | 3211 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3212 | { | 
|  | 3213 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3214 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3215 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3216 | if( ssl->handshake != NULL ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3217 | { | 
|  | 3218 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3219 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3220 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3221 | /* Double-check that sub-structures are indeed ready */ | 
|  | 3222 | if( ssl->transform == NULL || ssl->session == NULL ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3223 | { | 
|  | 3224 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3225 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3226 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3227 | /* There must be no pending incoming or outgoing data */ | 
|  | 3228 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3229 | { | 
|  | 3230 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3231 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3232 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3233 | if( ssl->out_left != 0 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3234 | { | 
|  | 3235 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3236 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3237 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3238 | /* Protocol must be DLTS, not TLS */ | 
|  | 3239 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3240 | { | 
|  | 3241 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3242 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3243 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3244 | /* Version must be 1.2 */ | 
|  | 3245 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3246 | { | 
|  | 3247 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3248 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3249 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3250 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3251 | { | 
|  | 3252 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3253 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3254 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3255 | /* We must be using an AEAD ciphersuite */ | 
|  | 3256 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3257 | { | 
|  | 3258 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3259 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3260 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3261 | /* Renegotiation must not be enabled */ | 
|  | 3262 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 3263 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3264 | { | 
|  | 3265 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3266 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3267 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3268 | #endif | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3269 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3270 | /* | 
|  | 3271 | * Version and format identifier | 
|  | 3272 | */ | 
|  | 3273 | used += sizeof( ssl_serialized_context_header ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3274 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3275 | if( used <= buf_len ) | 
|  | 3276 | { | 
|  | 3277 | memcpy( p, ssl_serialized_context_header, | 
|  | 3278 | sizeof( ssl_serialized_context_header ) ); | 
|  | 3279 | p += sizeof( ssl_serialized_context_header ); | 
|  | 3280 | } | 
|  | 3281 |  | 
|  | 3282 | /* | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3283 | * Session (length + data) | 
|  | 3284 | */ | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3285 | 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] | 3286 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) | 
|  | 3287 | return( ret ); | 
|  | 3288 |  | 
|  | 3289 | used += 4 + session_len; | 
|  | 3290 | if( used <= buf_len ) | 
|  | 3291 | { | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3292 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); | 
|  | 3293 | p += 4; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3294 |  | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3295 | ret = ssl_session_save( ssl->session, 1, | 
|  | 3296 | p, session_len, &session_len ); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3297 | if( ret != 0 ) | 
|  | 3298 | return( ret ); | 
|  | 3299 |  | 
|  | 3300 | p += session_len; | 
|  | 3301 | } | 
|  | 3302 |  | 
|  | 3303 | /* | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3304 | * Transform | 
|  | 3305 | */ | 
|  | 3306 | used += sizeof( ssl->transform->randbytes ); | 
|  | 3307 | if( used <= buf_len ) | 
|  | 3308 | { | 
|  | 3309 | memcpy( p, ssl->transform->randbytes, | 
|  | 3310 | sizeof( ssl->transform->randbytes ) ); | 
|  | 3311 | p += sizeof( ssl->transform->randbytes ); | 
|  | 3312 | } | 
|  | 3313 |  | 
|  | 3314 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 3315 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; | 
|  | 3316 | if( used <= buf_len ) | 
|  | 3317 | { | 
|  | 3318 | *p++ = ssl->transform->in_cid_len; | 
|  | 3319 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); | 
|  | 3320 | p += ssl->transform->in_cid_len; | 
|  | 3321 |  | 
|  | 3322 | *p++ = ssl->transform->out_cid_len; | 
|  | 3323 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); | 
|  | 3324 | p += ssl->transform->out_cid_len; | 
|  | 3325 | } | 
|  | 3326 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 3327 |  | 
|  | 3328 | /* | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3329 | * Saved fields from top-level ssl_context structure | 
|  | 3330 | */ | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3331 | used += 4; | 
|  | 3332 | if( used <= buf_len ) | 
|  | 3333 | { | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3334 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); | 
|  | 3335 | p += 4; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3336 | } | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3337 |  | 
|  | 3338 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 3339 | used += 16; | 
|  | 3340 | if( used <= buf_len ) | 
|  | 3341 | { | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3342 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); | 
|  | 3343 | p += 8; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3344 |  | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3345 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); | 
|  | 3346 | p += 8; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3347 | } | 
|  | 3348 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 3349 |  | 
|  | 3350 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3351 | used += 1; | 
|  | 3352 | if( used <= buf_len ) | 
|  | 3353 | { | 
|  | 3354 | *p++ = ssl->disable_datagram_packing; | 
|  | 3355 | } | 
|  | 3356 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3357 |  | 
| Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3358 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3359 | if( used <= buf_len ) | 
|  | 3360 | { | 
| Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3361 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); | 
|  | 3362 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3363 | } | 
|  | 3364 |  | 
|  | 3365 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3366 | used += 2; | 
|  | 3367 | if( used <= buf_len ) | 
|  | 3368 | { | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3369 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); | 
|  | 3370 | p += 2; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3371 | } | 
|  | 3372 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3373 |  | 
|  | 3374 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 3375 | { | 
|  | 3376 | const uint8_t alpn_len = ssl->alpn_chosen | 
| Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 3377 | ? (uint8_t) strlen( ssl->alpn_chosen ) | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3378 | : 0; | 
|  | 3379 |  | 
|  | 3380 | used += 1 + alpn_len; | 
|  | 3381 | if( used <= buf_len ) | 
|  | 3382 | { | 
|  | 3383 | *p++ = alpn_len; | 
|  | 3384 |  | 
|  | 3385 | if( ssl->alpn_chosen != NULL ) | 
|  | 3386 | { | 
|  | 3387 | memcpy( p, ssl->alpn_chosen, alpn_len ); | 
|  | 3388 | p += alpn_len; | 
|  | 3389 | } | 
|  | 3390 | } | 
|  | 3391 | } | 
|  | 3392 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 3393 |  | 
|  | 3394 | /* | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3395 | * Done | 
|  | 3396 | */ | 
|  | 3397 | *olen = used; | 
|  | 3398 |  | 
|  | 3399 | if( used > buf_len ) | 
|  | 3400 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3401 |  | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3402 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); | 
|  | 3403 |  | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3404 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3405 | } | 
|  | 3406 |  | 
|  | 3407 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3408 | * Deserialize context, see mbedtls_ssl_context_save() for format. | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3409 | * | 
|  | 3410 | * This internal version is wrapped by a public function that cleans up in | 
|  | 3411 | * case of error. | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3412 | */ | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3413 | static int ssl_context_load( mbedtls_ssl_context *ssl, | 
|  | 3414 | const unsigned char *buf, | 
|  | 3415 | size_t len ) | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3416 | { | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3417 | const unsigned char *p = buf; | 
|  | 3418 | const unsigned char * const end = buf + len; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3419 | size_t session_len; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3420 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3421 |  | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3422 | /* | 
|  | 3423 | * The context should have been freshly setup or reset. | 
|  | 3424 | * Give the user an error in case of obvious misuse. | 
| Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 3425 | * (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] | 3426 | * renegotiating, or if the user mistakenly loaded a session first.) | 
|  | 3427 | */ | 
|  | 3428 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || | 
|  | 3429 | ssl->session != NULL ) | 
|  | 3430 | { | 
|  | 3431 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3432 | } | 
|  | 3433 |  | 
|  | 3434 | /* | 
|  | 3435 | * We can't check that the config matches the initial one, but we can at | 
|  | 3436 | * least check it matches the requirements for serializing. | 
|  | 3437 | */ | 
|  | 3438 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | 
|  | 3439 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || | 
|  | 3440 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || | 
|  | 3441 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || | 
|  | 3442 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || | 
|  | 3443 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3444 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3445 | #endif | 
| Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3446 | 0 ) | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3447 | { | 
|  | 3448 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3449 | } | 
|  | 3450 |  | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3451 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); | 
|  | 3452 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3453 | /* | 
|  | 3454 | * Check version identifier | 
|  | 3455 | */ | 
|  | 3456 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) | 
|  | 3457 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3458 |  | 
|  | 3459 | if( memcmp( p, ssl_serialized_context_header, | 
|  | 3460 | sizeof( ssl_serialized_context_header ) ) != 0 ) | 
|  | 3461 | { | 
|  | 3462 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | 
|  | 3463 | } | 
|  | 3464 | p += sizeof( ssl_serialized_context_header ); | 
|  | 3465 |  | 
|  | 3466 | /* | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3467 | * Session | 
|  | 3468 | */ | 
|  | 3469 | if( (size_t)( end - p ) < 4 ) | 
|  | 3470 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3471 |  | 
|  | 3472 | session_len = ( (size_t) p[0] << 24 ) | | 
|  | 3473 | ( (size_t) p[1] << 16 ) | | 
|  | 3474 | ( (size_t) p[2] <<  8 ) | | 
|  | 3475 | ( (size_t) p[3]       ); | 
|  | 3476 | p += 4; | 
|  | 3477 |  | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3478 | /* This has been allocated by ssl_handshake_init(), called by | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3479 | * 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] | 3480 | ssl->session = ssl->session_negotiate; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3481 | ssl->session_in = ssl->session; | 
|  | 3482 | ssl->session_out = ssl->session; | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3483 | ssl->session_negotiate = NULL; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3484 |  | 
|  | 3485 | if( (size_t)( end - p ) < session_len ) | 
|  | 3486 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3487 |  | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3488 | ret = ssl_session_load( ssl->session, 1, p, session_len ); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3489 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3490 | { | 
|  | 3491 | mbedtls_ssl_session_free( ssl->session ); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3492 | return( ret ); | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3493 | } | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3494 |  | 
|  | 3495 | p += session_len; | 
|  | 3496 |  | 
|  | 3497 | /* | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3498 | * Transform | 
|  | 3499 | */ | 
|  | 3500 |  | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3501 | /* This has been allocated by ssl_handshake_init(), called by | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3502 | * 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] | 3503 | ssl->transform = ssl->transform_negotiate; | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3504 | ssl->transform_in = ssl->transform; | 
|  | 3505 | ssl->transform_out = ssl->transform; | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3506 | ssl->transform_negotiate = NULL; | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3507 |  | 
|  | 3508 | /* Read random bytes and populate structure */ | 
|  | 3509 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) | 
|  | 3510 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3511 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3512 | ret = ssl_tls12_populate_transform( ssl->transform, | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3513 | ssl->session->ciphersuite, | 
|  | 3514 | ssl->session->master, | 
| Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3515 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ | 
|  | 3516 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3517 | ssl->session->encrypt_then_mac, | 
| Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3518 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && | 
|  | 3519 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3520 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), | 
|  | 3521 | p, /* currently pointing to randbytes */ | 
|  | 3522 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ | 
|  | 3523 | ssl->conf->endpoint, | 
|  | 3524 | ssl ); | 
|  | 3525 | if( ret != 0 ) | 
|  | 3526 | return( ret ); | 
| Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3527 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3528 | p += sizeof( ssl->transform->randbytes ); | 
|  | 3529 |  | 
|  | 3530 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 3531 | /* Read connection IDs and store them */ | 
|  | 3532 | if( (size_t)( end - p ) < 1 ) | 
|  | 3533 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3534 |  | 
|  | 3535 | ssl->transform->in_cid_len = *p++; | 
|  | 3536 |  | 
| Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 3537 | 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] | 3538 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3539 |  | 
|  | 3540 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); | 
|  | 3541 | p += ssl->transform->in_cid_len; | 
|  | 3542 |  | 
|  | 3543 | ssl->transform->out_cid_len = *p++; | 
|  | 3544 |  | 
|  | 3545 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) | 
|  | 3546 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3547 |  | 
|  | 3548 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); | 
|  | 3549 | p += ssl->transform->out_cid_len; | 
|  | 3550 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 3551 |  | 
|  | 3552 | /* | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3553 | * Saved fields from top-level ssl_context structure | 
|  | 3554 | */ | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3555 | if( (size_t)( end - p ) < 4 ) | 
|  | 3556 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3557 |  | 
|  | 3558 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | | 
|  | 3559 | ( (uint32_t) p[1] << 16 ) | | 
|  | 3560 | ( (uint32_t) p[2] <<  8 ) | | 
|  | 3561 | ( (uint32_t) p[3]       ); | 
|  | 3562 | p += 4; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3563 |  | 
|  | 3564 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 3565 | if( (size_t)( end - p ) < 16 ) | 
|  | 3566 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3567 |  | 
|  | 3568 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | | 
|  | 3569 | ( (uint64_t) p[1] << 48 ) | | 
|  | 3570 | ( (uint64_t) p[2] << 40 ) | | 
|  | 3571 | ( (uint64_t) p[3] << 32 ) | | 
|  | 3572 | ( (uint64_t) p[4] << 24 ) | | 
|  | 3573 | ( (uint64_t) p[5] << 16 ) | | 
|  | 3574 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 3575 | ( (uint64_t) p[7]       ); | 
|  | 3576 | p += 8; | 
|  | 3577 |  | 
|  | 3578 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | | 
|  | 3579 | ( (uint64_t) p[1] << 48 ) | | 
|  | 3580 | ( (uint64_t) p[2] << 40 ) | | 
|  | 3581 | ( (uint64_t) p[3] << 32 ) | | 
|  | 3582 | ( (uint64_t) p[4] << 24 ) | | 
|  | 3583 | ( (uint64_t) p[5] << 16 ) | | 
|  | 3584 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 3585 | ( (uint64_t) p[7]       ); | 
|  | 3586 | p += 8; | 
|  | 3587 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 3588 |  | 
|  | 3589 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3590 | if( (size_t)( end - p ) < 1 ) | 
|  | 3591 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3592 |  | 
|  | 3593 | ssl->disable_datagram_packing = *p++; | 
|  | 3594 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3595 |  | 
| Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3596 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3597 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3598 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); | 
|  | 3599 | p += sizeof( ssl->cur_out_ctr ); | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3600 |  | 
|  | 3601 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3602 | if( (size_t)( end - p ) < 2 ) | 
|  | 3603 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3604 |  | 
|  | 3605 | ssl->mtu = ( p[0] << 8 ) | p[1]; | 
|  | 3606 | p += 2; | 
|  | 3607 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3608 |  | 
|  | 3609 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 3610 | { | 
|  | 3611 | uint8_t alpn_len; | 
|  | 3612 | const char **cur; | 
|  | 3613 |  | 
|  | 3614 | if( (size_t)( end - p ) < 1 ) | 
|  | 3615 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3616 |  | 
|  | 3617 | alpn_len = *p++; | 
|  | 3618 |  | 
|  | 3619 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) | 
|  | 3620 | { | 
|  | 3621 | /* alpn_chosen should point to an item in the configured list */ | 
|  | 3622 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) | 
|  | 3623 | { | 
|  | 3624 | if( strlen( *cur ) == alpn_len && | 
|  | 3625 | memcmp( p, cur, alpn_len ) == 0 ) | 
|  | 3626 | { | 
|  | 3627 | ssl->alpn_chosen = *cur; | 
|  | 3628 | break; | 
|  | 3629 | } | 
|  | 3630 | } | 
|  | 3631 | } | 
|  | 3632 |  | 
|  | 3633 | /* can only happen on conf mismatch */ | 
|  | 3634 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) | 
|  | 3635 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3636 |  | 
|  | 3637 | p += alpn_len; | 
|  | 3638 | } | 
|  | 3639 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 3640 |  | 
|  | 3641 | /* | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3642 | * Forced fields from top-level ssl_context structure | 
|  | 3643 | * | 
|  | 3644 | * Most of them already set to the correct value by mbedtls_ssl_init() and | 
|  | 3645 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. | 
|  | 3646 | */ | 
|  | 3647 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; | 
|  | 3648 |  | 
|  | 3649 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
|  | 3650 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; | 
|  | 3651 |  | 
| Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3652 | /* Adjust pointers for header fields of outgoing records to | 
|  | 3653 | * the given transform, accounting for explicit IV and CID. */ | 
| Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3654 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); | 
| Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3655 |  | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3656 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3657 | ssl->in_epoch = 1; | 
|  | 3658 | #endif | 
|  | 3659 |  | 
|  | 3660 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, | 
|  | 3661 | * 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] | 3662 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() | 
|  | 3663 | * inappropriately. */ | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3664 | if( ssl->handshake != NULL ) | 
|  | 3665 | { | 
|  | 3666 | mbedtls_ssl_handshake_free( ssl ); | 
|  | 3667 | mbedtls_free( ssl->handshake ); | 
|  | 3668 | ssl->handshake = NULL; | 
|  | 3669 | } | 
|  | 3670 |  | 
|  | 3671 | /* | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3672 | * Done - should have consumed entire buffer | 
|  | 3673 | */ | 
|  | 3674 | if( p != end ) | 
|  | 3675 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3676 |  | 
|  | 3677 | return( 0 ); | 
|  | 3678 | } | 
|  | 3679 |  | 
|  | 3680 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3681 | * Deserialize context: public wrapper for error cleaning | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3682 | */ | 
|  | 3683 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, | 
|  | 3684 | const unsigned char *buf, | 
|  | 3685 | size_t len ) | 
|  | 3686 | { | 
|  | 3687 | int ret = ssl_context_load( context, buf, len ); | 
|  | 3688 |  | 
|  | 3689 | if( ret != 0 ) | 
|  | 3690 | mbedtls_ssl_free( context ); | 
|  | 3691 |  | 
|  | 3692 | return( ret ); | 
|  | 3693 | } | 
| Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3694 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3695 |  | 
|  | 3696 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3697 | * Free an SSL context | 
|  | 3698 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3699 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3700 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3701 | if( ssl == NULL ) | 
|  | 3702 | return; | 
|  | 3703 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3704 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3705 |  | 
| Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3706 | if( ssl->out_buf != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3707 | { | 
| sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3708 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3709 | size_t out_buf_len = ssl->out_buf_len; | 
|  | 3710 | #else | 
|  | 3711 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
|  | 3712 | #endif | 
|  | 3713 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3714 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3715 | mbedtls_free( ssl->out_buf ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3716 | ssl->out_buf = NULL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3717 | } | 
|  | 3718 |  | 
| Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3719 | if( ssl->in_buf != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3720 | { | 
| sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3721 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3722 | size_t in_buf_len = ssl->in_buf_len; | 
|  | 3723 | #else | 
|  | 3724 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 3725 | #endif | 
|  | 3726 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3727 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3728 | mbedtls_free( ssl->in_buf ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3729 | ssl->in_buf = NULL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3730 | } | 
|  | 3731 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3732 | if( ssl->transform ) | 
|  | 3733 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3734 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 3735 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3736 | } | 
|  | 3737 |  | 
|  | 3738 | if( ssl->handshake ) | 
|  | 3739 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3740 | mbedtls_ssl_handshake_free( ssl ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3741 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); | 
|  | 3742 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3743 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3744 | mbedtls_free( ssl->handshake ); | 
|  | 3745 | mbedtls_free( ssl->transform_negotiate ); | 
|  | 3746 | mbedtls_free( ssl->session_negotiate ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3747 | } | 
|  | 3748 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3749 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3750 | mbedtls_ssl_transform_free( ssl->transform_application ); | 
|  | 3751 | mbedtls_free( ssl->transform_application ); | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3752 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3753 |  | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3754 | if( ssl->session ) | 
|  | 3755 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3756 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 3757 | mbedtls_free( ssl->session ); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3758 | } | 
|  | 3759 |  | 
| Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 3760 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3761 | if( ssl->hostname != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3762 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3763 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3764 | mbedtls_free( ssl->hostname ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3765 | } | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 3766 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3767 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3768 | #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] | 3769 | mbedtls_free( ssl->cli_id ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 3770 | #endif | 
|  | 3771 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3772 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); | 
| Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 3773 |  | 
| Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 3774 | /* Actually clear after last debug message */ | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3775 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3776 | } | 
|  | 3777 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 3778 | /* | 
|  | 3779 | * Initialze mbedtls_ssl_config | 
|  | 3780 | */ | 
|  | 3781 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) | 
|  | 3782 | { | 
|  | 3783 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); | 
|  | 3784 | } | 
|  | 3785 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3786 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3787 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3788 | /* 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] | 3789 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, | 
|  | 3790 | * for no fundamental reason. | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3791 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise | 
|  | 3792 | * about this list. */ | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3793 | static int ssl_preset_default_hashes[] = { | 
|  | 3794 | #if defined(MBEDTLS_SHA512_C) | 
|  | 3795 | MBEDTLS_MD_SHA512, | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 3796 | #endif | 
|  | 3797 | #if defined(MBEDTLS_SHA384_C) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3798 | MBEDTLS_MD_SHA384, | 
|  | 3799 | #endif | 
|  | 3800 | #if defined(MBEDTLS_SHA256_C) | 
|  | 3801 | MBEDTLS_MD_SHA256, | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 3802 | #endif | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3803 | MBEDTLS_MD_NONE | 
|  | 3804 | }; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3805 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 3806 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3807 |  | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3808 | /* The selection should be the same as mbedtls_x509_crt_profile_default in | 
|  | 3809 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3810 | * curves with a lower resource usage come first. | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3811 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3812 | * about this list. | 
|  | 3813 | */ | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3814 | static uint16_t ssl_preset_default_groups[] = { | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3815 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3816 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3817 | #endif | 
|  | 3818 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3819 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3820 | #endif | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3821 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3822 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3823 | #endif | 
|  | 3824 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3825 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3826 | #endif | 
|  | 3827 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3828 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3829 | #endif | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3830 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3831 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3832 | #endif | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3833 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3834 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3835 | #endif | 
|  | 3836 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3837 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3838 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3839 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3840 | }; | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3841 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3842 | static int ssl_preset_suiteb_ciphersuites[] = { | 
|  | 3843 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | 
|  | 3844 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | 
|  | 3845 | 0 | 
|  | 3846 | }; | 
|  | 3847 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3848 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3849 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3850 | static int ssl_preset_suiteb_hashes[] = { | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3851 | #if defined(MBEDTLS_SHA256_C) | 
|  | 3852 | MBEDTLS_MD_SHA256, | 
|  | 3853 | #endif | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3854 | #if defined(MBEDTLS_SHA384_C) | 
|  | 3855 | MBEDTLS_MD_SHA384, | 
|  | 3856 | #endif | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3857 | MBEDTLS_MD_NONE | 
|  | 3858 | }; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3859 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
| Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 3860 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3861 | /* NOTICE: | 
| Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 3862 | *   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] | 3863 | *   rules SHOULD be upheld. | 
|  | 3864 | *   - No duplicate entries. | 
|  | 3865 | *   - But if there is a good reason, do not change the order of the algorithms. | 
|  | 3866 | *   - ssl_tls12_present* is for TLS 1.2 use only. | 
|  | 3867 | *   - 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] | 3868 | */ | 
| Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 3869 | static uint16_t ssl_preset_default_sig_algs[] = { | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3870 |  | 
| Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 3871 | #if defined(MBEDTLS_ECDSA_C) &&  defined(MBEDTLS_SHA256_C) && \ | 
|  | 3872 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | 
|  | 3873 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, | 
|  | 3874 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && | 
|  | 3875 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3876 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3877 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ | 
|  | 3878 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
|  | 3879 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, | 
|  | 3880 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && | 
|  | 3881 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ | 
|  | 3882 |  | 
| Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 3883 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \ | 
|  | 3884 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) | 
|  | 3885 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, | 
|  | 3886 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && | 
|  | 3887 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3888 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3889 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) | 
|  | 3890 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, | 
|  | 3891 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ | 
|  | 3892 |  | 
| Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 3893 | #if defined(MBEDTLS_RSA_C) &&  defined(MBEDTLS_SHA256_C) | 
|  | 3894 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, | 
|  | 3895 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ | 
|  | 3896 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3897 | MBEDTLS_TLS1_3_SIG_NONE | 
|  | 3898 | }; | 
|  | 3899 |  | 
|  | 3900 | /* NOTICE: see above */ | 
|  | 3901 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3902 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3903 | #if defined(MBEDTLS_SHA512_C) | 
|  | 3904 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 ) | 
|  | 3905 | #endif | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 3906 | #if defined(MBEDTLS_SHA384_C) | 
|  | 3907 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) | 
|  | 3908 | #endif | 
|  | 3909 | #if defined(MBEDTLS_SHA256_C) | 
|  | 3910 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) | 
|  | 3911 | #endif | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3912 | MBEDTLS_TLS1_3_SIG_NONE | 
|  | 3913 | }; | 
|  | 3914 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 3915 | /* NOTICE: see above */ | 
|  | 3916 | static uint16_t ssl_preset_suiteb_sig_algs[] = { | 
|  | 3917 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3918 | #if defined(MBEDTLS_ECDSA_C) &&  defined(MBEDTLS_SHA256_C) && \ | 
|  | 3919 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | 
|  | 3920 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, | 
|  | 3921 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && | 
|  | 3922 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ | 
|  | 3923 |  | 
| Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 3924 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ | 
|  | 3925 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
|  | 3926 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, | 
|  | 3927 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && | 
|  | 3928 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3929 |  | 
|  | 3930 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) | 
|  | 3931 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, | 
|  | 3932 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3933 |  | 
| Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 3934 | #if defined(MBEDTLS_RSA_C) &&  defined(MBEDTLS_SHA256_C) | 
|  | 3935 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, | 
|  | 3936 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ | 
|  | 3937 |  | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3938 | MBEDTLS_TLS1_3_SIG_NONE | 
|  | 3939 | }; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3940 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3941 | /* NOTICE: see above */ | 
|  | 3942 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3943 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3944 | #if defined(MBEDTLS_SHA256_C) | 
|  | 3945 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) | 
|  | 3946 | #endif | 
| Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 3947 | #if defined(MBEDTLS_SHA384_C) | 
|  | 3948 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) | 
|  | 3949 | #endif | 
| Xiaofei Bai | 9539501 | 2021-11-25 08:51:30 +0000 | [diff] [blame] | 3950 | MBEDTLS_TLS1_3_SIG_NONE | 
| Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 3951 | }; | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3952 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 3953 |  | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3954 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3955 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3956 | static uint16_t ssl_preset_suiteb_groups[] = { | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 3957 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3958 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 3959 | #endif | 
|  | 3960 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3961 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 3962 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3963 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3964 | }; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3965 |  | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3966 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3967 | /* 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] | 3968 | * to make sure there are no duplicated signature algorithm entries. */ | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 3969 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3970 | { | 
|  | 3971 | size_t i, j; | 
|  | 3972 | int ret = 0; | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3973 |  | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 3974 | for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ ) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3975 | { | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 3976 | for( j = 0; j < i; j++ ) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3977 | { | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 3978 | if( sig_algs[i] != sig_algs[j] ) | 
|  | 3979 | continue; | 
|  | 3980 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET | 
|  | 3981 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", | 
|  | 3982 | sig_algs[i], j, i ); | 
|  | 3983 | ret = -1; | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3984 | } | 
|  | 3985 | } | 
|  | 3986 | return( ret ); | 
|  | 3987 | } | 
|  | 3988 |  | 
|  | 3989 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 3990 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 3991 | /* | 
| Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 3992 | * Load default in mbedtls_ssl_config | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 3993 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3994 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3995 | int endpoint, int transport, int preset ) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 3996 | { | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 3997 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3998 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 3999 | #endif | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4000 |  | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4001 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4002 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4003 | { | 
|  | 4004 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); | 
|  | 4005 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | 
|  | 4006 | } | 
|  | 4007 |  | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4008 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4009 | { | 
|  | 4010 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); | 
|  | 4011 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | 
|  | 4012 | } | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4013 |  | 
|  | 4014 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4015 | 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] | 4016 | { | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4017 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4018 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | 
|  | 4019 | } | 
|  | 4020 |  | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4021 | 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] | 4022 | { | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4023 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4024 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | 
|  | 4025 | } | 
|  | 4026 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4027 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 4028 |  | 
| Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4029 | /* Use the functions here so that they are covered in tests, | 
|  | 4030 | * but otherwise access member directly for efficiency */ | 
|  | 4031 | mbedtls_ssl_conf_endpoint( conf, endpoint ); | 
|  | 4032 | mbedtls_ssl_conf_transport( conf, transport ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4033 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4034 | /* | 
|  | 4035 | * Things that are common to all presets | 
|  | 4036 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4037 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 4038 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 4039 | { | 
|  | 4040 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; | 
|  | 4041 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 4042 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; | 
|  | 4043 | #endif | 
|  | 4044 | } | 
|  | 4045 | #endif | 
|  | 4046 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4047 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 4048 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | 
|  | 4049 | #endif | 
|  | 4050 |  | 
|  | 4051 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 4052 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | 
|  | 4053 | #endif | 
|  | 4054 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4055 | #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] | 4056 | conf->f_cookie_write = ssl_cookie_write_dummy; | 
|  | 4057 | conf->f_cookie_check = ssl_cookie_check_dummy; | 
|  | 4058 | #endif | 
|  | 4059 |  | 
|  | 4060 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 4061 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; | 
|  | 4062 | #endif | 
|  | 4063 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4064 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 4065 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; | 
| TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4066 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4067 | #endif | 
|  | 4068 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4069 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4070 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; | 
|  | 4071 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; | 
|  | 4072 | #endif | 
|  | 4073 |  | 
|  | 4074 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 4075 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; | 
| Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4076 | memset( conf->renego_period,     0x00, 2 ); | 
|  | 4077 | memset( conf->renego_period + 2, 0xFF, 6 ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4078 | #endif | 
|  | 4079 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4080 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4081 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 4082 | { | 
|  | 4083 | const unsigned char dhm_p[] = | 
|  | 4084 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; | 
|  | 4085 | const unsigned char dhm_g[] = | 
|  | 4086 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; | 
| Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4087 |  | 
| Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4088 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, | 
|  | 4089 | dhm_p, sizeof( dhm_p ), | 
|  | 4090 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) | 
|  | 4091 | { | 
|  | 4092 | return( ret ); | 
|  | 4093 | } | 
|  | 4094 | } | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4095 | #endif | 
|  | 4096 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4097 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4098 | /* | 
|  | 4099 | * Allow all TLS 1.3 key exchange modes by default. | 
|  | 4100 | */ | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4101 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4102 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4103 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4104 | /* | 
|  | 4105 | * Preset-specific defaults | 
|  | 4106 | */ | 
|  | 4107 | switch( preset ) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4108 | { | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4109 | /* | 
|  | 4110 | * NSA Suite B | 
|  | 4111 | */ | 
|  | 4112 | case MBEDTLS_SSL_PRESET_SUITEB: | 
|  | 4113 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
|  | 4114 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ | 
|  | 4115 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; | 
| Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4116 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 4117 | /* Hybrid TLS 1.2/1.3 is not supported yet */ | 
|  | 4118 | conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; | 
|  | 4119 | #else | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4120 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; | 
| Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4121 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4122 |  | 
| Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4123 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4124 |  | 
|  | 4125 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 4126 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4127 | #endif | 
|  | 4128 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4129 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4130 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4131 | conf->sig_hashes = ssl_preset_suiteb_hashes; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4132 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4133 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4134 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) | 
|  | 4135 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; | 
|  | 4136 | else | 
|  | 4137 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 4138 | conf->sig_algs = ssl_preset_suiteb_sig_algs; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4139 | #endif | 
|  | 4140 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4141 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 4142 | conf->curve_list = NULL; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4143 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4144 | conf->group_list = ssl_preset_suiteb_groups; | 
| Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4145 | break; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4146 |  | 
|  | 4147 | /* | 
|  | 4148 | * Default | 
|  | 4149 | */ | 
|  | 4150 | default: | 
| Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 4151 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > | 
|  | 4152 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? | 
|  | 4153 | MBEDTLS_SSL_MIN_MAJOR_VERSION : | 
|  | 4154 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; | 
|  | 4155 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > | 
|  | 4156 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? | 
|  | 4157 | MBEDTLS_SSL_MIN_MINOR_VERSION : | 
|  | 4158 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4159 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; | 
| Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4160 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 4161 | /* Hybrid TLS 1.2/1.3 is not supported yet */ | 
|  | 4162 | conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; | 
|  | 4163 | #else | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4164 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; | 
| Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4165 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4166 |  | 
|  | 4167 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4168 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| TRodziewicz | ef73f01 | 2021-05-13 14:53:36 +0200 | [diff] [blame] | 4169 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4170 | #endif | 
| Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4171 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4172 |  | 
|  | 4173 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 4174 | conf->cert_profile = &mbedtls_x509_crt_profile_default; | 
|  | 4175 | #endif | 
|  | 4176 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4177 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4178 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4179 | conf->sig_hashes = ssl_preset_default_hashes; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4180 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4181 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4182 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) | 
|  | 4183 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; | 
|  | 4184 | else | 
|  | 4185 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 4186 | conf->sig_algs = ssl_preset_default_sig_algs; | 
| Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 4187 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4188 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4189 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 4190 | conf->curve_list = NULL; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4191 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4192 | conf->group_list = ssl_preset_default_groups; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4193 |  | 
|  | 4194 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 4195 | conf->dhm_min_bitlen = 1024; | 
|  | 4196 | #endif | 
|  | 4197 | } | 
|  | 4198 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4199 | return( 0 ); | 
|  | 4200 | } | 
|  | 4201 |  | 
|  | 4202 | /* | 
|  | 4203 | * Free mbedtls_ssl_config | 
|  | 4204 | */ | 
|  | 4205 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) | 
|  | 4206 | { | 
|  | 4207 | #if defined(MBEDTLS_DHM_C) | 
|  | 4208 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 4209 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 4210 | #endif | 
|  | 4211 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4212 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4213 | if( conf->psk != NULL ) | 
|  | 4214 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4215 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4216 | mbedtls_free( conf->psk ); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4217 | conf->psk = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4218 | conf->psk_len = 0; | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4219 | } | 
|  | 4220 |  | 
|  | 4221 | if( conf->psk_identity != NULL ) | 
|  | 4222 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4223 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4224 | mbedtls_free( conf->psk_identity ); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4225 | conf->psk_identity = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4226 | conf->psk_identity_len = 0; | 
|  | 4227 | } | 
|  | 4228 | #endif | 
|  | 4229 |  | 
|  | 4230 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 4231 | ssl_key_cert_free( conf->key_cert ); | 
|  | 4232 | #endif | 
|  | 4233 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4234 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4235 | } | 
|  | 4236 |  | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4237 | #if defined(MBEDTLS_PK_C) && \ | 
|  | 4238 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4239 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4240 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4241 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4242 | 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] | 4243 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4244 | #if defined(MBEDTLS_RSA_C) | 
|  | 4245 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) | 
|  | 4246 | return( MBEDTLS_SSL_SIG_RSA ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4247 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4248 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 4249 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) | 
|  | 4250 | return( MBEDTLS_SSL_SIG_ECDSA ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4251 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4252 | return( MBEDTLS_SSL_SIG_ANON ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4253 | } | 
|  | 4254 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4255 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) | 
|  | 4256 | { | 
|  | 4257 | switch( type ) { | 
|  | 4258 | case MBEDTLS_PK_RSA: | 
|  | 4259 | return( MBEDTLS_SSL_SIG_RSA ); | 
|  | 4260 | case MBEDTLS_PK_ECDSA: | 
|  | 4261 | case MBEDTLS_PK_ECKEY: | 
|  | 4262 | return( MBEDTLS_SSL_SIG_ECDSA ); | 
|  | 4263 | default: | 
|  | 4264 | return( MBEDTLS_SSL_SIG_ANON ); | 
|  | 4265 | } | 
|  | 4266 | } | 
|  | 4267 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4268 | 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] | 4269 | { | 
|  | 4270 | switch( sig ) | 
|  | 4271 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4272 | #if defined(MBEDTLS_RSA_C) | 
|  | 4273 | case MBEDTLS_SSL_SIG_RSA: | 
|  | 4274 | return( MBEDTLS_PK_RSA ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4275 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4276 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 4277 | case MBEDTLS_SSL_SIG_ECDSA: | 
|  | 4278 | return( MBEDTLS_PK_ECDSA ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4279 | #endif | 
|  | 4280 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4281 | return( MBEDTLS_PK_NONE ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4282 | } | 
|  | 4283 | } | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4284 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4285 |  | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4286 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4287 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4288 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4289 | 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] | 4290 | { | 
|  | 4291 | switch( hash ) | 
|  | 4292 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4293 | #if defined(MBEDTLS_MD5_C) | 
|  | 4294 | case MBEDTLS_SSL_HASH_MD5: | 
|  | 4295 | return( MBEDTLS_MD_MD5 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4296 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4297 | #if defined(MBEDTLS_SHA1_C) | 
|  | 4298 | case MBEDTLS_SSL_HASH_SHA1: | 
|  | 4299 | return( MBEDTLS_MD_SHA1 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4300 | #endif | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4301 | #if defined(MBEDTLS_SHA224_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4302 | case MBEDTLS_SSL_HASH_SHA224: | 
|  | 4303 | return( MBEDTLS_MD_SHA224 ); | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4304 | #endif | 
|  | 4305 | #if defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4306 | case MBEDTLS_SSL_HASH_SHA256: | 
|  | 4307 | return( MBEDTLS_MD_SHA256 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4308 | #endif | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4309 | #if defined(MBEDTLS_SHA384_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4310 | case MBEDTLS_SSL_HASH_SHA384: | 
|  | 4311 | return( MBEDTLS_MD_SHA384 ); | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4312 | #endif | 
|  | 4313 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4314 | case MBEDTLS_SSL_HASH_SHA512: | 
|  | 4315 | return( MBEDTLS_MD_SHA512 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4316 | #endif | 
|  | 4317 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4318 | return( MBEDTLS_MD_NONE ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4319 | } | 
|  | 4320 | } | 
|  | 4321 |  | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4322 | /* | 
|  | 4323 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX | 
|  | 4324 | */ | 
|  | 4325 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) | 
|  | 4326 | { | 
|  | 4327 | switch( md ) | 
|  | 4328 | { | 
|  | 4329 | #if defined(MBEDTLS_MD5_C) | 
|  | 4330 | case MBEDTLS_MD_MD5: | 
|  | 4331 | return( MBEDTLS_SSL_HASH_MD5 ); | 
|  | 4332 | #endif | 
|  | 4333 | #if defined(MBEDTLS_SHA1_C) | 
|  | 4334 | case MBEDTLS_MD_SHA1: | 
|  | 4335 | return( MBEDTLS_SSL_HASH_SHA1 ); | 
|  | 4336 | #endif | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4337 | #if defined(MBEDTLS_SHA224_C) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4338 | case MBEDTLS_MD_SHA224: | 
|  | 4339 | return( MBEDTLS_SSL_HASH_SHA224 ); | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4340 | #endif | 
|  | 4341 | #if defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4342 | case MBEDTLS_MD_SHA256: | 
|  | 4343 | return( MBEDTLS_SSL_HASH_SHA256 ); | 
|  | 4344 | #endif | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4345 | #if defined(MBEDTLS_SHA384_C) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4346 | case MBEDTLS_MD_SHA384: | 
|  | 4347 | return( MBEDTLS_SSL_HASH_SHA384 ); | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4348 | #endif | 
|  | 4349 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4350 | case MBEDTLS_MD_SHA512: | 
|  | 4351 | return( MBEDTLS_SSL_HASH_SHA512 ); | 
|  | 4352 | #endif | 
|  | 4353 | default: | 
|  | 4354 | return( MBEDTLS_SSL_HASH_NONE ); | 
|  | 4355 | } | 
|  | 4356 | } | 
|  | 4357 |  | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4358 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4359 | * 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] | 4360 | * 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] | 4361 | */ | 
| Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4362 | int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id ) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4363 | { | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4364 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4365 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4366 | if( group_list == NULL ) | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4367 | return( -1 ); | 
|  | 4368 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4369 | for( ; *group_list != 0; group_list++ ) | 
|  | 4370 | { | 
|  | 4371 | if( *group_list == tls_id ) | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4372 | return( 0 ); | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4373 | } | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4374 |  | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4375 | return( -1 ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4376 | } | 
| Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4377 |  | 
|  | 4378 | #if defined(MBEDTLS_ECP_C) | 
|  | 4379 | /* | 
|  | 4380 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. | 
|  | 4381 | */ | 
|  | 4382 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) | 
|  | 4383 | { | 
| Manuel Pégourié-Gonnard | 422370d | 2022-02-07 11:55:21 +0100 | [diff] [blame] | 4384 | uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id; | 
| Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4385 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); | 
|  | 4386 | } | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4387 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4388 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4389 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 4390 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, | 
|  | 4391 | const mbedtls_ssl_ciphersuite_t *ciphersuite, | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4392 | int cert_endpoint, | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4393 | uint32_t *flags ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4394 | { | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4395 | int ret = 0; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4396 | int usage = 0; | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4397 | const char *ext_oid; | 
|  | 4398 | size_t ext_len; | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4399 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4400 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4401 | { | 
|  | 4402 | /* Server part of the key exchange */ | 
|  | 4403 | switch( ciphersuite->key_exchange ) | 
|  | 4404 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4405 | case MBEDTLS_KEY_EXCHANGE_RSA: | 
|  | 4406 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4407 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4408 | break; | 
|  | 4409 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4410 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: | 
|  | 4411 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: | 
|  | 4412 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: | 
|  | 4413 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4414 | break; | 
|  | 4415 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4416 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: | 
|  | 4417 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4418 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4419 | break; | 
|  | 4420 |  | 
|  | 4421 | /* 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] | 4422 | case MBEDTLS_KEY_EXCHANGE_NONE: | 
|  | 4423 | case MBEDTLS_KEY_EXCHANGE_PSK: | 
|  | 4424 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: | 
|  | 4425 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: | 
| Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 4426 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4427 | usage = 0; | 
|  | 4428 | } | 
|  | 4429 | } | 
|  | 4430 | else | 
|  | 4431 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4432 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ | 
|  | 4433 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4434 | } | 
|  | 4435 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4436 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4437 | { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4438 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4439 | ret = -1; | 
|  | 4440 | } | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4441 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4442 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4443 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4444 | ext_oid = MBEDTLS_OID_SERVER_AUTH; | 
|  | 4445 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4446 | } | 
|  | 4447 | else | 
|  | 4448 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4449 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; | 
|  | 4450 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4451 | } | 
|  | 4452 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4453 | 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] | 4454 | { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4455 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4456 | ret = -1; | 
|  | 4457 | } | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4458 |  | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4459 | return( ret ); | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4460 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4461 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 4462 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4463 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) | 
|  | 4464 | { | 
|  | 4465 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4466 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 4467 | return( -1 ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4468 |  | 
|  | 4469 | switch( md ) | 
|  | 4470 | { | 
| Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 4471 | #if defined(MBEDTLS_SHA384_C) | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4472 | case MBEDTLS_SSL_HASH_SHA384: | 
|  | 4473 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; | 
|  | 4474 | break; | 
|  | 4475 | #endif | 
|  | 4476 | #if defined(MBEDTLS_SHA256_C) | 
|  | 4477 | case MBEDTLS_SSL_HASH_SHA256: | 
|  | 4478 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; | 
|  | 4479 | break; | 
|  | 4480 | #endif | 
|  | 4481 | default: | 
| Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 4482 | return( -1 ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4483 | } | 
|  | 4484 |  | 
|  | 4485 | return 0; | 
|  | 4486 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 4487 | (void) ssl; | 
|  | 4488 | (void) md; | 
|  | 4489 |  | 
| Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 4490 | return( -1 ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4491 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 4492 | } | 
|  | 4493 |  | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4494 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 4495 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, | 
|  | 4496 | const mbedtls_md_type_t md, | 
|  | 4497 | unsigned char *dst, | 
|  | 4498 | size_t dst_len, | 
|  | 4499 | size_t *olen ) | 
|  | 4500 | { | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4501 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 4502 | psa_hash_operation_t *hash_operation_to_clone; | 
|  | 4503 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); | 
|  | 4504 |  | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4505 | *olen = 0; | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4506 |  | 
|  | 4507 | switch( md ) | 
|  | 4508 | { | 
|  | 4509 | #if defined(MBEDTLS_SHA384_C) | 
|  | 4510 | case MBEDTLS_MD_SHA384: | 
|  | 4511 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; | 
|  | 4512 | break; | 
|  | 4513 | #endif | 
|  | 4514 |  | 
|  | 4515 | #if defined(MBEDTLS_SHA256_C) | 
|  | 4516 | case MBEDTLS_MD_SHA256: | 
|  | 4517 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; | 
|  | 4518 | break; | 
|  | 4519 | #endif | 
|  | 4520 |  | 
|  | 4521 | default: | 
|  | 4522 | goto exit; | 
|  | 4523 | } | 
|  | 4524 |  | 
|  | 4525 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); | 
|  | 4526 | if( status != PSA_SUCCESS ) | 
|  | 4527 | goto exit; | 
|  | 4528 |  | 
|  | 4529 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); | 
|  | 4530 | if( status != PSA_SUCCESS ) | 
|  | 4531 | goto exit; | 
|  | 4532 |  | 
|  | 4533 | exit: | 
| Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 4534 | return( psa_ssl_status_to_mbedtls( status ) ); | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4535 | } | 
|  | 4536 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 4537 |  | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4538 | #if defined(MBEDTLS_SHA384_C) | 
| Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4539 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, | 
|  | 4540 | unsigned char *dst, | 
|  | 4541 | size_t dst_len, | 
|  | 4542 | size_t *olen ) | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4543 | { | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4544 | int ret; | 
|  | 4545 | mbedtls_sha512_context sha512; | 
|  | 4546 |  | 
|  | 4547 | if( dst_len < 48 ) | 
|  | 4548 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4549 |  | 
|  | 4550 | mbedtls_sha512_init( &sha512 ); | 
|  | 4551 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
|  | 4552 |  | 
|  | 4553 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) | 
|  | 4554 | { | 
|  | 4555 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); | 
|  | 4556 | goto exit; | 
|  | 4557 | } | 
|  | 4558 |  | 
|  | 4559 | *olen = 48; | 
|  | 4560 |  | 
|  | 4561 | exit: | 
|  | 4562 |  | 
|  | 4563 | mbedtls_sha512_free( &sha512 ); | 
|  | 4564 | return( ret ); | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4565 | } | 
|  | 4566 | #endif /* MBEDTLS_SHA384_C */ | 
|  | 4567 |  | 
|  | 4568 | #if defined(MBEDTLS_SHA256_C) | 
| Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4569 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, | 
|  | 4570 | unsigned char *dst, | 
|  | 4571 | size_t dst_len, | 
|  | 4572 | size_t *olen ) | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4573 | { | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4574 | int ret; | 
|  | 4575 | mbedtls_sha256_context sha256; | 
|  | 4576 |  | 
|  | 4577 | if( dst_len < 32 ) | 
|  | 4578 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4579 |  | 
|  | 4580 | mbedtls_sha256_init( &sha256 ); | 
|  | 4581 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4582 |  | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4583 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) | 
|  | 4584 | { | 
|  | 4585 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); | 
|  | 4586 | goto exit; | 
|  | 4587 | } | 
|  | 4588 |  | 
|  | 4589 | *olen = 32; | 
|  | 4590 |  | 
|  | 4591 | exit: | 
|  | 4592 |  | 
|  | 4593 | mbedtls_sha256_free( &sha256 ); | 
|  | 4594 | return( ret ); | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4595 | } | 
|  | 4596 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 4597 |  | 
| Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4598 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, | 
|  | 4599 | const mbedtls_md_type_t md, | 
|  | 4600 | unsigned char *dst, | 
|  | 4601 | size_t dst_len, | 
|  | 4602 | size_t *olen ) | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4603 | { | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4604 | switch( md ) | 
|  | 4605 | { | 
|  | 4606 |  | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4607 | #if defined(MBEDTLS_SHA384_C) | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4608 | case MBEDTLS_MD_SHA384: | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4609 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); | 
| Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4610 | #endif /* MBEDTLS_SHA384_C */ | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4611 |  | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4612 | #if defined(MBEDTLS_SHA256_C) | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4613 | case MBEDTLS_MD_SHA256: | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4614 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); | 
| Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4615 | #endif /* MBEDTLS_SHA256_C */ | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4616 |  | 
|  | 4617 | default: | 
|  | 4618 | break; | 
|  | 4619 | } | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4620 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4621 | } | 
| XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 4622 |  | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4623 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4624 |  | 
| Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 4625 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \ | 
|  | 4626 | defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
|  | 4627 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4628 | /* | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4629 | * Function for writing a supported groups (TLS 1.3) or supported elliptic | 
|  | 4630 | * curves (TLS 1.2) extension. | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4631 | * | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4632 | * The "extension_data" field of a supported groups extension contains a | 
|  | 4633 | * "NamedGroupList" value (TLS 1.3 RFC8446): | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4634 | *      enum { | 
|  | 4635 | *          secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019), | 
|  | 4636 | *          x25519(0x001D), x448(0x001E), | 
|  | 4637 | *          ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102), | 
|  | 4638 | *          ffdhe6144(0x0103), ffdhe8192(0x0104), | 
|  | 4639 | *          ffdhe_private_use(0x01FC..0x01FF), | 
|  | 4640 | *          ecdhe_private_use(0xFE00..0xFEFF), | 
|  | 4641 | *          (0xFFFF) | 
|  | 4642 | *      } NamedGroup; | 
|  | 4643 | *      struct { | 
|  | 4644 | *          NamedGroup named_group_list<2..2^16-1>; | 
|  | 4645 | *      } NamedGroupList; | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4646 | * | 
|  | 4647 | * The "extension_data" field of a supported elliptic curves extension contains | 
|  | 4648 | * a "NamedCurveList" value (TLS 1.2 RFC 8422): | 
| Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4649 | * enum { | 
|  | 4650 | *      deprecated(1..22), | 
|  | 4651 | *      secp256r1 (23), secp384r1 (24), secp521r1 (25), | 
|  | 4652 | *      x25519(29), x448(30), | 
|  | 4653 | *      reserved (0xFE00..0xFEFF), | 
|  | 4654 | *      deprecated(0xFF01..0xFF02), | 
|  | 4655 | *      (0xFFFF) | 
|  | 4656 | *  } NamedCurve; | 
|  | 4657 | * struct { | 
|  | 4658 | *      NamedCurve named_curve_list<2..2^16-1> | 
|  | 4659 | *  } NamedCurveList; | 
|  | 4660 | * | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4661 | * The TLS 1.3 supported groups extension was defined to be a compatible | 
|  | 4662 | * generalization of the TLS 1.2 supported elliptic curves extension. They both | 
|  | 4663 | * share the same extension identifier. | 
| Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4664 | * | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4665 | * DHE groups are not supported yet. | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4666 | */ | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4667 | int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, | 
|  | 4668 | unsigned char *buf, | 
| Jerry Yu | 1753261 | 2021-12-20 22:32:09 +0800 | [diff] [blame] | 4669 | const unsigned char *end, | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4670 | size_t *out_len ) | 
|  | 4671 | { | 
|  | 4672 | unsigned char *p = buf ; | 
|  | 4673 | unsigned char *named_group_list; /* Start of named_group_list */ | 
|  | 4674 | size_t named_group_list_len;     /* Length of named_group_list */ | 
|  | 4675 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); | 
|  | 4676 |  | 
|  | 4677 | *out_len = 0; | 
| Jerry Yu | f46b016 | 2022-01-11 16:28:00 +0800 | [diff] [blame] | 4678 |  | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4679 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) ); | 
|  | 4680 |  | 
|  | 4681 | /* Check if we have space for header and length fields: | 
|  | 4682 | * - extension_type            (2 bytes) | 
|  | 4683 | * - extension_data_length     (2 bytes) | 
|  | 4684 | * - named_group_list_length   (2 bytes) | 
|  | 4685 | */ | 
|  | 4686 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); | 
|  | 4687 | p += 6; | 
|  | 4688 |  | 
|  | 4689 | named_group_list = p; | 
|  | 4690 |  | 
|  | 4691 | if( group_list == NULL ) | 
|  | 4692 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 4693 |  | 
| Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4694 | for( ; *group_list != 0; group_list++ ) | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4695 | { | 
| Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4696 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) ); | 
| Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4697 |  | 
| Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 4698 | #if defined(MBEDTLS_ECP_C) | 
| Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4699 | if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) && | 
| Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4700 | mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) || | 
| Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4701 | ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) && | 
| Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4702 | mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) ) | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4703 | { | 
| Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4704 | const mbedtls_ecp_curve_info *curve_info; | 
|  | 4705 | curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list ); | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4706 | if( curve_info == NULL ) | 
| Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4707 | continue; | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4708 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
|  | 4709 | MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 ); | 
|  | 4710 | p += 2; | 
|  | 4711 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )", | 
|  | 4712 | curve_info->name, *group_list ) ); | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4713 | } | 
| Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4714 | #endif /* MBEDTLS_ECP_C */ | 
|  | 4715 | /* Add DHE groups here */ | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4716 |  | 
|  | 4717 | } | 
|  | 4718 |  | 
| Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4719 | /* Length of named_group_list */ | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4720 | named_group_list_len = p - named_group_list; | 
|  | 4721 | if( named_group_list_len == 0 ) | 
|  | 4722 | { | 
|  | 4723 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) ); | 
|  | 4724 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4725 | } | 
|  | 4726 |  | 
|  | 4727 | /* Write extension_type */ | 
|  | 4728 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 ); | 
|  | 4729 | /* Write extension_data_length */ | 
|  | 4730 | MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 ); | 
|  | 4731 | /* Write length of named_group_list */ | 
|  | 4732 | MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 ); | 
|  | 4733 |  | 
| Jerry Yu | 7f029d8 | 2022-01-11 11:08:53 +0800 | [diff] [blame] | 4734 | MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", | 
|  | 4735 | buf + 4, named_group_list_len + 2 ); | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4736 |  | 
|  | 4737 | *out_len = p - buf; | 
|  | 4738 |  | 
|  | 4739 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 4740 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; | 
|  | 4741 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 4742 |  | 
|  | 4743 | return( 0 ); | 
|  | 4744 | } | 
|  | 4745 |  | 
| Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 4746 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED || | 
|  | 4747 | MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
|  | 4748 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4749 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4750 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 4751 | /* | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4752 | * Function for writing a signature algorithm extension. | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4753 | * | 
| Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 4754 | * The `extension_data` field of signature algorithm contains  a `SignatureSchemeList` | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4755 | * value (TLS 1.3 RFC8446): | 
|  | 4756 | *      enum { | 
|  | 4757 | *         .... | 
|  | 4758 | *        ecdsa_secp256r1_sha256( 0x0403 ), | 
|  | 4759 | *        ecdsa_secp384r1_sha384( 0x0503 ), | 
|  | 4760 | *        ecdsa_secp521r1_sha512( 0x0603 ), | 
|  | 4761 | *         .... | 
|  | 4762 | *      } SignatureScheme; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4763 | * | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4764 | *      struct { | 
|  | 4765 | *         SignatureScheme supported_signature_algorithms<2..2^16-2>; | 
|  | 4766 | *      } SignatureSchemeList; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4767 | * | 
| Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 4768 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` | 
|  | 4769 | * value (TLS 1.2 RFC5246): | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4770 | *      enum { | 
|  | 4771 | *          none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), | 
|  | 4772 | *          sha512(6), (255) | 
|  | 4773 | *      } HashAlgorithm; | 
|  | 4774 | * | 
|  | 4775 | *      enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } | 
|  | 4776 | *        SignatureAlgorithm; | 
|  | 4777 | * | 
|  | 4778 | *      struct { | 
|  | 4779 | *          HashAlgorithm hash; | 
|  | 4780 | *          SignatureAlgorithm signature; | 
|  | 4781 | *      } SignatureAndHashAlgorithm; | 
|  | 4782 | * | 
|  | 4783 | *      SignatureAndHashAlgorithm | 
|  | 4784 | *        supported_signature_algorithms<2..2^16-2>; | 
|  | 4785 | * | 
|  | 4786 | * The TLS 1.3 signature algorithm extension was defined to be a compatible | 
|  | 4787 | * generalization of the TLS 1.2 signature algorithm extension. | 
|  | 4788 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by | 
|  | 4789 | * `SignatureScheme` field of TLS 1.3 | 
|  | 4790 | * | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4791 | */ | 
|  | 4792 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, | 
|  | 4793 | const unsigned char *end, size_t *out_len ) | 
|  | 4794 | { | 
|  | 4795 | unsigned char *p = buf; | 
|  | 4796 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ | 
|  | 4797 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ | 
|  | 4798 |  | 
|  | 4799 | *out_len = 0; | 
|  | 4800 |  | 
|  | 4801 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); | 
|  | 4802 |  | 
|  | 4803 | /* Check if we have space for header and length field: | 
|  | 4804 | * - extension_type         (2 bytes) | 
|  | 4805 | * - extension_data_length  (2 bytes) | 
|  | 4806 | * - supported_signature_algorithms_length   (2 bytes) | 
|  | 4807 | */ | 
|  | 4808 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); | 
|  | 4809 | p += 6; | 
|  | 4810 |  | 
|  | 4811 | /* | 
|  | 4812 | * Write supported_signature_algorithms | 
|  | 4813 | */ | 
|  | 4814 | supported_sig_alg = p; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4815 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); | 
|  | 4816 | if( sig_alg == NULL ) | 
|  | 4817 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 4818 |  | 
|  | 4819 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4820 | { | 
| Jerry Yu | 1bab301 | 2022-01-19 17:43:22 +0800 | [diff] [blame] | 4821 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) | 
|  | 4822 | continue; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4823 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
|  | 4824 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); | 
|  | 4825 | p += 2; | 
|  | 4826 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) ); | 
|  | 4827 | } | 
|  | 4828 |  | 
|  | 4829 | /* Length of supported_signature_algorithms */ | 
|  | 4830 | supported_sig_alg_len = p - supported_sig_alg; | 
|  | 4831 | if( supported_sig_alg_len == 0 ) | 
|  | 4832 | { | 
|  | 4833 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); | 
|  | 4834 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4835 | } | 
|  | 4836 |  | 
|  | 4837 | /* Write extension_type */ | 
|  | 4838 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); | 
|  | 4839 | /* Write extension_data_length */ | 
|  | 4840 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); | 
|  | 4841 | /* Write length of supported_signature_algorithms */ | 
|  | 4842 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); | 
|  | 4843 |  | 
|  | 4844 | /* Output the total length of signature algorithms extension. */ | 
|  | 4845 | *out_len = p - buf; | 
|  | 4846 |  | 
|  | 4847 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 4848 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; | 
|  | 4849 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 4850 | return( 0 ); | 
|  | 4851 | } | 
|  | 4852 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 4853 |  | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4854 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 4855 | int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl, | 
|  | 4856 | unsigned char *buf, | 
|  | 4857 | const unsigned char *end, | 
|  | 4858 | size_t *olen ) | 
|  | 4859 | { | 
|  | 4860 | unsigned char *p = buf; | 
|  | 4861 | size_t hostname_len; | 
|  | 4862 |  | 
|  | 4863 | *olen = 0; | 
|  | 4864 |  | 
|  | 4865 | if( ssl->hostname == NULL ) | 
|  | 4866 | return( 0 ); | 
|  | 4867 |  | 
|  | 4868 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 4869 | ( "client hello, adding server name extension: %s", | 
|  | 4870 | ssl->hostname ) ); | 
|  | 4871 |  | 
|  | 4872 | hostname_len = strlen( ssl->hostname ); | 
|  | 4873 |  | 
|  | 4874 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 ); | 
|  | 4875 |  | 
|  | 4876 | /* | 
|  | 4877 | * Sect. 3, RFC 6066 (TLS Extensions Definitions) | 
|  | 4878 | * | 
|  | 4879 | * In order to provide any of the server names, clients MAY include an | 
|  | 4880 | * extension of type "server_name" in the (extended) client hello. The | 
|  | 4881 | * "extension_data" field of this extension SHALL contain | 
|  | 4882 | * "ServerNameList" where: | 
|  | 4883 | * | 
|  | 4884 | * struct { | 
|  | 4885 | *     NameType name_type; | 
|  | 4886 | *     select (name_type) { | 
|  | 4887 | *         case host_name: HostName; | 
|  | 4888 | *     } name; | 
|  | 4889 | * } ServerName; | 
|  | 4890 | * | 
|  | 4891 | * enum { | 
|  | 4892 | *     host_name(0), (255) | 
|  | 4893 | * } NameType; | 
|  | 4894 | * | 
|  | 4895 | * opaque HostName<1..2^16-1>; | 
|  | 4896 | * | 
|  | 4897 | * struct { | 
|  | 4898 | *     ServerName server_name_list<1..2^16-1> | 
|  | 4899 | * } ServerNameList; | 
|  | 4900 | * | 
|  | 4901 | */ | 
|  | 4902 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 ); | 
|  | 4903 | p += 2; | 
|  | 4904 |  | 
|  | 4905 | MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 ); | 
|  | 4906 | p += 2; | 
|  | 4907 |  | 
|  | 4908 | MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 ); | 
|  | 4909 | p += 2; | 
|  | 4910 |  | 
|  | 4911 | *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); | 
|  | 4912 |  | 
|  | 4913 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); | 
|  | 4914 | p += 2; | 
|  | 4915 |  | 
|  | 4916 | memcpy( p, ssl->hostname, hostname_len ); | 
|  | 4917 |  | 
|  | 4918 | *olen = hostname_len + 9; | 
|  | 4919 |  | 
|  | 4920 | return( 0 ); | 
|  | 4921 | } | 
|  | 4922 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4923 |  | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4924 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4925 |  | 
|  | 4926 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 4927 |  | 
|  | 4928 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, | 
|  | 4929 | mbedtls_svc_key_id_t key, | 
|  | 4930 | psa_algorithm_t alg, | 
|  | 4931 | const unsigned char* seed, size_t seed_length, | 
|  | 4932 | const unsigned char* label, size_t label_length, | 
|  | 4933 | size_t capacity ) | 
|  | 4934 | { | 
|  | 4935 | psa_status_t status; | 
|  | 4936 |  | 
|  | 4937 | status = psa_key_derivation_setup( derivation, alg ); | 
|  | 4938 | if( status != PSA_SUCCESS ) | 
|  | 4939 | return( status ); | 
|  | 4940 |  | 
|  | 4941 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) | 
|  | 4942 | { | 
|  | 4943 | status = psa_key_derivation_input_bytes( derivation, | 
|  | 4944 | PSA_KEY_DERIVATION_INPUT_SEED, | 
|  | 4945 | seed, seed_length ); | 
|  | 4946 | if( status != PSA_SUCCESS ) | 
|  | 4947 | return( status ); | 
|  | 4948 |  | 
|  | 4949 | if( mbedtls_svc_key_id_is_null( key ) ) | 
|  | 4950 | { | 
|  | 4951 | status = psa_key_derivation_input_bytes( | 
|  | 4952 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, | 
|  | 4953 | NULL, 0 ); | 
|  | 4954 | } | 
|  | 4955 | else | 
|  | 4956 | { | 
|  | 4957 | status = psa_key_derivation_input_key( | 
|  | 4958 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); | 
|  | 4959 | } | 
|  | 4960 | if( status != PSA_SUCCESS ) | 
|  | 4961 | return( status ); | 
|  | 4962 |  | 
|  | 4963 | status = psa_key_derivation_input_bytes( derivation, | 
|  | 4964 | PSA_KEY_DERIVATION_INPUT_LABEL, | 
|  | 4965 | label, label_length ); | 
|  | 4966 | if( status != PSA_SUCCESS ) | 
|  | 4967 | return( status ); | 
|  | 4968 | } | 
|  | 4969 | else | 
|  | 4970 | { | 
|  | 4971 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 4972 | } | 
|  | 4973 |  | 
|  | 4974 | status = psa_key_derivation_set_capacity( derivation, capacity ); | 
|  | 4975 | if( status != PSA_SUCCESS ) | 
|  | 4976 | return( status ); | 
|  | 4977 |  | 
|  | 4978 | return( PSA_SUCCESS ); | 
|  | 4979 | } | 
|  | 4980 |  | 
|  | 4981 | static int tls_prf_generic( mbedtls_md_type_t md_type, | 
|  | 4982 | const unsigned char *secret, size_t slen, | 
|  | 4983 | const char *label, | 
|  | 4984 | const unsigned char *random, size_t rlen, | 
|  | 4985 | unsigned char *dstbuf, size_t dlen ) | 
|  | 4986 | { | 
|  | 4987 | psa_status_t status; | 
|  | 4988 | psa_algorithm_t alg; | 
|  | 4989 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 4990 | psa_key_derivation_operation_t derivation = | 
|  | 4991 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 4992 |  | 
|  | 4993 | if( md_type == MBEDTLS_MD_SHA384 ) | 
|  | 4994 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); | 
|  | 4995 | else | 
|  | 4996 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); | 
|  | 4997 |  | 
|  | 4998 | /* Normally a "secret" should be long enough to be impossible to | 
|  | 4999 | * find by brute force, and in particular should not be empty. But | 
|  | 5000 | * this PRF is also used to derive an IV, in particular in EAP-TLS, | 
|  | 5001 | * and for this use case it makes sense to have a 0-length "secret". | 
|  | 5002 | * Since the key API doesn't allow importing a key of length 0, | 
|  | 5003 | * keep master_key=0, which setup_psa_key_derivation() understands | 
|  | 5004 | * to mean a 0-length "secret" input. */ | 
|  | 5005 | if( slen != 0 ) | 
|  | 5006 | { | 
|  | 5007 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); | 
|  | 5008 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); | 
|  | 5009 | psa_set_key_algorithm( &key_attributes, alg ); | 
|  | 5010 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); | 
|  | 5011 |  | 
|  | 5012 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); | 
|  | 5013 | if( status != PSA_SUCCESS ) | 
|  | 5014 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5015 | } | 
|  | 5016 |  | 
|  | 5017 | status = setup_psa_key_derivation( &derivation, | 
|  | 5018 | master_key, alg, | 
|  | 5019 | random, rlen, | 
|  | 5020 | (unsigned char const *) label, | 
|  | 5021 | (size_t) strlen( label ), | 
|  | 5022 | dlen ); | 
|  | 5023 | if( status != PSA_SUCCESS ) | 
|  | 5024 | { | 
|  | 5025 | psa_key_derivation_abort( &derivation ); | 
|  | 5026 | psa_destroy_key( master_key ); | 
|  | 5027 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5028 | } | 
|  | 5029 |  | 
|  | 5030 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); | 
|  | 5031 | if( status != PSA_SUCCESS ) | 
|  | 5032 | { | 
|  | 5033 | psa_key_derivation_abort( &derivation ); | 
|  | 5034 | psa_destroy_key( master_key ); | 
|  | 5035 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5036 | } | 
|  | 5037 |  | 
|  | 5038 | status = psa_key_derivation_abort( &derivation ); | 
|  | 5039 | if( status != PSA_SUCCESS ) | 
|  | 5040 | { | 
|  | 5041 | psa_destroy_key( master_key ); | 
|  | 5042 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5043 | } | 
|  | 5044 |  | 
|  | 5045 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) | 
|  | 5046 | status = psa_destroy_key( master_key ); | 
|  | 5047 | if( status != PSA_SUCCESS ) | 
|  | 5048 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5049 |  | 
|  | 5050 | return( 0 ); | 
|  | 5051 | } | 
|  | 5052 |  | 
|  | 5053 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 5054 |  | 
|  | 5055 | static int tls_prf_generic( mbedtls_md_type_t md_type, | 
|  | 5056 | const unsigned char *secret, size_t slen, | 
|  | 5057 | const char *label, | 
|  | 5058 | const unsigned char *random, size_t rlen, | 
|  | 5059 | unsigned char *dstbuf, size_t dlen ) | 
|  | 5060 | { | 
|  | 5061 | size_t nb; | 
|  | 5062 | size_t i, j, k, md_len; | 
|  | 5063 | unsigned char *tmp; | 
|  | 5064 | size_t tmp_len = 0; | 
|  | 5065 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; | 
|  | 5066 | const mbedtls_md_info_t *md_info; | 
|  | 5067 | mbedtls_md_context_t md_ctx; | 
|  | 5068 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5069 |  | 
|  | 5070 | mbedtls_md_init( &md_ctx ); | 
|  | 5071 |  | 
|  | 5072 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) | 
|  | 5073 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5074 |  | 
|  | 5075 | md_len = mbedtls_md_get_size( md_info ); | 
|  | 5076 |  | 
|  | 5077 | tmp_len = md_len + strlen( label ) + rlen; | 
|  | 5078 | tmp = mbedtls_calloc( 1, tmp_len ); | 
|  | 5079 | if( tmp == NULL ) | 
|  | 5080 | { | 
|  | 5081 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 5082 | goto exit; | 
|  | 5083 | } | 
|  | 5084 |  | 
|  | 5085 | nb = strlen( label ); | 
|  | 5086 | memcpy( tmp + md_len, label, nb ); | 
|  | 5087 | memcpy( tmp + md_len + nb, random, rlen ); | 
|  | 5088 | nb += rlen; | 
|  | 5089 |  | 
|  | 5090 | /* | 
|  | 5091 | * Compute P_<hash>(secret, label + random)[0..dlen] | 
|  | 5092 | */ | 
|  | 5093 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) | 
|  | 5094 | goto exit; | 
|  | 5095 |  | 
|  | 5096 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); | 
|  | 5097 | if( ret != 0 ) | 
|  | 5098 | goto exit; | 
|  | 5099 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); | 
|  | 5100 | if( ret != 0 ) | 
|  | 5101 | goto exit; | 
|  | 5102 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
|  | 5103 | if( ret != 0 ) | 
|  | 5104 | goto exit; | 
|  | 5105 |  | 
|  | 5106 | for( i = 0; i < dlen; i += md_len ) | 
|  | 5107 | { | 
|  | 5108 | ret = mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 5109 | if( ret != 0 ) | 
|  | 5110 | goto exit; | 
|  | 5111 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); | 
|  | 5112 | if( ret != 0 ) | 
|  | 5113 | goto exit; | 
|  | 5114 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); | 
|  | 5115 | if( ret != 0 ) | 
|  | 5116 | goto exit; | 
|  | 5117 |  | 
|  | 5118 | ret = mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 5119 | if( ret != 0 ) | 
|  | 5120 | goto exit; | 
|  | 5121 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); | 
|  | 5122 | if( ret != 0 ) | 
|  | 5123 | goto exit; | 
|  | 5124 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
|  | 5125 | if( ret != 0 ) | 
|  | 5126 | goto exit; | 
|  | 5127 |  | 
|  | 5128 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; | 
|  | 5129 |  | 
|  | 5130 | for( j = 0; j < k; j++ ) | 
|  | 5131 | dstbuf[i + j]  = h_i[j]; | 
|  | 5132 | } | 
|  | 5133 |  | 
|  | 5134 | exit: | 
|  | 5135 | mbedtls_md_free( &md_ctx ); | 
|  | 5136 |  | 
|  | 5137 | mbedtls_platform_zeroize( tmp, tmp_len ); | 
|  | 5138 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); | 
|  | 5139 |  | 
|  | 5140 | mbedtls_free( tmp ); | 
|  | 5141 |  | 
|  | 5142 | return( ret ); | 
|  | 5143 | } | 
|  | 5144 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 5145 |  | 
|  | 5146 | #if defined(MBEDTLS_SHA256_C) | 
|  | 5147 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, | 
|  | 5148 | const char *label, | 
|  | 5149 | const unsigned char *random, size_t rlen, | 
|  | 5150 | unsigned char *dstbuf, size_t dlen ) | 
|  | 5151 | { | 
|  | 5152 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, | 
|  | 5153 | label, random, rlen, dstbuf, dlen ) ); | 
|  | 5154 | } | 
|  | 5155 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 5156 |  | 
|  | 5157 | #if defined(MBEDTLS_SHA384_C) | 
|  | 5158 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, | 
|  | 5159 | const char *label, | 
|  | 5160 | const unsigned char *random, size_t rlen, | 
|  | 5161 | unsigned char *dstbuf, size_t dlen ) | 
|  | 5162 | { | 
|  | 5163 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, | 
|  | 5164 | label, random, rlen, dstbuf, dlen ) ); | 
|  | 5165 | } | 
|  | 5166 | #endif /* MBEDTLS_SHA384_C */ | 
|  | 5167 |  | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5168 | /* | 
|  | 5169 | * Set appropriate PRF function and other SSL / TLS1.2 functions | 
|  | 5170 | * | 
|  | 5171 | * Inputs: | 
|  | 5172 | * - SSL/TLS minor version | 
|  | 5173 | * - hash associated with the ciphersuite (only used by TLS 1.2) | 
|  | 5174 | * | 
|  | 5175 | * Outputs: | 
|  | 5176 | * - the tls_prf, calc_verify and calc_finished members of handshake structure | 
|  | 5177 | */ | 
|  | 5178 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, | 
|  | 5179 | int minor_ver, | 
|  | 5180 | mbedtls_md_type_t hash ) | 
|  | 5181 | { | 
|  | 5182 |  | 
|  | 5183 | #if defined(MBEDTLS_SHA384_C) | 
|  | 5184 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && | 
|  | 5185 | hash == MBEDTLS_MD_SHA384 ) | 
|  | 5186 | { | 
|  | 5187 | handshake->tls_prf = tls_prf_sha384; | 
|  | 5188 | handshake->calc_verify = ssl_calc_verify_tls_sha384; | 
|  | 5189 | handshake->calc_finished = ssl_calc_finished_tls_sha384; | 
|  | 5190 | } | 
|  | 5191 | else | 
|  | 5192 | #endif | 
|  | 5193 | #if defined(MBEDTLS_SHA256_C) | 
|  | 5194 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 5195 | { | 
|  | 5196 | handshake->tls_prf = tls_prf_sha256; | 
|  | 5197 | handshake->calc_verify = ssl_calc_verify_tls_sha256; | 
|  | 5198 | handshake->calc_finished = ssl_calc_finished_tls_sha256; | 
|  | 5199 | } | 
|  | 5200 | else | 
|  | 5201 | #endif | 
|  | 5202 | { | 
|  | 5203 | (void) hash; | 
|  | 5204 | (void) minor_ver; | 
|  | 5205 | (void) handshake; | 
|  | 5206 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5207 | } | 
|  | 5208 |  | 
|  | 5209 | return( 0 ); | 
|  | 5210 | } | 
| Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5211 |  | 
|  | 5212 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ | 
|  | 5213 | defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 5214 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) | 
|  | 5215 | { | 
|  | 5216 | if( ssl->conf->f_psk != NULL ) | 
|  | 5217 | { | 
|  | 5218 | /* If we've used a callback to select the PSK, | 
|  | 5219 | * the static configuration is irrelevant. */ | 
|  | 5220 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) | 
|  | 5221 | return( 1 ); | 
|  | 5222 |  | 
|  | 5223 | return( 0 ); | 
|  | 5224 | } | 
|  | 5225 |  | 
|  | 5226 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) | 
|  | 5227 | return( 1 ); | 
|  | 5228 |  | 
|  | 5229 | return( 0 ); | 
|  | 5230 | } | 
|  | 5231 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
|  | 5232 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
|  | 5233 |  | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5234 | /* | 
|  | 5235 | * Compute master secret if needed | 
|  | 5236 | * | 
|  | 5237 | * Parameters: | 
|  | 5238 | * [in/out] handshake | 
|  | 5239 | *          [in] resume, premaster, extended_ms, calc_verify, tls_prf | 
|  | 5240 | *               (PSA-PSK) ciphersuite_info, psk_opaque | 
|  | 5241 | *          [out] premaster (cleared) | 
|  | 5242 | * [out] master | 
|  | 5243 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK | 
|  | 5244 | *      debug: conf->f_dbg, conf->p_dbg | 
|  | 5245 | *      EMS: passed to calc_verify (debug + session_negotiate) | 
|  | 5246 | *      PSA-PSA: minor_ver, conf | 
|  | 5247 | */ | 
|  | 5248 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, | 
|  | 5249 | unsigned char *master, | 
|  | 5250 | const mbedtls_ssl_context *ssl ) | 
|  | 5251 | { | 
|  | 5252 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5253 |  | 
|  | 5254 | /* cf. RFC 5246, Section 8.1: | 
|  | 5255 | * "The master secret is always exactly 48 bytes in length." */ | 
|  | 5256 | size_t const master_secret_len = 48; | 
|  | 5257 |  | 
|  | 5258 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 5259 | unsigned char session_hash[48]; | 
|  | 5260 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
|  | 5261 |  | 
|  | 5262 | /* The label for the KDF used for key expansion. | 
|  | 5263 | * This is either "master secret" or "extended master secret" | 
|  | 5264 | * depending on whether the Extended Master Secret extension | 
|  | 5265 | * is used. */ | 
|  | 5266 | char const *lbl = "master secret"; | 
|  | 5267 |  | 
|  | 5268 | /* The salt for the KDF used for key expansion. | 
|  | 5269 | * - If the Extended Master Secret extension is not used, | 
|  | 5270 | *   this is ClientHello.Random + ServerHello.Random | 
|  | 5271 | *   (see Sect. 8.1 in RFC 5246). | 
|  | 5272 | * - If the Extended Master Secret extension is used, | 
|  | 5273 | *   this is the transcript of the handshake so far. | 
|  | 5274 | *   (see Sect. 4 in RFC 7627). */ | 
|  | 5275 | unsigned char const *salt = handshake->randbytes; | 
|  | 5276 | size_t salt_len = 64; | 
|  | 5277 |  | 
|  | 5278 | #if !defined(MBEDTLS_DEBUG_C) &&                    \ | 
|  | 5279 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ | 
|  | 5280 | !(defined(MBEDTLS_USE_PSA_CRYPTO) &&            \ | 
|  | 5281 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) | 
|  | 5282 | ssl = NULL; /* make sure we don't use it except for those cases */ | 
|  | 5283 | (void) ssl; | 
|  | 5284 | #endif | 
|  | 5285 |  | 
|  | 5286 | if( handshake->resume != 0 ) | 
|  | 5287 | { | 
|  | 5288 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); | 
|  | 5289 | return( 0 ); | 
|  | 5290 | } | 
|  | 5291 |  | 
|  | 5292 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 5293 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) | 
|  | 5294 | { | 
|  | 5295 | lbl  = "extended master secret"; | 
|  | 5296 | salt = session_hash; | 
|  | 5297 | handshake->calc_verify( ssl, session_hash, &salt_len ); | 
|  | 5298 |  | 
|  | 5299 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", | 
|  | 5300 | session_hash, salt_len ); | 
|  | 5301 | } | 
|  | 5302 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ | 
|  | 5303 |  | 
|  | 5304 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&          \ | 
|  | 5305 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 5306 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && | 
|  | 5307 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && | 
|  | 5308 | ssl_use_opaque_psk( ssl ) == 1 ) | 
|  | 5309 | { | 
|  | 5310 | /* Perform PSK-to-MS expansion in a single step. */ | 
|  | 5311 | psa_status_t status; | 
|  | 5312 | psa_algorithm_t alg; | 
|  | 5313 | mbedtls_svc_key_id_t psk; | 
|  | 5314 | psa_key_derivation_operation_t derivation = | 
|  | 5315 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 5316 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; | 
|  | 5317 |  | 
|  | 5318 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); | 
|  | 5319 |  | 
|  | 5320 | psk = mbedtls_ssl_get_opaque_psk( ssl ); | 
|  | 5321 |  | 
|  | 5322 | if( hash_alg == MBEDTLS_MD_SHA384 ) | 
|  | 5323 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); | 
|  | 5324 | else | 
|  | 5325 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); | 
|  | 5326 |  | 
|  | 5327 | status = setup_psa_key_derivation( &derivation, psk, alg, | 
|  | 5328 | salt, salt_len, | 
|  | 5329 | (unsigned char const *) lbl, | 
|  | 5330 | (size_t) strlen( lbl ), | 
|  | 5331 | master_secret_len ); | 
|  | 5332 | if( status != PSA_SUCCESS ) | 
|  | 5333 | { | 
|  | 5334 | psa_key_derivation_abort( &derivation ); | 
|  | 5335 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5336 | } | 
|  | 5337 |  | 
|  | 5338 | status = psa_key_derivation_output_bytes( &derivation, | 
|  | 5339 | master, | 
|  | 5340 | master_secret_len ); | 
|  | 5341 | if( status != PSA_SUCCESS ) | 
|  | 5342 | { | 
|  | 5343 | psa_key_derivation_abort( &derivation ); | 
|  | 5344 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5345 | } | 
|  | 5346 |  | 
|  | 5347 | status = psa_key_derivation_abort( &derivation ); | 
|  | 5348 | if( status != PSA_SUCCESS ) | 
|  | 5349 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5350 | } | 
|  | 5351 | else | 
|  | 5352 | #endif | 
|  | 5353 | { | 
|  | 5354 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, | 
|  | 5355 | lbl, salt, salt_len, | 
|  | 5356 | master, | 
|  | 5357 | master_secret_len ); | 
|  | 5358 | if( ret != 0 ) | 
|  | 5359 | { | 
|  | 5360 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
|  | 5361 | return( ret ); | 
|  | 5362 | } | 
|  | 5363 |  | 
|  | 5364 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", | 
|  | 5365 | handshake->premaster, | 
|  | 5366 | handshake->pmslen ); | 
|  | 5367 |  | 
|  | 5368 | mbedtls_platform_zeroize( handshake->premaster, | 
|  | 5369 | sizeof(handshake->premaster) ); | 
|  | 5370 | } | 
|  | 5371 |  | 
|  | 5372 | return( 0 ); | 
|  | 5373 | } | 
|  | 5374 |  | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5375 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) | 
|  | 5376 | { | 
|  | 5377 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5378 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = | 
|  | 5379 | ssl->handshake->ciphersuite_info; | 
|  | 5380 |  | 
|  | 5381 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); | 
|  | 5382 |  | 
|  | 5383 | /* Set PRF, calc_verify and calc_finished function pointers */ | 
|  | 5384 | ret = ssl_set_handshake_prfs( ssl->handshake, | 
|  | 5385 | ssl->minor_ver, | 
|  | 5386 | ciphersuite_info->mac ); | 
|  | 5387 | if( ret != 0 ) | 
|  | 5388 | { | 
|  | 5389 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); | 
|  | 5390 | return( ret ); | 
|  | 5391 | } | 
|  | 5392 |  | 
|  | 5393 | /* Compute master secret if needed */ | 
|  | 5394 | ret = ssl_compute_master( ssl->handshake, | 
|  | 5395 | ssl->session_negotiate->master, | 
|  | 5396 | ssl ); | 
|  | 5397 | if( ret != 0 ) | 
|  | 5398 | { | 
|  | 5399 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); | 
|  | 5400 | return( ret ); | 
|  | 5401 | } | 
|  | 5402 |  | 
|  | 5403 | /* Swap the client and server random values: | 
|  | 5404 | * - MS derivation wanted client+server (RFC 5246 8.1) | 
|  | 5405 | * - key derivation wants server+client (RFC 5246 6.3) */ | 
|  | 5406 | { | 
|  | 5407 | unsigned char tmp[64]; | 
|  | 5408 | memcpy( tmp, ssl->handshake->randbytes, 64 ); | 
|  | 5409 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); | 
|  | 5410 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); | 
|  | 5411 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
|  | 5412 | } | 
|  | 5413 |  | 
|  | 5414 | /* Populate transform structure */ | 
|  | 5415 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, | 
|  | 5416 | ssl->session_negotiate->ciphersuite, | 
|  | 5417 | ssl->session_negotiate->master, | 
|  | 5418 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ | 
|  | 5419 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 5420 | ssl->session_negotiate->encrypt_then_mac, | 
|  | 5421 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && | 
|  | 5422 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 5423 | ssl->handshake->tls_prf, | 
|  | 5424 | ssl->handshake->randbytes, | 
|  | 5425 | ssl->minor_ver, | 
|  | 5426 | ssl->conf->endpoint, | 
|  | 5427 | ssl ); | 
|  | 5428 | if( ret != 0 ) | 
|  | 5429 | { | 
|  | 5430 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); | 
|  | 5431 | return( ret ); | 
|  | 5432 | } | 
|  | 5433 |  | 
|  | 5434 | /* We no longer need Server/ClientHello.random values */ | 
|  | 5435 | mbedtls_platform_zeroize( ssl->handshake->randbytes, | 
|  | 5436 | sizeof( ssl->handshake->randbytes ) ); | 
|  | 5437 |  | 
|  | 5438 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); | 
|  | 5439 |  | 
|  | 5440 | return( 0 ); | 
|  | 5441 | } | 
| Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5442 |  | 
|  | 5443 | #if defined(MBEDTLS_SHA256_C) | 
|  | 5444 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, | 
|  | 5445 | unsigned char *hash, | 
|  | 5446 | size_t *hlen ) | 
|  | 5447 | { | 
|  | 5448 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 5449 | size_t hash_size; | 
|  | 5450 | psa_status_t status; | 
|  | 5451 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); | 
|  | 5452 |  | 
|  | 5453 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); | 
|  | 5454 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); | 
|  | 5455 | if( status != PSA_SUCCESS ) | 
|  | 5456 | { | 
|  | 5457 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 5458 | return; | 
|  | 5459 | } | 
|  | 5460 |  | 
|  | 5461 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); | 
|  | 5462 | if( status != PSA_SUCCESS ) | 
|  | 5463 | { | 
|  | 5464 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 5465 | return; | 
|  | 5466 | } | 
|  | 5467 |  | 
|  | 5468 | *hlen = 32; | 
|  | 5469 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); | 
|  | 5470 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); | 
|  | 5471 | #else | 
|  | 5472 | mbedtls_sha256_context sha256; | 
|  | 5473 |  | 
|  | 5474 | mbedtls_sha256_init( &sha256 ); | 
|  | 5475 |  | 
|  | 5476 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); | 
|  | 5477 |  | 
|  | 5478 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
|  | 5479 | mbedtls_sha256_finish( &sha256, hash ); | 
|  | 5480 |  | 
|  | 5481 | *hlen = 32; | 
|  | 5482 |  | 
|  | 5483 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
|  | 5484 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
|  | 5485 |  | 
|  | 5486 | mbedtls_sha256_free( &sha256 ); | 
|  | 5487 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 5488 | return; | 
|  | 5489 | } | 
|  | 5490 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 5491 |  | 
| Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5492 | #if defined(MBEDTLS_SHA384_C) | 
|  | 5493 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, | 
|  | 5494 | unsigned char *hash, | 
|  | 5495 | size_t *hlen ) | 
|  | 5496 | { | 
|  | 5497 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 5498 | size_t hash_size; | 
|  | 5499 | psa_status_t status; | 
|  | 5500 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); | 
|  | 5501 |  | 
|  | 5502 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); | 
|  | 5503 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); | 
|  | 5504 | if( status != PSA_SUCCESS ) | 
|  | 5505 | { | 
|  | 5506 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 5507 | return; | 
|  | 5508 | } | 
|  | 5509 |  | 
|  | 5510 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); | 
|  | 5511 | if( status != PSA_SUCCESS ) | 
|  | 5512 | { | 
|  | 5513 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 5514 | return; | 
|  | 5515 | } | 
|  | 5516 |  | 
|  | 5517 | *hlen = 48; | 
|  | 5518 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); | 
|  | 5519 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); | 
|  | 5520 | #else | 
|  | 5521 | mbedtls_sha512_context sha512; | 
|  | 5522 |  | 
|  | 5523 | mbedtls_sha512_init( &sha512 ); | 
|  | 5524 |  | 
|  | 5525 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); | 
|  | 5526 |  | 
|  | 5527 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
|  | 5528 | mbedtls_sha512_finish( &sha512, hash ); | 
|  | 5529 |  | 
|  | 5530 | *hlen = 48; | 
|  | 5531 |  | 
|  | 5532 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
|  | 5533 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
|  | 5534 |  | 
|  | 5535 | mbedtls_sha512_free( &sha512 ); | 
|  | 5536 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 5537 | return; | 
|  | 5538 | } | 
|  | 5539 | #endif /* MBEDTLS_SHA384_C */ | 
|  | 5540 |  | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5541 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
|  | 5542 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) | 
|  | 5543 | { | 
|  | 5544 | unsigned char *p = ssl->handshake->premaster; | 
|  | 5545 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); | 
|  | 5546 | const unsigned char *psk = NULL; | 
|  | 5547 | size_t psk_len = 0; | 
|  | 5548 |  | 
|  | 5549 | if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) | 
|  | 5550 | == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) | 
|  | 5551 | { | 
|  | 5552 | /* | 
|  | 5553 | * This should never happen because the existence of a PSK is always | 
|  | 5554 | * checked before calling this function | 
|  | 5555 | */ | 
|  | 5556 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5557 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5558 | } | 
|  | 5559 |  | 
|  | 5560 | /* | 
|  | 5561 | * PMS = struct { | 
|  | 5562 | *     opaque other_secret<0..2^16-1>; | 
|  | 5563 | *     opaque psk<0..2^16-1>; | 
|  | 5564 | * }; | 
|  | 5565 | * with "other_secret" depending on the particular key exchange | 
|  | 5566 | */ | 
|  | 5567 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 5568 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) | 
|  | 5569 | { | 
|  | 5570 | if( end - p < 2 ) | 
|  | 5571 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5572 |  | 
|  | 5573 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); | 
|  | 5574 | p += 2; | 
|  | 5575 |  | 
|  | 5576 | if( end < p || (size_t)( end - p ) < psk_len ) | 
|  | 5577 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5578 |  | 
|  | 5579 | memset( p, 0, psk_len ); | 
|  | 5580 | p += psk_len; | 
|  | 5581 | } | 
|  | 5582 | else | 
|  | 5583 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
|  | 5584 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 5585 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
|  | 5586 | { | 
|  | 5587 | /* | 
|  | 5588 | * other_secret already set by the ClientKeyExchange message, | 
|  | 5589 | * and is 48 bytes long | 
|  | 5590 | */ | 
|  | 5591 | if( end - p < 2 ) | 
|  | 5592 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5593 |  | 
|  | 5594 | *p++ = 0; | 
|  | 5595 | *p++ = 48; | 
|  | 5596 | p += 48; | 
|  | 5597 | } | 
|  | 5598 | else | 
|  | 5599 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
|  | 5600 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 5601 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
|  | 5602 | { | 
|  | 5603 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5604 | size_t len; | 
|  | 5605 |  | 
|  | 5606 | /* Write length only when we know the actual value */ | 
|  | 5607 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | 
|  | 5608 | p + 2, end - ( p + 2 ), &len, | 
|  | 5609 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
|  | 5610 | { | 
|  | 5611 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
|  | 5612 | return( ret ); | 
|  | 5613 | } | 
|  | 5614 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); | 
|  | 5615 | p += 2 + len; | 
|  | 5616 |  | 
|  | 5617 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K  ); | 
|  | 5618 | } | 
|  | 5619 | else | 
|  | 5620 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
|  | 5621 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 5622 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
|  | 5623 | { | 
|  | 5624 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5625 | size_t zlen; | 
|  | 5626 |  | 
|  | 5627 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, | 
|  | 5628 | p + 2, end - ( p + 2 ), | 
|  | 5629 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
|  | 5630 | { | 
|  | 5631 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
|  | 5632 | return( ret ); | 
|  | 5633 | } | 
|  | 5634 |  | 
|  | 5635 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); | 
|  | 5636 | p += 2 + zlen; | 
|  | 5637 |  | 
|  | 5638 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 5639 | MBEDTLS_DEBUG_ECDH_Z ); | 
|  | 5640 | } | 
|  | 5641 | else | 
|  | 5642 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
|  | 5643 | { | 
|  | 5644 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5645 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5646 | } | 
|  | 5647 |  | 
|  | 5648 | /* opaque psk<0..2^16-1>; */ | 
|  | 5649 | if( end - p < 2 ) | 
|  | 5650 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5651 |  | 
|  | 5652 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); | 
|  | 5653 | p += 2; | 
|  | 5654 |  | 
|  | 5655 | if( end < p || (size_t)( end - p ) < psk_len ) | 
|  | 5656 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5657 |  | 
|  | 5658 | memcpy( p, psk, psk_len ); | 
|  | 5659 | p += psk_len; | 
|  | 5660 |  | 
|  | 5661 | ssl->handshake->pmslen = p - ssl->handshake->premaster; | 
|  | 5662 |  | 
|  | 5663 | return( 0 ); | 
|  | 5664 | } | 
|  | 5665 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 5666 |  | 
|  | 5667 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 5668 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); | 
|  | 5669 |  | 
|  | 5670 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5671 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) | 
|  | 5672 | { | 
|  | 5673 | /* If renegotiation is not enforced, retransmit until we would reach max | 
|  | 5674 | * timeout if we were using the usual handshake doubling scheme */ | 
|  | 5675 | if( ssl->conf->renego_max_records < 0 ) | 
|  | 5676 | { | 
|  | 5677 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; | 
|  | 5678 | unsigned char doublings = 1; | 
|  | 5679 |  | 
|  | 5680 | while( ratio != 0 ) | 
|  | 5681 | { | 
|  | 5682 | ++doublings; | 
|  | 5683 | ratio >>= 1; | 
|  | 5684 | } | 
|  | 5685 |  | 
|  | 5686 | if( ++ssl->renego_records_seen > doublings ) | 
|  | 5687 | { | 
|  | 5688 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); | 
|  | 5689 | return( 0 ); | 
|  | 5690 | } | 
|  | 5691 | } | 
|  | 5692 |  | 
|  | 5693 | return( ssl_write_hello_request( ssl ) ); | 
|  | 5694 | } | 
|  | 5695 | #endif | 
|  | 5696 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5697 |  | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5698 | /* | 
|  | 5699 | * Handshake functions | 
|  | 5700 | */ | 
|  | 5701 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 5702 | /* No certificate support -> dummy functions */ | 
|  | 5703 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | 
|  | 5704 | { | 
|  | 5705 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 5706 | ssl->handshake->ciphersuite_info; | 
|  | 5707 |  | 
|  | 5708 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | 
|  | 5709 |  | 
|  | 5710 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
|  | 5711 | { | 
|  | 5712 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
|  | 5713 | ssl->state++; | 
|  | 5714 | return( 0 ); | 
|  | 5715 | } | 
|  | 5716 |  | 
|  | 5717 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5718 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5719 | } | 
|  | 5720 |  | 
|  | 5721 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | 
|  | 5722 | { | 
|  | 5723 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 5724 | ssl->handshake->ciphersuite_info; | 
|  | 5725 |  | 
|  | 5726 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | 
|  | 5727 |  | 
|  | 5728 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
|  | 5729 | { | 
|  | 5730 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
|  | 5731 | ssl->state++; | 
|  | 5732 | return( 0 ); | 
|  | 5733 | } | 
|  | 5734 |  | 
|  | 5735 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5736 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5737 | } | 
|  | 5738 |  | 
|  | 5739 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 5740 | /* Some certificate support -> implement write and parse */ | 
|  | 5741 |  | 
|  | 5742 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | 
|  | 5743 | { | 
|  | 5744 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
|  | 5745 | size_t i, n; | 
|  | 5746 | const mbedtls_x509_crt *crt; | 
|  | 5747 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 5748 | ssl->handshake->ciphersuite_info; | 
|  | 5749 |  | 
|  | 5750 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | 
|  | 5751 |  | 
|  | 5752 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
|  | 5753 | { | 
|  | 5754 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
|  | 5755 | ssl->state++; | 
|  | 5756 | return( 0 ); | 
|  | 5757 | } | 
|  | 5758 |  | 
|  | 5759 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 5760 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 5761 | { | 
|  | 5762 | if( ssl->handshake->client_auth == 0 ) | 
|  | 5763 | { | 
|  | 5764 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
|  | 5765 | ssl->state++; | 
|  | 5766 | return( 0 ); | 
|  | 5767 | } | 
|  | 5768 | } | 
|  | 5769 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 5770 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 5771 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 5772 | { | 
|  | 5773 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) | 
|  | 5774 | { | 
|  | 5775 | /* Should never happen because we shouldn't have picked the | 
|  | 5776 | * ciphersuite if we don't have a certificate. */ | 
|  | 5777 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5778 | } | 
|  | 5779 | } | 
|  | 5780 | #endif | 
|  | 5781 |  | 
|  | 5782 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); | 
|  | 5783 |  | 
|  | 5784 | /* | 
|  | 5785 | *     0  .  0    handshake type | 
|  | 5786 | *     1  .  3    handshake length | 
|  | 5787 | *     4  .  6    length of all certs | 
|  | 5788 | *     7  .  9    length of cert. 1 | 
|  | 5789 | *    10  . n-1   peer certificate | 
|  | 5790 | *     n  . n+2   length of cert. 2 | 
|  | 5791 | *    n+3 . ...   upper level cert, etc. | 
|  | 5792 | */ | 
|  | 5793 | i = 7; | 
|  | 5794 | crt = mbedtls_ssl_own_cert( ssl ); | 
|  | 5795 |  | 
|  | 5796 | while( crt != NULL ) | 
|  | 5797 | { | 
|  | 5798 | n = crt->raw.len; | 
|  | 5799 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) | 
|  | 5800 | { | 
|  | 5801 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET | 
|  | 5802 | " > %" MBEDTLS_PRINTF_SIZET, | 
|  | 5803 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); | 
|  | 5804 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 5805 | } | 
|  | 5806 |  | 
|  | 5807 | ssl->out_msg[i    ] = MBEDTLS_BYTE_2( n ); | 
|  | 5808 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); | 
|  | 5809 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); | 
|  | 5810 |  | 
|  | 5811 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); | 
|  | 5812 | i += n; crt = crt->next; | 
|  | 5813 | } | 
|  | 5814 |  | 
|  | 5815 | ssl->out_msg[4]  = MBEDTLS_BYTE_2( i - 7 ); | 
|  | 5816 | ssl->out_msg[5]  = MBEDTLS_BYTE_1( i - 7 ); | 
|  | 5817 | ssl->out_msg[6]  = MBEDTLS_BYTE_0( i - 7 ); | 
|  | 5818 |  | 
|  | 5819 | ssl->out_msglen  = i; | 
|  | 5820 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 5821 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE; | 
|  | 5822 |  | 
|  | 5823 | ssl->state++; | 
|  | 5824 |  | 
|  | 5825 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
|  | 5826 | { | 
|  | 5827 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
|  | 5828 | return( ret ); | 
|  | 5829 | } | 
|  | 5830 |  | 
|  | 5831 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); | 
|  | 5832 |  | 
|  | 5833 | return( ret ); | 
|  | 5834 | } | 
|  | 5835 |  | 
|  | 5836 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 5837 |  | 
|  | 5838 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 5839 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, | 
|  | 5840 | unsigned char *crt_buf, | 
|  | 5841 | size_t crt_buf_len ) | 
|  | 5842 | { | 
|  | 5843 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; | 
|  | 5844 |  | 
|  | 5845 | if( peer_crt == NULL ) | 
|  | 5846 | return( -1 ); | 
|  | 5847 |  | 
|  | 5848 | if( peer_crt->raw.len != crt_buf_len ) | 
|  | 5849 | return( -1 ); | 
|  | 5850 |  | 
|  | 5851 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); | 
|  | 5852 | } | 
|  | 5853 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 5854 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, | 
|  | 5855 | unsigned char *crt_buf, | 
|  | 5856 | size_t crt_buf_len ) | 
|  | 5857 | { | 
|  | 5858 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5859 | unsigned char const * const peer_cert_digest = | 
|  | 5860 | ssl->session->peer_cert_digest; | 
|  | 5861 | mbedtls_md_type_t const peer_cert_digest_type = | 
|  | 5862 | ssl->session->peer_cert_digest_type; | 
|  | 5863 | mbedtls_md_info_t const * const digest_info = | 
|  | 5864 | mbedtls_md_info_from_type( peer_cert_digest_type ); | 
|  | 5865 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; | 
|  | 5866 | size_t digest_len; | 
|  | 5867 |  | 
|  | 5868 | if( peer_cert_digest == NULL || digest_info == NULL ) | 
|  | 5869 | return( -1 ); | 
|  | 5870 |  | 
|  | 5871 | digest_len = mbedtls_md_get_size( digest_info ); | 
|  | 5872 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) | 
|  | 5873 | return( -1 ); | 
|  | 5874 |  | 
|  | 5875 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); | 
|  | 5876 | if( ret != 0 ) | 
|  | 5877 | return( -1 ); | 
|  | 5878 |  | 
|  | 5879 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); | 
|  | 5880 | } | 
|  | 5881 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 5882 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
|  | 5883 |  | 
|  | 5884 | /* | 
|  | 5885 | * Once the certificate message is read, parse it into a cert chain and | 
|  | 5886 | * perform basic checks, but leave actual verification to the caller | 
|  | 5887 | */ | 
|  | 5888 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, | 
|  | 5889 | mbedtls_x509_crt *chain ) | 
|  | 5890 | { | 
|  | 5891 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5892 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 5893 | int crt_cnt=0; | 
|  | 5894 | #endif | 
|  | 5895 | size_t i, n; | 
|  | 5896 | uint8_t alert; | 
|  | 5897 |  | 
|  | 5898 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
|  | 5899 | { | 
|  | 5900 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 5901 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5902 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 5903 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
|  | 5904 | } | 
|  | 5905 |  | 
|  | 5906 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE  ) | 
|  | 5907 | { | 
|  | 5908 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5909 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 5910 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
|  | 5911 | } | 
|  | 5912 |  | 
|  | 5913 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) | 
|  | 5914 | { | 
|  | 5915 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 5916 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5917 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 5918 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | 
|  | 5919 | } | 
|  | 5920 |  | 
|  | 5921 | i = mbedtls_ssl_hs_hdr_len( ssl ); | 
|  | 5922 |  | 
|  | 5923 | /* | 
|  | 5924 | * Same message structure as in mbedtls_ssl_write_certificate() | 
|  | 5925 | */ | 
|  | 5926 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; | 
|  | 5927 |  | 
|  | 5928 | if( ssl->in_msg[i] != 0 || | 
|  | 5929 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) | 
|  | 5930 | { | 
|  | 5931 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 5932 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5933 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 5934 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | 
|  | 5935 | } | 
|  | 5936 |  | 
|  | 5937 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ | 
|  | 5938 | i += 3; | 
|  | 5939 |  | 
|  | 5940 | /* Iterate through and parse the CRTs in the provided chain. */ | 
|  | 5941 | while( i < ssl->in_hslen ) | 
|  | 5942 | { | 
|  | 5943 | /* Check that there's room for the next CRT's length fields. */ | 
|  | 5944 | if ( i + 3 > ssl->in_hslen ) { | 
|  | 5945 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 5946 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 5947 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5948 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 5949 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | 
|  | 5950 | } | 
|  | 5951 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support | 
|  | 5952 | * anything beyond 2**16 ~ 64K. */ | 
|  | 5953 | if( ssl->in_msg[i] != 0 ) | 
|  | 5954 | { | 
|  | 5955 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 5956 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 5957 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5958 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); | 
|  | 5959 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); | 
|  | 5960 | } | 
|  | 5961 |  | 
|  | 5962 | /* Read length of the next CRT in the chain. */ | 
|  | 5963 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) | 
|  | 5964 | | (unsigned int) ssl->in_msg[i + 2]; | 
|  | 5965 | i += 3; | 
|  | 5966 |  | 
|  | 5967 | if( n < 128 || i + n > ssl->in_hslen ) | 
|  | 5968 | { | 
|  | 5969 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 5970 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 5971 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5972 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 5973 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | 
|  | 5974 | } | 
|  | 5975 |  | 
|  | 5976 | /* Check if we're handling the first CRT in the chain. */ | 
|  | 5977 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 5978 | if( crt_cnt++ == 0 && | 
|  | 5979 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 5980 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
|  | 5981 | { | 
|  | 5982 | /* During client-side renegotiation, check that the server's | 
|  | 5983 | * end-CRTs hasn't changed compared to the initial handshake, | 
|  | 5984 | * mitigating the triple handshake attack. On success, reuse | 
|  | 5985 | * the original end-CRT instead of parsing it again. */ | 
|  | 5986 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); | 
|  | 5987 | if( ssl_check_peer_crt_unchanged( ssl, | 
|  | 5988 | &ssl->in_msg[i], | 
|  | 5989 | n ) != 0 ) | 
|  | 5990 | { | 
|  | 5991 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); | 
|  | 5992 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 5993 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5994 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); | 
|  | 5995 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); | 
|  | 5996 | } | 
|  | 5997 |  | 
|  | 5998 | /* Now we can safely free the original chain. */ | 
|  | 5999 | ssl_clear_peer_cert( ssl->session ); | 
|  | 6000 | } | 
|  | 6001 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
|  | 6002 |  | 
|  | 6003 | /* Parse the next certificate in the chain. */ | 
|  | 6004 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 6005 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); | 
|  | 6006 | #else | 
|  | 6007 | /* If we don't need to store the CRT chain permanently, parse | 
|  | 6008 | * it in-place from the input buffer instead of making a copy. */ | 
|  | 6009 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); | 
|  | 6010 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 6011 | switch( ret ) | 
|  | 6012 | { | 
|  | 6013 | case 0: /*ok*/ | 
|  | 6014 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: | 
|  | 6015 | /* Ignore certificate with an unknown algorithm: maybe a | 
|  | 6016 | prior certificate was already trusted. */ | 
|  | 6017 | break; | 
|  | 6018 |  | 
|  | 6019 | case MBEDTLS_ERR_X509_ALLOC_FAILED: | 
|  | 6020 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; | 
|  | 6021 | goto crt_parse_der_failed; | 
|  | 6022 |  | 
|  | 6023 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: | 
|  | 6024 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6025 | goto crt_parse_der_failed; | 
|  | 6026 |  | 
|  | 6027 | default: | 
|  | 6028 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
|  | 6029 | crt_parse_der_failed: | 
|  | 6030 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); | 
|  | 6031 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); | 
|  | 6032 | return( ret ); | 
|  | 6033 | } | 
|  | 6034 |  | 
|  | 6035 | i += n; | 
|  | 6036 | } | 
|  | 6037 |  | 
|  | 6038 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); | 
|  | 6039 | return( 0 ); | 
|  | 6040 | } | 
|  | 6041 |  | 
|  | 6042 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6043 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) | 
|  | 6044 | { | 
|  | 6045 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6046 | return( -1 ); | 
|  | 6047 |  | 
|  | 6048 | if( ssl->in_hslen   == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && | 
|  | 6049 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE    && | 
|  | 6050 | ssl->in_msg[0]  == MBEDTLS_SSL_HS_CERTIFICATE   && | 
|  | 6051 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) | 
|  | 6052 | { | 
|  | 6053 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); | 
|  | 6054 | return( 0 ); | 
|  | 6055 | } | 
|  | 6056 | return( -1 ); | 
|  | 6057 | } | 
|  | 6058 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 6059 |  | 
|  | 6060 | /* Check if a certificate message is expected. | 
|  | 6061 | * Return either | 
|  | 6062 | * - SSL_CERTIFICATE_EXPECTED, or | 
|  | 6063 | * - SSL_CERTIFICATE_SKIP | 
|  | 6064 | * indicating whether a Certificate message is expected or not. | 
|  | 6065 | */ | 
|  | 6066 | #define SSL_CERTIFICATE_EXPECTED 0 | 
|  | 6067 | #define SSL_CERTIFICATE_SKIP     1 | 
|  | 6068 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, | 
|  | 6069 | int authmode ) | 
|  | 6070 | { | 
|  | 6071 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 6072 | ssl->handshake->ciphersuite_info; | 
|  | 6073 |  | 
|  | 6074 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
|  | 6075 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 6076 |  | 
|  | 6077 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6078 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 6079 | { | 
|  | 6080 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
|  | 6081 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 6082 |  | 
|  | 6083 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
|  | 6084 | { | 
|  | 6085 | ssl->session_negotiate->verify_result = | 
|  | 6086 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; | 
|  | 6087 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 6088 | } | 
|  | 6089 | } | 
|  | 6090 | #else | 
|  | 6091 | ((void) authmode); | 
|  | 6092 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 6093 |  | 
|  | 6094 | return( SSL_CERTIFICATE_EXPECTED ); | 
|  | 6095 | } | 
|  | 6096 |  | 
|  | 6097 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, | 
|  | 6098 | int authmode, | 
|  | 6099 | mbedtls_x509_crt *chain, | 
|  | 6100 | void *rs_ctx ) | 
|  | 6101 | { | 
|  | 6102 | int ret = 0; | 
|  | 6103 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 6104 | ssl->handshake->ciphersuite_info; | 
|  | 6105 | int have_ca_chain = 0; | 
|  | 6106 |  | 
|  | 6107 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); | 
|  | 6108 | void *p_vrfy; | 
|  | 6109 |  | 
|  | 6110 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
|  | 6111 | return( 0 ); | 
|  | 6112 |  | 
|  | 6113 | if( ssl->f_vrfy != NULL ) | 
|  | 6114 | { | 
|  | 6115 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); | 
|  | 6116 | f_vrfy = ssl->f_vrfy; | 
|  | 6117 | p_vrfy = ssl->p_vrfy; | 
|  | 6118 | } | 
|  | 6119 | else | 
|  | 6120 | { | 
|  | 6121 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); | 
|  | 6122 | f_vrfy = ssl->conf->f_vrfy; | 
|  | 6123 | p_vrfy = ssl->conf->p_vrfy; | 
|  | 6124 | } | 
|  | 6125 |  | 
|  | 6126 | /* | 
|  | 6127 | * Main check: verify certificate | 
|  | 6128 | */ | 
|  | 6129 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
|  | 6130 | if( ssl->conf->f_ca_cb != NULL ) | 
|  | 6131 | { | 
|  | 6132 | ((void) rs_ctx); | 
|  | 6133 | have_ca_chain = 1; | 
|  | 6134 |  | 
|  | 6135 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); | 
|  | 6136 | ret = mbedtls_x509_crt_verify_with_ca_cb( | 
|  | 6137 | chain, | 
|  | 6138 | ssl->conf->f_ca_cb, | 
|  | 6139 | ssl->conf->p_ca_cb, | 
|  | 6140 | ssl->conf->cert_profile, | 
|  | 6141 | ssl->hostname, | 
|  | 6142 | &ssl->session_negotiate->verify_result, | 
|  | 6143 | f_vrfy, p_vrfy ); | 
|  | 6144 | } | 
|  | 6145 | else | 
|  | 6146 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
|  | 6147 | { | 
|  | 6148 | mbedtls_x509_crt *ca_chain; | 
|  | 6149 | mbedtls_x509_crl *ca_crl; | 
|  | 6150 |  | 
|  | 6151 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 6152 | if( ssl->handshake->sni_ca_chain != NULL ) | 
|  | 6153 | { | 
|  | 6154 | ca_chain = ssl->handshake->sni_ca_chain; | 
|  | 6155 | ca_crl   = ssl->handshake->sni_ca_crl; | 
|  | 6156 | } | 
|  | 6157 | else | 
|  | 6158 | #endif | 
|  | 6159 | { | 
|  | 6160 | ca_chain = ssl->conf->ca_chain; | 
|  | 6161 | ca_crl   = ssl->conf->ca_crl; | 
|  | 6162 | } | 
|  | 6163 |  | 
|  | 6164 | if( ca_chain != NULL ) | 
|  | 6165 | have_ca_chain = 1; | 
|  | 6166 |  | 
|  | 6167 | ret = mbedtls_x509_crt_verify_restartable( | 
|  | 6168 | chain, | 
|  | 6169 | ca_chain, ca_crl, | 
|  | 6170 | ssl->conf->cert_profile, | 
|  | 6171 | ssl->hostname, | 
|  | 6172 | &ssl->session_negotiate->verify_result, | 
|  | 6173 | f_vrfy, p_vrfy, rs_ctx ); | 
|  | 6174 | } | 
|  | 6175 |  | 
|  | 6176 | if( ret != 0 ) | 
|  | 6177 | { | 
|  | 6178 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); | 
|  | 6179 | } | 
|  | 6180 |  | 
|  | 6181 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
|  | 6182 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 6183 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); | 
|  | 6184 | #endif | 
|  | 6185 |  | 
|  | 6186 | /* | 
|  | 6187 | * Secondary checks: always done, but change 'ret' only if it was 0 | 
|  | 6188 | */ | 
|  | 6189 |  | 
|  | 6190 | #if defined(MBEDTLS_ECP_C) | 
|  | 6191 | { | 
|  | 6192 | const mbedtls_pk_context *pk = &chain->pk; | 
|  | 6193 |  | 
|  | 6194 | /* If certificate uses an EC key, make sure the curve is OK */ | 
|  | 6195 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && | 
|  | 6196 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) | 
|  | 6197 | { | 
|  | 6198 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; | 
|  | 6199 |  | 
|  | 6200 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); | 
|  | 6201 | if( ret == 0 ) | 
|  | 6202 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | 
|  | 6203 | } | 
|  | 6204 | } | 
|  | 6205 | #endif /* MBEDTLS_ECP_C */ | 
|  | 6206 |  | 
|  | 6207 | if( mbedtls_ssl_check_cert_usage( chain, | 
|  | 6208 | ciphersuite_info, | 
|  | 6209 | ! ssl->conf->endpoint, | 
|  | 6210 | &ssl->session_negotiate->verify_result ) != 0 ) | 
|  | 6211 | { | 
|  | 6212 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); | 
|  | 6213 | if( ret == 0 ) | 
|  | 6214 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | 
|  | 6215 | } | 
|  | 6216 |  | 
|  | 6217 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a | 
|  | 6218 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, | 
|  | 6219 | * with details encoded in the verification flags. All other kinds | 
|  | 6220 | * of error codes, including those from the user provided f_vrfy | 
|  | 6221 | * functions, are treated as fatal and lead to a failure of | 
|  | 6222 | * ssl_parse_certificate even if verification was optional. */ | 
|  | 6223 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && | 
|  | 6224 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || | 
|  | 6225 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) | 
|  | 6226 | { | 
|  | 6227 | ret = 0; | 
|  | 6228 | } | 
|  | 6229 |  | 
|  | 6230 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) | 
|  | 6231 | { | 
|  | 6232 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); | 
|  | 6233 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; | 
|  | 6234 | } | 
|  | 6235 |  | 
|  | 6236 | if( ret != 0 ) | 
|  | 6237 | { | 
|  | 6238 | uint8_t alert; | 
|  | 6239 |  | 
|  | 6240 | /* The certificate may have been rejected for several reasons. | 
|  | 6241 | Pick one and send the corresponding alert. Which alert to send | 
|  | 6242 | may be a subject of debate in some cases. */ | 
|  | 6243 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) | 
|  | 6244 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; | 
|  | 6245 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) | 
|  | 6246 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
|  | 6247 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) | 
|  | 6248 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6249 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) | 
|  | 6250 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6251 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) | 
|  | 6252 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6253 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) | 
|  | 6254 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6255 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) | 
|  | 6256 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6257 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) | 
|  | 6258 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; | 
|  | 6259 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) | 
|  | 6260 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; | 
|  | 6261 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) | 
|  | 6262 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; | 
|  | 6263 | else | 
|  | 6264 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; | 
|  | 6265 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6266 | alert ); | 
|  | 6267 | } | 
|  | 6268 |  | 
|  | 6269 | #if defined(MBEDTLS_DEBUG_C) | 
|  | 6270 | if( ssl->session_negotiate->verify_result != 0 ) | 
|  | 6271 | { | 
|  | 6272 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", | 
|  | 6273 | (unsigned int) ssl->session_negotiate->verify_result ) ); | 
|  | 6274 | } | 
|  | 6275 | else | 
|  | 6276 | { | 
|  | 6277 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); | 
|  | 6278 | } | 
|  | 6279 | #endif /* MBEDTLS_DEBUG_C */ | 
|  | 6280 |  | 
|  | 6281 | return( ret ); | 
|  | 6282 | } | 
|  | 6283 |  | 
|  | 6284 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 6285 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, | 
|  | 6286 | unsigned char *start, size_t len ) | 
|  | 6287 | { | 
|  | 6288 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6289 | /* Remember digest of the peer's end-CRT. */ | 
|  | 6290 | ssl->session_negotiate->peer_cert_digest = | 
|  | 6291 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); | 
|  | 6292 | if( ssl->session_negotiate->peer_cert_digest == NULL ) | 
|  | 6293 | { | 
|  | 6294 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | 
|  | 6295 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); | 
|  | 6296 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 6297 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6298 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 6299 |  | 
|  | 6300 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 6301 | } | 
|  | 6302 |  | 
|  | 6303 | ret = mbedtls_md( mbedtls_md_info_from_type( | 
|  | 6304 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), | 
|  | 6305 | start, len, | 
|  | 6306 | ssl->session_negotiate->peer_cert_digest ); | 
|  | 6307 |  | 
|  | 6308 | ssl->session_negotiate->peer_cert_digest_type = | 
|  | 6309 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; | 
|  | 6310 | ssl->session_negotiate->peer_cert_digest_len = | 
|  | 6311 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; | 
|  | 6312 |  | 
|  | 6313 | return( ret ); | 
|  | 6314 | } | 
|  | 6315 |  | 
|  | 6316 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, | 
|  | 6317 | unsigned char *start, size_t len ) | 
|  | 6318 | { | 
|  | 6319 | unsigned char *end = start + len; | 
|  | 6320 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6321 |  | 
|  | 6322 | /* Make a copy of the peer's raw public key. */ | 
|  | 6323 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); | 
|  | 6324 | ret = mbedtls_pk_parse_subpubkey( &start, end, | 
|  | 6325 | &ssl->handshake->peer_pubkey ); | 
|  | 6326 | if( ret != 0 ) | 
|  | 6327 | { | 
|  | 6328 | /* We should have parsed the public key before. */ | 
|  | 6329 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 6330 | } | 
|  | 6331 |  | 
|  | 6332 | return( 0 ); | 
|  | 6333 | } | 
|  | 6334 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 6335 |  | 
|  | 6336 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | 
|  | 6337 | { | 
|  | 6338 | int ret = 0; | 
|  | 6339 | int crt_expected; | 
|  | 6340 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 6341 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET | 
|  | 6342 | ? ssl->handshake->sni_authmode | 
|  | 6343 | : ssl->conf->authmode; | 
|  | 6344 | #else | 
|  | 6345 | const int authmode = ssl->conf->authmode; | 
|  | 6346 | #endif | 
|  | 6347 | void *rs_ctx = NULL; | 
|  | 6348 | mbedtls_x509_crt *chain = NULL; | 
|  | 6349 |  | 
|  | 6350 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | 
|  | 6351 |  | 
|  | 6352 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); | 
|  | 6353 | if( crt_expected == SSL_CERTIFICATE_SKIP ) | 
|  | 6354 | { | 
|  | 6355 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
|  | 6356 | goto exit; | 
|  | 6357 | } | 
|  | 6358 |  | 
|  | 6359 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
|  | 6360 | if( ssl->handshake->ecrs_enabled && | 
|  | 6361 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) | 
|  | 6362 | { | 
|  | 6363 | chain = ssl->handshake->ecrs_peer_cert; | 
|  | 6364 | ssl->handshake->ecrs_peer_cert = NULL; | 
|  | 6365 | goto crt_verify; | 
|  | 6366 | } | 
|  | 6367 | #endif | 
|  | 6368 |  | 
|  | 6369 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
|  | 6370 | { | 
|  | 6371 | /* mbedtls_ssl_read_record may have sent an alert already. We | 
|  | 6372 | let it decide whether to alert. */ | 
|  | 6373 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 6374 | goto exit; | 
|  | 6375 | } | 
|  | 6376 |  | 
|  | 6377 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6378 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) | 
|  | 6379 | { | 
|  | 6380 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; | 
|  | 6381 |  | 
|  | 6382 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) | 
|  | 6383 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; | 
|  | 6384 |  | 
|  | 6385 | goto exit; | 
|  | 6386 | } | 
|  | 6387 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 6388 |  | 
|  | 6389 | /* Clear existing peer CRT structure in case we tried to | 
|  | 6390 | * reuse a session but it failed, and allocate a new one. */ | 
|  | 6391 | ssl_clear_peer_cert( ssl->session_negotiate ); | 
|  | 6392 |  | 
|  | 6393 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); | 
|  | 6394 | if( chain == NULL ) | 
|  | 6395 | { | 
|  | 6396 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", | 
|  | 6397 | sizeof( mbedtls_x509_crt ) ) ); | 
|  | 6398 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 6399 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6400 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 6401 |  | 
|  | 6402 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 6403 | goto exit; | 
|  | 6404 | } | 
|  | 6405 | mbedtls_x509_crt_init( chain ); | 
|  | 6406 |  | 
|  | 6407 | ret = ssl_parse_certificate_chain( ssl, chain ); | 
|  | 6408 | if( ret != 0 ) | 
|  | 6409 | goto exit; | 
|  | 6410 |  | 
|  | 6411 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
|  | 6412 | if( ssl->handshake->ecrs_enabled) | 
|  | 6413 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; | 
|  | 6414 |  | 
|  | 6415 | crt_verify: | 
|  | 6416 | if( ssl->handshake->ecrs_enabled) | 
|  | 6417 | rs_ctx = &ssl->handshake->ecrs_ctx; | 
|  | 6418 | #endif | 
|  | 6419 |  | 
|  | 6420 | ret = ssl_parse_certificate_verify( ssl, authmode, | 
|  | 6421 | chain, rs_ctx ); | 
|  | 6422 | if( ret != 0 ) | 
|  | 6423 | goto exit; | 
|  | 6424 |  | 
|  | 6425 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 6426 | { | 
|  | 6427 | unsigned char *crt_start, *pk_start; | 
|  | 6428 | size_t crt_len, pk_len; | 
|  | 6429 |  | 
|  | 6430 | /* We parse the CRT chain without copying, so | 
|  | 6431 | * these pointers point into the input buffer, | 
|  | 6432 | * and are hence still valid after freeing the | 
|  | 6433 | * CRT chain. */ | 
|  | 6434 |  | 
|  | 6435 | crt_start = chain->raw.p; | 
|  | 6436 | crt_len   = chain->raw.len; | 
|  | 6437 |  | 
|  | 6438 | pk_start = chain->pk_raw.p; | 
|  | 6439 | pk_len   = chain->pk_raw.len; | 
|  | 6440 |  | 
|  | 6441 | /* Free the CRT structures before computing | 
|  | 6442 | * digest and copying the peer's public key. */ | 
|  | 6443 | mbedtls_x509_crt_free( chain ); | 
|  | 6444 | mbedtls_free( chain ); | 
|  | 6445 | chain = NULL; | 
|  | 6446 |  | 
|  | 6447 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); | 
|  | 6448 | if( ret != 0 ) | 
|  | 6449 | goto exit; | 
|  | 6450 |  | 
|  | 6451 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); | 
|  | 6452 | if( ret != 0 ) | 
|  | 6453 | goto exit; | 
|  | 6454 | } | 
|  | 6455 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 6456 | /* Pass ownership to session structure. */ | 
|  | 6457 | ssl->session_negotiate->peer_cert = chain; | 
|  | 6458 | chain = NULL; | 
|  | 6459 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 6460 |  | 
|  | 6461 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); | 
|  | 6462 |  | 
|  | 6463 | exit: | 
|  | 6464 |  | 
|  | 6465 | if( ret == 0 ) | 
|  | 6466 | ssl->state++; | 
|  | 6467 |  | 
|  | 6468 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
|  | 6469 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) | 
|  | 6470 | { | 
|  | 6471 | ssl->handshake->ecrs_peer_cert = chain; | 
|  | 6472 | chain = NULL; | 
|  | 6473 | } | 
|  | 6474 | #endif | 
|  | 6475 |  | 
|  | 6476 | if( chain != NULL ) | 
|  | 6477 | { | 
|  | 6478 | mbedtls_x509_crt_free( chain ); | 
|  | 6479 | mbedtls_free( chain ); | 
|  | 6480 | } | 
|  | 6481 |  | 
|  | 6482 | return( ret ); | 
|  | 6483 | } | 
|  | 6484 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 6485 |  | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 6486 | #if defined(MBEDTLS_SHA256_C) | 
|  | 6487 | static void ssl_calc_finished_tls_sha256( | 
|  | 6488 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
|  | 6489 | { | 
|  | 6490 | int len = 12; | 
|  | 6491 | const char *sender; | 
|  | 6492 | unsigned char padbuf[32]; | 
|  | 6493 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6494 | size_t hash_size; | 
|  | 6495 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; | 
|  | 6496 | psa_status_t status; | 
|  | 6497 | #else | 
|  | 6498 | mbedtls_sha256_context sha256; | 
|  | 6499 | #endif | 
|  | 6500 |  | 
|  | 6501 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
|  | 6502 | if( !session ) | 
|  | 6503 | session = ssl->session; | 
|  | 6504 |  | 
|  | 6505 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6506 | ? "client finished" | 
|  | 6507 | : "server finished"; | 
|  | 6508 |  | 
|  | 6509 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6510 | sha256_psa = psa_hash_operation_init(); | 
|  | 6511 |  | 
|  | 6512 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); | 
|  | 6513 |  | 
|  | 6514 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); | 
|  | 6515 | if( status != PSA_SUCCESS ) | 
|  | 6516 | { | 
|  | 6517 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 6518 | return; | 
|  | 6519 | } | 
|  | 6520 |  | 
|  | 6521 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); | 
|  | 6522 | if( status != PSA_SUCCESS ) | 
|  | 6523 | { | 
|  | 6524 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 6525 | return; | 
|  | 6526 | } | 
|  | 6527 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); | 
|  | 6528 | #else | 
|  | 6529 |  | 
|  | 6530 | mbedtls_sha256_init( &sha256 ); | 
|  | 6531 |  | 
|  | 6532 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha256" ) ); | 
|  | 6533 |  | 
|  | 6534 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
|  | 6535 |  | 
|  | 6536 | /* | 
|  | 6537 | * TLSv1.2: | 
|  | 6538 | *   hash = PRF( master, finished_label, | 
|  | 6539 | *               Hash( handshake ) )[0.11] | 
|  | 6540 | */ | 
|  | 6541 |  | 
|  | 6542 | #if !defined(MBEDTLS_SHA256_ALT) | 
|  | 6543 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) | 
|  | 6544 | sha256.state, sizeof( sha256.state ) ); | 
|  | 6545 | #endif | 
|  | 6546 |  | 
|  | 6547 | mbedtls_sha256_finish( &sha256, padbuf ); | 
|  | 6548 | mbedtls_sha256_free( &sha256 ); | 
|  | 6549 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 6550 |  | 
|  | 6551 | ssl->handshake->tls_prf( session->master, 48, sender, | 
|  | 6552 | padbuf, 32, buf, len ); | 
|  | 6553 |  | 
|  | 6554 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
|  | 6555 |  | 
|  | 6556 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
|  | 6557 |  | 
|  | 6558 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
|  | 6559 | } | 
|  | 6560 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 6561 |  | 
| Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 6562 |  | 
|  | 6563 | #if defined(MBEDTLS_SHA384_C) | 
|  | 6564 |  | 
|  | 6565 | static void ssl_calc_finished_tls_sha384( | 
|  | 6566 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
|  | 6567 | { | 
|  | 6568 | int len = 12; | 
|  | 6569 | const char *sender; | 
|  | 6570 | unsigned char padbuf[48]; | 
|  | 6571 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6572 | size_t hash_size; | 
|  | 6573 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; | 
|  | 6574 | psa_status_t status; | 
|  | 6575 | #else | 
|  | 6576 | mbedtls_sha512_context sha512; | 
|  | 6577 | #endif | 
|  | 6578 |  | 
|  | 6579 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
|  | 6580 | if( !session ) | 
|  | 6581 | session = ssl->session; | 
|  | 6582 |  | 
|  | 6583 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6584 | ? "client finished" | 
|  | 6585 | : "server finished"; | 
|  | 6586 |  | 
|  | 6587 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6588 | sha384_psa = psa_hash_operation_init(); | 
|  | 6589 |  | 
|  | 6590 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); | 
|  | 6591 |  | 
|  | 6592 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); | 
|  | 6593 | if( status != PSA_SUCCESS ) | 
|  | 6594 | { | 
|  | 6595 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 6596 | return; | 
|  | 6597 | } | 
|  | 6598 |  | 
|  | 6599 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); | 
|  | 6600 | if( status != PSA_SUCCESS ) | 
|  | 6601 | { | 
|  | 6602 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 6603 | return; | 
|  | 6604 | } | 
|  | 6605 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); | 
|  | 6606 | #else | 
|  | 6607 | mbedtls_sha512_init( &sha512 ); | 
|  | 6608 |  | 
|  | 6609 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha384" ) ); | 
|  | 6610 |  | 
|  | 6611 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
|  | 6612 |  | 
|  | 6613 | /* | 
|  | 6614 | * TLSv1.2: | 
|  | 6615 | *   hash = PRF( master, finished_label, | 
|  | 6616 | *               Hash( handshake ) )[0.11] | 
|  | 6617 | */ | 
|  | 6618 |  | 
|  | 6619 | #if !defined(MBEDTLS_SHA512_ALT) | 
|  | 6620 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) | 
|  | 6621 | sha512.state, sizeof( sha512.state ) ); | 
|  | 6622 | #endif | 
|  | 6623 | mbedtls_sha512_finish( &sha512, padbuf ); | 
|  | 6624 |  | 
|  | 6625 | mbedtls_sha512_free( &sha512 ); | 
|  | 6626 | #endif | 
|  | 6627 |  | 
|  | 6628 | ssl->handshake->tls_prf( session->master, 48, sender, | 
|  | 6629 | padbuf, 48, buf, len ); | 
|  | 6630 |  | 
|  | 6631 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
|  | 6632 |  | 
|  | 6633 | mbedtls_platform_zeroize(  padbuf, sizeof( padbuf ) ); | 
|  | 6634 |  | 
|  | 6635 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
|  | 6636 | } | 
|  | 6637 | #endif /* MBEDTLS_SHA384_C */ | 
|  | 6638 |  | 
| Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 6639 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) | 
|  | 6640 | { | 
|  | 6641 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); | 
|  | 6642 |  | 
|  | 6643 | /* | 
|  | 6644 | * Free our handshake params | 
|  | 6645 | */ | 
|  | 6646 | mbedtls_ssl_handshake_free( ssl ); | 
|  | 6647 | mbedtls_free( ssl->handshake ); | 
|  | 6648 | ssl->handshake = NULL; | 
|  | 6649 |  | 
|  | 6650 | /* | 
|  | 6651 | * Free the previous transform and swith in the current one | 
|  | 6652 | */ | 
|  | 6653 | if( ssl->transform ) | 
|  | 6654 | { | 
|  | 6655 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 6656 | mbedtls_free( ssl->transform ); | 
|  | 6657 | } | 
|  | 6658 | ssl->transform = ssl->transform_negotiate; | 
|  | 6659 | ssl->transform_negotiate = NULL; | 
|  | 6660 |  | 
|  | 6661 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); | 
|  | 6662 | } | 
|  | 6663 |  | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 6664 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) | 
|  | 6665 | { | 
|  | 6666 | int resume = ssl->handshake->resume; | 
|  | 6667 |  | 
|  | 6668 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); | 
|  | 6669 |  | 
|  | 6670 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6671 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
|  | 6672 | { | 
|  | 6673 | ssl->renego_status =  MBEDTLS_SSL_RENEGOTIATION_DONE; | 
|  | 6674 | ssl->renego_records_seen = 0; | 
|  | 6675 | } | 
|  | 6676 | #endif | 
|  | 6677 |  | 
|  | 6678 | /* | 
|  | 6679 | * Free the previous session and switch in the current one | 
|  | 6680 | */ | 
|  | 6681 | if( ssl->session ) | 
|  | 6682 | { | 
|  | 6683 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 6684 | /* RFC 7366 3.1: keep the EtM state */ | 
|  | 6685 | ssl->session_negotiate->encrypt_then_mac = | 
|  | 6686 | ssl->session->encrypt_then_mac; | 
|  | 6687 | #endif | 
|  | 6688 |  | 
|  | 6689 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 6690 | mbedtls_free( ssl->session ); | 
|  | 6691 | } | 
|  | 6692 | ssl->session = ssl->session_negotiate; | 
|  | 6693 | ssl->session_negotiate = NULL; | 
|  | 6694 |  | 
|  | 6695 | /* | 
|  | 6696 | * Add cache entry | 
|  | 6697 | */ | 
|  | 6698 | if( ssl->conf->f_set_cache != NULL && | 
|  | 6699 | ssl->session->id_len != 0 && | 
|  | 6700 | resume == 0 ) | 
|  | 6701 | { | 
|  | 6702 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, | 
|  | 6703 | ssl->session->id, | 
|  | 6704 | ssl->session->id_len, | 
|  | 6705 | ssl->session ) != 0 ) | 
|  | 6706 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); | 
|  | 6707 | } | 
|  | 6708 |  | 
|  | 6709 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6710 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 6711 | ssl->handshake->flight != NULL ) | 
|  | 6712 | { | 
|  | 6713 | /* Cancel handshake timer */ | 
|  | 6714 | mbedtls_ssl_set_timer( ssl, 0 ); | 
|  | 6715 |  | 
|  | 6716 | /* Keep last flight around in case we need to resend it: | 
|  | 6717 | * we need the handshake and transform structures for that */ | 
|  | 6718 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); | 
|  | 6719 | } | 
|  | 6720 | else | 
|  | 6721 | #endif | 
|  | 6722 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); | 
|  | 6723 |  | 
|  | 6724 | ssl->state++; | 
|  | 6725 |  | 
|  | 6726 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); | 
|  | 6727 | } | 
|  | 6728 |  | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 6729 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) | 
|  | 6730 | { | 
|  | 6731 | int ret, hash_len; | 
|  | 6732 |  | 
|  | 6733 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); | 
|  | 6734 |  | 
|  | 6735 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); | 
|  | 6736 |  | 
|  | 6737 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); | 
|  | 6738 |  | 
|  | 6739 | /* | 
|  | 6740 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites | 
|  | 6741 | * may define some other value. Currently (early 2016), no defined | 
|  | 6742 | * ciphersuite does this (and this is unlikely to change as activity has | 
|  | 6743 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. | 
|  | 6744 | */ | 
|  | 6745 | hash_len = 12; | 
|  | 6746 |  | 
|  | 6747 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6748 | ssl->verify_data_len = hash_len; | 
|  | 6749 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); | 
|  | 6750 | #endif | 
|  | 6751 |  | 
|  | 6752 | ssl->out_msglen  = 4 + hash_len; | 
|  | 6753 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 6754 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_FINISHED; | 
|  | 6755 |  | 
|  | 6756 | /* | 
|  | 6757 | * In case of session resuming, invert the client and server | 
|  | 6758 | * ChangeCipherSpec messages order. | 
|  | 6759 | */ | 
|  | 6760 | if( ssl->handshake->resume != 0 ) | 
|  | 6761 | { | 
|  | 6762 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 6763 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6764 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
|  | 6765 | #endif | 
|  | 6766 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6767 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 6768 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
|  | 6769 | #endif | 
|  | 6770 | } | 
|  | 6771 | else | 
|  | 6772 | ssl->state++; | 
|  | 6773 |  | 
|  | 6774 | /* | 
|  | 6775 | * Switch to our negotiated transform and session parameters for outbound | 
|  | 6776 | * data. | 
|  | 6777 | */ | 
|  | 6778 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); | 
|  | 6779 |  | 
|  | 6780 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6781 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 6782 | { | 
|  | 6783 | unsigned char i; | 
|  | 6784 |  | 
|  | 6785 | /* Remember current epoch settings for resending */ | 
|  | 6786 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
|  | 6787 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, | 
|  | 6788 | sizeof( ssl->handshake->alt_out_ctr ) ); | 
|  | 6789 |  | 
|  | 6790 | /* Set sequence_number to zero */ | 
|  | 6791 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); | 
|  | 6792 |  | 
|  | 6793 |  | 
|  | 6794 | /* Increment epoch */ | 
|  | 6795 | for( i = 2; i > 0; i-- ) | 
|  | 6796 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) | 
|  | 6797 | break; | 
|  | 6798 |  | 
|  | 6799 | /* The loop goes to its end iff the counter is wrapping */ | 
|  | 6800 | if( i == 0 ) | 
|  | 6801 | { | 
|  | 6802 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); | 
|  | 6803 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
|  | 6804 | } | 
|  | 6805 | } | 
|  | 6806 | else | 
|  | 6807 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 6808 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); | 
|  | 6809 |  | 
|  | 6810 | ssl->transform_out = ssl->transform_negotiate; | 
|  | 6811 | ssl->session_out = ssl->session_negotiate; | 
|  | 6812 |  | 
|  | 6813 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6814 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 6815 | mbedtls_ssl_send_flight_completed( ssl ); | 
|  | 6816 | #endif | 
|  | 6817 |  | 
|  | 6818 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
|  | 6819 | { | 
|  | 6820 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
|  | 6821 | return( ret ); | 
|  | 6822 | } | 
|  | 6823 |  | 
|  | 6824 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6825 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 6826 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 6827 | { | 
|  | 6828 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 6829 | return( ret ); | 
|  | 6830 | } | 
|  | 6831 | #endif | 
|  | 6832 |  | 
|  | 6833 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); | 
|  | 6834 |  | 
|  | 6835 | return( 0 ); | 
|  | 6836 | } | 
|  | 6837 |  | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 6838 | #define SSL_MAX_HASH_LEN 12 | 
|  | 6839 |  | 
|  | 6840 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) | 
|  | 6841 | { | 
|  | 6842 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6843 | unsigned int hash_len = 12; | 
|  | 6844 | unsigned char buf[SSL_MAX_HASH_LEN]; | 
|  | 6845 |  | 
|  | 6846 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); | 
|  | 6847 |  | 
|  | 6848 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); | 
|  | 6849 |  | 
|  | 6850 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
|  | 6851 | { | 
|  | 6852 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 6853 | goto exit; | 
|  | 6854 | } | 
|  | 6855 |  | 
|  | 6856 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
|  | 6857 | { | 
|  | 6858 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
|  | 6859 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6860 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 6861 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | 
|  | 6862 | goto exit; | 
|  | 6863 | } | 
|  | 6864 |  | 
|  | 6865 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED  ) | 
|  | 6866 | { | 
|  | 6867 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6868 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 6869 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | 
|  | 6870 | goto exit; | 
|  | 6871 | } | 
|  | 6872 |  | 
|  | 6873 | if( ssl->in_hslen  != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) | 
|  | 6874 | { | 
|  | 6875 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
|  | 6876 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6877 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 6878 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; | 
|  | 6879 | goto exit; | 
|  | 6880 | } | 
|  | 6881 |  | 
|  | 6882 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), | 
|  | 6883 | buf, hash_len ) != 0 ) | 
|  | 6884 | { | 
|  | 6885 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
|  | 6886 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6887 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); | 
|  | 6888 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; | 
|  | 6889 | goto exit; | 
|  | 6890 | } | 
|  | 6891 |  | 
|  | 6892 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6893 | ssl->verify_data_len = hash_len; | 
|  | 6894 | memcpy( ssl->peer_verify_data, buf, hash_len ); | 
|  | 6895 | #endif | 
|  | 6896 |  | 
|  | 6897 | if( ssl->handshake->resume != 0 ) | 
|  | 6898 | { | 
|  | 6899 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 6900 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6901 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
|  | 6902 | #endif | 
|  | 6903 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6904 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 6905 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
|  | 6906 | #endif | 
|  | 6907 | } | 
|  | 6908 | else | 
|  | 6909 | ssl->state++; | 
|  | 6910 |  | 
|  | 6911 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6912 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 6913 | mbedtls_ssl_recv_flight_completed( ssl ); | 
|  | 6914 | #endif | 
|  | 6915 |  | 
|  | 6916 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); | 
|  | 6917 |  | 
|  | 6918 | exit: | 
|  | 6919 | mbedtls_platform_zeroize( buf, hash_len ); | 
|  | 6920 | return( ret ); | 
|  | 6921 | } | 
|  | 6922 |  | 
| Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 6923 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
|  | 6924 | /* | 
|  | 6925 | * Helper to get TLS 1.2 PRF from ciphersuite | 
|  | 6926 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) | 
|  | 6927 | */ | 
|  | 6928 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) | 
|  | 6929 | { | 
|  | 6930 | #if defined(MBEDTLS_SHA384_C) | 
|  | 6931 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = | 
|  | 6932 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); | 
|  | 6933 |  | 
|  | 6934 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) | 
|  | 6935 | return( tls_prf_sha384 ); | 
|  | 6936 | #else | 
|  | 6937 | (void) ciphersuite_id; | 
|  | 6938 | #endif | 
|  | 6939 | return( tls_prf_sha256 ); | 
|  | 6940 | } | 
|  | 6941 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
| Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 6942 |  | 
|  | 6943 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) | 
|  | 6944 | { | 
|  | 6945 | ((void) tls_prf); | 
|  | 6946 | #if defined(MBEDTLS_SHA384_C) | 
|  | 6947 | if( tls_prf == tls_prf_sha384 ) | 
|  | 6948 | { | 
|  | 6949 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); | 
|  | 6950 | } | 
|  | 6951 | else | 
|  | 6952 | #endif | 
|  | 6953 | #if defined(MBEDTLS_SHA256_C) | 
|  | 6954 | if( tls_prf == tls_prf_sha256 ) | 
|  | 6955 | { | 
|  | 6956 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); | 
|  | 6957 | } | 
|  | 6958 | else | 
|  | 6959 | #endif | 
|  | 6960 | return( MBEDTLS_SSL_TLS_PRF_NONE ); | 
|  | 6961 | } | 
|  | 6962 |  | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6963 | /* | 
|  | 6964 | * Populate a transform structure with session keys and all the other | 
|  | 6965 | * necessary information. | 
|  | 6966 | * | 
|  | 6967 | * Parameters: | 
|  | 6968 | * - [in/out]: transform: structure to populate | 
|  | 6969 | *      [in] must be just initialised with mbedtls_ssl_transform_init() | 
|  | 6970 | *      [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() | 
|  | 6971 | * - [in] ciphersuite | 
|  | 6972 | * - [in] master | 
|  | 6973 | * - [in] encrypt_then_mac | 
|  | 6974 | * - [in] compression | 
|  | 6975 | * - [in] tls_prf: pointer to PRF to use for key derivation | 
|  | 6976 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random | 
|  | 6977 | * - [in] minor_ver: SSL/TLS minor version | 
|  | 6978 | * - [in] endpoint: client or server | 
|  | 6979 | * - [in] ssl: used for: | 
|  | 6980 | *        - ssl->conf->{f,p}_export_keys | 
|  | 6981 | *      [in] optionally used for: | 
|  | 6982 | *        - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg | 
|  | 6983 | */ | 
|  | 6984 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, | 
|  | 6985 | int ciphersuite, | 
|  | 6986 | const unsigned char master[48], | 
|  | 6987 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ | 
|  | 6988 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 6989 | int encrypt_then_mac, | 
|  | 6990 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && | 
|  | 6991 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 6992 | ssl_tls_prf_t tls_prf, | 
|  | 6993 | const unsigned char randbytes[64], | 
|  | 6994 | int minor_ver, | 
|  | 6995 | unsigned endpoint, | 
|  | 6996 | const mbedtls_ssl_context *ssl ) | 
|  | 6997 | { | 
|  | 6998 | int ret = 0; | 
|  | 6999 | unsigned char keyblk[256]; | 
|  | 7000 | unsigned char *key1; | 
|  | 7001 | unsigned char *key2; | 
|  | 7002 | unsigned char *mac_enc; | 
|  | 7003 | unsigned char *mac_dec; | 
|  | 7004 | size_t mac_key_len = 0; | 
|  | 7005 | size_t iv_copy_len; | 
|  | 7006 | size_t keylen; | 
|  | 7007 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
|  | 7008 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 7009 | const mbedtls_md_info_t *md_info; | 
|  | 7010 |  | 
|  | 7011 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 7012 | psa_key_type_t key_type; | 
|  | 7013 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 7014 | psa_algorithm_t alg; | 
|  | 7015 | size_t key_bits; | 
|  | 7016 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 7017 | #endif | 
|  | 7018 |  | 
|  | 7019 | #if !defined(MBEDTLS_DEBUG_C) && \ | 
|  | 7020 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 7021 | if( ssl->f_export_keys == NULL ) | 
|  | 7022 | { | 
|  | 7023 | ssl = NULL; /* make sure we don't use it except for these cases */ | 
|  | 7024 | (void) ssl; | 
|  | 7025 | } | 
|  | 7026 | #endif | 
|  | 7027 |  | 
|  | 7028 | /* | 
|  | 7029 | * Some data just needs copying into the structure | 
|  | 7030 | */ | 
|  | 7031 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ | 
|  | 7032 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
|  | 7033 | transform->encrypt_then_mac = encrypt_then_mac; | 
|  | 7034 | #endif | 
|  | 7035 | transform->minor_ver = minor_ver; | 
|  | 7036 |  | 
|  | 7037 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
|  | 7038 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); | 
|  | 7039 | #endif | 
|  | 7040 |  | 
|  | 7041 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 7042 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) | 
|  | 7043 | { | 
|  | 7044 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform | 
|  | 7045 | * generation separate. This should never happen. */ | 
|  | 7046 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 7047 | } | 
|  | 7048 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 7049 |  | 
|  | 7050 | /* | 
|  | 7051 | * Get various info structures | 
|  | 7052 | */ | 
|  | 7053 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); | 
|  | 7054 | if( ciphersuite_info == NULL ) | 
|  | 7055 | { | 
|  | 7056 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", | 
|  | 7057 | ciphersuite ) ); | 
|  | 7058 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7059 | } | 
|  | 7060 |  | 
|  | 7061 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); | 
|  | 7062 | if( cipher_info == NULL ) | 
|  | 7063 | { | 
|  | 7064 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", | 
|  | 7065 | ciphersuite_info->cipher ) ); | 
|  | 7066 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7067 | } | 
|  | 7068 |  | 
|  | 7069 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); | 
|  | 7070 | if( md_info == NULL ) | 
|  | 7071 | { | 
|  | 7072 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", | 
|  | 7073 | (unsigned) ciphersuite_info->mac ) ); | 
|  | 7074 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7075 | } | 
|  | 7076 |  | 
|  | 7077 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 7078 | /* Copy own and peer's CID if the use of the CID | 
|  | 7079 | * extension has been negotiated. */ | 
|  | 7080 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) | 
|  | 7081 | { | 
|  | 7082 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); | 
|  | 7083 |  | 
|  | 7084 | transform->in_cid_len = ssl->own_cid_len; | 
|  | 7085 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); | 
|  | 7086 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, | 
|  | 7087 | transform->in_cid_len ); | 
|  | 7088 |  | 
|  | 7089 | transform->out_cid_len = ssl->handshake->peer_cid_len; | 
|  | 7090 | memcpy( transform->out_cid, ssl->handshake->peer_cid, | 
|  | 7091 | ssl->handshake->peer_cid_len ); | 
|  | 7092 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, | 
|  | 7093 | transform->out_cid_len ); | 
|  | 7094 | } | 
|  | 7095 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 7096 |  | 
|  | 7097 | /* | 
|  | 7098 | * Compute key block using the PRF | 
|  | 7099 | */ | 
|  | 7100 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); | 
|  | 7101 | if( ret != 0 ) | 
|  | 7102 | { | 
|  | 7103 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
|  | 7104 | return( ret ); | 
|  | 7105 | } | 
|  | 7106 |  | 
|  | 7107 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", | 
|  | 7108 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); | 
|  | 7109 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); | 
|  | 7110 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); | 
|  | 7111 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); | 
|  | 7112 |  | 
|  | 7113 | /* | 
|  | 7114 | * Determine the appropriate key, IV and MAC length. | 
|  | 7115 | */ | 
|  | 7116 |  | 
|  | 7117 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; | 
|  | 7118 |  | 
|  | 7119 | #if defined(MBEDTLS_GCM_C) ||                           \ | 
|  | 7120 | defined(MBEDTLS_CCM_C) ||                           \ | 
|  | 7121 | defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 7122 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM || | 
|  | 7123 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM || | 
|  | 7124 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) | 
|  | 7125 | { | 
|  | 7126 | size_t explicit_ivlen; | 
|  | 7127 |  | 
|  | 7128 | transform->maclen = 0; | 
|  | 7129 | mac_key_len = 0; | 
|  | 7130 | transform->taglen = | 
|  | 7131 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; | 
|  | 7132 |  | 
|  | 7133 | /* All modes haves 96-bit IVs, but the length of the static parts vary | 
|  | 7134 | * with mode and version: | 
|  | 7135 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes | 
|  | 7136 | *   (to be concatenated with a dynamically chosen IV of 8 Bytes) | 
|  | 7137 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's | 
|  | 7138 | *   a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record | 
|  | 7139 | *   sequence number). | 
|  | 7140 | */ | 
|  | 7141 | transform->ivlen = 12; | 
|  | 7142 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) | 
|  | 7143 | transform->fixed_ivlen = 12; | 
|  | 7144 | else | 
|  | 7145 | transform->fixed_ivlen = 4; | 
|  | 7146 |  | 
|  | 7147 | /* Minimum length of encrypted record */ | 
|  | 7148 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; | 
|  | 7149 | transform->minlen = explicit_ivlen + transform->taglen; | 
|  | 7150 | } | 
|  | 7151 | else | 
|  | 7152 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ | 
|  | 7153 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
|  | 7154 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM || | 
|  | 7155 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) | 
|  | 7156 | { | 
|  | 7157 | /* Initialize HMAC contexts */ | 
|  | 7158 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || | 
|  | 7159 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) | 
|  | 7160 | { | 
|  | 7161 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | 
|  | 7162 | goto end; | 
|  | 7163 | } | 
|  | 7164 |  | 
|  | 7165 | /* Get MAC length */ | 
|  | 7166 | mac_key_len = mbedtls_md_get_size( md_info ); | 
|  | 7167 | transform->maclen = mac_key_len; | 
|  | 7168 |  | 
|  | 7169 | /* IV length */ | 
|  | 7170 | transform->ivlen = cipher_info->iv_size; | 
|  | 7171 |  | 
|  | 7172 | /* Minimum length */ | 
|  | 7173 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ) | 
|  | 7174 | transform->minlen = transform->maclen; | 
|  | 7175 | else | 
|  | 7176 | { | 
|  | 7177 | /* | 
|  | 7178 | * GenericBlockCipher: | 
|  | 7179 | * 1. if EtM is in use: one block plus MAC | 
|  | 7180 | *    otherwise: * first multiple of blocklen greater than maclen | 
|  | 7181 | * 2. IV | 
|  | 7182 | */ | 
|  | 7183 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 7184 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) | 
|  | 7185 | { | 
|  | 7186 | transform->minlen = transform->maclen | 
|  | 7187 | + cipher_info->block_size; | 
|  | 7188 | } | 
|  | 7189 | else | 
|  | 7190 | #endif | 
|  | 7191 | { | 
|  | 7192 | transform->minlen = transform->maclen | 
|  | 7193 | + cipher_info->block_size | 
|  | 7194 | - transform->maclen % cipher_info->block_size; | 
|  | 7195 | } | 
|  | 7196 |  | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7197 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 7198 | { | 
|  | 7199 | transform->minlen += transform->ivlen; | 
|  | 7200 | } | 
|  | 7201 | else | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7202 | { | 
|  | 7203 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 7204 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 7205 | goto end; | 
|  | 7206 | } | 
|  | 7207 | } | 
|  | 7208 | } | 
|  | 7209 | else | 
|  | 7210 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 7211 | { | 
|  | 7212 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 7213 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 7214 | } | 
|  | 7215 |  | 
|  | 7216 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", | 
|  | 7217 | (unsigned) keylen, | 
|  | 7218 | (unsigned) transform->minlen, | 
|  | 7219 | (unsigned) transform->ivlen, | 
|  | 7220 | (unsigned) transform->maclen ) ); | 
|  | 7221 |  | 
|  | 7222 | /* | 
|  | 7223 | * Finally setup the cipher contexts, IVs and MAC secrets. | 
|  | 7224 | */ | 
|  | 7225 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 7226 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 7227 | { | 
|  | 7228 | key1 = keyblk + mac_key_len * 2; | 
|  | 7229 | key2 = keyblk + mac_key_len * 2 + keylen; | 
|  | 7230 |  | 
|  | 7231 | mac_enc = keyblk; | 
|  | 7232 | mac_dec = keyblk + mac_key_len; | 
|  | 7233 |  | 
|  | 7234 | /* | 
|  | 7235 | * This is not used in TLS v1.1. | 
|  | 7236 | */ | 
|  | 7237 | iv_copy_len = ( transform->fixed_ivlen ) ? | 
|  | 7238 | transform->fixed_ivlen : transform->ivlen; | 
|  | 7239 | memcpy( transform->iv_enc, key2 + keylen,  iv_copy_len ); | 
|  | 7240 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, | 
|  | 7241 | iv_copy_len ); | 
|  | 7242 | } | 
|  | 7243 | else | 
|  | 7244 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 7245 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 7246 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 7247 | { | 
|  | 7248 | key1 = keyblk + mac_key_len * 2 + keylen; | 
|  | 7249 | key2 = keyblk + mac_key_len * 2; | 
|  | 7250 |  | 
|  | 7251 | mac_enc = keyblk + mac_key_len; | 
|  | 7252 | mac_dec = keyblk; | 
|  | 7253 |  | 
|  | 7254 | /* | 
|  | 7255 | * This is not used in TLS v1.1. | 
|  | 7256 | */ | 
|  | 7257 | iv_copy_len = ( transform->fixed_ivlen ) ? | 
|  | 7258 | transform->fixed_ivlen : transform->ivlen; | 
|  | 7259 | memcpy( transform->iv_dec, key1 + keylen,  iv_copy_len ); | 
|  | 7260 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, | 
|  | 7261 | iv_copy_len ); | 
|  | 7262 | } | 
|  | 7263 | else | 
|  | 7264 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 7265 | { | 
|  | 7266 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 7267 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 7268 | goto end; | 
|  | 7269 | } | 
|  | 7270 |  | 
|  | 7271 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7272 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. | 
|  | 7273 | For AEAD-based ciphersuites, there is nothing to do here. */ | 
|  | 7274 | if( mac_key_len != 0 ) | 
|  | 7275 | { | 
|  | 7276 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); | 
|  | 7277 | if( ret != 0 ) | 
|  | 7278 | goto end; | 
|  | 7279 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); | 
|  | 7280 | if( ret != 0 ) | 
|  | 7281 | goto end; | 
|  | 7282 | } | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7283 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 7284 |  | 
|  | 7285 | ((void) mac_dec); | 
|  | 7286 | ((void) mac_enc); | 
|  | 7287 |  | 
|  | 7288 | if( ssl != NULL && ssl->f_export_keys != NULL ) | 
|  | 7289 | { | 
|  | 7290 | ssl->f_export_keys( ssl->p_export_keys, | 
|  | 7291 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, | 
|  | 7292 | master, 48, | 
|  | 7293 | randbytes + 32, | 
|  | 7294 | randbytes, | 
|  | 7295 | tls_prf_get_type( tls_prf ) ); | 
|  | 7296 | } | 
|  | 7297 |  | 
|  | 7298 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 7299 | if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type, | 
|  | 7300 | transform->taglen, | 
|  | 7301 | &alg, | 
|  | 7302 | &key_type, | 
|  | 7303 | &key_bits ) ) != PSA_SUCCESS ) | 
|  | 7304 | { | 
|  | 7305 | ret = psa_ssl_status_to_mbedtls( status ); | 
|  | 7306 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); | 
|  | 7307 | goto end; | 
|  | 7308 | } | 
|  | 7309 |  | 
|  | 7310 | transform->psa_alg = alg; | 
|  | 7311 |  | 
|  | 7312 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) | 
|  | 7313 | { | 
|  | 7314 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); | 
|  | 7315 | psa_set_key_algorithm( &attributes, alg ); | 
|  | 7316 | psa_set_key_type( &attributes, key_type ); | 
|  | 7317 |  | 
|  | 7318 | if( ( status = psa_import_key( &attributes, | 
|  | 7319 | key1, | 
|  | 7320 | PSA_BITS_TO_BYTES( key_bits ), | 
|  | 7321 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) | 
|  | 7322 | { | 
|  | 7323 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); | 
|  | 7324 | ret = psa_ssl_status_to_mbedtls( status ); | 
|  | 7325 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); | 
|  | 7326 | goto end; | 
|  | 7327 | } | 
|  | 7328 |  | 
|  | 7329 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); | 
|  | 7330 |  | 
|  | 7331 | if( ( status = psa_import_key( &attributes, | 
|  | 7332 | key2, | 
|  | 7333 | PSA_BITS_TO_BYTES( key_bits ), | 
|  | 7334 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) | 
|  | 7335 | { | 
|  | 7336 | ret = psa_ssl_status_to_mbedtls( status ); | 
|  | 7337 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); | 
|  | 7338 | goto end; | 
|  | 7339 | } | 
|  | 7340 | } | 
|  | 7341 | #else | 
|  | 7342 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, | 
|  | 7343 | cipher_info ) ) != 0 ) | 
|  | 7344 | { | 
|  | 7345 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | 
|  | 7346 | goto end; | 
|  | 7347 | } | 
|  | 7348 |  | 
|  | 7349 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, | 
|  | 7350 | cipher_info ) ) != 0 ) | 
|  | 7351 | { | 
|  | 7352 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | 
|  | 7353 | goto end; | 
|  | 7354 | } | 
|  | 7355 |  | 
|  | 7356 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, | 
|  | 7357 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), | 
|  | 7358 | MBEDTLS_ENCRYPT ) ) != 0 ) | 
|  | 7359 | { | 
|  | 7360 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | 
|  | 7361 | goto end; | 
|  | 7362 | } | 
|  | 7363 |  | 
|  | 7364 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, | 
|  | 7365 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), | 
|  | 7366 | MBEDTLS_DECRYPT ) ) != 0 ) | 
|  | 7367 | { | 
|  | 7368 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | 
|  | 7369 | goto end; | 
|  | 7370 | } | 
|  | 7371 |  | 
|  | 7372 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
|  | 7373 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) | 
|  | 7374 | { | 
|  | 7375 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, | 
|  | 7376 | MBEDTLS_PADDING_NONE ) ) != 0 ) | 
|  | 7377 | { | 
|  | 7378 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); | 
|  | 7379 | goto end; | 
|  | 7380 | } | 
|  | 7381 |  | 
|  | 7382 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, | 
|  | 7383 | MBEDTLS_PADDING_NONE ) ) != 0 ) | 
|  | 7384 | { | 
|  | 7385 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); | 
|  | 7386 | goto end; | 
|  | 7387 | } | 
|  | 7388 | } | 
|  | 7389 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
|  | 7390 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 7391 |  | 
|  | 7392 | end: | 
|  | 7393 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); | 
|  | 7394 | return( ret ); | 
|  | 7395 | } | 
|  | 7396 |  | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 7397 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 7398 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | 
|  | 7399 | unsigned char *hash, size_t *hashlen, | 
|  | 7400 | unsigned char *data, size_t data_len, | 
|  | 7401 | mbedtls_md_type_t md_alg ) | 
|  | 7402 | { | 
|  | 7403 | psa_status_t status; | 
|  | 7404 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; | 
|  | 7405 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); | 
|  | 7406 |  | 
|  | 7407 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); | 
|  | 7408 |  | 
|  | 7409 | if( ( status = psa_hash_setup( &hash_operation, | 
|  | 7410 | hash_alg ) ) != PSA_SUCCESS ) | 
|  | 7411 | { | 
|  | 7412 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); | 
|  | 7413 | goto exit; | 
|  | 7414 | } | 
|  | 7415 |  | 
|  | 7416 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, | 
|  | 7417 | 64 ) ) != PSA_SUCCESS ) | 
|  | 7418 | { | 
|  | 7419 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); | 
|  | 7420 | goto exit; | 
|  | 7421 | } | 
|  | 7422 |  | 
|  | 7423 | if( ( status = psa_hash_update( &hash_operation, | 
|  | 7424 | data, data_len ) ) != PSA_SUCCESS ) | 
|  | 7425 | { | 
|  | 7426 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); | 
|  | 7427 | goto exit; | 
|  | 7428 | } | 
|  | 7429 |  | 
|  | 7430 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, | 
|  | 7431 | hashlen ) ) != PSA_SUCCESS ) | 
|  | 7432 | { | 
|  | 7433 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); | 
|  | 7434 | goto exit; | 
|  | 7435 | } | 
|  | 7436 |  | 
|  | 7437 | exit: | 
|  | 7438 | if( status != PSA_SUCCESS ) | 
|  | 7439 | { | 
|  | 7440 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7441 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 7442 | switch( status ) | 
|  | 7443 | { | 
|  | 7444 | case PSA_ERROR_NOT_SUPPORTED: | 
|  | 7445 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); | 
|  | 7446 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ | 
|  | 7447 | case PSA_ERROR_BUFFER_TOO_SMALL: | 
|  | 7448 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); | 
|  | 7449 | case PSA_ERROR_INSUFFICIENT_MEMORY: | 
|  | 7450 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); | 
|  | 7451 | default: | 
|  | 7452 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); | 
|  | 7453 | } | 
|  | 7454 | } | 
|  | 7455 | return( 0 ); | 
|  | 7456 | } | 
|  | 7457 |  | 
|  | 7458 | #else | 
|  | 7459 |  | 
|  | 7460 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | 
|  | 7461 | unsigned char *hash, size_t *hashlen, | 
|  | 7462 | unsigned char *data, size_t data_len, | 
|  | 7463 | mbedtls_md_type_t md_alg ) | 
|  | 7464 | { | 
|  | 7465 | int ret = 0; | 
|  | 7466 | mbedtls_md_context_t ctx; | 
|  | 7467 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); | 
|  | 7468 | *hashlen = mbedtls_md_get_size( md_info ); | 
|  | 7469 |  | 
|  | 7470 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); | 
|  | 7471 |  | 
|  | 7472 | mbedtls_md_init( &ctx ); | 
|  | 7473 |  | 
|  | 7474 | /* | 
|  | 7475 | * digitally-signed struct { | 
|  | 7476 | *     opaque client_random[32]; | 
|  | 7477 | *     opaque server_random[32]; | 
|  | 7478 | *     ServerDHParams params; | 
|  | 7479 | * }; | 
|  | 7480 | */ | 
|  | 7481 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) | 
|  | 7482 | { | 
|  | 7483 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | 
|  | 7484 | goto exit; | 
|  | 7485 | } | 
|  | 7486 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) | 
|  | 7487 | { | 
|  | 7488 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); | 
|  | 7489 | goto exit; | 
|  | 7490 | } | 
|  | 7491 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 7492 | { | 
|  | 7493 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | 
|  | 7494 | goto exit; | 
|  | 7495 | } | 
|  | 7496 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) | 
|  | 7497 | { | 
|  | 7498 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | 
|  | 7499 | goto exit; | 
|  | 7500 | } | 
|  | 7501 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) | 
|  | 7502 | { | 
|  | 7503 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); | 
|  | 7504 | goto exit; | 
|  | 7505 | } | 
|  | 7506 |  | 
|  | 7507 | exit: | 
|  | 7508 | mbedtls_md_free( &ctx ); | 
|  | 7509 |  | 
|  | 7510 | if( ret != 0 ) | 
|  | 7511 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7512 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 7513 |  | 
|  | 7514 | return( ret ); | 
|  | 7515 | } | 
|  | 7516 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 7517 |  | 
| Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7518 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 7519 |  | 
|  | 7520 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ | 
|  | 7521 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 7522 | mbedtls_pk_type_t sig_alg ) | 
|  | 7523 | { | 
|  | 7524 | switch( sig_alg ) | 
|  | 7525 | { | 
|  | 7526 | case MBEDTLS_PK_RSA: | 
|  | 7527 | return( set->rsa ); | 
|  | 7528 | case MBEDTLS_PK_ECDSA: | 
|  | 7529 | return( set->ecdsa ); | 
|  | 7530 | default: | 
|  | 7531 | return( MBEDTLS_MD_NONE ); | 
|  | 7532 | } | 
|  | 7533 | } | 
|  | 7534 |  | 
|  | 7535 | /* Add a signature-hash-pair to a signature-hash set */ | 
|  | 7536 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 7537 | mbedtls_pk_type_t sig_alg, | 
|  | 7538 | mbedtls_md_type_t md_alg ) | 
|  | 7539 | { | 
|  | 7540 | switch( sig_alg ) | 
|  | 7541 | { | 
|  | 7542 | case MBEDTLS_PK_RSA: | 
|  | 7543 | if( set->rsa == MBEDTLS_MD_NONE ) | 
|  | 7544 | set->rsa = md_alg; | 
|  | 7545 | break; | 
|  | 7546 |  | 
|  | 7547 | case MBEDTLS_PK_ECDSA: | 
|  | 7548 | if( set->ecdsa == MBEDTLS_MD_NONE ) | 
|  | 7549 | set->ecdsa = md_alg; | 
|  | 7550 | break; | 
|  | 7551 |  | 
|  | 7552 | default: | 
|  | 7553 | break; | 
|  | 7554 | } | 
|  | 7555 | } | 
|  | 7556 |  | 
|  | 7557 | /* Allow exactly one hash algorithm for each signature. */ | 
|  | 7558 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 7559 | mbedtls_md_type_t md_alg ) | 
|  | 7560 | { | 
|  | 7561 | set->rsa   = md_alg; | 
|  | 7562 | set->ecdsa = md_alg; | 
|  | 7563 | } | 
|  | 7564 |  | 
|  | 7565 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 7566 |  | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7567 | /* Serialization of TLS 1.2 sessions: | 
|  | 7568 | * | 
|  | 7569 | * struct { | 
|  | 7570 | *    uint64 start_time; | 
|  | 7571 | *    uint8 ciphersuite[2];           // defined by the standard | 
|  | 7572 | *    uint8 compression;              // 0 or 1 | 
|  | 7573 | *    uint8 session_id_len;           // at most 32 | 
|  | 7574 | *    opaque session_id[32]; | 
|  | 7575 | *    opaque master[48];              // fixed length in the standard | 
|  | 7576 | *    uint32 verify_result; | 
|  | 7577 | *    opaque peer_cert<0..2^24-1>;    // length 0 means no peer cert | 
|  | 7578 | *    opaque ticket<0..2^24-1>;       // length 0 means no ticket | 
|  | 7579 | *    uint32 ticket_lifetime; | 
|  | 7580 | *    uint8 mfl_code;                 // up to 255 according to standard | 
|  | 7581 | *    uint8 encrypt_then_mac;         // 0 or 1 | 
|  | 7582 | * } serialized_session_tls12; | 
|  | 7583 | * | 
|  | 7584 | */ | 
|  | 7585 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, | 
|  | 7586 | unsigned char *buf, | 
|  | 7587 | size_t buf_len ) | 
|  | 7588 | { | 
|  | 7589 | unsigned char *p = buf; | 
|  | 7590 | size_t used = 0; | 
|  | 7591 |  | 
|  | 7592 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 7593 | uint64_t start; | 
|  | 7594 | #endif | 
|  | 7595 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7596 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7597 | size_t cert_len; | 
|  | 7598 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7599 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 7600 |  | 
|  | 7601 | /* | 
|  | 7602 | * Time | 
|  | 7603 | */ | 
|  | 7604 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 7605 | used += 8; | 
|  | 7606 |  | 
|  | 7607 | if( used <= buf_len ) | 
|  | 7608 | { | 
|  | 7609 | start = (uint64_t) session->start; | 
|  | 7610 |  | 
|  | 7611 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); | 
|  | 7612 | p += 8; | 
|  | 7613 | } | 
|  | 7614 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 7615 |  | 
|  | 7616 | /* | 
|  | 7617 | * Basic mandatory fields | 
|  | 7618 | */ | 
|  | 7619 | used += 2   /* ciphersuite */ | 
|  | 7620 | + 1   /* compression */ | 
|  | 7621 | + 1   /* id_len */ | 
|  | 7622 | + sizeof( session->id ) | 
|  | 7623 | + sizeof( session->master ) | 
|  | 7624 | + 4;  /* verify_result */ | 
|  | 7625 |  | 
|  | 7626 | if( used <= buf_len ) | 
|  | 7627 | { | 
|  | 7628 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); | 
|  | 7629 | p += 2; | 
|  | 7630 |  | 
|  | 7631 | *p++ = MBEDTLS_BYTE_0( session->compression ); | 
|  | 7632 |  | 
|  | 7633 | *p++ = MBEDTLS_BYTE_0( session->id_len ); | 
|  | 7634 | memcpy( p, session->id, 32 ); | 
|  | 7635 | p += 32; | 
|  | 7636 |  | 
|  | 7637 | memcpy( p, session->master, 48 ); | 
|  | 7638 | p += 48; | 
|  | 7639 |  | 
|  | 7640 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); | 
|  | 7641 | p += 4; | 
|  | 7642 | } | 
|  | 7643 |  | 
|  | 7644 | /* | 
|  | 7645 | * Peer's end-entity certificate | 
|  | 7646 | */ | 
|  | 7647 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7648 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7649 | if( session->peer_cert == NULL ) | 
|  | 7650 | cert_len = 0; | 
|  | 7651 | else | 
|  | 7652 | cert_len = session->peer_cert->raw.len; | 
|  | 7653 |  | 
|  | 7654 | used += 3 + cert_len; | 
|  | 7655 |  | 
|  | 7656 | if( used <= buf_len ) | 
|  | 7657 | { | 
|  | 7658 | *p++ = MBEDTLS_BYTE_2( cert_len ); | 
|  | 7659 | *p++ = MBEDTLS_BYTE_1( cert_len ); | 
|  | 7660 | *p++ = MBEDTLS_BYTE_0( cert_len ); | 
|  | 7661 |  | 
|  | 7662 | if( session->peer_cert != NULL ) | 
|  | 7663 | { | 
|  | 7664 | memcpy( p, session->peer_cert->raw.p, cert_len ); | 
|  | 7665 | p += cert_len; | 
|  | 7666 | } | 
|  | 7667 | } | 
|  | 7668 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7669 | if( session->peer_cert_digest != NULL ) | 
|  | 7670 | { | 
|  | 7671 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; | 
|  | 7672 | if( used <= buf_len ) | 
|  | 7673 | { | 
|  | 7674 | *p++ = (unsigned char) session->peer_cert_digest_type; | 
|  | 7675 | *p++ = (unsigned char) session->peer_cert_digest_len; | 
|  | 7676 | memcpy( p, session->peer_cert_digest, | 
|  | 7677 | session->peer_cert_digest_len ); | 
|  | 7678 | p += session->peer_cert_digest_len; | 
|  | 7679 | } | 
|  | 7680 | } | 
|  | 7681 | else | 
|  | 7682 | { | 
|  | 7683 | used += 2; | 
|  | 7684 | if( used <= buf_len ) | 
|  | 7685 | { | 
|  | 7686 | *p++ = (unsigned char) MBEDTLS_MD_NONE; | 
|  | 7687 | *p++ = 0; | 
|  | 7688 | } | 
|  | 7689 | } | 
|  | 7690 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7691 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 7692 |  | 
|  | 7693 | /* | 
|  | 7694 | * Session ticket if any, plus associated data | 
|  | 7695 | */ | 
|  | 7696 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 7697 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ | 
|  | 7698 |  | 
|  | 7699 | if( used <= buf_len ) | 
|  | 7700 | { | 
|  | 7701 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); | 
|  | 7702 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); | 
|  | 7703 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); | 
|  | 7704 |  | 
|  | 7705 | if( session->ticket != NULL ) | 
|  | 7706 | { | 
|  | 7707 | memcpy( p, session->ticket, session->ticket_len ); | 
|  | 7708 | p += session->ticket_len; | 
|  | 7709 | } | 
|  | 7710 |  | 
|  | 7711 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); | 
|  | 7712 | p += 4; | 
|  | 7713 | } | 
|  | 7714 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 7715 |  | 
|  | 7716 | /* | 
|  | 7717 | * Misc extension-related info | 
|  | 7718 | */ | 
|  | 7719 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 7720 | used += 1; | 
|  | 7721 |  | 
|  | 7722 | if( used <= buf_len ) | 
|  | 7723 | *p++ = session->mfl_code; | 
|  | 7724 | #endif | 
|  | 7725 |  | 
|  | 7726 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 7727 | used += 1; | 
|  | 7728 |  | 
|  | 7729 | if( used <= buf_len ) | 
|  | 7730 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); | 
|  | 7731 | #endif | 
|  | 7732 |  | 
|  | 7733 | return( used ); | 
|  | 7734 | } | 
|  | 7735 |  | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7736 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, | 
|  | 7737 | const unsigned char *buf, | 
|  | 7738 | size_t len ) | 
|  | 7739 | { | 
|  | 7740 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 7741 | uint64_t start; | 
|  | 7742 | #endif | 
|  | 7743 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7744 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7745 | size_t cert_len; | 
|  | 7746 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7747 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 7748 |  | 
|  | 7749 | const unsigned char *p = buf; | 
|  | 7750 | const unsigned char * const end = buf + len; | 
|  | 7751 |  | 
|  | 7752 | /* | 
|  | 7753 | * Time | 
|  | 7754 | */ | 
|  | 7755 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 7756 | if( 8 > (size_t)( end - p ) ) | 
|  | 7757 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7758 |  | 
|  | 7759 | start = ( (uint64_t) p[0] << 56 ) | | 
|  | 7760 | ( (uint64_t) p[1] << 48 ) | | 
|  | 7761 | ( (uint64_t) p[2] << 40 ) | | 
|  | 7762 | ( (uint64_t) p[3] << 32 ) | | 
|  | 7763 | ( (uint64_t) p[4] << 24 ) | | 
|  | 7764 | ( (uint64_t) p[5] << 16 ) | | 
|  | 7765 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 7766 | ( (uint64_t) p[7]       ); | 
|  | 7767 | p += 8; | 
|  | 7768 |  | 
|  | 7769 | session->start = (time_t) start; | 
|  | 7770 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 7771 |  | 
|  | 7772 | /* | 
|  | 7773 | * Basic mandatory fields | 
|  | 7774 | */ | 
|  | 7775 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) | 
|  | 7776 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7777 |  | 
|  | 7778 | session->ciphersuite = ( p[0] << 8 ) | p[1]; | 
|  | 7779 | p += 2; | 
|  | 7780 |  | 
|  | 7781 | session->compression = *p++; | 
|  | 7782 |  | 
|  | 7783 | session->id_len = *p++; | 
|  | 7784 | memcpy( session->id, p, 32 ); | 
|  | 7785 | p += 32; | 
|  | 7786 |  | 
|  | 7787 | memcpy( session->master, p, 48 ); | 
|  | 7788 | p += 48; | 
|  | 7789 |  | 
|  | 7790 | session->verify_result = ( (uint32_t) p[0] << 24 ) | | 
|  | 7791 | ( (uint32_t) p[1] << 16 ) | | 
|  | 7792 | ( (uint32_t) p[2] <<  8 ) | | 
|  | 7793 | ( (uint32_t) p[3]       ); | 
|  | 7794 | p += 4; | 
|  | 7795 |  | 
|  | 7796 | /* Immediately clear invalid pointer values that have been read, in case | 
|  | 7797 | * we exit early before we replaced them with valid ones. */ | 
|  | 7798 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7799 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7800 | session->peer_cert = NULL; | 
|  | 7801 | #else | 
|  | 7802 | session->peer_cert_digest = NULL; | 
|  | 7803 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7804 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 7805 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 7806 | session->ticket = NULL; | 
|  | 7807 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 7808 |  | 
|  | 7809 | /* | 
|  | 7810 | * Peer certificate | 
|  | 7811 | */ | 
|  | 7812 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7813 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7814 | /* Deserialize CRT from the end of the ticket. */ | 
|  | 7815 | if( 3 > (size_t)( end - p ) ) | 
|  | 7816 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7817 |  | 
|  | 7818 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; | 
|  | 7819 | p += 3; | 
|  | 7820 |  | 
|  | 7821 | if( cert_len != 0 ) | 
|  | 7822 | { | 
|  | 7823 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 7824 |  | 
|  | 7825 | if( cert_len > (size_t)( end - p ) ) | 
|  | 7826 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7827 |  | 
|  | 7828 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); | 
|  | 7829 |  | 
|  | 7830 | if( session->peer_cert == NULL ) | 
|  | 7831 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 7832 |  | 
|  | 7833 | mbedtls_x509_crt_init( session->peer_cert ); | 
|  | 7834 |  | 
|  | 7835 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, | 
|  | 7836 | p, cert_len ) ) != 0 ) | 
|  | 7837 | { | 
|  | 7838 | mbedtls_x509_crt_free( session->peer_cert ); | 
|  | 7839 | mbedtls_free( session->peer_cert ); | 
|  | 7840 | session->peer_cert = NULL; | 
|  | 7841 | return( ret ); | 
|  | 7842 | } | 
|  | 7843 |  | 
|  | 7844 | p += cert_len; | 
|  | 7845 | } | 
|  | 7846 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7847 | /* Deserialize CRT digest from the end of the ticket. */ | 
|  | 7848 | if( 2 > (size_t)( end - p ) ) | 
|  | 7849 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7850 |  | 
|  | 7851 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; | 
|  | 7852 | session->peer_cert_digest_len  = (size_t) *p++; | 
|  | 7853 |  | 
|  | 7854 | if( session->peer_cert_digest_len != 0 ) | 
|  | 7855 | { | 
|  | 7856 | const mbedtls_md_info_t *md_info = | 
|  | 7857 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); | 
|  | 7858 | if( md_info == NULL ) | 
|  | 7859 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7860 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) | 
|  | 7861 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7862 |  | 
|  | 7863 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) | 
|  | 7864 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7865 |  | 
|  | 7866 | session->peer_cert_digest = | 
|  | 7867 | mbedtls_calloc( 1, session->peer_cert_digest_len ); | 
|  | 7868 | if( session->peer_cert_digest == NULL ) | 
|  | 7869 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 7870 |  | 
|  | 7871 | memcpy( session->peer_cert_digest, p, | 
|  | 7872 | session->peer_cert_digest_len ); | 
|  | 7873 | p += session->peer_cert_digest_len; | 
|  | 7874 | } | 
|  | 7875 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7876 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 7877 |  | 
|  | 7878 | /* | 
|  | 7879 | * Session ticket and associated data | 
|  | 7880 | */ | 
|  | 7881 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 7882 | if( 3 > (size_t)( end - p ) ) | 
|  | 7883 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7884 |  | 
|  | 7885 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; | 
|  | 7886 | p += 3; | 
|  | 7887 |  | 
|  | 7888 | if( session->ticket_len != 0 ) | 
|  | 7889 | { | 
|  | 7890 | if( session->ticket_len > (size_t)( end - p ) ) | 
|  | 7891 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7892 |  | 
|  | 7893 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); | 
|  | 7894 | if( session->ticket == NULL ) | 
|  | 7895 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 7896 |  | 
|  | 7897 | memcpy( session->ticket, p, session->ticket_len ); | 
|  | 7898 | p += session->ticket_len; | 
|  | 7899 | } | 
|  | 7900 |  | 
|  | 7901 | if( 4 > (size_t)( end - p ) ) | 
|  | 7902 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7903 |  | 
|  | 7904 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | | 
|  | 7905 | ( (uint32_t) p[1] << 16 ) | | 
|  | 7906 | ( (uint32_t) p[2] <<  8 ) | | 
|  | 7907 | ( (uint32_t) p[3]       ); | 
|  | 7908 | p += 4; | 
|  | 7909 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 7910 |  | 
|  | 7911 | /* | 
|  | 7912 | * Misc extension-related info | 
|  | 7913 | */ | 
|  | 7914 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 7915 | if( 1 > (size_t)( end - p ) ) | 
|  | 7916 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7917 |  | 
|  | 7918 | session->mfl_code = *p++; | 
|  | 7919 | #endif | 
|  | 7920 |  | 
|  | 7921 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 7922 | if( 1 > (size_t)( end - p ) ) | 
|  | 7923 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7924 |  | 
|  | 7925 | session->encrypt_then_mac = *p++; | 
|  | 7926 | #endif | 
|  | 7927 |  | 
|  | 7928 | /* Done, should have consumed entire buffer */ | 
|  | 7929 | if( p != end ) | 
|  | 7930 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7931 |  | 
|  | 7932 | return( 0 ); | 
|  | 7933 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 7934 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 7935 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7936 | #endif /* MBEDTLS_SSL_TLS_C */ |