| 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 || | 
| Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 123 | mbedtls_ssl_is_handshake_over( ssl ) == 0 ) | 
| Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 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 |  | 
| Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 478 | static void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, | 
|  | 479 | unsigned hs_type, | 
|  | 480 | size_t total_hs_len ) | 
|  | 481 | { | 
|  | 482 | unsigned char hs_hdr[4]; | 
|  | 483 |  | 
|  | 484 | /* Build HS header for checksum update. */ | 
|  | 485 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); | 
|  | 486 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); | 
|  | 487 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); | 
|  | 488 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); | 
|  | 489 |  | 
|  | 490 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); | 
|  | 491 | } | 
|  | 492 |  | 
|  | 493 | void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl, | 
|  | 494 | unsigned hs_type, | 
|  | 495 | unsigned char const *msg, | 
|  | 496 | size_t msg_len ) | 
|  | 497 | { | 
|  | 498 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len ); | 
|  | 499 | ssl->handshake->update_checksum( ssl, msg, msg_len ); | 
|  | 500 | } | 
|  | 501 |  | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 502 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) | 
|  | 503 | { | 
|  | 504 | ((void) ssl); | 
|  | 505 | #if defined(MBEDTLS_SHA256_C) | 
|  | 506 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 507 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); | 
|  | 508 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); | 
|  | 509 | #else | 
|  | 510 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); | 
|  | 511 | #endif | 
|  | 512 | #endif | 
|  | 513 | #if defined(MBEDTLS_SHA384_C) | 
|  | 514 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 515 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); | 
|  | 516 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); | 
|  | 517 | #else | 
|  | 518 | mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 ); | 
|  | 519 | #endif | 
|  | 520 | #endif | 
|  | 521 | } | 
|  | 522 |  | 
|  | 523 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, | 
|  | 524 | const unsigned char *buf, size_t len ) | 
|  | 525 | { | 
|  | 526 | #if defined(MBEDTLS_SHA256_C) | 
|  | 527 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 528 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); | 
|  | 529 | #else | 
|  | 530 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); | 
|  | 531 | #endif | 
|  | 532 | #endif | 
|  | 533 | #if defined(MBEDTLS_SHA384_C) | 
|  | 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 | #endif | 
|  | 540 | } | 
|  | 541 |  | 
|  | 542 | #if defined(MBEDTLS_SHA256_C) | 
|  | 543 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, | 
|  | 544 | const unsigned char *buf, size_t len ) | 
|  | 545 | { | 
|  | 546 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 547 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); | 
|  | 548 | #else | 
|  | 549 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); | 
|  | 550 | #endif | 
|  | 551 | } | 
|  | 552 | #endif | 
|  | 553 |  | 
|  | 554 | #if defined(MBEDTLS_SHA384_C) | 
|  | 555 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, | 
|  | 556 | const unsigned char *buf, size_t len ) | 
|  | 557 | { | 
|  | 558 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 559 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); | 
|  | 560 | #else | 
|  | 561 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); | 
|  | 562 | #endif | 
|  | 563 | } | 
|  | 564 | #endif | 
|  | 565 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 567 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 568 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 569 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 570 | #if defined(MBEDTLS_SHA256_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 571 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 572 | handshake->fin_sha256_psa = psa_hash_operation_init(); | 
|  | 573 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); | 
|  | 574 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | mbedtls_sha256_init(   &handshake->fin_sha256    ); | 
| TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 576 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 577 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 578 | #endif | 
| Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 579 | #if defined(MBEDTLS_SHA384_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 580 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 581 | handshake->fin_sha384_psa = psa_hash_operation_init(); | 
|  | 582 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 583 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | mbedtls_sha512_init(   &handshake->fin_sha512    ); | 
| TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 585 | mbedtls_sha512_starts( &handshake->fin_sha512, 1 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 586 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 587 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 588 |  | 
|  | 589 | handshake->update_checksum = ssl_update_checksum_start; | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 590 |  | 
|  | 591 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 592 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 593 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); | 
|  | 594 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 595 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | #if defined(MBEDTLS_DHM_C) | 
|  | 597 | mbedtls_dhm_init( &handshake->dhm_ctx ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 598 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | #if defined(MBEDTLS_ECDH_C) | 
|  | 600 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 601 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 602 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 603 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 604 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 605 | handshake->ecjpake_cache = NULL; | 
|  | 606 | handshake->ecjpake_cache_len = 0; | 
|  | 607 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 608 | #endif | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 609 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 610 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 611 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 612 | #endif | 
|  | 613 |  | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 614 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 615 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; | 
|  | 616 | #endif | 
| Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 617 |  | 
|  | 618 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 619 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 620 | mbedtls_pk_init( &handshake->peer_pubkey ); | 
|  | 621 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 622 | } | 
|  | 623 |  | 
| Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 624 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 625 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 627 |  | 
| Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 628 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 629 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 630 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; | 
| Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 631 | #else | 
|  | 632 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); | 
|  | 633 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); | 
| Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 634 | #endif | 
|  | 635 |  | 
| Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 636 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
| Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 637 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 638 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 639 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; | 
| Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 640 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 641 | mbedtls_md_init( &transform->md_ctx_enc ); | 
|  | 642 | mbedtls_md_init( &transform->md_ctx_dec ); | 
| Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 643 | #endif | 
| Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 644 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 645 | } | 
|  | 646 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 647 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 648 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | memset( session, 0, sizeof(mbedtls_ssl_session) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 650 | } | 
|  | 651 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 652 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 653 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 654 | /* Clear old handshake information if present */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 655 | if( ssl->transform_negotiate ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 657 | if( ssl->session_negotiate ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 659 | if( ssl->handshake ) | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 660 | mbedtls_ssl_handshake_free( ssl ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 661 |  | 
|  | 662 | /* | 
|  | 663 | * Either the pointers are now NULL or cleared properly and can be freed. | 
|  | 664 | * Now allocate missing structures. | 
|  | 665 | */ | 
|  | 666 | if( ssl->transform_negotiate == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 667 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 668 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 669 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 670 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 671 | if( ssl->session_negotiate == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 672 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 673 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 674 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 675 |  | 
| Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 676 | if( ssl->handshake == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 677 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 678 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 679 | } | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 680 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 681 | /* If the buffers are too small - reallocate */ | 
| Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 682 |  | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 683 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, | 
|  | 684 | MBEDTLS_SSL_OUT_BUFFER_LEN ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 685 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 686 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 687 | /* All pointers should exist and can be directly freed without issue */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 688 | if( ssl->handshake == NULL || | 
|  | 689 | ssl->transform_negotiate == NULL || | 
|  | 690 | ssl->session_negotiate == NULL ) | 
|  | 691 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 692 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 693 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | mbedtls_free( ssl->handshake ); | 
|  | 695 | mbedtls_free( ssl->transform_negotiate ); | 
|  | 696 | mbedtls_free( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 697 |  | 
|  | 698 | ssl->handshake = NULL; | 
|  | 699 | ssl->transform_negotiate = NULL; | 
|  | 700 | ssl->session_negotiate = NULL; | 
|  | 701 |  | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 702 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 703 | } | 
|  | 704 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 705 | /* Initialize structures */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 706 | mbedtls_ssl_session_init( ssl->session_negotiate ); | 
| Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 707 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); | 
| Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 708 | ssl_handshake_params_init( ssl->handshake ); | 
|  | 709 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 710 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 711 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 712 | { | 
|  | 713 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 714 |  | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 715 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 716 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; | 
|  | 717 | else | 
|  | 718 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 719 |  | 
| Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 720 | mbedtls_ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 721 | } | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 722 | #endif | 
|  | 723 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 724 | /* | 
|  | 725 | * curve_list is translated to IANA TLS group identifiers here because | 
|  | 726 | * mbedtls_ssl_conf_curves returns void and so can't return | 
|  | 727 | * any error codes. | 
|  | 728 | */ | 
|  | 729 | #if defined(MBEDTLS_ECP_C) | 
|  | 730 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 731 | /* Heap allocate and translate curve_list from internal to IANA group ids */ | 
|  | 732 | if ( ssl->conf->curve_list != NULL ) | 
|  | 733 | { | 
|  | 734 | size_t length; | 
|  | 735 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; | 
|  | 736 |  | 
|  | 737 | for( length = 0;  ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && | 
|  | 738 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} | 
|  | 739 |  | 
|  | 740 | /* Leave room for zero termination */ | 
|  | 741 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); | 
|  | 742 | if ( group_list == NULL ) | 
|  | 743 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 744 |  | 
|  | 745 | for( size_t i = 0; i < length; i++ ) | 
|  | 746 | { | 
|  | 747 | const mbedtls_ecp_curve_info *info = | 
|  | 748 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); | 
|  | 749 | if ( info == NULL ) | 
|  | 750 | { | 
|  | 751 | mbedtls_free( group_list ); | 
|  | 752 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 753 | } | 
|  | 754 | group_list[i] = info->tls_id; | 
|  | 755 | } | 
|  | 756 |  | 
|  | 757 | group_list[length] = 0; | 
|  | 758 |  | 
|  | 759 | ssl->handshake->group_list = group_list; | 
|  | 760 | ssl->handshake->group_list_heap_allocated = 1; | 
|  | 761 | } | 
|  | 762 | else | 
|  | 763 | { | 
|  | 764 | ssl->handshake->group_list = ssl->conf->group_list; | 
|  | 765 | ssl->handshake->group_list_heap_allocated = 0; | 
|  | 766 | } | 
|  | 767 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 768 | #endif /* MBEDTLS_ECP_C */ | 
|  | 769 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 770 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 771 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 772 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 773 | /* Heap allocate and translate sig_hashes from internal hash identifiers to | 
|  | 774 | signature algorithms IANA identifiers.  */ | 
|  | 775 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 776 | ssl->conf->sig_hashes != NULL ) | 
|  | 777 | { | 
|  | 778 | const int *md; | 
|  | 779 | const int *sig_hashes = ssl->conf->sig_hashes; | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 780 | size_t sig_algs_len = 0; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 781 | uint16_t *p; | 
|  | 782 |  | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 783 | #if defined(static_assert) | 
|  | 784 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN | 
|  | 785 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), | 
|  | 786 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); | 
| Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 787 | #endif | 
|  | 788 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 789 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) | 
|  | 790 | { | 
|  | 791 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) | 
|  | 792 | continue; | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 793 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 794 | sig_algs_len += sizeof( uint16_t ); | 
|  | 795 | #endif | 
| Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 796 |  | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 797 | #if defined(MBEDTLS_RSA_C) | 
|  | 798 | sig_algs_len += sizeof( uint16_t ); | 
|  | 799 | #endif | 
|  | 800 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) | 
|  | 801 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 802 | } | 
|  | 803 |  | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 804 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 805 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 806 |  | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 807 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + | 
|  | 808 | sizeof( uint16_t )); | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 809 | if( ssl->handshake->sig_algs == NULL ) | 
|  | 810 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 811 |  | 
|  | 812 | p = (uint16_t *)ssl->handshake->sig_algs; | 
|  | 813 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) | 
|  | 814 | { | 
|  | 815 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); | 
|  | 816 | if( hash == MBEDTLS_SSL_HASH_NONE ) | 
|  | 817 | continue; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 818 | #if defined(MBEDTLS_ECDSA_C) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 819 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); | 
|  | 820 | p++; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 821 | #endif | 
|  | 822 | #if defined(MBEDTLS_RSA_C) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 823 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); | 
|  | 824 | p++; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 825 | #endif | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 826 | } | 
|  | 827 | *p = MBEDTLS_TLS1_3_SIG_NONE; | 
|  | 828 | ssl->handshake->sig_algs_heap_allocated = 1; | 
|  | 829 | } | 
|  | 830 | else | 
| Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 831 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 832 | { | 
|  | 833 | ssl->handshake->sig_algs = ssl->conf->sig_algs; | 
|  | 834 | ssl->handshake->sig_algs_heap_allocated = 0; | 
|  | 835 | } | 
|  | 836 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 837 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 838 | return( 0 ); | 
|  | 839 | } | 
|  | 840 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 841 | #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] | 842 | /* Dummy cookie callbacks for defaults */ | 
|  | 843 | static int ssl_cookie_write_dummy( void *ctx, | 
|  | 844 | unsigned char **p, unsigned char *end, | 
|  | 845 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 846 | { | 
|  | 847 | ((void) ctx); | 
|  | 848 | ((void) p); | 
|  | 849 | ((void) end); | 
|  | 850 | ((void) cli_id); | 
|  | 851 | ((void) cli_id_len); | 
|  | 852 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 853 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 854 | } | 
|  | 855 |  | 
|  | 856 | static int ssl_cookie_check_dummy( void *ctx, | 
|  | 857 | const unsigned char *cookie, size_t cookie_len, | 
|  | 858 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 859 | { | 
|  | 860 | ((void) ctx); | 
|  | 861 | ((void) cookie); | 
|  | 862 | ((void) cookie_len); | 
|  | 863 | ((void) cli_id); | 
|  | 864 | ((void) cli_id_len); | 
|  | 865 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 866 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 867 | } | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 868 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 869 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 870 | /* | 
|  | 871 | * Initialize an SSL context | 
|  | 872 | */ | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 873 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) | 
|  | 874 | { | 
|  | 875 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); | 
|  | 876 | } | 
|  | 877 |  | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 878 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) | 
|  | 879 | { | 
| Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 880 | const mbedtls_ssl_config *conf = ssl->conf; | 
|  | 881 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 882 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 883 | if( mbedtls_ssl_conf_is_tls13_enabled( conf ) && | 
|  | 884 | ( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) ) | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 885 | { | 
| Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 886 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); | 
|  | 887 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 888 | } | 
|  | 889 |  | 
|  | 890 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) | 
|  | 891 | { | 
|  | 892 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 893 | { | 
| Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 894 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 895 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 896 | } | 
|  | 897 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); | 
|  | 898 | return( 0 ); | 
|  | 899 | } | 
|  | 900 | #endif | 
|  | 901 |  | 
|  | 902 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 903 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 904 | { | 
|  | 905 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); | 
|  | 906 | return( 0 ); | 
|  | 907 | } | 
|  | 908 | #endif | 
|  | 909 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 910 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 911 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 912 | { | 
|  | 913 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) ); | 
|  | 914 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 915 | } | 
|  | 916 | #endif | 
|  | 917 |  | 
|  | 918 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); | 
|  | 919 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 920 | } | 
|  | 921 |  | 
|  | 922 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) | 
|  | 923 | { | 
|  | 924 | int ret; | 
|  | 925 | ret = ssl_conf_version_check( ssl ); | 
|  | 926 | if( ret != 0 ) | 
|  | 927 | return( ret ); | 
|  | 928 |  | 
|  | 929 | /* Space for further checks */ | 
|  | 930 |  | 
|  | 931 | return( 0 ); | 
|  | 932 | } | 
|  | 933 |  | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 934 | /* | 
|  | 935 | * Setup an SSL context | 
|  | 936 | */ | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 937 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 938 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 939 | const mbedtls_ssl_config *conf ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 940 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 941 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 942 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 943 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 944 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 945 | ssl->conf = conf; | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 946 |  | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 947 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) | 
|  | 948 | return( ret ); | 
|  | 949 |  | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 950 | /* | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 951 | * Prepare base structures | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 952 | */ | 
| k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 953 |  | 
|  | 954 | /* Set to NULL in case of an error condition */ | 
|  | 955 | ssl->out_buf = NULL; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 956 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 957 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 958 | ssl->in_buf_len = in_buf_len; | 
|  | 959 | #endif | 
|  | 960 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 961 | if( ssl->in_buf == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 962 | { | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 963 | 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] | 964 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 965 | goto error; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 966 | } | 
|  | 967 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 968 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 969 | ssl->out_buf_len = out_buf_len; | 
|  | 970 | #endif | 
|  | 971 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 972 | if( ssl->out_buf == NULL ) | 
|  | 973 | { | 
| Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 974 | 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] | 975 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 976 | goto error; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 977 | } | 
|  | 978 |  | 
| Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 979 | mbedtls_ssl_reset_in_out_pointers( ssl ); | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 980 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 981 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 982 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 983 | #endif | 
|  | 984 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 985 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 986 | goto error; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 987 |  | 
|  | 988 | return( 0 ); | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 989 |  | 
|  | 990 | error: | 
|  | 991 | mbedtls_free( ssl->in_buf ); | 
|  | 992 | mbedtls_free( ssl->out_buf ); | 
|  | 993 |  | 
|  | 994 | ssl->conf = NULL; | 
|  | 995 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 996 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 997 | ssl->in_buf_len = 0; | 
|  | 998 | ssl->out_buf_len = 0; | 
|  | 999 | #endif | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1000 | ssl->in_buf = NULL; | 
|  | 1001 | ssl->out_buf = NULL; | 
|  | 1002 |  | 
|  | 1003 | ssl->in_hdr = NULL; | 
|  | 1004 | ssl->in_ctr = NULL; | 
|  | 1005 | ssl->in_len = NULL; | 
|  | 1006 | ssl->in_iv = NULL; | 
|  | 1007 | ssl->in_msg = NULL; | 
|  | 1008 |  | 
|  | 1009 | ssl->out_hdr = NULL; | 
|  | 1010 | ssl->out_ctr = NULL; | 
|  | 1011 | ssl->out_len = NULL; | 
|  | 1012 | ssl->out_iv = NULL; | 
|  | 1013 | ssl->out_msg = NULL; | 
|  | 1014 |  | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1015 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1016 | } | 
|  | 1017 |  | 
|  | 1018 | /* | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1019 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 1020 | * all application-set variables, function pointers and data. | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1021 | * | 
|  | 1022 | * If partial is non-zero, keep data in the input buffer and client ID. | 
|  | 1023 | * (Use when a DTLS client reconnects from the same port.) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1024 | */ | 
| XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1025 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, | 
|  | 1026 | int partial ) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1027 | { | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1028 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 1029 | size_t in_buf_len = ssl->in_buf_len; | 
|  | 1030 | size_t out_buf_len = ssl->out_buf_len; | 
|  | 1031 | #else | 
|  | 1032 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 1033 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
|  | 1034 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1035 |  | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1036 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) | 
|  | 1037 | partial = 0; | 
| Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1038 | #endif | 
|  | 1039 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1040 | /* Cancel any possibly running timer */ | 
| Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1041 | mbedtls_ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1042 |  | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1043 | mbedtls_ssl_reset_in_out_pointers( ssl ); | 
|  | 1044 |  | 
|  | 1045 | /* Reset incoming message parsing */ | 
|  | 1046 | ssl->in_offt    = NULL; | 
|  | 1047 | ssl->nb_zero    = 0; | 
|  | 1048 | ssl->in_msgtype = 0; | 
|  | 1049 | ssl->in_msglen  = 0; | 
|  | 1050 | ssl->in_hslen   = 0; | 
|  | 1051 | ssl->keep_current_message = 0; | 
|  | 1052 | ssl->transform_in  = NULL; | 
|  | 1053 |  | 
|  | 1054 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 1055 | ssl->next_record_offset = 0; | 
|  | 1056 | ssl->in_epoch = 0; | 
|  | 1057 | #endif | 
|  | 1058 |  | 
|  | 1059 | /* Keep current datagram if partial == 1 */ | 
|  | 1060 | if( partial == 0 ) | 
|  | 1061 | { | 
|  | 1062 | ssl->in_left = 0; | 
|  | 1063 | memset( ssl->in_buf, 0, in_buf_len ); | 
|  | 1064 | } | 
|  | 1065 |  | 
|  | 1066 | /* Reset outgoing message writing */ | 
|  | 1067 | ssl->out_msgtype = 0; | 
|  | 1068 | ssl->out_msglen  = 0; | 
|  | 1069 | ssl->out_left    = 0; | 
|  | 1070 | memset( ssl->out_buf, 0, out_buf_len ); | 
|  | 1071 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); | 
|  | 1072 | ssl->transform_out = NULL; | 
|  | 1073 |  | 
|  | 1074 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 1075 | mbedtls_ssl_dtls_replay_reset( ssl ); | 
|  | 1076 | #endif | 
|  | 1077 |  | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1078 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1079 | if( ssl->transform ) | 
|  | 1080 | { | 
|  | 1081 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 1082 | mbedtls_free( ssl->transform ); | 
|  | 1083 | ssl->transform = NULL; | 
|  | 1084 | } | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1085 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 1086 |  | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1087 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 1088 | mbedtls_ssl_transform_free( ssl->transform_application ); | 
|  | 1089 | mbedtls_free( ssl->transform_application ); | 
|  | 1090 | ssl->transform_application = NULL; | 
|  | 1091 |  | 
|  | 1092 | if( ssl->handshake != NULL ) | 
|  | 1093 | { | 
|  | 1094 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); | 
|  | 1095 | mbedtls_free( ssl->handshake->transform_earlydata ); | 
|  | 1096 | ssl->handshake->transform_earlydata = NULL; | 
|  | 1097 |  | 
|  | 1098 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); | 
|  | 1099 | mbedtls_free( ssl->handshake->transform_handshake ); | 
|  | 1100 | ssl->handshake->transform_handshake = NULL; | 
|  | 1101 | } | 
|  | 1102 |  | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1103 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1104 | } | 
|  | 1105 |  | 
|  | 1106 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) | 
|  | 1107 | { | 
|  | 1108 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 1109 |  | 
|  | 1110 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
|  | 1111 |  | 
| XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1112 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1113 |  | 
|  | 1114 | /* Reset renegotiation state */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1115 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1116 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1117 | ssl->renego_records_seen = 0; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1118 |  | 
|  | 1119 | ssl->verify_data_len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1120 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); | 
|  | 1121 | 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] | 1122 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1123 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1124 |  | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1125 | ssl->session_in  = NULL; | 
| Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1126 | ssl->session_out = NULL; | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1127 | if( ssl->session ) | 
|  | 1128 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1129 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 1130 | mbedtls_free( ssl->session ); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1131 | ssl->session = NULL; | 
|  | 1132 | } | 
|  | 1133 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1134 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1135 | ssl->alpn_chosen = NULL; | 
|  | 1136 | #endif | 
|  | 1137 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1138 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1139 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1140 | if( partial == 0 ) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1141 | #endif | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1142 | { | 
|  | 1143 | mbedtls_free( ssl->cli_id ); | 
|  | 1144 | ssl->cli_id = NULL; | 
|  | 1145 | ssl->cli_id_len = 0; | 
|  | 1146 | } | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1147 | #endif | 
|  | 1148 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1149 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
|  | 1150 | return( ret ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1151 |  | 
|  | 1152 | return( 0 ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1153 | } | 
|  | 1154 |  | 
| Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1155 | /* | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1156 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 1157 | * all application-set variables, function pointers and data. | 
|  | 1158 | */ | 
|  | 1159 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) | 
|  | 1160 | { | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1161 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1162 | } | 
|  | 1163 |  | 
|  | 1164 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1165 | * SSL set accessors | 
|  | 1166 | */ | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1167 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) | 
| 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->endpoint   = endpoint; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1170 | } | 
|  | 1171 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1172 | 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] | 1173 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1174 | conf->transport = transport; | 
| Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1175 | } | 
|  | 1176 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1177 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1178 | 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] | 1179 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1180 | conf->anti_replay = mode; | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1181 | } | 
|  | 1182 | #endif | 
|  | 1183 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1184 | 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] | 1185 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1186 | conf->badmac_limit = limit; | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1187 | } | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1188 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1189 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1190 |  | 
| Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1191 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, | 
|  | 1192 | unsigned allow_packing ) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1193 | { | 
|  | 1194 | ssl->disable_datagram_packing = !allow_packing; | 
|  | 1195 | } | 
|  | 1196 |  | 
|  | 1197 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, | 
|  | 1198 | uint32_t min, uint32_t max ) | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1199 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1200 | conf->hs_timeout_min = min; | 
|  | 1201 | conf->hs_timeout_max = max; | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1202 | } | 
|  | 1203 | #endif | 
|  | 1204 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1205 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1206 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1207 | conf->authmode   = authmode; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1208 | } | 
|  | 1209 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1210 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1211 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1212 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1213 | void *p_vrfy ) | 
|  | 1214 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1215 | conf->f_vrfy      = f_vrfy; | 
|  | 1216 | conf->p_vrfy      = p_vrfy; | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1217 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1218 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1219 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1220 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1221 | int (*f_rng)(void *, unsigned char *, size_t), | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1222 | void *p_rng ) | 
|  | 1223 | { | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1224 | conf->f_rng      = f_rng; | 
|  | 1225 | conf->p_rng      = p_rng; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1226 | } | 
|  | 1227 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1228 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1229 | void (*f_dbg)(void *, int, const char *, int, const char *), | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1230 | void  *p_dbg ) | 
|  | 1231 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1232 | conf->f_dbg      = f_dbg; | 
|  | 1233 | conf->p_dbg      = p_dbg; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1234 | } | 
|  | 1235 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1236 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1237 | void *p_bio, | 
| Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1238 | mbedtls_ssl_send_t *f_send, | 
|  | 1239 | mbedtls_ssl_recv_t *f_recv, | 
|  | 1240 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1241 | { | 
|  | 1242 | ssl->p_bio          = p_bio; | 
|  | 1243 | ssl->f_send         = f_send; | 
|  | 1244 | ssl->f_recv         = f_recv; | 
|  | 1245 | ssl->f_recv_timeout = f_recv_timeout; | 
| Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1246 | } | 
|  | 1247 |  | 
| Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1248 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 1249 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) | 
|  | 1250 | { | 
|  | 1251 | ssl->mtu = mtu; | 
|  | 1252 | } | 
|  | 1253 | #endif | 
|  | 1254 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1255 | 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] | 1256 | { | 
|  | 1257 | conf->read_timeout   = timeout; | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1258 | } | 
|  | 1259 |  | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1260 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, | 
|  | 1261 | void *p_timer, | 
| Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1262 | mbedtls_ssl_set_timer_t *f_set_timer, | 
|  | 1263 | mbedtls_ssl_get_timer_t *f_get_timer ) | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1264 | { | 
|  | 1265 | ssl->p_timer        = p_timer; | 
|  | 1266 | ssl->f_set_timer    = f_set_timer; | 
|  | 1267 | ssl->f_get_timer    = f_get_timer; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1268 |  | 
|  | 1269 | /* Make sure we start with no timer running */ | 
| Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1270 | mbedtls_ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1271 | } | 
|  | 1272 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1273 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Glenn Strauss | 2ed9527 | 2022-01-21 18:02:17 -0500 | [diff] [blame] | 1274 | void mbedtls_ssl_conf_cert_cb( mbedtls_ssl_config *conf, | 
|  | 1275 | int (*f_cert_cb)(mbedtls_ssl_context *) ) | 
|  | 1276 | { | 
|  | 1277 | conf->f_cert_cb = f_cert_cb; | 
|  | 1278 | } | 
|  | 1279 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 1280 |  | 
|  | 1281 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1282 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, | 
| Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1283 | void *p_cache, | 
|  | 1284 | mbedtls_ssl_cache_get_t *f_get_cache, | 
|  | 1285 | mbedtls_ssl_cache_set_t *f_set_cache ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1286 | { | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1287 | conf->p_cache = p_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1288 | conf->f_get_cache = f_get_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1289 | conf->f_set_cache = f_set_cache; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1290 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1291 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1292 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1293 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 1294 | 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] | 1295 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1296 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1297 |  | 
|  | 1298 | if( ssl == NULL || | 
|  | 1299 | session == NULL || | 
|  | 1300 | ssl->session_negotiate == NULL || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1301 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1302 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1303 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1304 | } | 
|  | 1305 |  | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1306 | if( ssl->handshake->resume == 1 ) | 
|  | 1307 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 1308 |  | 
| Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1309 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, | 
|  | 1310 | session ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1311 | return( ret ); | 
|  | 1312 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1313 | ssl->handshake->resume = 1; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1314 |  | 
|  | 1315 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1316 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1317 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1318 |  | 
| Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1319 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, | 
|  | 1320 | const int *ciphersuites ) | 
|  | 1321 | { | 
| Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1322 | conf->ciphersuite_list = ciphersuites; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1323 | } | 
|  | 1324 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1325 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1326 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1327 | const int kex_modes ) | 
|  | 1328 | { | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1329 | 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] | 1330 | } | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1331 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1332 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1333 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1334 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, | 
| Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1335 | const mbedtls_x509_crt_profile *profile ) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1336 | { | 
|  | 1337 | conf->cert_profile = profile; | 
|  | 1338 | } | 
|  | 1339 |  | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1340 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) | 
|  | 1341 | { | 
|  | 1342 | mbedtls_ssl_key_cert *cur = key_cert, *next; | 
|  | 1343 |  | 
|  | 1344 | while( cur != NULL ) | 
|  | 1345 | { | 
|  | 1346 | next = cur->next; | 
|  | 1347 | mbedtls_free( cur ); | 
|  | 1348 | cur = next; | 
|  | 1349 | } | 
|  | 1350 | } | 
|  | 1351 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1352 | /* Append a new keycert entry to a (possibly empty) list */ | 
|  | 1353 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, | 
|  | 1354 | mbedtls_x509_crt *cert, | 
|  | 1355 | mbedtls_pk_context *key ) | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1356 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1357 | mbedtls_ssl_key_cert *new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1358 |  | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1359 | if( cert == NULL ) | 
|  | 1360 | { | 
|  | 1361 | /* Free list if cert is null */ | 
|  | 1362 | ssl_key_cert_free( *head ); | 
|  | 1363 | *head = NULL; | 
|  | 1364 | return( 0 ); | 
|  | 1365 | } | 
|  | 1366 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1367 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); | 
|  | 1368 | if( new_cert == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1369 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1370 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1371 | new_cert->cert = cert; | 
|  | 1372 | new_cert->key  = key; | 
|  | 1373 | new_cert->next = NULL; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1374 |  | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1375 | /* Update head if the list was null, else add to the end */ | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1376 | if( *head == NULL ) | 
| Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1377 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1378 | *head = new_cert; | 
| Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1379 | } | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1380 | else | 
|  | 1381 | { | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1382 | mbedtls_ssl_key_cert *cur = *head; | 
|  | 1383 | while( cur->next != NULL ) | 
|  | 1384 | cur = cur->next; | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1385 | cur->next = new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1386 | } | 
|  | 1387 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1388 | return( 0 ); | 
|  | 1389 | } | 
|  | 1390 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1391 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1392 | mbedtls_x509_crt *own_cert, | 
|  | 1393 | mbedtls_pk_context *pk_key ) | 
|  | 1394 | { | 
| Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1395 | 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] | 1396 | } | 
|  | 1397 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1398 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1399 | mbedtls_x509_crt *ca_chain, | 
|  | 1400 | mbedtls_x509_crl *ca_crl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1401 | { | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1402 | conf->ca_chain   = ca_chain; | 
|  | 1403 | conf->ca_crl     = ca_crl; | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1404 |  | 
|  | 1405 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
|  | 1406 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() | 
|  | 1407 | * cannot be used together. */ | 
|  | 1408 | conf->f_ca_cb = NULL; | 
|  | 1409 | conf->p_ca_cb = NULL; | 
|  | 1410 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1411 | } | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1412 |  | 
|  | 1413 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
|  | 1414 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, | 
| Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1415 | mbedtls_x509_crt_ca_cb_t f_ca_cb, | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1416 | void *p_ca_cb ) | 
|  | 1417 | { | 
|  | 1418 | conf->f_ca_cb = f_ca_cb; | 
|  | 1419 | conf->p_ca_cb = p_ca_cb; | 
|  | 1420 |  | 
|  | 1421 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() | 
|  | 1422 | * cannot be used together. */ | 
|  | 1423 | conf->ca_chain   = NULL; | 
|  | 1424 | conf->ca_crl     = NULL; | 
|  | 1425 | } | 
|  | 1426 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1427 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1428 |  | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1429 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1430 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, | 
|  | 1431 | size_t *name_len ) | 
|  | 1432 | { | 
|  | 1433 | *name_len = ssl->handshake->sni_name_len; | 
|  | 1434 | return( ssl->handshake->sni_name ); | 
|  | 1435 | } | 
|  | 1436 |  | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1437 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, | 
|  | 1438 | mbedtls_x509_crt *own_cert, | 
|  | 1439 | mbedtls_pk_context *pk_key ) | 
|  | 1440 | { | 
|  | 1441 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, | 
|  | 1442 | own_cert, pk_key ) ); | 
|  | 1443 | } | 
| Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1444 |  | 
|  | 1445 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, | 
|  | 1446 | mbedtls_x509_crt *ca_chain, | 
|  | 1447 | mbedtls_x509_crl *ca_crl ) | 
|  | 1448 | { | 
|  | 1449 | ssl->handshake->sni_ca_chain   = ca_chain; | 
|  | 1450 | ssl->handshake->sni_ca_crl     = ca_crl; | 
|  | 1451 | } | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1452 |  | 
|  | 1453 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, | 
|  | 1454 | int authmode ) | 
|  | 1455 | { | 
|  | 1456 | ssl->handshake->sni_authmode = authmode; | 
|  | 1457 | } | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1458 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
|  | 1459 |  | 
| Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1460 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 1461 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, | 
|  | 1462 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | 
|  | 1463 | void *p_vrfy ) | 
|  | 1464 | { | 
|  | 1465 | ssl->f_vrfy = f_vrfy; | 
|  | 1466 | ssl->p_vrfy = p_vrfy; | 
|  | 1467 | } | 
|  | 1468 | #endif | 
|  | 1469 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1470 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1471 | /* | 
|  | 1472 | * Set EC J-PAKE password for current handshake | 
|  | 1473 | */ | 
|  | 1474 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, | 
|  | 1475 | const unsigned char *pw, | 
|  | 1476 | size_t pw_len ) | 
|  | 1477 | { | 
|  | 1478 | mbedtls_ecjpake_role role; | 
|  | 1479 |  | 
| Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1480 | if( ssl->handshake == NULL || ssl->conf == NULL ) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1481 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1482 |  | 
|  | 1483 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 1484 | role = MBEDTLS_ECJPAKE_SERVER; | 
|  | 1485 | else | 
|  | 1486 | role = MBEDTLS_ECJPAKE_CLIENT; | 
|  | 1487 |  | 
|  | 1488 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, | 
|  | 1489 | role, | 
|  | 1490 | MBEDTLS_MD_SHA256, | 
|  | 1491 | MBEDTLS_ECP_DP_SECP256R1, | 
|  | 1492 | pw, pw_len ) ); | 
|  | 1493 | } | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1494 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1495 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1496 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1497 |  | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1498 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) | 
|  | 1499 | { | 
|  | 1500 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 1501 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) | 
|  | 1502 | return( 1 ); | 
|  | 1503 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 1504 |  | 
|  | 1505 | if( conf->psk != NULL ) | 
|  | 1506 | return( 1 ); | 
|  | 1507 |  | 
|  | 1508 | return( 0 ); | 
|  | 1509 | } | 
|  | 1510 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1511 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) | 
|  | 1512 | { | 
|  | 1513 | /* Remove reference to existing PSK, if any. */ | 
|  | 1514 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1515 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1516 | { | 
|  | 1517 | /* The maintenance of the PSK key slot is the | 
|  | 1518 | * user's responsibility. */ | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1519 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1520 | } | 
| Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 1521 | /* This and the following branch should never | 
|  | 1522 | * be taken simultaenously as we maintain the | 
|  | 1523 | * invariant that raw and opaque PSKs are never | 
|  | 1524 | * configured simultaneously. As a safeguard, | 
|  | 1525 | * though, `else` is omitted here. */ | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1526 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 1527 | if( conf->psk != NULL ) | 
|  | 1528 | { | 
|  | 1529 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | 
|  | 1530 |  | 
|  | 1531 | mbedtls_free( conf->psk ); | 
|  | 1532 | conf->psk = NULL; | 
|  | 1533 | conf->psk_len = 0; | 
|  | 1534 | } | 
|  | 1535 |  | 
|  | 1536 | /* Remove reference to PSK identity, if any. */ | 
|  | 1537 | if( conf->psk_identity != NULL ) | 
|  | 1538 | { | 
|  | 1539 | mbedtls_free( conf->psk_identity ); | 
|  | 1540 | conf->psk_identity = NULL; | 
|  | 1541 | conf->psk_identity_len = 0; | 
|  | 1542 | } | 
|  | 1543 | } | 
|  | 1544 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1545 | /* This function assumes that PSK identity in the SSL config is unset. | 
|  | 1546 | * It checks that the provided identity is well-formed and attempts | 
|  | 1547 | * to make a copy of it in the SSL config. | 
|  | 1548 | * On failure, the PSK identity in the config remains unset. */ | 
|  | 1549 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, | 
|  | 1550 | unsigned char const *psk_identity, | 
|  | 1551 | size_t psk_identity_len ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1552 | { | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1553 | /* Identity len will be encoded on two bytes */ | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1554 | if( psk_identity               == NULL || | 
|  | 1555 | ( psk_identity_len >> 16 ) != 0    || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1556 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1557 | { | 
|  | 1558 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1559 | } | 
|  | 1560 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1561 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); | 
|  | 1562 | if( conf->psk_identity == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1563 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1564 |  | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1565 | conf->psk_identity_len = psk_identity_len; | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1566 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); | 
| Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1567 |  | 
|  | 1568 | return( 0 ); | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1569 | } | 
|  | 1570 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1571 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, | 
|  | 1572 | const unsigned char *psk, size_t psk_len, | 
|  | 1573 | const unsigned char *psk_identity, size_t psk_identity_len ) | 
|  | 1574 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1575 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1576 |  | 
|  | 1577 | /* We currently only support one PSK, raw or opaque. */ | 
|  | 1578 | if( ssl_conf_psk_is_configured( conf ) ) | 
|  | 1579 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1580 |  | 
|  | 1581 | /* Check and set raw PSK */ | 
| Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1582 | if( psk == NULL ) | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1583 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1584 | if( psk_len == 0 ) | 
|  | 1585 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1586 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | 
|  | 1587 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1588 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1589 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) | 
|  | 1590 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 1591 | conf->psk_len = psk_len; | 
|  | 1592 | memcpy( conf->psk, psk, conf->psk_len ); | 
|  | 1593 |  | 
|  | 1594 | /* Check and set PSK Identity */ | 
|  | 1595 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); | 
|  | 1596 | if( ret != 0 ) | 
|  | 1597 | ssl_conf_remove_psk( conf ); | 
|  | 1598 |  | 
|  | 1599 | return( ret ); | 
|  | 1600 | } | 
|  | 1601 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1602 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) | 
|  | 1603 | { | 
|  | 1604 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1605 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1606 | { | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1607 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1608 | } | 
|  | 1609 | else | 
|  | 1610 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 1611 | if( ssl->handshake->psk != NULL ) | 
|  | 1612 | { | 
|  | 1613 | mbedtls_platform_zeroize( ssl->handshake->psk, | 
|  | 1614 | ssl->handshake->psk_len ); | 
|  | 1615 | mbedtls_free( ssl->handshake->psk ); | 
|  | 1616 | ssl->handshake->psk_len = 0; | 
|  | 1617 | } | 
|  | 1618 | } | 
|  | 1619 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1620 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, | 
|  | 1621 | const unsigned char *psk, size_t psk_len ) | 
|  | 1622 | { | 
|  | 1623 | if( psk == NULL || ssl->handshake == NULL ) | 
|  | 1624 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1625 |  | 
|  | 1626 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | 
|  | 1627 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1628 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1629 | ssl_remove_psk( ssl ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1630 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1631 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1632 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1633 |  | 
|  | 1634 | ssl->handshake->psk_len = psk_len; | 
|  | 1635 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); | 
|  | 1636 |  | 
|  | 1637 | return( 0 ); | 
|  | 1638 | } | 
|  | 1639 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1640 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 1641 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, | 
| Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1642 | mbedtls_svc_key_id_t psk, | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1643 | const unsigned char *psk_identity, | 
|  | 1644 | size_t psk_identity_len ) | 
|  | 1645 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1646 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1647 |  | 
|  | 1648 | /* We currently only support one PSK, raw or opaque. */ | 
|  | 1649 | if( ssl_conf_psk_is_configured( conf ) ) | 
|  | 1650 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1651 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1652 | /* Check and set opaque PSK */ | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1653 | if( mbedtls_svc_key_id_is_null( psk ) ) | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1654 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1655 | conf->psk_opaque = psk; | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1656 |  | 
|  | 1657 | /* Check and set PSK Identity */ | 
|  | 1658 | ret = ssl_conf_set_psk_identity( conf, psk_identity, | 
|  | 1659 | psk_identity_len ); | 
|  | 1660 | if( ret != 0 ) | 
|  | 1661 | ssl_conf_remove_psk( conf ); | 
|  | 1662 |  | 
|  | 1663 | return( ret ); | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1664 | } | 
|  | 1665 |  | 
| Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1666 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, | 
|  | 1667 | mbedtls_svc_key_id_t psk ) | 
|  | 1668 | { | 
|  | 1669 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || | 
|  | 1670 | ( ssl->handshake == NULL ) ) | 
|  | 1671 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1672 |  | 
|  | 1673 | ssl_remove_psk( ssl ); | 
|  | 1674 | ssl->handshake->psk_opaque = psk; | 
|  | 1675 | return( 0 ); | 
|  | 1676 | } | 
|  | 1677 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 1678 |  | 
|  | 1679 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, | 
|  | 1680 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, | 
|  | 1681 | size_t), | 
|  | 1682 | void *p_psk ) | 
|  | 1683 | { | 
|  | 1684 | conf->f_psk = f_psk; | 
|  | 1685 | conf->p_psk = p_psk; | 
|  | 1686 | } | 
|  | 1687 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
|  | 1688 |  | 
|  | 1689 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1690 | 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] | 1691 | size_t taglen, | 
|  | 1692 | psa_algorithm_t *alg, | 
|  | 1693 | psa_key_type_t *key_type, | 
|  | 1694 | size_t *key_size ) | 
|  | 1695 | { | 
|  | 1696 | switch ( mbedtls_cipher_type ) | 
|  | 1697 | { | 
|  | 1698 | case MBEDTLS_CIPHER_AES_128_CBC: | 
|  | 1699 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1700 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1701 | *key_size = 128; | 
|  | 1702 | break; | 
|  | 1703 | case MBEDTLS_CIPHER_AES_128_CCM: | 
|  | 1704 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1705 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1706 | *key_size = 128; | 
|  | 1707 | break; | 
|  | 1708 | case MBEDTLS_CIPHER_AES_128_GCM: | 
|  | 1709 | *alg = PSA_ALG_GCM; | 
|  | 1710 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1711 | *key_size = 128; | 
|  | 1712 | break; | 
|  | 1713 | case MBEDTLS_CIPHER_AES_192_CCM: | 
|  | 1714 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1715 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1716 | *key_size = 192; | 
|  | 1717 | break; | 
|  | 1718 | case MBEDTLS_CIPHER_AES_192_GCM: | 
|  | 1719 | *alg = PSA_ALG_GCM; | 
|  | 1720 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1721 | *key_size = 192; | 
|  | 1722 | break; | 
|  | 1723 | case MBEDTLS_CIPHER_AES_256_CBC: | 
|  | 1724 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1725 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1726 | *key_size = 256; | 
|  | 1727 | break; | 
|  | 1728 | case MBEDTLS_CIPHER_AES_256_CCM: | 
|  | 1729 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1730 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1731 | *key_size = 256; | 
|  | 1732 | break; | 
|  | 1733 | case MBEDTLS_CIPHER_AES_256_GCM: | 
|  | 1734 | *alg = PSA_ALG_GCM; | 
|  | 1735 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 1736 | *key_size = 256; | 
|  | 1737 | break; | 
|  | 1738 | case MBEDTLS_CIPHER_ARIA_128_CBC: | 
|  | 1739 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1740 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1741 | *key_size = 128; | 
|  | 1742 | break; | 
|  | 1743 | case MBEDTLS_CIPHER_ARIA_128_CCM: | 
|  | 1744 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1745 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1746 | *key_size = 128; | 
|  | 1747 | break; | 
|  | 1748 | case MBEDTLS_CIPHER_ARIA_128_GCM: | 
|  | 1749 | *alg = PSA_ALG_GCM; | 
|  | 1750 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1751 | *key_size = 128; | 
|  | 1752 | break; | 
|  | 1753 | case MBEDTLS_CIPHER_ARIA_192_CCM: | 
|  | 1754 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1755 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1756 | *key_size = 192; | 
|  | 1757 | break; | 
|  | 1758 | case MBEDTLS_CIPHER_ARIA_192_GCM: | 
|  | 1759 | *alg = PSA_ALG_GCM; | 
|  | 1760 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1761 | *key_size = 192; | 
|  | 1762 | break; | 
|  | 1763 | case MBEDTLS_CIPHER_ARIA_256_CBC: | 
|  | 1764 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1765 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1766 | *key_size = 256; | 
|  | 1767 | break; | 
|  | 1768 | case MBEDTLS_CIPHER_ARIA_256_CCM: | 
|  | 1769 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1770 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1771 | *key_size = 256; | 
|  | 1772 | break; | 
|  | 1773 | case MBEDTLS_CIPHER_ARIA_256_GCM: | 
|  | 1774 | *alg = PSA_ALG_GCM; | 
|  | 1775 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 1776 | *key_size = 256; | 
|  | 1777 | break; | 
|  | 1778 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: | 
|  | 1779 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1780 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1781 | *key_size = 128; | 
|  | 1782 | break; | 
|  | 1783 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: | 
|  | 1784 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1785 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1786 | *key_size = 128; | 
|  | 1787 | break; | 
|  | 1788 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: | 
|  | 1789 | *alg = PSA_ALG_GCM; | 
|  | 1790 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1791 | *key_size = 128; | 
|  | 1792 | break; | 
|  | 1793 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: | 
|  | 1794 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1795 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1796 | *key_size = 192; | 
|  | 1797 | break; | 
|  | 1798 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: | 
|  | 1799 | *alg = PSA_ALG_GCM; | 
|  | 1800 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1801 | *key_size = 192; | 
|  | 1802 | break; | 
|  | 1803 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: | 
|  | 1804 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 1805 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1806 | *key_size = 256; | 
|  | 1807 | break; | 
|  | 1808 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: | 
|  | 1809 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | 
|  | 1810 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1811 | *key_size = 256; | 
|  | 1812 | break; | 
|  | 1813 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: | 
|  | 1814 | *alg = PSA_ALG_GCM; | 
|  | 1815 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 1816 | *key_size = 256; | 
|  | 1817 | break; | 
|  | 1818 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: | 
|  | 1819 | *alg = PSA_ALG_CHACHA20_POLY1305; | 
|  | 1820 | *key_type = PSA_KEY_TYPE_CHACHA20; | 
|  | 1821 | *key_size = 256; | 
|  | 1822 | break; | 
|  | 1823 | case MBEDTLS_CIPHER_NULL: | 
|  | 1824 | *alg = MBEDTLS_SSL_NULL_CIPHER; | 
|  | 1825 | *key_type = 0; | 
|  | 1826 | *key_size = 0; | 
|  | 1827 | break; | 
|  | 1828 | default: | 
|  | 1829 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 1830 | } | 
|  | 1831 |  | 
|  | 1832 | return PSA_SUCCESS; | 
|  | 1833 | } | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1834 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 1835 |  | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 1836 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1837 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, | 
|  | 1838 | const unsigned char *dhm_P, size_t P_len, | 
|  | 1839 | const unsigned char *dhm_G, size_t G_len ) | 
|  | 1840 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1841 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1842 |  | 
| Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 1843 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 1844 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 1845 |  | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1846 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || | 
|  | 1847 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) | 
|  | 1848 | { | 
|  | 1849 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 1850 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 1851 | return( ret ); | 
|  | 1852 | } | 
|  | 1853 |  | 
|  | 1854 | return( 0 ); | 
|  | 1855 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1856 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1857 | 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] | 1858 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1859 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1860 |  | 
| Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 1861 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 1862 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 1863 |  | 
| Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 1864 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, | 
|  | 1865 | &conf->dhm_P ) ) != 0 || | 
|  | 1866 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, | 
|  | 1867 | &conf->dhm_G ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 1868 | { | 
|  | 1869 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 1870 | mbedtls_mpi_free( &conf->dhm_G ); | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1871 | return( ret ); | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 1872 | } | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1873 |  | 
|  | 1874 | return( 0 ); | 
|  | 1875 | } | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 1876 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1877 |  | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 1878 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 1879 | /* | 
|  | 1880 | * Set the minimum length for Diffie-Hellman parameters | 
|  | 1881 | */ | 
|  | 1882 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, | 
|  | 1883 | unsigned int bitlen ) | 
|  | 1884 | { | 
|  | 1885 | conf->dhm_min_bitlen = bitlen; | 
|  | 1886 | } | 
|  | 1887 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ | 
|  | 1888 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1889 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 1890 | #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] | 1891 | /* | 
|  | 1892 | * Set allowed/preferred hashes for handshake signatures | 
|  | 1893 | */ | 
|  | 1894 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, | 
|  | 1895 | const int *hashes ) | 
|  | 1896 | { | 
|  | 1897 | conf->sig_hashes = hashes; | 
|  | 1898 | } | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 1899 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1900 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1901 | /* Configure allowed signature algorithms for handshake */ | 
| Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1902 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, | 
|  | 1903 | const uint16_t* sig_algs ) | 
|  | 1904 | { | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1905 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 1906 | conf->sig_hashes = NULL; | 
|  | 1907 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 1908 | conf->sig_algs = sig_algs; | 
| Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1909 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1910 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 1911 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 1912 | #if defined(MBEDTLS_ECP_C) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1913 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1914 | /* | 
|  | 1915 | * Set the allowed elliptic curves | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1916 | * | 
|  | 1917 | * mbedtls_ssl_setup() takes the provided list | 
|  | 1918 | * and translates it to a list of IANA TLS group identifiers, | 
|  | 1919 | * stored in ssl->handshake->group_list. | 
|  | 1920 | * | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1921 | */ | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1922 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1923 | const mbedtls_ecp_group_id *curve_list ) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1924 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1925 | conf->curve_list = curve_list; | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1926 | conf->group_list = NULL; | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1927 | } | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1928 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1929 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1930 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1931 | /* | 
|  | 1932 | * Set the allowed groups | 
|  | 1933 | */ | 
|  | 1934 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, | 
|  | 1935 | const uint16_t *group_list ) | 
|  | 1936 | { | 
|  | 1937 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 1938 | conf->curve_list = NULL; | 
|  | 1939 | #endif | 
|  | 1940 | conf->group_list = group_list; | 
|  | 1941 | } | 
|  | 1942 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1943 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1944 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1945 | { | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1946 | /* Initialize to suppress unnecessary compiler warning */ | 
|  | 1947 | size_t hostname_len = 0; | 
|  | 1948 |  | 
|  | 1949 | /* Check if new hostname is valid before | 
|  | 1950 | * making any change to current one */ | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1951 | if( hostname != NULL ) | 
|  | 1952 | { | 
|  | 1953 | hostname_len = strlen( hostname ); | 
|  | 1954 |  | 
|  | 1955 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) | 
|  | 1956 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1957 | } | 
|  | 1958 |  | 
|  | 1959 | /* Now it's clear that we will overwrite the old hostname, | 
|  | 1960 | * so we can free it safely */ | 
|  | 1961 |  | 
|  | 1962 | if( ssl->hostname != NULL ) | 
|  | 1963 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1964 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1965 | mbedtls_free( ssl->hostname ); | 
|  | 1966 | } | 
|  | 1967 |  | 
|  | 1968 | /* Passing NULL as hostname shall clear the old one */ | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 1969 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1970 | if( hostname == NULL ) | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1971 | { | 
|  | 1972 | ssl->hostname = NULL; | 
|  | 1973 | } | 
|  | 1974 | else | 
|  | 1975 | { | 
|  | 1976 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1977 | if( ssl->hostname == NULL ) | 
|  | 1978 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 1979 |  | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1980 | memcpy( ssl->hostname, hostname, hostname_len ); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 1981 |  | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1982 | ssl->hostname[hostname_len] = '\0'; | 
|  | 1983 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1984 |  | 
|  | 1985 | return( 0 ); | 
|  | 1986 | } | 
| Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 1987 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1988 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1989 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1990 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1991 | int (*f_sni)(void *, mbedtls_ssl_context *, | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1992 | const unsigned char *, size_t), | 
|  | 1993 | void *p_sni ) | 
|  | 1994 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1995 | conf->f_sni = f_sni; | 
|  | 1996 | conf->p_sni = p_sni; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1997 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1998 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1999 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2000 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2001 | 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] | 2002 | { | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2003 | size_t cur_len, tot_len; | 
|  | 2004 | const char **p; | 
|  | 2005 |  | 
|  | 2006 | /* | 
| Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2007 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings | 
|  | 2008 | * MUST NOT be truncated." | 
|  | 2009 | * We check lengths now rather than later. | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2010 | */ | 
|  | 2011 | tot_len = 0; | 
|  | 2012 | for( p = protos; *p != NULL; p++ ) | 
|  | 2013 | { | 
|  | 2014 | cur_len = strlen( *p ); | 
|  | 2015 | tot_len += cur_len; | 
|  | 2016 |  | 
| Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2017 | if( ( cur_len == 0 ) || | 
|  | 2018 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || | 
|  | 2019 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2020 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2021 | } | 
|  | 2022 |  | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2023 | conf->alpn_list = protos; | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2024 |  | 
|  | 2025 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2026 | } | 
|  | 2027 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2028 | 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] | 2029 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2030 | return( ssl->alpn_chosen ); | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2031 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2032 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2033 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2034 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2035 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, | 
|  | 2036 | int support_mki_value ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2037 | { | 
|  | 2038 | conf->dtls_srtp_mki_support = support_mki_value; | 
|  | 2039 | } | 
|  | 2040 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2041 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, | 
|  | 2042 | unsigned char *mki_value, | 
| Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2043 | uint16_t mki_len ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2044 | { | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2045 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2046 | { | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2047 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2048 | } | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2049 |  | 
|  | 2050 | 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] | 2051 | { | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2052 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2053 | } | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2054 |  | 
|  | 2055 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); | 
|  | 2056 | ssl->dtls_srtp_info.mki_len = mki_len; | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2057 | return( 0 ); | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2058 | } | 
|  | 2059 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2060 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2061 | const mbedtls_ssl_srtp_profile *profiles ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2062 | { | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2063 | const mbedtls_ssl_srtp_profile *p; | 
|  | 2064 | size_t list_size = 0; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2065 |  | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2066 | /* check the profiles list: all entry must be valid, | 
|  | 2067 | * 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] | 2068 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && | 
|  | 2069 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; | 
|  | 2070 | p++ ) | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2071 | { | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2072 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2073 | { | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2074 | list_size++; | 
|  | 2075 | } | 
|  | 2076 | else | 
|  | 2077 | { | 
|  | 2078 | /* unsupported value, stop parsing and set the size to an error value */ | 
|  | 2079 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2080 | } | 
|  | 2081 | } | 
|  | 2082 |  | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2083 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) | 
| Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2084 | { | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2085 | conf->dtls_srtp_profile_list = NULL; | 
|  | 2086 | conf->dtls_srtp_profile_list_len = 0; | 
|  | 2087 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2088 | } | 
|  | 2089 |  | 
| Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2090 | conf->dtls_srtp_profile_list = profiles; | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2091 | conf->dtls_srtp_profile_list_len = list_size; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2092 |  | 
|  | 2093 | return( 0 ); | 
|  | 2094 | } | 
|  | 2095 |  | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2096 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, | 
|  | 2097 | mbedtls_dtls_srtp_info *dtls_srtp_info ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2098 | { | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2099 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; | 
|  | 2100 | /* do not copy the mki value if there is no chosen profile */ | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2101 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) | 
| Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2102 | { | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2103 | dtls_srtp_info->mki_len = 0; | 
| Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2104 | } | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2105 | else | 
|  | 2106 | { | 
|  | 2107 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2108 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, | 
|  | 2109 | ssl->dtls_srtp_info.mki_len ); | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2110 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2111 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2112 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 2113 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2114 | 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] | 2115 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2116 | conf->max_major_ver = major; | 
|  | 2117 | conf->max_minor_ver = minor; | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2118 | } | 
|  | 2119 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2120 | 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] | 2121 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2122 | conf->min_major_ver = major; | 
|  | 2123 | conf->min_minor_ver = minor; | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2124 | } | 
|  | 2125 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2126 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 2127 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, | 
|  | 2128 | char cert_req_ca_list ) | 
|  | 2129 | { | 
|  | 2130 | conf->cert_req_ca_list = cert_req_ca_list; | 
|  | 2131 | } | 
|  | 2132 | #endif | 
|  | 2133 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2134 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2135 | 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] | 2136 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2137 | conf->encrypt_then_mac = etm; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2138 | } | 
|  | 2139 | #endif | 
|  | 2140 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2141 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2142 | 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] | 2143 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2144 | conf->extended_ms = ems; | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2145 | } | 
|  | 2146 | #endif | 
|  | 2147 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2148 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2149 | 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] | 2150 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2151 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2152 | 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] | 2153 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2154 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2155 | } | 
|  | 2156 |  | 
| Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2157 | conf->mfl_code = mfl_code; | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2158 |  | 
|  | 2159 | return( 0 ); | 
|  | 2160 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2161 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2162 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2163 | 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] | 2164 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2165 | conf->allow_legacy_renegotiation = allow_legacy; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2166 | } | 
|  | 2167 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2168 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2169 | 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] | 2170 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2171 | conf->disable_renegotiation = renegotiation; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2172 | } | 
|  | 2173 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2174 | 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] | 2175 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2176 | conf->renego_max_records = max_records; | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2177 | } | 
|  | 2178 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2179 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2180 | const unsigned char period[8] ) | 
|  | 2181 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2182 | memcpy( conf->renego_period, period, 8 ); | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2183 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2184 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2185 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2186 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2187 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 2188 | 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] | 2189 | { | 
| Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2190 | conf->session_tickets = use_tickets; | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2191 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2192 | #endif | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2193 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2194 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2195 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, | 
|  | 2196 | mbedtls_ssl_ticket_write_t *f_ticket_write, | 
|  | 2197 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, | 
|  | 2198 | void *p_ticket ) | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2199 | { | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2200 | conf->f_ticket_write = f_ticket_write; | 
|  | 2201 | conf->f_ticket_parse = f_ticket_parse; | 
|  | 2202 | conf->p_ticket       = p_ticket; | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2203 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2204 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2205 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2206 |  | 
| Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2207 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, | 
|  | 2208 | mbedtls_ssl_export_keys_t *f_export_keys, | 
|  | 2209 | void *p_export_keys ) | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2210 | { | 
| Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2211 | ssl->f_export_keys = f_export_keys; | 
|  | 2212 | ssl->p_export_keys = p_export_keys; | 
| Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2213 | } | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2214 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2215 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2216 | void mbedtls_ssl_conf_async_private_cb( | 
|  | 2217 | mbedtls_ssl_config *conf, | 
|  | 2218 | mbedtls_ssl_async_sign_t *f_async_sign, | 
|  | 2219 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, | 
|  | 2220 | mbedtls_ssl_async_resume_t *f_async_resume, | 
|  | 2221 | mbedtls_ssl_async_cancel_t *f_async_cancel, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2222 | void *async_config_data ) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2223 | { | 
|  | 2224 | conf->f_async_sign_start = f_async_sign; | 
|  | 2225 | conf->f_async_decrypt_start = f_async_decrypt; | 
|  | 2226 | conf->f_async_resume = f_async_resume; | 
|  | 2227 | conf->f_async_cancel = f_async_cancel; | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2228 | conf->p_async_config_data = async_config_data; | 
|  | 2229 | } | 
|  | 2230 |  | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2231 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) | 
|  | 2232 | { | 
|  | 2233 | return( conf->p_async_config_data ); | 
|  | 2234 | } | 
|  | 2235 |  | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2236 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2237 | { | 
|  | 2238 | if( ssl->handshake == NULL ) | 
|  | 2239 | return( NULL ); | 
|  | 2240 | else | 
|  | 2241 | return( ssl->handshake->user_async_ctx ); | 
|  | 2242 | } | 
|  | 2243 |  | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2244 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2245 | void *ctx ) | 
|  | 2246 | { | 
|  | 2247 | if( ssl->handshake != NULL ) | 
|  | 2248 | ssl->handshake->user_async_ctx = ctx; | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2249 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2250 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2251 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2252 | /* | 
|  | 2253 | * SSL get accessors | 
|  | 2254 | */ | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2255 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2256 | { | 
| Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2257 | if( ssl->session != NULL ) | 
|  | 2258 | return( ssl->session->verify_result ); | 
|  | 2259 |  | 
|  | 2260 | if( ssl->session_negotiate != NULL ) | 
|  | 2261 | return( ssl->session_negotiate->verify_result ); | 
|  | 2262 |  | 
| Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2263 | return( 0xFFFFFFFF ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2264 | } | 
|  | 2265 |  | 
| Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2266 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) | 
|  | 2267 | { | 
|  | 2268 | if( ssl == NULL || ssl->session == NULL ) | 
|  | 2269 | return( 0 ); | 
|  | 2270 |  | 
|  | 2271 | return( ssl->session->ciphersuite ); | 
|  | 2272 | } | 
|  | 2273 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2274 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2275 | { | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2276 | if( ssl == NULL || ssl->session == NULL ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2277 | return( NULL ); | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2278 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2279 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2280 | } | 
|  | 2281 |  | 
| Gilles Peskine | e1a0c25 | 2022-01-13 01:08:03 +0100 | [diff] [blame] | 2282 | mbedtls_ssl_protocol_version mbedtls_ssl_get_version_number( | 
|  | 2283 | const mbedtls_ssl_context *ssl ) | 
|  | 2284 | { | 
|  | 2285 | /* For major_ver, only 3 is supported, so skip checking it. */ | 
|  | 2286 | switch( ssl->minor_ver ) | 
|  | 2287 | { | 
|  | 2288 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
|  | 2289 | return( MBEDTLS_SSL_VERSION_1_2 ); | 
|  | 2290 | case MBEDTLS_SSL_MINOR_VERSION_4: | 
|  | 2291 | return( MBEDTLS_SSL_VERSION_1_3 ); | 
|  | 2292 | default: | 
|  | 2293 | return( MBEDTLS_SSL_VERSION_UNKNOWN ); | 
|  | 2294 | } | 
|  | 2295 | } | 
|  | 2296 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2297 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2298 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2299 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2300 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2301 | { | 
|  | 2302 | switch( ssl->minor_ver ) | 
|  | 2303 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2304 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2305 | return( "DTLSv1.2" ); | 
|  | 2306 |  | 
|  | 2307 | default: | 
|  | 2308 | return( "unknown (DTLS)" ); | 
|  | 2309 | } | 
|  | 2310 | } | 
|  | 2311 | #endif | 
|  | 2312 |  | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2313 | switch( ssl->minor_ver ) | 
|  | 2314 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2315 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2316 | return( "TLSv1.2" ); | 
| Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2317 | case MBEDTLS_SSL_MINOR_VERSION_4: | 
|  | 2318 | return( "TLSv1.3" ); | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2319 | default: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2320 | return( "unknown" ); | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2321 | } | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2322 | } | 
|  | 2323 |  | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2324 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2325 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) | 
|  | 2326 | { | 
| David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2327 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2328 | size_t read_mfl; | 
|  | 2329 |  | 
|  | 2330 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ | 
|  | 2331 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 2332 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) | 
|  | 2333 | { | 
|  | 2334 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); | 
|  | 2335 | } | 
|  | 2336 |  | 
|  | 2337 | /* Check if a smaller max length was negotiated */ | 
|  | 2338 | if( ssl->session_out != NULL ) | 
|  | 2339 | { | 
|  | 2340 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); | 
|  | 2341 | if( read_mfl < max_len ) | 
|  | 2342 | { | 
|  | 2343 | max_len = read_mfl; | 
|  | 2344 | } | 
|  | 2345 | } | 
|  | 2346 |  | 
|  | 2347 | // During a handshake, use the value being negotiated | 
|  | 2348 | if( ssl->session_negotiate != NULL ) | 
|  | 2349 | { | 
|  | 2350 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); | 
|  | 2351 | if( read_mfl < max_len ) | 
|  | 2352 | { | 
|  | 2353 | max_len = read_mfl; | 
|  | 2354 | } | 
|  | 2355 | } | 
|  | 2356 |  | 
|  | 2357 | return( max_len ); | 
|  | 2358 | } | 
|  | 2359 |  | 
|  | 2360 | 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] | 2361 | { | 
|  | 2362 | size_t max_len; | 
|  | 2363 |  | 
|  | 2364 | /* | 
|  | 2365 | * Assume mfl_code is correct since it was checked when set | 
|  | 2366 | */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2367 | 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] | 2368 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2369 | /* Check if a smaller max length was negotiated */ | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2370 | if( ssl->session_out != NULL && | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2371 | 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] | 2372 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2373 | 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] | 2374 | } | 
|  | 2375 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2376 | /* During a handshake, use the value being negotiated */ | 
|  | 2377 | if( ssl->session_negotiate != NULL && | 
|  | 2378 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) | 
|  | 2379 | { | 
|  | 2380 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); | 
|  | 2381 | } | 
|  | 2382 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2383 | return( max_len ); | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2384 | } | 
|  | 2385 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 2386 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2387 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2388 | 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] | 2389 | { | 
| Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2390 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ | 
|  | 2391 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 2392 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || | 
|  | 2393 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) | 
|  | 2394 | return ( 0 ); | 
|  | 2395 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2396 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) | 
|  | 2397 | return( ssl->mtu ); | 
|  | 2398 |  | 
|  | 2399 | if( ssl->mtu == 0 ) | 
|  | 2400 | return( ssl->handshake->mtu ); | 
|  | 2401 |  | 
|  | 2402 | return( ssl->mtu < ssl->handshake->mtu ? | 
|  | 2403 | ssl->mtu : ssl->handshake->mtu ); | 
|  | 2404 | } | 
|  | 2405 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 2406 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2407 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) | 
|  | 2408 | { | 
|  | 2409 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; | 
|  | 2410 |  | 
| Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2411 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ | 
|  | 2412 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2413 | (void) ssl; | 
|  | 2414 | #endif | 
|  | 2415 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2416 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2417 | 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] | 2418 |  | 
|  | 2419 | if( max_len > mfl ) | 
|  | 2420 | max_len = mfl; | 
|  | 2421 | #endif | 
|  | 2422 |  | 
|  | 2423 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2424 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2425 | { | 
| Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2426 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2427 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); | 
|  | 2428 | const size_t overhead = (size_t) ret; | 
|  | 2429 |  | 
|  | 2430 | if( ret < 0 ) | 
|  | 2431 | return( ret ); | 
|  | 2432 |  | 
|  | 2433 | if( mtu <= overhead ) | 
|  | 2434 | { | 
|  | 2435 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); | 
|  | 2436 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 2437 | } | 
|  | 2438 |  | 
|  | 2439 | if( max_len > mtu - overhead ) | 
|  | 2440 | max_len = mtu - overhead; | 
|  | 2441 | } | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2442 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2443 |  | 
| Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2444 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) &&        \ | 
|  | 2445 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2446 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2447 | #endif | 
|  | 2448 |  | 
|  | 2449 | return( (int) max_len ); | 
|  | 2450 | } | 
|  | 2451 |  | 
| Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2452 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) | 
|  | 2453 | { | 
|  | 2454 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; | 
|  | 2455 |  | 
|  | 2456 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 2457 | (void) ssl; | 
|  | 2458 | #endif | 
|  | 2459 |  | 
|  | 2460 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 2461 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); | 
|  | 2462 |  | 
|  | 2463 | if( max_len > mfl ) | 
|  | 2464 | max_len = mfl; | 
|  | 2465 | #endif | 
|  | 2466 |  | 
|  | 2467 | return( (int) max_len ); | 
|  | 2468 | } | 
|  | 2469 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2470 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 2471 | 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] | 2472 | { | 
|  | 2473 | if( ssl == NULL || ssl->session == NULL ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2474 | return( NULL ); | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2475 |  | 
| Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2476 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2477 | return( ssl->session->peer_cert ); | 
| Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2478 | #else | 
|  | 2479 | return( NULL ); | 
|  | 2480 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2481 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2482 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2483 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2484 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2485 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, | 
|  | 2486 | mbedtls_ssl_session *dst ) | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2487 | { | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2488 | int ret; | 
|  | 2489 |  | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2490 | if( ssl == NULL || | 
|  | 2491 | dst == NULL || | 
|  | 2492 | ssl->session == NULL || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2493 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2494 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2495 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2496 | } | 
|  | 2497 |  | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2498 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer | 
|  | 2499 | * idempotent: Each session can only be exported once. | 
|  | 2500 | * | 
|  | 2501 | * (This is in preparation for TLS 1.3 support where we will | 
|  | 2502 | * need the ability to export multiple sessions (aka tickets), | 
|  | 2503 | * which will be achieved by calling mbedtls_ssl_get_session() | 
|  | 2504 | * multiple times until it fails.) | 
|  | 2505 | * | 
|  | 2506 | * Check whether we have already exported the current session, | 
|  | 2507 | * and fail if so. | 
|  | 2508 | */ | 
|  | 2509 | if( ssl->session->exported == 1 ) | 
|  | 2510 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 2511 |  | 
|  | 2512 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); | 
|  | 2513 | if( ret != 0 ) | 
|  | 2514 | return( ret ); | 
|  | 2515 |  | 
|  | 2516 | /* Remember that we've exported the session. */ | 
|  | 2517 | ssl->session->exported = 1; | 
|  | 2518 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2519 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2520 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2521 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2522 | /* | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2523 | * Define ticket header determining Mbed TLS version | 
|  | 2524 | * and structure of the ticket. | 
|  | 2525 | */ | 
|  | 2526 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2527 | /* | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2528 | * Define bitflag determining compile-time settings influencing | 
|  | 2529 | * structure of serialized SSL sessions. | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2530 | */ | 
|  | 2531 |  | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2532 | #if defined(MBEDTLS_HAVE_TIME) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2533 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2534 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2535 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2536 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 2537 |  | 
|  | 2538 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2539 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2540 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2541 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2542 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 2543 |  | 
|  | 2544 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2545 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2546 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2547 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2548 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 2549 |  | 
|  | 2550 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2551 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2552 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2553 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2554 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 2555 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2556 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2557 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2558 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2559 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2560 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
|  | 2561 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2562 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 2563 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 | 
|  | 2564 | #else | 
|  | 2565 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 | 
|  | 2566 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 2567 |  | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2568 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT          0 | 
|  | 2569 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT           1 | 
|  | 2570 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 | 
|  | 2571 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT           3 | 
| Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 2572 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT           4 | 
|  | 2573 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT        5 | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2574 |  | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2575 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG                           \ | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2576 | ( (uint16_t) (                                                      \ | 
|  | 2577 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME          << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT          ) | \ | 
|  | 2578 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT           << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT           ) | \ | 
|  | 2579 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ | 
|  | 2580 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL           << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT           ) | \ | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2581 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM           << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT           ) | \ | 
| Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 2582 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET        << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT        ) ) ) | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2583 |  | 
| Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2584 | static unsigned char ssl_serialized_session_header[] = { | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2585 | MBEDTLS_VERSION_MAJOR, | 
|  | 2586 | MBEDTLS_VERSION_MINOR, | 
|  | 2587 | MBEDTLS_VERSION_PATCH, | 
| Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 2588 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), | 
|  | 2589 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), | 
| Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2590 | }; | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2591 |  | 
|  | 2592 | /* | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2593 | * Serialize a session in the following format: | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 2594 | * (in the presentation language of TLS, RFC 8446 section 3) | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2595 | * | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2596 | *  struct { | 
| Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2597 | * | 
| Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 2598 | *    opaque mbedtls_version[3];   // library version: major, minor, patch | 
|  | 2599 | *    opaque session_format[2];    // library-version specific 16-bit field | 
|  | 2600 | *                                 // determining the format of the remaining | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2601 | *                                 // serialized data. | 
| Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2602 | * | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2603 | *          Note: When updating the format, remember to keep | 
|  | 2604 | *          these version+format bytes. | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2605 | * | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2606 | *                                 // In this version, `session_format` determines | 
|  | 2607 | *                                 // the setting of those compile-time | 
|  | 2608 | *                                 // configuration options which influence | 
|  | 2609 | *                                 // the structure of mbedtls_ssl_session. | 
|  | 2610 | * | 
| Hanno Becker | fa0d61e | 2021-08-02 08:56:14 +0100 | [diff] [blame] | 2611 | *    uint8_t minor_ver;           // Protocol-version. Possible values: | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2612 | *                                 // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3) | 
|  | 2613 | * | 
|  | 2614 | *    select (serialized_session.minor_ver) { | 
|  | 2615 | * | 
|  | 2616 | *      case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2 | 
|  | 2617 | *        serialized_session_tls12 data; | 
|  | 2618 | * | 
|  | 2619 | *   }; | 
|  | 2620 | * | 
|  | 2621 | * } serialized_session; | 
|  | 2622 | * | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2623 | */ | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2624 |  | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2625 | static int ssl_session_save( const mbedtls_ssl_session *session, | 
|  | 2626 | unsigned char omit_header, | 
|  | 2627 | unsigned char *buf, | 
|  | 2628 | size_t buf_len, | 
|  | 2629 | size_t *olen ) | 
|  | 2630 | { | 
|  | 2631 | unsigned char *p = buf; | 
|  | 2632 | size_t used = 0; | 
|  | 2633 |  | 
|  | 2634 | if( !omit_header ) | 
|  | 2635 | { | 
|  | 2636 | /* | 
|  | 2637 | * Add Mbed TLS version identifier | 
|  | 2638 | */ | 
|  | 2639 |  | 
|  | 2640 | used += sizeof( ssl_serialized_session_header ); | 
|  | 2641 |  | 
|  | 2642 | if( used <= buf_len ) | 
|  | 2643 | { | 
|  | 2644 | memcpy( p, ssl_serialized_session_header, | 
|  | 2645 | sizeof( ssl_serialized_session_header ) ); | 
|  | 2646 | p += sizeof( ssl_serialized_session_header ); | 
|  | 2647 | } | 
|  | 2648 | } | 
|  | 2649 |  | 
|  | 2650 | /* | 
|  | 2651 | * TLS version identifier | 
|  | 2652 | */ | 
|  | 2653 | used += 1; | 
|  | 2654 | if( used <= buf_len ) | 
|  | 2655 | { | 
|  | 2656 | *p++ = session->minor_ver; | 
|  | 2657 | } | 
|  | 2658 |  | 
|  | 2659 | /* Forward to version-specific serialization routine. */ | 
|  | 2660 | switch( session->minor_ver ) | 
|  | 2661 | { | 
|  | 2662 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2663 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
|  | 2664 | { | 
|  | 2665 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; | 
|  | 2666 | used += ssl_session_save_tls12( session, p, remaining_len ); | 
|  | 2667 | break; | 
|  | 2668 | } | 
|  | 2669 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 2670 |  | 
|  | 2671 | default: | 
|  | 2672 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 2673 | } | 
|  | 2674 |  | 
|  | 2675 | *olen = used; | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 2676 | if( used > buf_len ) | 
|  | 2677 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2678 |  | 
|  | 2679 | return( 0 ); | 
|  | 2680 | } | 
|  | 2681 |  | 
|  | 2682 | /* | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2683 | * Public wrapper for ssl_session_save() | 
|  | 2684 | */ | 
|  | 2685 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, | 
|  | 2686 | unsigned char *buf, | 
|  | 2687 | size_t buf_len, | 
|  | 2688 | size_t *olen ) | 
|  | 2689 | { | 
|  | 2690 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); | 
|  | 2691 | } | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2692 |  | 
| Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 2693 | /* | 
|  | 2694 | * Deserialize session, see mbedtls_ssl_session_save() for format. | 
|  | 2695 | * | 
|  | 2696 | * This internal version is wrapped by a public function that cleans up in | 
|  | 2697 | * case of error, and has an extra option omit_header. | 
|  | 2698 | */ | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2699 | static int ssl_session_load( mbedtls_ssl_session *session, | 
|  | 2700 | unsigned char omit_header, | 
|  | 2701 | const unsigned char *buf, | 
|  | 2702 | size_t len ) | 
|  | 2703 | { | 
|  | 2704 | const unsigned char *p = buf; | 
|  | 2705 | const unsigned char * const end = buf + len; | 
|  | 2706 |  | 
|  | 2707 | if( !omit_header ) | 
|  | 2708 | { | 
|  | 2709 | /* | 
|  | 2710 | * Check Mbed TLS version identifier | 
|  | 2711 | */ | 
|  | 2712 |  | 
|  | 2713 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) | 
|  | 2714 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2715 |  | 
|  | 2716 | if( memcmp( p, ssl_serialized_session_header, | 
|  | 2717 | sizeof( ssl_serialized_session_header ) ) != 0 ) | 
|  | 2718 | { | 
|  | 2719 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | 
|  | 2720 | } | 
|  | 2721 | p += sizeof( ssl_serialized_session_header ); | 
|  | 2722 | } | 
|  | 2723 |  | 
|  | 2724 | /* | 
|  | 2725 | * TLS version identifier | 
|  | 2726 | */ | 
|  | 2727 | if( 1 > (size_t)( end - p ) ) | 
|  | 2728 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2729 | session->minor_ver = *p++; | 
|  | 2730 |  | 
|  | 2731 | /* Dispatch according to TLS version. */ | 
|  | 2732 | switch( session->minor_ver ) | 
|  | 2733 | { | 
|  | 2734 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2735 | case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */ | 
|  | 2736 | { | 
|  | 2737 | size_t remaining_len = ( end - p ); | 
|  | 2738 | return( ssl_session_load_tls12( session, p, remaining_len ) ); | 
|  | 2739 | } | 
|  | 2740 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 2741 |  | 
|  | 2742 | default: | 
|  | 2743 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2744 | } | 
|  | 2745 | } | 
|  | 2746 |  | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2747 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 2748 | * Deserialize session: public wrapper for error cleaning | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2749 | */ | 
|  | 2750 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, | 
|  | 2751 | const unsigned char *buf, | 
|  | 2752 | size_t len ) | 
|  | 2753 | { | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2754 | int ret = ssl_session_load( session, 0, buf, len ); | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2755 |  | 
|  | 2756 | if( ret != 0 ) | 
|  | 2757 | mbedtls_ssl_session_free( session ); | 
|  | 2758 |  | 
|  | 2759 | return( ret ); | 
|  | 2760 | } | 
|  | 2761 |  | 
|  | 2762 | /* | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2763 | * Perform a single step of the SSL handshake | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2764 | */ | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2765 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) | 
|  | 2766 | { | 
|  | 2767 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 2768 |  | 
| Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 2769 | /* | 
|  | 2770 | * We may have not been able to send to the peer all the handshake data | 
| Ronald Cron | 3f20b77 | 2022-03-08 16:00:02 +0100 | [diff] [blame] | 2771 | * that were written into the output buffer by the previous handshake step, | 
|  | 2772 | * if the write to the network callback returned with the | 
| Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 2773 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. | 
|  | 2774 | * We proceed to the next handshake step only when all data from the | 
|  | 2775 | * previous one have been sent to the peer, thus we make sure that this is | 
|  | 2776 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may | 
|  | 2777 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case | 
|  | 2778 | * we have to wait before to go ahead. | 
|  | 2779 | * In the case of TLS 1.3, handshake step handlers do not send data to the | 
|  | 2780 | * peer. Data are only sent here and through | 
|  | 2781 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an | 
|  | 2782 | * alert occured. | 
|  | 2783 | */ | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2784 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
|  | 2785 | return( ret ); | 
|  | 2786 |  | 
|  | 2787 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2788 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 2789 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | 
|  | 2790 | { | 
|  | 2791 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 2792 | return( ret ); | 
|  | 2793 | } | 
|  | 2794 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 2795 |  | 
|  | 2796 | return( ret ); | 
|  | 2797 | } | 
|  | 2798 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2799 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2800 | { | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2801 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2802 |  | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2803 | if( ssl            == NULL                       || | 
|  | 2804 | ssl->conf      == NULL                       || | 
|  | 2805 | ssl->handshake == NULL                       || | 
| Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2806 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2807 | { | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2808 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2809 | } | 
|  | 2810 |  | 
|  | 2811 | ret = ssl_prepare_handshake_step( ssl ); | 
|  | 2812 | if( ret != 0 ) | 
|  | 2813 | return( ret ); | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2814 |  | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2815 | ret = mbedtls_ssl_handle_pending_alert( ssl ); | 
|  | 2816 | if( ret != 0 ) | 
|  | 2817 | goto cleanup; | 
|  | 2818 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2819 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2820 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2821 | { | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2822 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2823 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) | 
| Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 2824 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2825 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2826 |  | 
|  | 2827 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2828 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) | 
|  | 2829 | ret = mbedtls_ssl_handshake_client_step( ssl ); | 
|  | 2830 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 2831 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2832 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2833 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2834 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2835 | { | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2836 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2837 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) | 
| Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 2838 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2839 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2840 |  | 
|  | 2841 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2842 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) | 
|  | 2843 | ret = mbedtls_ssl_handshake_server_step( ssl ); | 
|  | 2844 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 2845 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2846 | #endif | 
|  | 2847 |  | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2848 | if( ret != 0 ) | 
|  | 2849 | { | 
| Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 2850 | /* handshake_step return error. And it is same | 
|  | 2851 | * with alert_reason. | 
|  | 2852 | */ | 
| Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 2853 | if( ssl->send_alert ) | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2854 | { | 
| Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 2855 | ret = mbedtls_ssl_handle_pending_alert( ssl ); | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2856 | goto cleanup; | 
|  | 2857 | } | 
|  | 2858 | } | 
|  | 2859 |  | 
|  | 2860 | cleanup: | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2861 | return( ret ); | 
|  | 2862 | } | 
|  | 2863 |  | 
|  | 2864 | /* | 
|  | 2865 | * Perform the SSL handshake | 
|  | 2866 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2867 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2868 | { | 
|  | 2869 | int ret = 0; | 
|  | 2870 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2871 | /* Sanity checks */ | 
|  | 2872 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2873 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 2874 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2875 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2876 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2877 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 2878 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) | 
|  | 2879 | { | 
|  | 2880 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " | 
|  | 2881 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); | 
|  | 2882 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2883 | } | 
|  | 2884 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 2885 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2886 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2887 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2888 | /* Main handshake loop */ | 
| Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2889 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2890 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2891 | ret = mbedtls_ssl_handshake_step( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2892 |  | 
|  | 2893 | if( ret != 0 ) | 
|  | 2894 | break; | 
|  | 2895 | } | 
|  | 2896 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2897 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2898 |  | 
|  | 2899 | return( ret ); | 
|  | 2900 | } | 
|  | 2901 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2902 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2903 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2904 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2905 | * Write HelloRequest to request renegotiation on server | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2906 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2907 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2908 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2909 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2910 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2911 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2912 |  | 
|  | 2913 | ssl->out_msglen  = 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2914 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 2915 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_HELLO_REQUEST; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2916 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2917 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2918 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2919 | 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] | 2920 | return( ret ); | 
|  | 2921 | } | 
|  | 2922 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2923 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2924 |  | 
|  | 2925 | return( 0 ); | 
|  | 2926 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2927 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2928 |  | 
|  | 2929 | /* | 
|  | 2930 | * Actually renegotiate current connection, triggered by either: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2931 | * - any side: calling mbedtls_ssl_renegotiate(), | 
|  | 2932 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), | 
|  | 2933 | * - 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] | 2934 | *   the initial handshake is completed. | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2935 | * 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] | 2936 | * 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] | 2937 | */ | 
| Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 2938 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2939 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2940 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2941 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2942 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2943 |  | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2944 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
|  | 2945 | return( ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2946 |  | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2947 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and | 
|  | 2948 | * the ServerHello will have message_seq = 1" */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2949 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2950 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2951 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2952 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2953 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2954 | ssl->handshake->out_msg_seq = 1; | 
|  | 2955 | else | 
|  | 2956 | ssl->handshake->in_msg_seq = 1; | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2957 | } | 
|  | 2958 | #endif | 
|  | 2959 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2960 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
|  | 2961 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2962 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2963 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2964 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2965 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2966 | return( ret ); | 
|  | 2967 | } | 
|  | 2968 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2969 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2970 |  | 
|  | 2971 | return( 0 ); | 
|  | 2972 | } | 
|  | 2973 |  | 
|  | 2974 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2975 | * Renegotiate current connection on client, | 
|  | 2976 | * or request renegotiation on server | 
|  | 2977 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2978 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2979 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2980 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2981 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2982 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 2983 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2984 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2985 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2986 | /* On server, just send the request */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2987 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2988 | { | 
| Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2989 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2990 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2991 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2992 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 2993 |  | 
|  | 2994 | /* Did we already try/start sending HelloRequest? */ | 
|  | 2995 | if( ssl->out_left != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2996 | return( mbedtls_ssl_flush_output( ssl ) ); | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 2997 |  | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2998 | return( ssl_write_hello_request( ssl ) ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2999 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3000 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3001 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3002 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3003 | /* | 
|  | 3004 | * On client, either start the renegotiation process or, | 
|  | 3005 | * if already in progress, continue the handshake | 
|  | 3006 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3007 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3008 | { | 
| Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3009 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3010 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3011 |  | 
| Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3012 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3013 | { | 
| Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3014 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3015 | return( ret ); | 
|  | 3016 | } | 
|  | 3017 | } | 
|  | 3018 | else | 
|  | 3019 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3020 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3021 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3022 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3023 | return( ret ); | 
|  | 3024 | } | 
|  | 3025 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3026 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3027 |  | 
| Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3028 | return( ret ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3029 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3030 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3031 |  | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3032 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3033 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3034 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
|  | 3035 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3036 | if( handshake == NULL ) | 
|  | 3037 | return; | 
|  | 3038 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3039 | #if defined(MBEDTLS_ECP_C) | 
|  | 3040 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 3041 | if ( ssl->handshake->group_list_heap_allocated ) | 
|  | 3042 | mbedtls_free( (void*) handshake->group_list ); | 
|  | 3043 | handshake->group_list = NULL; | 
|  | 3044 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 3045 | #endif /* MBEDTLS_ECP_C */ | 
|  | 3046 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3047 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 3048 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 3049 | if ( ssl->handshake->sig_algs_heap_allocated ) | 
|  | 3050 | mbedtls_free( (void*) handshake->sig_algs ); | 
|  | 3051 | handshake->sig_algs = NULL; | 
|  | 3052 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3053 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 3054 | if( ssl->handshake->certificate_request_context ) | 
|  | 3055 | { | 
|  | 3056 | mbedtls_free( (void*) handshake->certificate_request_context ); | 
|  | 3057 | } | 
|  | 3058 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3059 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 3060 |  | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3061 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
|  | 3062 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) | 
|  | 3063 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3064 | ssl->conf->f_async_cancel( ssl ); | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3065 | handshake->async_in_progress = 0; | 
|  | 3066 | } | 
|  | 3067 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
|  | 3068 |  | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3069 | #if defined(MBEDTLS_SHA256_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3070 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3071 | psa_hash_abort( &handshake->fin_sha256_psa ); | 
|  | 3072 | #else | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3073 | mbedtls_sha256_free(   &handshake->fin_sha256    ); | 
|  | 3074 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3075 | #endif | 
| Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 3076 | #if defined(MBEDTLS_SHA384_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3077 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3078 | psa_hash_abort( &handshake->fin_sha384_psa ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3079 | #else | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3080 | mbedtls_sha512_free(   &handshake->fin_sha512    ); | 
|  | 3081 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3082 | #endif | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3083 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3084 | #if defined(MBEDTLS_DHM_C) | 
|  | 3085 | mbedtls_dhm_free( &handshake->dhm_ctx ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3086 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3087 | #if defined(MBEDTLS_ECDH_C) | 
|  | 3088 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3089 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3090 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3091 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3092 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 3093 | mbedtls_free( handshake->ecjpake_cache ); | 
|  | 3094 | handshake->ecjpake_cache = NULL; | 
|  | 3095 | handshake->ecjpake_cache_len = 0; | 
|  | 3096 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3097 | #endif | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3098 |  | 
| Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3099 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
|  | 3100 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3101 | /* explicit void pointer cast for buggy MS compiler */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3102 | mbedtls_free( (void *) handshake->curves ); | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3103 | #endif | 
|  | 3104 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3105 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3106 | if( handshake->psk != NULL ) | 
|  | 3107 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3108 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3109 | mbedtls_free( handshake->psk ); | 
|  | 3110 | } | 
|  | 3111 | #endif | 
|  | 3112 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3113 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 3114 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3115 | /* | 
|  | 3116 | * Free only the linked list wrapper, not the keys themselves | 
|  | 3117 | * since the belong to the SNI callback | 
|  | 3118 | */ | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3119 | ssl_key_cert_free( handshake->sni_key_cert ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3120 | #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] | 3121 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3122 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3123 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3124 | if( handshake->ecrs_peer_cert != NULL ) | 
|  | 3125 | { | 
|  | 3126 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); | 
|  | 3127 | mbedtls_free( handshake->ecrs_peer_cert ); | 
|  | 3128 | } | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3129 | #endif | 
|  | 3130 |  | 
| Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3131 | #if defined(MBEDTLS_X509_CRT_PARSE_C) &&        \ | 
|  | 3132 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3133 | mbedtls_pk_free( &handshake->peer_pubkey ); | 
|  | 3134 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 3135 |  | 
| XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3136 | #if defined(MBEDTLS_SSL_CLI_C) && \ | 
|  | 3137 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) | 
|  | 3138 | mbedtls_free( handshake->cookie ); | 
|  | 3139 | #endif /* MBEDTLS_SSL_CLI_C && | 
|  | 3140 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ | 
| XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3141 |  | 
|  | 3142 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3143 | mbedtls_ssl_flight_free( handshake->flight ); | 
|  | 3144 | mbedtls_ssl_buffering_free( ssl ); | 
| XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3145 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3146 |  | 
| Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3147 | #if defined(MBEDTLS_ECDH_C) && \ | 
|  | 3148 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3149 | psa_destroy_key( handshake->ecdh_psa_privkey ); | 
|  | 3150 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 3151 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3152 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3153 | mbedtls_ssl_transform_free( handshake->transform_handshake ); | 
|  | 3154 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); | 
| Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3155 | mbedtls_free( handshake->transform_earlydata ); | 
|  | 3156 | mbedtls_free( handshake->transform_handshake ); | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3157 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3158 |  | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3159 |  | 
|  | 3160 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3161 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS | 
|  | 3162 | * processes datagrams and the fact that a datagram is allowed to have | 
|  | 3163 | * several records in it, it is possible that the I/O buffers are not | 
|  | 3164 | * empty at this stage */ | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3165 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), | 
|  | 3166 | mbedtls_ssl_get_output_buflen( ssl ) ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3167 | #endif | 
| Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3168 |  | 
| Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3169 | /* mbedtls_platform_zeroize MUST be last one in this function */ | 
|  | 3170 | mbedtls_platform_zeroize( handshake, | 
|  | 3171 | sizeof( mbedtls_ssl_handshake_params ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3172 | } | 
|  | 3173 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3174 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3175 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3176 | if( session == NULL ) | 
|  | 3177 | return; | 
|  | 3178 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3179 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3180 | ssl_clear_peer_cert( session ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3181 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3182 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3183 | #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] | 3184 | mbedtls_free( session->ticket ); | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3185 | #endif | 
| Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3186 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3187 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3188 | } | 
|  | 3189 |  | 
| Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3190 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3191 |  | 
|  | 3192 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 3193 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u | 
|  | 3194 | #else | 
|  | 3195 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u | 
|  | 3196 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 3197 |  | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3198 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3199 |  | 
|  | 3200 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 3201 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u | 
|  | 3202 | #else | 
|  | 3203 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u | 
|  | 3204 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 3205 |  | 
|  | 3206 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 3207 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u | 
|  | 3208 | #else | 
|  | 3209 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u | 
|  | 3210 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 3211 |  | 
|  | 3212 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT    0 | 
|  | 3213 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT     1 | 
|  | 3214 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT      2 | 
|  | 3215 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT                  3 | 
|  | 3216 |  | 
|  | 3217 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG   \ | 
|  | 3218 | ( (uint32_t) (                              \ | 
|  | 3219 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID     << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT     ) | \ | 
|  | 3220 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT      << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT      ) | \ | 
|  | 3221 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY       << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT       ) | \ | 
|  | 3222 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN                   << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT                   ) | \ | 
|  | 3223 | 0u ) ) | 
|  | 3224 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3225 | static unsigned char ssl_serialized_context_header[] = { | 
|  | 3226 | MBEDTLS_VERSION_MAJOR, | 
|  | 3227 | MBEDTLS_VERSION_MINOR, | 
|  | 3228 | MBEDTLS_VERSION_PATCH, | 
| Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3229 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), | 
|  | 3230 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), | 
|  | 3231 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), | 
|  | 3232 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), | 
|  | 3233 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3234 | }; | 
|  | 3235 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3236 | /* | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3237 | * Serialize a full SSL context | 
| Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3238 | * | 
|  | 3239 | * The format of the serialized data is: | 
|  | 3240 | * (in the presentation language of TLS, RFC 8446 section 3) | 
|  | 3241 | * | 
|  | 3242 | *  // header | 
|  | 3243 | *  opaque mbedtls_version[3];   // major, minor, patch | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3244 | *  opaque context_format[5];    // version-specific field determining | 
| Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3245 | *                               // the format of the remaining | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3246 | *                               // serialized data. | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3247 | *  Note: When updating the format, remember to keep these | 
|  | 3248 | *        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] | 3249 | * | 
|  | 3250 | *  // session sub-structure | 
|  | 3251 | *  opaque session<1..2^32-1>;  // see mbedtls_ssl_session_save() | 
|  | 3252 | *  // transform sub-structure | 
|  | 3253 | *  uint8 random[64];           // ServerHello.random+ClientHello.random | 
|  | 3254 | *  uint8 in_cid<0..2^8-1>      // Connection ID: expected incoming value | 
|  | 3255 | *  uint8 out_cid<0..2^8-1>     // Connection ID: outgoing value to use | 
|  | 3256 | *  // fields from ssl_context | 
|  | 3257 | *  uint32 badmac_seen;         // DTLS: number of records with failing MAC | 
|  | 3258 | *  uint64 in_window_top;       // DTLS: last validated record seq_num | 
|  | 3259 | *  uint64 in_window;           // DTLS: bitmask for replay protection | 
|  | 3260 | *  uint8 disable_datagram_packing; // DTLS: only one record per datagram | 
|  | 3261 | *  uint64 cur_out_ctr;         // Record layer: outgoing sequence number | 
|  | 3262 | *  uint16 mtu;                 // DTLS: path mtu (max outgoing fragment size) | 
|  | 3263 | *  uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol | 
|  | 3264 | * | 
|  | 3265 | * Note that many fields of the ssl_context or sub-structures are not | 
|  | 3266 | * serialized, as they fall in one of the following categories: | 
|  | 3267 | * | 
|  | 3268 | *  1. forced value (eg in_left must be 0) | 
|  | 3269 | *  2. pointer to dynamically-allocated memory (eg session, transform) | 
|  | 3270 | *  3. value can be re-derived from other data (eg session keys from MS) | 
|  | 3271 | *  4. value was temporary (eg content of input buffer) | 
|  | 3272 | *  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] | 3273 | */ | 
|  | 3274 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, | 
|  | 3275 | unsigned char *buf, | 
|  | 3276 | size_t buf_len, | 
|  | 3277 | size_t *olen ) | 
|  | 3278 | { | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3279 | unsigned char *p = buf; | 
|  | 3280 | size_t used = 0; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3281 | size_t session_len; | 
|  | 3282 | int ret = 0; | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3283 |  | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3284 | /* | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3285 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in | 
|  | 3286 | * this function's documentation. | 
|  | 3287 | * | 
|  | 3288 | * These are due to assumptions/limitations in the implementation. Some of | 
|  | 3289 | * them are likely to stay (no handshake in progress) some might go away | 
|  | 3290 | * (only DTLS) but are currently used to simplify the implementation. | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3291 | */ | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3292 | /* The initial handshake must be over */ | 
| Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3293 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3294 | { | 
|  | 3295 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3296 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3297 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3298 | if( ssl->handshake != NULL ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3299 | { | 
|  | 3300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3301 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3302 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3303 | /* Double-check that sub-structures are indeed ready */ | 
|  | 3304 | if( ssl->transform == NULL || ssl->session == NULL ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3305 | { | 
|  | 3306 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3307 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3308 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3309 | /* There must be no pending incoming or outgoing data */ | 
|  | 3310 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3311 | { | 
|  | 3312 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3313 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3314 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3315 | if( ssl->out_left != 0 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3316 | { | 
|  | 3317 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3318 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3319 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3320 | /* Protocol must be DLTS, not TLS */ | 
|  | 3321 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3322 | { | 
|  | 3323 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3324 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3325 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3326 | /* Version must be 1.2 */ | 
|  | 3327 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3328 | { | 
|  | 3329 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3330 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3331 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3332 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3333 | { | 
|  | 3334 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3335 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3336 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3337 | /* We must be using an AEAD ciphersuite */ | 
|  | 3338 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3339 | { | 
|  | 3340 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3341 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3342 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3343 | /* Renegotiation must not be enabled */ | 
|  | 3344 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 3345 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3346 | { | 
|  | 3347 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3348 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3349 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3350 | #endif | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3351 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3352 | /* | 
|  | 3353 | * Version and format identifier | 
|  | 3354 | */ | 
|  | 3355 | used += sizeof( ssl_serialized_context_header ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3356 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3357 | if( used <= buf_len ) | 
|  | 3358 | { | 
|  | 3359 | memcpy( p, ssl_serialized_context_header, | 
|  | 3360 | sizeof( ssl_serialized_context_header ) ); | 
|  | 3361 | p += sizeof( ssl_serialized_context_header ); | 
|  | 3362 | } | 
|  | 3363 |  | 
|  | 3364 | /* | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3365 | * Session (length + data) | 
|  | 3366 | */ | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3367 | 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] | 3368 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) | 
|  | 3369 | return( ret ); | 
|  | 3370 |  | 
|  | 3371 | used += 4 + session_len; | 
|  | 3372 | if( used <= buf_len ) | 
|  | 3373 | { | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3374 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); | 
|  | 3375 | p += 4; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3376 |  | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3377 | ret = ssl_session_save( ssl->session, 1, | 
|  | 3378 | p, session_len, &session_len ); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3379 | if( ret != 0 ) | 
|  | 3380 | return( ret ); | 
|  | 3381 |  | 
|  | 3382 | p += session_len; | 
|  | 3383 | } | 
|  | 3384 |  | 
|  | 3385 | /* | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3386 | * Transform | 
|  | 3387 | */ | 
|  | 3388 | used += sizeof( ssl->transform->randbytes ); | 
|  | 3389 | if( used <= buf_len ) | 
|  | 3390 | { | 
|  | 3391 | memcpy( p, ssl->transform->randbytes, | 
|  | 3392 | sizeof( ssl->transform->randbytes ) ); | 
|  | 3393 | p += sizeof( ssl->transform->randbytes ); | 
|  | 3394 | } | 
|  | 3395 |  | 
|  | 3396 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 3397 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; | 
|  | 3398 | if( used <= buf_len ) | 
|  | 3399 | { | 
|  | 3400 | *p++ = ssl->transform->in_cid_len; | 
|  | 3401 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); | 
|  | 3402 | p += ssl->transform->in_cid_len; | 
|  | 3403 |  | 
|  | 3404 | *p++ = ssl->transform->out_cid_len; | 
|  | 3405 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); | 
|  | 3406 | p += ssl->transform->out_cid_len; | 
|  | 3407 | } | 
|  | 3408 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 3409 |  | 
|  | 3410 | /* | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3411 | * Saved fields from top-level ssl_context structure | 
|  | 3412 | */ | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3413 | used += 4; | 
|  | 3414 | if( used <= buf_len ) | 
|  | 3415 | { | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3416 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); | 
|  | 3417 | p += 4; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3418 | } | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3419 |  | 
|  | 3420 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 3421 | used += 16; | 
|  | 3422 | if( used <= buf_len ) | 
|  | 3423 | { | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3424 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); | 
|  | 3425 | p += 8; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3426 |  | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3427 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); | 
|  | 3428 | p += 8; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3429 | } | 
|  | 3430 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 3431 |  | 
|  | 3432 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3433 | used += 1; | 
|  | 3434 | if( used <= buf_len ) | 
|  | 3435 | { | 
|  | 3436 | *p++ = ssl->disable_datagram_packing; | 
|  | 3437 | } | 
|  | 3438 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3439 |  | 
| Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3440 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3441 | if( used <= buf_len ) | 
|  | 3442 | { | 
| Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3443 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); | 
|  | 3444 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3445 | } | 
|  | 3446 |  | 
|  | 3447 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3448 | used += 2; | 
|  | 3449 | if( used <= buf_len ) | 
|  | 3450 | { | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3451 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); | 
|  | 3452 | p += 2; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3453 | } | 
|  | 3454 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3455 |  | 
|  | 3456 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 3457 | { | 
|  | 3458 | const uint8_t alpn_len = ssl->alpn_chosen | 
| Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 3459 | ? (uint8_t) strlen( ssl->alpn_chosen ) | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3460 | : 0; | 
|  | 3461 |  | 
|  | 3462 | used += 1 + alpn_len; | 
|  | 3463 | if( used <= buf_len ) | 
|  | 3464 | { | 
|  | 3465 | *p++ = alpn_len; | 
|  | 3466 |  | 
|  | 3467 | if( ssl->alpn_chosen != NULL ) | 
|  | 3468 | { | 
|  | 3469 | memcpy( p, ssl->alpn_chosen, alpn_len ); | 
|  | 3470 | p += alpn_len; | 
|  | 3471 | } | 
|  | 3472 | } | 
|  | 3473 | } | 
|  | 3474 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 3475 |  | 
|  | 3476 | /* | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3477 | * Done | 
|  | 3478 | */ | 
|  | 3479 | *olen = used; | 
|  | 3480 |  | 
|  | 3481 | if( used > buf_len ) | 
|  | 3482 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3483 |  | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3484 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); | 
|  | 3485 |  | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3486 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3487 | } | 
|  | 3488 |  | 
|  | 3489 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3490 | * Deserialize context, see mbedtls_ssl_context_save() for format. | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3491 | * | 
|  | 3492 | * This internal version is wrapped by a public function that cleans up in | 
|  | 3493 | * case of error. | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3494 | */ | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3495 | static int ssl_context_load( mbedtls_ssl_context *ssl, | 
|  | 3496 | const unsigned char *buf, | 
|  | 3497 | size_t len ) | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3498 | { | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3499 | const unsigned char *p = buf; | 
|  | 3500 | const unsigned char * const end = buf + len; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3501 | size_t session_len; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3502 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3503 |  | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3504 | /* | 
|  | 3505 | * The context should have been freshly setup or reset. | 
|  | 3506 | * Give the user an error in case of obvious misuse. | 
| Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 3507 | * (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] | 3508 | * renegotiating, or if the user mistakenly loaded a session first.) | 
|  | 3509 | */ | 
|  | 3510 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || | 
|  | 3511 | ssl->session != NULL ) | 
|  | 3512 | { | 
|  | 3513 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3514 | } | 
|  | 3515 |  | 
|  | 3516 | /* | 
|  | 3517 | * We can't check that the config matches the initial one, but we can at | 
|  | 3518 | * least check it matches the requirements for serializing. | 
|  | 3519 | */ | 
|  | 3520 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | 
|  | 3521 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || | 
|  | 3522 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || | 
|  | 3523 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || | 
|  | 3524 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || | 
|  | 3525 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3526 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3527 | #endif | 
| Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3528 | 0 ) | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3529 | { | 
|  | 3530 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3531 | } | 
|  | 3532 |  | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3533 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); | 
|  | 3534 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3535 | /* | 
|  | 3536 | * Check version identifier | 
|  | 3537 | */ | 
|  | 3538 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) | 
|  | 3539 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3540 |  | 
|  | 3541 | if( memcmp( p, ssl_serialized_context_header, | 
|  | 3542 | sizeof( ssl_serialized_context_header ) ) != 0 ) | 
|  | 3543 | { | 
|  | 3544 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | 
|  | 3545 | } | 
|  | 3546 | p += sizeof( ssl_serialized_context_header ); | 
|  | 3547 |  | 
|  | 3548 | /* | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3549 | * Session | 
|  | 3550 | */ | 
|  | 3551 | if( (size_t)( end - p ) < 4 ) | 
|  | 3552 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3553 |  | 
|  | 3554 | session_len = ( (size_t) p[0] << 24 ) | | 
|  | 3555 | ( (size_t) p[1] << 16 ) | | 
|  | 3556 | ( (size_t) p[2] <<  8 ) | | 
|  | 3557 | ( (size_t) p[3]       ); | 
|  | 3558 | p += 4; | 
|  | 3559 |  | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3560 | /* This has been allocated by ssl_handshake_init(), called by | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3561 | * 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] | 3562 | ssl->session = ssl->session_negotiate; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3563 | ssl->session_in = ssl->session; | 
|  | 3564 | ssl->session_out = ssl->session; | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3565 | ssl->session_negotiate = NULL; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3566 |  | 
|  | 3567 | if( (size_t)( end - p ) < session_len ) | 
|  | 3568 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3569 |  | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3570 | ret = ssl_session_load( ssl->session, 1, p, session_len ); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3571 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3572 | { | 
|  | 3573 | mbedtls_ssl_session_free( ssl->session ); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3574 | return( ret ); | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3575 | } | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3576 |  | 
|  | 3577 | p += session_len; | 
|  | 3578 |  | 
|  | 3579 | /* | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3580 | * Transform | 
|  | 3581 | */ | 
|  | 3582 |  | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3583 | /* This has been allocated by ssl_handshake_init(), called by | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3584 | * 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] | 3585 | ssl->transform = ssl->transform_negotiate; | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3586 | ssl->transform_in = ssl->transform; | 
|  | 3587 | ssl->transform_out = ssl->transform; | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3588 | ssl->transform_negotiate = NULL; | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3589 |  | 
|  | 3590 | /* Read random bytes and populate structure */ | 
|  | 3591 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) | 
|  | 3592 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3593 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3594 | ret = ssl_tls12_populate_transform( ssl->transform, | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3595 | ssl->session->ciphersuite, | 
|  | 3596 | ssl->session->master, | 
| Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3597 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ | 
|  | 3598 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3599 | ssl->session->encrypt_then_mac, | 
| Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3600 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && | 
|  | 3601 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3602 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), | 
|  | 3603 | p, /* currently pointing to randbytes */ | 
|  | 3604 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ | 
|  | 3605 | ssl->conf->endpoint, | 
|  | 3606 | ssl ); | 
|  | 3607 | if( ret != 0 ) | 
|  | 3608 | return( ret ); | 
| Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3609 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3610 | p += sizeof( ssl->transform->randbytes ); | 
|  | 3611 |  | 
|  | 3612 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 3613 | /* Read connection IDs and store them */ | 
|  | 3614 | if( (size_t)( end - p ) < 1 ) | 
|  | 3615 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3616 |  | 
|  | 3617 | ssl->transform->in_cid_len = *p++; | 
|  | 3618 |  | 
| Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 3619 | 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] | 3620 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3621 |  | 
|  | 3622 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); | 
|  | 3623 | p += ssl->transform->in_cid_len; | 
|  | 3624 |  | 
|  | 3625 | ssl->transform->out_cid_len = *p++; | 
|  | 3626 |  | 
|  | 3627 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) | 
|  | 3628 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3629 |  | 
|  | 3630 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); | 
|  | 3631 | p += ssl->transform->out_cid_len; | 
|  | 3632 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 3633 |  | 
|  | 3634 | /* | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3635 | * Saved fields from top-level ssl_context structure | 
|  | 3636 | */ | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3637 | if( (size_t)( end - p ) < 4 ) | 
|  | 3638 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3639 |  | 
|  | 3640 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | | 
|  | 3641 | ( (uint32_t) p[1] << 16 ) | | 
|  | 3642 | ( (uint32_t) p[2] <<  8 ) | | 
|  | 3643 | ( (uint32_t) p[3]       ); | 
|  | 3644 | p += 4; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3645 |  | 
|  | 3646 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 3647 | if( (size_t)( end - p ) < 16 ) | 
|  | 3648 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3649 |  | 
|  | 3650 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | | 
|  | 3651 | ( (uint64_t) p[1] << 48 ) | | 
|  | 3652 | ( (uint64_t) p[2] << 40 ) | | 
|  | 3653 | ( (uint64_t) p[3] << 32 ) | | 
|  | 3654 | ( (uint64_t) p[4] << 24 ) | | 
|  | 3655 | ( (uint64_t) p[5] << 16 ) | | 
|  | 3656 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 3657 | ( (uint64_t) p[7]       ); | 
|  | 3658 | p += 8; | 
|  | 3659 |  | 
|  | 3660 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | | 
|  | 3661 | ( (uint64_t) p[1] << 48 ) | | 
|  | 3662 | ( (uint64_t) p[2] << 40 ) | | 
|  | 3663 | ( (uint64_t) p[3] << 32 ) | | 
|  | 3664 | ( (uint64_t) p[4] << 24 ) | | 
|  | 3665 | ( (uint64_t) p[5] << 16 ) | | 
|  | 3666 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 3667 | ( (uint64_t) p[7]       ); | 
|  | 3668 | p += 8; | 
|  | 3669 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 3670 |  | 
|  | 3671 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3672 | if( (size_t)( end - p ) < 1 ) | 
|  | 3673 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3674 |  | 
|  | 3675 | ssl->disable_datagram_packing = *p++; | 
|  | 3676 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3677 |  | 
| Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3678 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3679 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3680 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); | 
|  | 3681 | p += sizeof( ssl->cur_out_ctr ); | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3682 |  | 
|  | 3683 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3684 | if( (size_t)( end - p ) < 2 ) | 
|  | 3685 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3686 |  | 
|  | 3687 | ssl->mtu = ( p[0] << 8 ) | p[1]; | 
|  | 3688 | p += 2; | 
|  | 3689 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3690 |  | 
|  | 3691 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 3692 | { | 
|  | 3693 | uint8_t alpn_len; | 
|  | 3694 | const char **cur; | 
|  | 3695 |  | 
|  | 3696 | if( (size_t)( end - p ) < 1 ) | 
|  | 3697 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3698 |  | 
|  | 3699 | alpn_len = *p++; | 
|  | 3700 |  | 
|  | 3701 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) | 
|  | 3702 | { | 
|  | 3703 | /* alpn_chosen should point to an item in the configured list */ | 
|  | 3704 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) | 
|  | 3705 | { | 
|  | 3706 | if( strlen( *cur ) == alpn_len && | 
|  | 3707 | memcmp( p, cur, alpn_len ) == 0 ) | 
|  | 3708 | { | 
|  | 3709 | ssl->alpn_chosen = *cur; | 
|  | 3710 | break; | 
|  | 3711 | } | 
|  | 3712 | } | 
|  | 3713 | } | 
|  | 3714 |  | 
|  | 3715 | /* can only happen on conf mismatch */ | 
|  | 3716 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) | 
|  | 3717 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3718 |  | 
|  | 3719 | p += alpn_len; | 
|  | 3720 | } | 
|  | 3721 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 3722 |  | 
|  | 3723 | /* | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3724 | * Forced fields from top-level ssl_context structure | 
|  | 3725 | * | 
|  | 3726 | * Most of them already set to the correct value by mbedtls_ssl_init() and | 
|  | 3727 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. | 
|  | 3728 | */ | 
|  | 3729 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; | 
|  | 3730 |  | 
|  | 3731 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
|  | 3732 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; | 
|  | 3733 |  | 
| Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3734 | /* Adjust pointers for header fields of outgoing records to | 
|  | 3735 | * the given transform, accounting for explicit IV and CID. */ | 
| Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3736 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); | 
| Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3737 |  | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3738 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3739 | ssl->in_epoch = 1; | 
|  | 3740 | #endif | 
|  | 3741 |  | 
|  | 3742 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, | 
|  | 3743 | * 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] | 3744 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() | 
|  | 3745 | * inappropriately. */ | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3746 | if( ssl->handshake != NULL ) | 
|  | 3747 | { | 
|  | 3748 | mbedtls_ssl_handshake_free( ssl ); | 
|  | 3749 | mbedtls_free( ssl->handshake ); | 
|  | 3750 | ssl->handshake = NULL; | 
|  | 3751 | } | 
|  | 3752 |  | 
|  | 3753 | /* | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3754 | * Done - should have consumed entire buffer | 
|  | 3755 | */ | 
|  | 3756 | if( p != end ) | 
|  | 3757 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3758 |  | 
|  | 3759 | return( 0 ); | 
|  | 3760 | } | 
|  | 3761 |  | 
|  | 3762 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3763 | * Deserialize context: public wrapper for error cleaning | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3764 | */ | 
|  | 3765 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, | 
|  | 3766 | const unsigned char *buf, | 
|  | 3767 | size_t len ) | 
|  | 3768 | { | 
|  | 3769 | int ret = ssl_context_load( context, buf, len ); | 
|  | 3770 |  | 
|  | 3771 | if( ret != 0 ) | 
|  | 3772 | mbedtls_ssl_free( context ); | 
|  | 3773 |  | 
|  | 3774 | return( ret ); | 
|  | 3775 | } | 
| Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3776 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3777 |  | 
|  | 3778 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3779 | * Free an SSL context | 
|  | 3780 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3781 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3782 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3783 | if( ssl == NULL ) | 
|  | 3784 | return; | 
|  | 3785 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3786 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3787 |  | 
| Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3788 | if( ssl->out_buf != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3789 | { | 
| sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3790 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3791 | size_t out_buf_len = ssl->out_buf_len; | 
|  | 3792 | #else | 
|  | 3793 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
|  | 3794 | #endif | 
|  | 3795 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3796 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3797 | mbedtls_free( ssl->out_buf ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3798 | ssl->out_buf = NULL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3799 | } | 
|  | 3800 |  | 
| Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3801 | if( ssl->in_buf != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3802 | { | 
| sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3803 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3804 | size_t in_buf_len = ssl->in_buf_len; | 
|  | 3805 | #else | 
|  | 3806 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 3807 | #endif | 
|  | 3808 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3809 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3810 | mbedtls_free( ssl->in_buf ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3811 | ssl->in_buf = NULL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3812 | } | 
|  | 3813 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3814 | if( ssl->transform ) | 
|  | 3815 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3816 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 3817 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3818 | } | 
|  | 3819 |  | 
|  | 3820 | if( ssl->handshake ) | 
|  | 3821 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3822 | mbedtls_ssl_handshake_free( ssl ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3823 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); | 
|  | 3824 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3825 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3826 | mbedtls_free( ssl->handshake ); | 
|  | 3827 | mbedtls_free( ssl->transform_negotiate ); | 
|  | 3828 | mbedtls_free( ssl->session_negotiate ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3829 | } | 
|  | 3830 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3831 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3832 | mbedtls_ssl_transform_free( ssl->transform_application ); | 
|  | 3833 | mbedtls_free( ssl->transform_application ); | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3834 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3835 |  | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3836 | if( ssl->session ) | 
|  | 3837 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3838 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 3839 | mbedtls_free( ssl->session ); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3840 | } | 
|  | 3841 |  | 
| Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 3842 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3843 | if( ssl->hostname != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3844 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3845 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3846 | mbedtls_free( ssl->hostname ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3847 | } | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 3848 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3849 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3850 | #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] | 3851 | mbedtls_free( ssl->cli_id ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 3852 | #endif | 
|  | 3853 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3854 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); | 
| Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 3855 |  | 
| Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 3856 | /* Actually clear after last debug message */ | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3857 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3858 | } | 
|  | 3859 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 3860 | /* | 
|  | 3861 | * Initialze mbedtls_ssl_config | 
|  | 3862 | */ | 
|  | 3863 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) | 
|  | 3864 | { | 
|  | 3865 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); | 
|  | 3866 | } | 
|  | 3867 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3868 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3869 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3870 | /* 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] | 3871 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, | 
|  | 3872 | * for no fundamental reason. | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3873 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise | 
|  | 3874 | * about this list. */ | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3875 | static int ssl_preset_default_hashes[] = { | 
|  | 3876 | #if defined(MBEDTLS_SHA512_C) | 
|  | 3877 | MBEDTLS_MD_SHA512, | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 3878 | #endif | 
|  | 3879 | #if defined(MBEDTLS_SHA384_C) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3880 | MBEDTLS_MD_SHA384, | 
|  | 3881 | #endif | 
|  | 3882 | #if defined(MBEDTLS_SHA256_C) | 
|  | 3883 | MBEDTLS_MD_SHA256, | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 3884 | #endif | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3885 | MBEDTLS_MD_NONE | 
|  | 3886 | }; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3887 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 3888 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3889 |  | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3890 | /* The selection should be the same as mbedtls_x509_crt_profile_default in | 
|  | 3891 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3892 | * curves with a lower resource usage come first. | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3893 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3894 | * about this list. | 
|  | 3895 | */ | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3896 | static uint16_t ssl_preset_default_groups[] = { | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3897 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3898 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3899 | #endif | 
|  | 3900 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3901 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3902 | #endif | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3903 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3904 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3905 | #endif | 
|  | 3906 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3907 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3908 | #endif | 
|  | 3909 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3910 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3911 | #endif | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3912 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3913 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3914 | #endif | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3915 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3916 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3917 | #endif | 
|  | 3918 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3919 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3920 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3921 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3922 | }; | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3923 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3924 | static int ssl_preset_suiteb_ciphersuites[] = { | 
|  | 3925 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | 
|  | 3926 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | 
|  | 3927 | 0 | 
|  | 3928 | }; | 
|  | 3929 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3930 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3931 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3932 | static int ssl_preset_suiteb_hashes[] = { | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3933 | #if defined(MBEDTLS_SHA256_C) | 
|  | 3934 | MBEDTLS_MD_SHA256, | 
|  | 3935 | #endif | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3936 | #if defined(MBEDTLS_SHA384_C) | 
|  | 3937 | MBEDTLS_MD_SHA384, | 
|  | 3938 | #endif | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3939 | MBEDTLS_MD_NONE | 
|  | 3940 | }; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3941 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
| Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 3942 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3943 | /* NOTICE: | 
| Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 3944 | *   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] | 3945 | *   rules SHOULD be upheld. | 
|  | 3946 | *   - No duplicate entries. | 
|  | 3947 | *   - But if there is a good reason, do not change the order of the algorithms. | 
|  | 3948 | *   - ssl_tls12_present* is for TLS 1.2 use only. | 
|  | 3949 | *   - 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] | 3950 | */ | 
| Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 3951 | static uint16_t ssl_preset_default_sig_algs[] = { | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3952 |  | 
| Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 3953 | #if defined(MBEDTLS_ECDSA_C) &&  defined(MBEDTLS_SHA256_C) && \ | 
|  | 3954 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | 
|  | 3955 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, | 
|  | 3956 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && | 
|  | 3957 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3958 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3959 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ | 
|  | 3960 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
|  | 3961 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, | 
|  | 3962 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && | 
|  | 3963 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ | 
|  | 3964 |  | 
| Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 3965 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \ | 
|  | 3966 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) | 
|  | 3967 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, | 
|  | 3968 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && | 
|  | 3969 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3970 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3971 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) | 
|  | 3972 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, | 
|  | 3973 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ | 
|  | 3974 |  | 
| Ronald Cron | 8540cf6 | 2022-03-16 08:01:09 +0100 | [diff] [blame] | 3975 | #if defined(MBEDTLS_RSA_C) &&  defined(MBEDTLS_SHA512_C) | 
|  | 3976 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, | 
|  | 3977 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ | 
|  | 3978 |  | 
|  | 3979 | #if defined(MBEDTLS_RSA_C) &&  defined(MBEDTLS_SHA384_C) | 
|  | 3980 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, | 
|  | 3981 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ | 
|  | 3982 |  | 
| Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 3983 | #if defined(MBEDTLS_RSA_C) &&  defined(MBEDTLS_SHA256_C) | 
|  | 3984 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, | 
|  | 3985 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ | 
|  | 3986 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3987 | MBEDTLS_TLS1_3_SIG_NONE | 
|  | 3988 | }; | 
|  | 3989 |  | 
|  | 3990 | /* NOTICE: see above */ | 
|  | 3991 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3992 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3993 | #if defined(MBEDTLS_SHA512_C) | 
|  | 3994 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 ) | 
|  | 3995 | #endif | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 3996 | #if defined(MBEDTLS_SHA384_C) | 
|  | 3997 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) | 
|  | 3998 | #endif | 
|  | 3999 | #if defined(MBEDTLS_SHA256_C) | 
|  | 4000 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) | 
|  | 4001 | #endif | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4002 | MBEDTLS_TLS1_3_SIG_NONE | 
|  | 4003 | }; | 
|  | 4004 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 4005 | /* NOTICE: see above */ | 
|  | 4006 | static uint16_t ssl_preset_suiteb_sig_algs[] = { | 
|  | 4007 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4008 | #if defined(MBEDTLS_ECDSA_C) &&  defined(MBEDTLS_SHA256_C) && \ | 
|  | 4009 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | 
|  | 4010 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, | 
|  | 4011 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && | 
|  | 4012 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ | 
|  | 4013 |  | 
| Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4014 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ | 
|  | 4015 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
|  | 4016 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, | 
|  | 4017 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && | 
|  | 4018 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4019 |  | 
|  | 4020 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) | 
|  | 4021 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, | 
|  | 4022 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4023 |  | 
| Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4024 | #if defined(MBEDTLS_RSA_C) &&  defined(MBEDTLS_SHA256_C) | 
|  | 4025 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, | 
|  | 4026 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ | 
|  | 4027 |  | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4028 | MBEDTLS_TLS1_3_SIG_NONE | 
|  | 4029 | }; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4030 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4031 | /* NOTICE: see above */ | 
|  | 4032 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4033 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4034 | #if defined(MBEDTLS_SHA256_C) | 
|  | 4035 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) | 
|  | 4036 | #endif | 
| Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 4037 | #if defined(MBEDTLS_SHA384_C) | 
|  | 4038 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) | 
|  | 4039 | #endif | 
| Xiaofei Bai | 9539501 | 2021-11-25 08:51:30 +0000 | [diff] [blame] | 4040 | MBEDTLS_TLS1_3_SIG_NONE | 
| Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4041 | }; | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4042 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 4043 |  | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4044 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4045 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4046 | static uint16_t ssl_preset_suiteb_groups[] = { | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4047 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4048 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4049 | #endif | 
|  | 4050 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4051 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4052 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4053 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4054 | }; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4055 |  | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4056 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4057 | /* 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] | 4058 | * to make sure there are no duplicated signature algorithm entries. */ | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4059 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4060 | { | 
|  | 4061 | size_t i, j; | 
|  | 4062 | int ret = 0; | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4063 |  | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4064 | for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ ) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4065 | { | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4066 | for( j = 0; j < i; j++ ) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4067 | { | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4068 | if( sig_algs[i] != sig_algs[j] ) | 
|  | 4069 | continue; | 
|  | 4070 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET | 
|  | 4071 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", | 
|  | 4072 | sig_algs[i], j, i ); | 
|  | 4073 | ret = -1; | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4074 | } | 
|  | 4075 | } | 
|  | 4076 | return( ret ); | 
|  | 4077 | } | 
|  | 4078 |  | 
|  | 4079 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 4080 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4081 | /* | 
| Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4082 | * Load default in mbedtls_ssl_config | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4083 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4084 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4085 | int endpoint, int transport, int preset ) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4086 | { | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4087 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4088 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4089 | #endif | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4090 |  | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4091 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4092 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4093 | { | 
|  | 4094 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); | 
|  | 4095 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | 
|  | 4096 | } | 
|  | 4097 |  | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4098 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4099 | { | 
|  | 4100 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); | 
|  | 4101 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | 
|  | 4102 | } | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4103 |  | 
|  | 4104 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4105 | 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] | 4106 | { | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4107 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4108 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | 
|  | 4109 | } | 
|  | 4110 |  | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4111 | 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] | 4112 | { | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4113 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4114 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | 
|  | 4115 | } | 
|  | 4116 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4117 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 4118 |  | 
| Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4119 | /* Use the functions here so that they are covered in tests, | 
|  | 4120 | * but otherwise access member directly for efficiency */ | 
|  | 4121 | mbedtls_ssl_conf_endpoint( conf, endpoint ); | 
|  | 4122 | mbedtls_ssl_conf_transport( conf, transport ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4123 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4124 | /* | 
|  | 4125 | * Things that are common to all presets | 
|  | 4126 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4127 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 4128 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 4129 | { | 
|  | 4130 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; | 
|  | 4131 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 4132 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; | 
|  | 4133 | #endif | 
|  | 4134 | } | 
|  | 4135 | #endif | 
|  | 4136 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4137 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 4138 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | 
|  | 4139 | #endif | 
|  | 4140 |  | 
|  | 4141 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 4142 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | 
|  | 4143 | #endif | 
|  | 4144 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4145 | #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] | 4146 | conf->f_cookie_write = ssl_cookie_write_dummy; | 
|  | 4147 | conf->f_cookie_check = ssl_cookie_check_dummy; | 
|  | 4148 | #endif | 
|  | 4149 |  | 
|  | 4150 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 4151 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; | 
|  | 4152 | #endif | 
|  | 4153 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4154 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 4155 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; | 
| TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4156 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4157 | #endif | 
|  | 4158 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4159 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4160 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; | 
|  | 4161 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; | 
|  | 4162 | #endif | 
|  | 4163 |  | 
|  | 4164 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 4165 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; | 
| Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4166 | memset( conf->renego_period,     0x00, 2 ); | 
|  | 4167 | memset( conf->renego_period + 2, 0xFF, 6 ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4168 | #endif | 
|  | 4169 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4170 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4171 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 4172 | { | 
|  | 4173 | const unsigned char dhm_p[] = | 
|  | 4174 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; | 
|  | 4175 | const unsigned char dhm_g[] = | 
|  | 4176 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; | 
| Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4177 |  | 
| Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4178 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, | 
|  | 4179 | dhm_p, sizeof( dhm_p ), | 
|  | 4180 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) | 
|  | 4181 | { | 
|  | 4182 | return( ret ); | 
|  | 4183 | } | 
|  | 4184 | } | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4185 | #endif | 
|  | 4186 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4187 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4188 | /* | 
|  | 4189 | * Allow all TLS 1.3 key exchange modes by default. | 
|  | 4190 | */ | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4191 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4192 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4193 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4194 | /* | 
|  | 4195 | * Preset-specific defaults | 
|  | 4196 | */ | 
|  | 4197 | switch( preset ) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4198 | { | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4199 | /* | 
|  | 4200 | * NSA Suite B | 
|  | 4201 | */ | 
|  | 4202 | case MBEDTLS_SSL_PRESET_SUITEB: | 
|  | 4203 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
|  | 4204 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ | 
|  | 4205 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; | 
| Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4206 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 4207 | /* Hybrid TLS 1.2/1.3 is not supported yet */ | 
|  | 4208 | conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; | 
|  | 4209 | #else | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4210 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; | 
| Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4211 | #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] | 4212 |  | 
| Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4213 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4214 |  | 
|  | 4215 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 4216 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4217 | #endif | 
|  | 4218 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4219 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4220 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4221 | conf->sig_hashes = ssl_preset_suiteb_hashes; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4222 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4223 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4224 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) | 
|  | 4225 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; | 
|  | 4226 | else | 
|  | 4227 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 4228 | conf->sig_algs = ssl_preset_suiteb_sig_algs; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4229 | #endif | 
|  | 4230 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4231 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 4232 | conf->curve_list = NULL; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4233 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4234 | conf->group_list = ssl_preset_suiteb_groups; | 
| Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4235 | break; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4236 |  | 
|  | 4237 | /* | 
|  | 4238 | * Default | 
|  | 4239 | */ | 
|  | 4240 | default: | 
| Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 4241 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > | 
|  | 4242 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? | 
|  | 4243 | MBEDTLS_SSL_MIN_MAJOR_VERSION : | 
|  | 4244 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; | 
|  | 4245 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > | 
|  | 4246 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? | 
|  | 4247 | MBEDTLS_SSL_MIN_MINOR_VERSION : | 
|  | 4248 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4249 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; | 
| Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4250 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 4251 | /* Hybrid TLS 1.2/1.3 is not supported yet */ | 
|  | 4252 | conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; | 
|  | 4253 | #else | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4254 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; | 
| Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4255 | #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] | 4256 |  | 
|  | 4257 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4258 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| TRodziewicz | ef73f01 | 2021-05-13 14:53:36 +0200 | [diff] [blame] | 4259 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4260 | #endif | 
| Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4261 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4262 |  | 
|  | 4263 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 4264 | conf->cert_profile = &mbedtls_x509_crt_profile_default; | 
|  | 4265 | #endif | 
|  | 4266 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4267 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4268 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4269 | conf->sig_hashes = ssl_preset_default_hashes; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4270 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4271 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4272 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) | 
|  | 4273 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; | 
|  | 4274 | else | 
|  | 4275 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 4276 | conf->sig_algs = ssl_preset_default_sig_algs; | 
| Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 4277 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4278 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4279 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 4280 | conf->curve_list = NULL; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4281 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4282 | conf->group_list = ssl_preset_default_groups; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4283 |  | 
|  | 4284 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 4285 | conf->dhm_min_bitlen = 1024; | 
|  | 4286 | #endif | 
|  | 4287 | } | 
|  | 4288 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4289 | return( 0 ); | 
|  | 4290 | } | 
|  | 4291 |  | 
|  | 4292 | /* | 
|  | 4293 | * Free mbedtls_ssl_config | 
|  | 4294 | */ | 
|  | 4295 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) | 
|  | 4296 | { | 
|  | 4297 | #if defined(MBEDTLS_DHM_C) | 
|  | 4298 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 4299 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 4300 | #endif | 
|  | 4301 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4302 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4303 | if( conf->psk != NULL ) | 
|  | 4304 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4305 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4306 | mbedtls_free( conf->psk ); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4307 | conf->psk = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4308 | conf->psk_len = 0; | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4309 | } | 
|  | 4310 |  | 
|  | 4311 | if( conf->psk_identity != NULL ) | 
|  | 4312 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4313 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4314 | mbedtls_free( conf->psk_identity ); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4315 | conf->psk_identity = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4316 | conf->psk_identity_len = 0; | 
|  | 4317 | } | 
|  | 4318 | #endif | 
|  | 4319 |  | 
|  | 4320 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 4321 | ssl_key_cert_free( conf->key_cert ); | 
|  | 4322 | #endif | 
|  | 4323 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4324 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4325 | } | 
|  | 4326 |  | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4327 | #if defined(MBEDTLS_PK_C) && \ | 
|  | 4328 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4329 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4330 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4331 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4332 | 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] | 4333 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4334 | #if defined(MBEDTLS_RSA_C) | 
|  | 4335 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) | 
|  | 4336 | return( MBEDTLS_SSL_SIG_RSA ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4337 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4338 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 4339 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) | 
|  | 4340 | return( MBEDTLS_SSL_SIG_ECDSA ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4341 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4342 | return( MBEDTLS_SSL_SIG_ANON ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4343 | } | 
|  | 4344 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4345 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) | 
|  | 4346 | { | 
|  | 4347 | switch( type ) { | 
|  | 4348 | case MBEDTLS_PK_RSA: | 
|  | 4349 | return( MBEDTLS_SSL_SIG_RSA ); | 
|  | 4350 | case MBEDTLS_PK_ECDSA: | 
|  | 4351 | case MBEDTLS_PK_ECKEY: | 
|  | 4352 | return( MBEDTLS_SSL_SIG_ECDSA ); | 
|  | 4353 | default: | 
|  | 4354 | return( MBEDTLS_SSL_SIG_ANON ); | 
|  | 4355 | } | 
|  | 4356 | } | 
|  | 4357 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4358 | 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] | 4359 | { | 
|  | 4360 | switch( sig ) | 
|  | 4361 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4362 | #if defined(MBEDTLS_RSA_C) | 
|  | 4363 | case MBEDTLS_SSL_SIG_RSA: | 
|  | 4364 | return( MBEDTLS_PK_RSA ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4365 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4366 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 4367 | case MBEDTLS_SSL_SIG_ECDSA: | 
|  | 4368 | return( MBEDTLS_PK_ECDSA ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4369 | #endif | 
|  | 4370 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4371 | return( MBEDTLS_PK_NONE ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4372 | } | 
|  | 4373 | } | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4374 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4375 |  | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4376 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4377 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4378 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4379 | 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] | 4380 | { | 
|  | 4381 | switch( hash ) | 
|  | 4382 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4383 | #if defined(MBEDTLS_MD5_C) | 
|  | 4384 | case MBEDTLS_SSL_HASH_MD5: | 
|  | 4385 | return( MBEDTLS_MD_MD5 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4386 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4387 | #if defined(MBEDTLS_SHA1_C) | 
|  | 4388 | case MBEDTLS_SSL_HASH_SHA1: | 
|  | 4389 | return( MBEDTLS_MD_SHA1 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4390 | #endif | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4391 | #if defined(MBEDTLS_SHA224_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4392 | case MBEDTLS_SSL_HASH_SHA224: | 
|  | 4393 | return( MBEDTLS_MD_SHA224 ); | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4394 | #endif | 
|  | 4395 | #if defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4396 | case MBEDTLS_SSL_HASH_SHA256: | 
|  | 4397 | return( MBEDTLS_MD_SHA256 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4398 | #endif | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4399 | #if defined(MBEDTLS_SHA384_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4400 | case MBEDTLS_SSL_HASH_SHA384: | 
|  | 4401 | return( MBEDTLS_MD_SHA384 ); | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4402 | #endif | 
|  | 4403 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4404 | case MBEDTLS_SSL_HASH_SHA512: | 
|  | 4405 | return( MBEDTLS_MD_SHA512 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4406 | #endif | 
|  | 4407 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4408 | return( MBEDTLS_MD_NONE ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4409 | } | 
|  | 4410 | } | 
|  | 4411 |  | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4412 | /* | 
|  | 4413 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX | 
|  | 4414 | */ | 
|  | 4415 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) | 
|  | 4416 | { | 
|  | 4417 | switch( md ) | 
|  | 4418 | { | 
|  | 4419 | #if defined(MBEDTLS_MD5_C) | 
|  | 4420 | case MBEDTLS_MD_MD5: | 
|  | 4421 | return( MBEDTLS_SSL_HASH_MD5 ); | 
|  | 4422 | #endif | 
|  | 4423 | #if defined(MBEDTLS_SHA1_C) | 
|  | 4424 | case MBEDTLS_MD_SHA1: | 
|  | 4425 | return( MBEDTLS_SSL_HASH_SHA1 ); | 
|  | 4426 | #endif | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4427 | #if defined(MBEDTLS_SHA224_C) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4428 | case MBEDTLS_MD_SHA224: | 
|  | 4429 | return( MBEDTLS_SSL_HASH_SHA224 ); | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4430 | #endif | 
|  | 4431 | #if defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4432 | case MBEDTLS_MD_SHA256: | 
|  | 4433 | return( MBEDTLS_SSL_HASH_SHA256 ); | 
|  | 4434 | #endif | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4435 | #if defined(MBEDTLS_SHA384_C) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4436 | case MBEDTLS_MD_SHA384: | 
|  | 4437 | return( MBEDTLS_SSL_HASH_SHA384 ); | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4438 | #endif | 
|  | 4439 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4440 | case MBEDTLS_MD_SHA512: | 
|  | 4441 | return( MBEDTLS_SSL_HASH_SHA512 ); | 
|  | 4442 | #endif | 
|  | 4443 | default: | 
|  | 4444 | return( MBEDTLS_SSL_HASH_NONE ); | 
|  | 4445 | } | 
|  | 4446 | } | 
|  | 4447 |  | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4448 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4449 | * 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] | 4450 | * 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] | 4451 | */ | 
| Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4452 | 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] | 4453 | { | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4454 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4455 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4456 | if( group_list == NULL ) | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4457 | return( -1 ); | 
|  | 4458 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4459 | for( ; *group_list != 0; group_list++ ) | 
|  | 4460 | { | 
|  | 4461 | if( *group_list == tls_id ) | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4462 | return( 0 ); | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4463 | } | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4464 |  | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4465 | return( -1 ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4466 | } | 
| Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4467 |  | 
|  | 4468 | #if defined(MBEDTLS_ECP_C) | 
|  | 4469 | /* | 
|  | 4470 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. | 
|  | 4471 | */ | 
|  | 4472 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) | 
|  | 4473 | { | 
| Manuel Pégourié-Gonnard | 422370d | 2022-02-07 11:55:21 +0100 | [diff] [blame] | 4474 | 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] | 4475 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); | 
|  | 4476 | } | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4477 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4478 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4479 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 4480 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, | 
|  | 4481 | const mbedtls_ssl_ciphersuite_t *ciphersuite, | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4482 | int cert_endpoint, | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4483 | uint32_t *flags ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4484 | { | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4485 | int ret = 0; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4486 | int usage = 0; | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4487 | const char *ext_oid; | 
|  | 4488 | size_t ext_len; | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4489 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4490 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4491 | { | 
|  | 4492 | /* Server part of the key exchange */ | 
|  | 4493 | switch( ciphersuite->key_exchange ) | 
|  | 4494 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4495 | case MBEDTLS_KEY_EXCHANGE_RSA: | 
|  | 4496 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4497 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4498 | break; | 
|  | 4499 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4500 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: | 
|  | 4501 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: | 
|  | 4502 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: | 
|  | 4503 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4504 | break; | 
|  | 4505 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4506 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: | 
|  | 4507 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4508 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4509 | break; | 
|  | 4510 |  | 
|  | 4511 | /* 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] | 4512 | case MBEDTLS_KEY_EXCHANGE_NONE: | 
|  | 4513 | case MBEDTLS_KEY_EXCHANGE_PSK: | 
|  | 4514 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: | 
|  | 4515 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: | 
| Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 4516 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4517 | usage = 0; | 
|  | 4518 | } | 
|  | 4519 | } | 
|  | 4520 | else | 
|  | 4521 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4522 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ | 
|  | 4523 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4524 | } | 
|  | 4525 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4526 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4527 | { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4528 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4529 | ret = -1; | 
|  | 4530 | } | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4531 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4532 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4533 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4534 | ext_oid = MBEDTLS_OID_SERVER_AUTH; | 
|  | 4535 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4536 | } | 
|  | 4537 | else | 
|  | 4538 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4539 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; | 
|  | 4540 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4541 | } | 
|  | 4542 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4543 | 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] | 4544 | { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4545 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4546 | ret = -1; | 
|  | 4547 | } | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4548 |  | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4549 | return( ret ); | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4550 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4551 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 4552 |  | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4553 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 4554 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, | 
|  | 4555 | const mbedtls_md_type_t md, | 
|  | 4556 | unsigned char *dst, | 
|  | 4557 | size_t dst_len, | 
|  | 4558 | size_t *olen ) | 
|  | 4559 | { | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4560 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 4561 | psa_hash_operation_t *hash_operation_to_clone; | 
|  | 4562 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); | 
|  | 4563 |  | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4564 | *olen = 0; | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4565 |  | 
|  | 4566 | switch( md ) | 
|  | 4567 | { | 
|  | 4568 | #if defined(MBEDTLS_SHA384_C) | 
|  | 4569 | case MBEDTLS_MD_SHA384: | 
|  | 4570 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; | 
|  | 4571 | break; | 
|  | 4572 | #endif | 
|  | 4573 |  | 
|  | 4574 | #if defined(MBEDTLS_SHA256_C) | 
|  | 4575 | case MBEDTLS_MD_SHA256: | 
|  | 4576 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; | 
|  | 4577 | break; | 
|  | 4578 | #endif | 
|  | 4579 |  | 
|  | 4580 | default: | 
|  | 4581 | goto exit; | 
|  | 4582 | } | 
|  | 4583 |  | 
|  | 4584 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); | 
|  | 4585 | if( status != PSA_SUCCESS ) | 
|  | 4586 | goto exit; | 
|  | 4587 |  | 
|  | 4588 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); | 
|  | 4589 | if( status != PSA_SUCCESS ) | 
|  | 4590 | goto exit; | 
|  | 4591 |  | 
|  | 4592 | exit: | 
| Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 4593 | return( psa_ssl_status_to_mbedtls( status ) ); | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4594 | } | 
|  | 4595 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 4596 |  | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4597 | #if defined(MBEDTLS_SHA384_C) | 
| Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4598 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, | 
|  | 4599 | unsigned char *dst, | 
|  | 4600 | size_t dst_len, | 
|  | 4601 | size_t *olen ) | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4602 | { | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4603 | int ret; | 
|  | 4604 | mbedtls_sha512_context sha512; | 
|  | 4605 |  | 
|  | 4606 | if( dst_len < 48 ) | 
|  | 4607 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4608 |  | 
|  | 4609 | mbedtls_sha512_init( &sha512 ); | 
|  | 4610 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
|  | 4611 |  | 
|  | 4612 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) | 
|  | 4613 | { | 
|  | 4614 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); | 
|  | 4615 | goto exit; | 
|  | 4616 | } | 
|  | 4617 |  | 
|  | 4618 | *olen = 48; | 
|  | 4619 |  | 
|  | 4620 | exit: | 
|  | 4621 |  | 
|  | 4622 | mbedtls_sha512_free( &sha512 ); | 
|  | 4623 | return( ret ); | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4624 | } | 
|  | 4625 | #endif /* MBEDTLS_SHA384_C */ | 
|  | 4626 |  | 
|  | 4627 | #if defined(MBEDTLS_SHA256_C) | 
| Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4628 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, | 
|  | 4629 | unsigned char *dst, | 
|  | 4630 | size_t dst_len, | 
|  | 4631 | size_t *olen ) | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4632 | { | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4633 | int ret; | 
|  | 4634 | mbedtls_sha256_context sha256; | 
|  | 4635 |  | 
|  | 4636 | if( dst_len < 32 ) | 
|  | 4637 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4638 |  | 
|  | 4639 | mbedtls_sha256_init( &sha256 ); | 
|  | 4640 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4641 |  | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4642 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) | 
|  | 4643 | { | 
|  | 4644 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); | 
|  | 4645 | goto exit; | 
|  | 4646 | } | 
|  | 4647 |  | 
|  | 4648 | *olen = 32; | 
|  | 4649 |  | 
|  | 4650 | exit: | 
|  | 4651 |  | 
|  | 4652 | mbedtls_sha256_free( &sha256 ); | 
|  | 4653 | return( ret ); | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4654 | } | 
|  | 4655 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 4656 |  | 
| Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4657 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, | 
|  | 4658 | const mbedtls_md_type_t md, | 
|  | 4659 | unsigned char *dst, | 
|  | 4660 | size_t dst_len, | 
|  | 4661 | size_t *olen ) | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4662 | { | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4663 | switch( md ) | 
|  | 4664 | { | 
|  | 4665 |  | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4666 | #if defined(MBEDTLS_SHA384_C) | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4667 | case MBEDTLS_MD_SHA384: | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4668 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); | 
| Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4669 | #endif /* MBEDTLS_SHA384_C */ | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4670 |  | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4671 | #if defined(MBEDTLS_SHA256_C) | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4672 | case MBEDTLS_MD_SHA256: | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4673 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); | 
| Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4674 | #endif /* MBEDTLS_SHA256_C */ | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4675 |  | 
|  | 4676 | default: | 
|  | 4677 | break; | 
|  | 4678 | } | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4679 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4680 | } | 
| XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 4681 |  | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4682 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4683 |  | 
| Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 4684 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \ | 
|  | 4685 | defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
|  | 4686 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4687 | /* | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4688 | * Function for writing a supported groups (TLS 1.3) or supported elliptic | 
|  | 4689 | * curves (TLS 1.2) extension. | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4690 | * | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4691 | * The "extension_data" field of a supported groups extension contains a | 
|  | 4692 | * "NamedGroupList" value (TLS 1.3 RFC8446): | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4693 | *      enum { | 
|  | 4694 | *          secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019), | 
|  | 4695 | *          x25519(0x001D), x448(0x001E), | 
|  | 4696 | *          ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102), | 
|  | 4697 | *          ffdhe6144(0x0103), ffdhe8192(0x0104), | 
|  | 4698 | *          ffdhe_private_use(0x01FC..0x01FF), | 
|  | 4699 | *          ecdhe_private_use(0xFE00..0xFEFF), | 
|  | 4700 | *          (0xFFFF) | 
|  | 4701 | *      } NamedGroup; | 
|  | 4702 | *      struct { | 
|  | 4703 | *          NamedGroup named_group_list<2..2^16-1>; | 
|  | 4704 | *      } NamedGroupList; | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4705 | * | 
|  | 4706 | * The "extension_data" field of a supported elliptic curves extension contains | 
|  | 4707 | * a "NamedCurveList" value (TLS 1.2 RFC 8422): | 
| Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4708 | * enum { | 
|  | 4709 | *      deprecated(1..22), | 
|  | 4710 | *      secp256r1 (23), secp384r1 (24), secp521r1 (25), | 
|  | 4711 | *      x25519(29), x448(30), | 
|  | 4712 | *      reserved (0xFE00..0xFEFF), | 
|  | 4713 | *      deprecated(0xFF01..0xFF02), | 
|  | 4714 | *      (0xFFFF) | 
|  | 4715 | *  } NamedCurve; | 
|  | 4716 | * struct { | 
|  | 4717 | *      NamedCurve named_curve_list<2..2^16-1> | 
|  | 4718 | *  } NamedCurveList; | 
|  | 4719 | * | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4720 | * The TLS 1.3 supported groups extension was defined to be a compatible | 
|  | 4721 | * generalization of the TLS 1.2 supported elliptic curves extension. They both | 
|  | 4722 | * share the same extension identifier. | 
| Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4723 | * | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4724 | * DHE groups are not supported yet. | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4725 | */ | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4726 | int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, | 
|  | 4727 | unsigned char *buf, | 
| Jerry Yu | 1753261 | 2021-12-20 22:32:09 +0800 | [diff] [blame] | 4728 | const unsigned char *end, | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4729 | size_t *out_len ) | 
|  | 4730 | { | 
|  | 4731 | unsigned char *p = buf ; | 
|  | 4732 | unsigned char *named_group_list; /* Start of named_group_list */ | 
|  | 4733 | size_t named_group_list_len;     /* Length of named_group_list */ | 
|  | 4734 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); | 
|  | 4735 |  | 
|  | 4736 | *out_len = 0; | 
| Jerry Yu | f46b016 | 2022-01-11 16:28:00 +0800 | [diff] [blame] | 4737 |  | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4738 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) ); | 
|  | 4739 |  | 
|  | 4740 | /* Check if we have space for header and length fields: | 
|  | 4741 | * - extension_type            (2 bytes) | 
|  | 4742 | * - extension_data_length     (2 bytes) | 
|  | 4743 | * - named_group_list_length   (2 bytes) | 
|  | 4744 | */ | 
|  | 4745 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); | 
|  | 4746 | p += 6; | 
|  | 4747 |  | 
|  | 4748 | named_group_list = p; | 
|  | 4749 |  | 
|  | 4750 | if( group_list == NULL ) | 
|  | 4751 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 4752 |  | 
| Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4753 | for( ; *group_list != 0; group_list++ ) | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4754 | { | 
| Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4755 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) ); | 
| Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4756 |  | 
| Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 4757 | #if defined(MBEDTLS_ECP_C) | 
| Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4758 | if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) && | 
| Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4759 | mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) || | 
| Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4760 | ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) && | 
| Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4761 | mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) ) | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4762 | { | 
| Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4763 | const mbedtls_ecp_curve_info *curve_info; | 
|  | 4764 | curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list ); | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4765 | if( curve_info == NULL ) | 
| Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4766 | continue; | 
| Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4767 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
|  | 4768 | MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 ); | 
|  | 4769 | p += 2; | 
|  | 4770 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )", | 
|  | 4771 | curve_info->name, *group_list ) ); | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4772 | } | 
| Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4773 | #endif /* MBEDTLS_ECP_C */ | 
|  | 4774 | /* Add DHE groups here */ | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4775 |  | 
|  | 4776 | } | 
|  | 4777 |  | 
| Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4778 | /* Length of named_group_list */ | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4779 | named_group_list_len = p - named_group_list; | 
|  | 4780 | if( named_group_list_len == 0 ) | 
|  | 4781 | { | 
|  | 4782 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) ); | 
|  | 4783 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4784 | } | 
|  | 4785 |  | 
|  | 4786 | /* Write extension_type */ | 
|  | 4787 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 ); | 
|  | 4788 | /* Write extension_data_length */ | 
|  | 4789 | MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 ); | 
|  | 4790 | /* Write length of named_group_list */ | 
|  | 4791 | MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 ); | 
|  | 4792 |  | 
| Jerry Yu | 7f029d8 | 2022-01-11 11:08:53 +0800 | [diff] [blame] | 4793 | MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", | 
|  | 4794 | buf + 4, named_group_list_len + 2 ); | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4795 |  | 
|  | 4796 | *out_len = p - buf; | 
|  | 4797 |  | 
|  | 4798 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 4799 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; | 
|  | 4800 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 4801 |  | 
|  | 4802 | return( 0 ); | 
|  | 4803 | } | 
|  | 4804 |  | 
| Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 4805 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED || | 
|  | 4806 | MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || | 
|  | 4807 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4808 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4809 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 4810 | /* | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4811 | * Function for writing a signature algorithm extension. | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4812 | * | 
| Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 4813 | * The `extension_data` field of signature algorithm contains  a `SignatureSchemeList` | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4814 | * value (TLS 1.3 RFC8446): | 
|  | 4815 | *      enum { | 
|  | 4816 | *         .... | 
|  | 4817 | *        ecdsa_secp256r1_sha256( 0x0403 ), | 
|  | 4818 | *        ecdsa_secp384r1_sha384( 0x0503 ), | 
|  | 4819 | *        ecdsa_secp521r1_sha512( 0x0603 ), | 
|  | 4820 | *         .... | 
|  | 4821 | *      } SignatureScheme; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4822 | * | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4823 | *      struct { | 
|  | 4824 | *         SignatureScheme supported_signature_algorithms<2..2^16-2>; | 
|  | 4825 | *      } SignatureSchemeList; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4826 | * | 
| Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 4827 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` | 
|  | 4828 | * value (TLS 1.2 RFC5246): | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4829 | *      enum { | 
|  | 4830 | *          none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), | 
|  | 4831 | *          sha512(6), (255) | 
|  | 4832 | *      } HashAlgorithm; | 
|  | 4833 | * | 
|  | 4834 | *      enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } | 
|  | 4835 | *        SignatureAlgorithm; | 
|  | 4836 | * | 
|  | 4837 | *      struct { | 
|  | 4838 | *          HashAlgorithm hash; | 
|  | 4839 | *          SignatureAlgorithm signature; | 
|  | 4840 | *      } SignatureAndHashAlgorithm; | 
|  | 4841 | * | 
|  | 4842 | *      SignatureAndHashAlgorithm | 
|  | 4843 | *        supported_signature_algorithms<2..2^16-2>; | 
|  | 4844 | * | 
|  | 4845 | * The TLS 1.3 signature algorithm extension was defined to be a compatible | 
|  | 4846 | * generalization of the TLS 1.2 signature algorithm extension. | 
|  | 4847 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by | 
|  | 4848 | * `SignatureScheme` field of TLS 1.3 | 
|  | 4849 | * | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4850 | */ | 
|  | 4851 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, | 
|  | 4852 | const unsigned char *end, size_t *out_len ) | 
|  | 4853 | { | 
|  | 4854 | unsigned char *p = buf; | 
|  | 4855 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ | 
|  | 4856 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ | 
|  | 4857 |  | 
|  | 4858 | *out_len = 0; | 
|  | 4859 |  | 
|  | 4860 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); | 
|  | 4861 |  | 
|  | 4862 | /* Check if we have space for header and length field: | 
|  | 4863 | * - extension_type         (2 bytes) | 
|  | 4864 | * - extension_data_length  (2 bytes) | 
|  | 4865 | * - supported_signature_algorithms_length   (2 bytes) | 
|  | 4866 | */ | 
|  | 4867 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); | 
|  | 4868 | p += 6; | 
|  | 4869 |  | 
|  | 4870 | /* | 
|  | 4871 | * Write supported_signature_algorithms | 
|  | 4872 | */ | 
|  | 4873 | supported_sig_alg = p; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4874 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); | 
|  | 4875 | if( sig_alg == NULL ) | 
|  | 4876 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | 
|  | 4877 |  | 
|  | 4878 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4879 | { | 
| Jerry Yu | 1bab301 | 2022-01-19 17:43:22 +0800 | [diff] [blame] | 4880 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) | 
|  | 4881 | continue; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4882 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | 
|  | 4883 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); | 
|  | 4884 | p += 2; | 
|  | 4885 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) ); | 
|  | 4886 | } | 
|  | 4887 |  | 
|  | 4888 | /* Length of supported_signature_algorithms */ | 
|  | 4889 | supported_sig_alg_len = p - supported_sig_alg; | 
|  | 4890 | if( supported_sig_alg_len == 0 ) | 
|  | 4891 | { | 
|  | 4892 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); | 
|  | 4893 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4894 | } | 
|  | 4895 |  | 
|  | 4896 | /* Write extension_type */ | 
|  | 4897 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); | 
|  | 4898 | /* Write extension_data_length */ | 
|  | 4899 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); | 
|  | 4900 | /* Write length of supported_signature_algorithms */ | 
|  | 4901 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); | 
|  | 4902 |  | 
|  | 4903 | /* Output the total length of signature algorithms extension. */ | 
|  | 4904 | *out_len = p - buf; | 
|  | 4905 |  | 
|  | 4906 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 4907 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; | 
|  | 4908 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 4909 | return( 0 ); | 
|  | 4910 | } | 
|  | 4911 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 4912 |  | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4913 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 4914 | int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl, | 
|  | 4915 | unsigned char *buf, | 
|  | 4916 | const unsigned char *end, | 
|  | 4917 | size_t *olen ) | 
|  | 4918 | { | 
|  | 4919 | unsigned char *p = buf; | 
|  | 4920 | size_t hostname_len; | 
|  | 4921 |  | 
|  | 4922 | *olen = 0; | 
|  | 4923 |  | 
|  | 4924 | if( ssl->hostname == NULL ) | 
|  | 4925 | return( 0 ); | 
|  | 4926 |  | 
|  | 4927 | MBEDTLS_SSL_DEBUG_MSG( 3, | 
|  | 4928 | ( "client hello, adding server name extension: %s", | 
|  | 4929 | ssl->hostname ) ); | 
|  | 4930 |  | 
|  | 4931 | hostname_len = strlen( ssl->hostname ); | 
|  | 4932 |  | 
|  | 4933 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 ); | 
|  | 4934 |  | 
|  | 4935 | /* | 
|  | 4936 | * Sect. 3, RFC 6066 (TLS Extensions Definitions) | 
|  | 4937 | * | 
|  | 4938 | * In order to provide any of the server names, clients MAY include an | 
|  | 4939 | * extension of type "server_name" in the (extended) client hello. The | 
|  | 4940 | * "extension_data" field of this extension SHALL contain | 
|  | 4941 | * "ServerNameList" where: | 
|  | 4942 | * | 
|  | 4943 | * struct { | 
|  | 4944 | *     NameType name_type; | 
|  | 4945 | *     select (name_type) { | 
|  | 4946 | *         case host_name: HostName; | 
|  | 4947 | *     } name; | 
|  | 4948 | * } ServerName; | 
|  | 4949 | * | 
|  | 4950 | * enum { | 
|  | 4951 | *     host_name(0), (255) | 
|  | 4952 | * } NameType; | 
|  | 4953 | * | 
|  | 4954 | * opaque HostName<1..2^16-1>; | 
|  | 4955 | * | 
|  | 4956 | * struct { | 
|  | 4957 | *     ServerName server_name_list<1..2^16-1> | 
|  | 4958 | * } ServerNameList; | 
|  | 4959 | * | 
|  | 4960 | */ | 
|  | 4961 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 ); | 
|  | 4962 | p += 2; | 
|  | 4963 |  | 
|  | 4964 | MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 ); | 
|  | 4965 | p += 2; | 
|  | 4966 |  | 
|  | 4967 | MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 ); | 
|  | 4968 | p += 2; | 
|  | 4969 |  | 
|  | 4970 | *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); | 
|  | 4971 |  | 
|  | 4972 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); | 
|  | 4973 | p += 2; | 
|  | 4974 |  | 
|  | 4975 | memcpy( p, ssl->hostname, hostname_len ); | 
|  | 4976 |  | 
|  | 4977 | *olen = hostname_len + 9; | 
|  | 4978 |  | 
|  | 4979 | return( 0 ); | 
|  | 4980 | } | 
|  | 4981 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4982 |  | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4983 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4984 |  | 
|  | 4985 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 4986 |  | 
|  | 4987 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, | 
|  | 4988 | mbedtls_svc_key_id_t key, | 
|  | 4989 | psa_algorithm_t alg, | 
|  | 4990 | const unsigned char* seed, size_t seed_length, | 
|  | 4991 | const unsigned char* label, size_t label_length, | 
|  | 4992 | size_t capacity ) | 
|  | 4993 | { | 
|  | 4994 | psa_status_t status; | 
|  | 4995 |  | 
|  | 4996 | status = psa_key_derivation_setup( derivation, alg ); | 
|  | 4997 | if( status != PSA_SUCCESS ) | 
|  | 4998 | return( status ); | 
|  | 4999 |  | 
|  | 5000 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) | 
|  | 5001 | { | 
|  | 5002 | status = psa_key_derivation_input_bytes( derivation, | 
|  | 5003 | PSA_KEY_DERIVATION_INPUT_SEED, | 
|  | 5004 | seed, seed_length ); | 
|  | 5005 | if( status != PSA_SUCCESS ) | 
|  | 5006 | return( status ); | 
|  | 5007 |  | 
|  | 5008 | if( mbedtls_svc_key_id_is_null( key ) ) | 
|  | 5009 | { | 
|  | 5010 | status = psa_key_derivation_input_bytes( | 
|  | 5011 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, | 
|  | 5012 | NULL, 0 ); | 
|  | 5013 | } | 
|  | 5014 | else | 
|  | 5015 | { | 
|  | 5016 | status = psa_key_derivation_input_key( | 
|  | 5017 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); | 
|  | 5018 | } | 
|  | 5019 | if( status != PSA_SUCCESS ) | 
|  | 5020 | return( status ); | 
|  | 5021 |  | 
|  | 5022 | status = psa_key_derivation_input_bytes( derivation, | 
|  | 5023 | PSA_KEY_DERIVATION_INPUT_LABEL, | 
|  | 5024 | label, label_length ); | 
|  | 5025 | if( status != PSA_SUCCESS ) | 
|  | 5026 | return( status ); | 
|  | 5027 | } | 
|  | 5028 | else | 
|  | 5029 | { | 
|  | 5030 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 5031 | } | 
|  | 5032 |  | 
|  | 5033 | status = psa_key_derivation_set_capacity( derivation, capacity ); | 
|  | 5034 | if( status != PSA_SUCCESS ) | 
|  | 5035 | return( status ); | 
|  | 5036 |  | 
|  | 5037 | return( PSA_SUCCESS ); | 
|  | 5038 | } | 
|  | 5039 |  | 
|  | 5040 | static int tls_prf_generic( mbedtls_md_type_t md_type, | 
|  | 5041 | const unsigned char *secret, size_t slen, | 
|  | 5042 | const char *label, | 
|  | 5043 | const unsigned char *random, size_t rlen, | 
|  | 5044 | unsigned char *dstbuf, size_t dlen ) | 
|  | 5045 | { | 
|  | 5046 | psa_status_t status; | 
|  | 5047 | psa_algorithm_t alg; | 
|  | 5048 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 5049 | psa_key_derivation_operation_t derivation = | 
|  | 5050 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 5051 |  | 
|  | 5052 | if( md_type == MBEDTLS_MD_SHA384 ) | 
|  | 5053 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); | 
|  | 5054 | else | 
|  | 5055 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); | 
|  | 5056 |  | 
|  | 5057 | /* Normally a "secret" should be long enough to be impossible to | 
|  | 5058 | * find by brute force, and in particular should not be empty. But | 
|  | 5059 | * this PRF is also used to derive an IV, in particular in EAP-TLS, | 
|  | 5060 | * and for this use case it makes sense to have a 0-length "secret". | 
|  | 5061 | * Since the key API doesn't allow importing a key of length 0, | 
|  | 5062 | * keep master_key=0, which setup_psa_key_derivation() understands | 
|  | 5063 | * to mean a 0-length "secret" input. */ | 
|  | 5064 | if( slen != 0 ) | 
|  | 5065 | { | 
|  | 5066 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); | 
|  | 5067 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); | 
|  | 5068 | psa_set_key_algorithm( &key_attributes, alg ); | 
|  | 5069 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); | 
|  | 5070 |  | 
|  | 5071 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); | 
|  | 5072 | if( status != PSA_SUCCESS ) | 
|  | 5073 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5074 | } | 
|  | 5075 |  | 
|  | 5076 | status = setup_psa_key_derivation( &derivation, | 
|  | 5077 | master_key, alg, | 
|  | 5078 | random, rlen, | 
|  | 5079 | (unsigned char const *) label, | 
|  | 5080 | (size_t) strlen( label ), | 
|  | 5081 | dlen ); | 
|  | 5082 | if( status != PSA_SUCCESS ) | 
|  | 5083 | { | 
|  | 5084 | psa_key_derivation_abort( &derivation ); | 
|  | 5085 | psa_destroy_key( master_key ); | 
|  | 5086 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5087 | } | 
|  | 5088 |  | 
|  | 5089 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); | 
|  | 5090 | if( status != PSA_SUCCESS ) | 
|  | 5091 | { | 
|  | 5092 | psa_key_derivation_abort( &derivation ); | 
|  | 5093 | psa_destroy_key( master_key ); | 
|  | 5094 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5095 | } | 
|  | 5096 |  | 
|  | 5097 | status = psa_key_derivation_abort( &derivation ); | 
|  | 5098 | if( status != PSA_SUCCESS ) | 
|  | 5099 | { | 
|  | 5100 | psa_destroy_key( master_key ); | 
|  | 5101 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5102 | } | 
|  | 5103 |  | 
|  | 5104 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) | 
|  | 5105 | status = psa_destroy_key( master_key ); | 
|  | 5106 | if( status != PSA_SUCCESS ) | 
|  | 5107 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5108 |  | 
|  | 5109 | return( 0 ); | 
|  | 5110 | } | 
|  | 5111 |  | 
|  | 5112 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 5113 |  | 
|  | 5114 | static int tls_prf_generic( mbedtls_md_type_t md_type, | 
|  | 5115 | const unsigned char *secret, size_t slen, | 
|  | 5116 | const char *label, | 
|  | 5117 | const unsigned char *random, size_t rlen, | 
|  | 5118 | unsigned char *dstbuf, size_t dlen ) | 
|  | 5119 | { | 
|  | 5120 | size_t nb; | 
|  | 5121 | size_t i, j, k, md_len; | 
|  | 5122 | unsigned char *tmp; | 
|  | 5123 | size_t tmp_len = 0; | 
|  | 5124 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; | 
|  | 5125 | const mbedtls_md_info_t *md_info; | 
|  | 5126 | mbedtls_md_context_t md_ctx; | 
|  | 5127 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5128 |  | 
|  | 5129 | mbedtls_md_init( &md_ctx ); | 
|  | 5130 |  | 
|  | 5131 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) | 
|  | 5132 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5133 |  | 
|  | 5134 | md_len = mbedtls_md_get_size( md_info ); | 
|  | 5135 |  | 
|  | 5136 | tmp_len = md_len + strlen( label ) + rlen; | 
|  | 5137 | tmp = mbedtls_calloc( 1, tmp_len ); | 
|  | 5138 | if( tmp == NULL ) | 
|  | 5139 | { | 
|  | 5140 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 5141 | goto exit; | 
|  | 5142 | } | 
|  | 5143 |  | 
|  | 5144 | nb = strlen( label ); | 
|  | 5145 | memcpy( tmp + md_len, label, nb ); | 
|  | 5146 | memcpy( tmp + md_len + nb, random, rlen ); | 
|  | 5147 | nb += rlen; | 
|  | 5148 |  | 
|  | 5149 | /* | 
|  | 5150 | * Compute P_<hash>(secret, label + random)[0..dlen] | 
|  | 5151 | */ | 
|  | 5152 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) | 
|  | 5153 | goto exit; | 
|  | 5154 |  | 
|  | 5155 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); | 
|  | 5156 | if( ret != 0 ) | 
|  | 5157 | goto exit; | 
|  | 5158 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); | 
|  | 5159 | if( ret != 0 ) | 
|  | 5160 | goto exit; | 
|  | 5161 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
|  | 5162 | if( ret != 0 ) | 
|  | 5163 | goto exit; | 
|  | 5164 |  | 
|  | 5165 | for( i = 0; i < dlen; i += md_len ) | 
|  | 5166 | { | 
|  | 5167 | ret = mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 5168 | if( ret != 0 ) | 
|  | 5169 | goto exit; | 
|  | 5170 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); | 
|  | 5171 | if( ret != 0 ) | 
|  | 5172 | goto exit; | 
|  | 5173 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); | 
|  | 5174 | if( ret != 0 ) | 
|  | 5175 | goto exit; | 
|  | 5176 |  | 
|  | 5177 | ret = mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 5178 | if( ret != 0 ) | 
|  | 5179 | goto exit; | 
|  | 5180 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); | 
|  | 5181 | if( ret != 0 ) | 
|  | 5182 | goto exit; | 
|  | 5183 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
|  | 5184 | if( ret != 0 ) | 
|  | 5185 | goto exit; | 
|  | 5186 |  | 
|  | 5187 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; | 
|  | 5188 |  | 
|  | 5189 | for( j = 0; j < k; j++ ) | 
|  | 5190 | dstbuf[i + j]  = h_i[j]; | 
|  | 5191 | } | 
|  | 5192 |  | 
|  | 5193 | exit: | 
|  | 5194 | mbedtls_md_free( &md_ctx ); | 
|  | 5195 |  | 
|  | 5196 | mbedtls_platform_zeroize( tmp, tmp_len ); | 
|  | 5197 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); | 
|  | 5198 |  | 
|  | 5199 | mbedtls_free( tmp ); | 
|  | 5200 |  | 
|  | 5201 | return( ret ); | 
|  | 5202 | } | 
|  | 5203 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 5204 |  | 
|  | 5205 | #if defined(MBEDTLS_SHA256_C) | 
|  | 5206 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, | 
|  | 5207 | const char *label, | 
|  | 5208 | const unsigned char *random, size_t rlen, | 
|  | 5209 | unsigned char *dstbuf, size_t dlen ) | 
|  | 5210 | { | 
|  | 5211 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, | 
|  | 5212 | label, random, rlen, dstbuf, dlen ) ); | 
|  | 5213 | } | 
|  | 5214 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 5215 |  | 
|  | 5216 | #if defined(MBEDTLS_SHA384_C) | 
|  | 5217 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, | 
|  | 5218 | const char *label, | 
|  | 5219 | const unsigned char *random, size_t rlen, | 
|  | 5220 | unsigned char *dstbuf, size_t dlen ) | 
|  | 5221 | { | 
|  | 5222 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, | 
|  | 5223 | label, random, rlen, dstbuf, dlen ) ); | 
|  | 5224 | } | 
|  | 5225 | #endif /* MBEDTLS_SHA384_C */ | 
|  | 5226 |  | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5227 | /* | 
|  | 5228 | * Set appropriate PRF function and other SSL / TLS1.2 functions | 
|  | 5229 | * | 
|  | 5230 | * Inputs: | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5231 | * - hash associated with the ciphersuite (only used by TLS 1.2) | 
|  | 5232 | * | 
|  | 5233 | * Outputs: | 
|  | 5234 | * - the tls_prf, calc_verify and calc_finished members of handshake structure | 
|  | 5235 | */ | 
|  | 5236 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5237 | mbedtls_md_type_t hash ) | 
|  | 5238 | { | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5239 | #if defined(MBEDTLS_SHA384_C) | 
| Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5240 | if( hash == MBEDTLS_MD_SHA384 ) | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5241 | { | 
|  | 5242 | handshake->tls_prf = tls_prf_sha384; | 
|  | 5243 | handshake->calc_verify = ssl_calc_verify_tls_sha384; | 
|  | 5244 | handshake->calc_finished = ssl_calc_finished_tls_sha384; | 
|  | 5245 | } | 
|  | 5246 | else | 
|  | 5247 | #endif | 
|  | 5248 | #if defined(MBEDTLS_SHA256_C) | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5249 | { | 
| Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5250 | (void) hash; | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5251 | handshake->tls_prf = tls_prf_sha256; | 
|  | 5252 | handshake->calc_verify = ssl_calc_verify_tls_sha256; | 
|  | 5253 | handshake->calc_finished = ssl_calc_finished_tls_sha256; | 
|  | 5254 | } | 
| Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5255 | #else | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5256 | { | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5257 | (void) handshake; | 
| Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5258 | (void) hash; | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5259 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5260 | } | 
| Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5261 | #endif | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5262 |  | 
|  | 5263 | return( 0 ); | 
|  | 5264 | } | 
| Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5265 |  | 
|  | 5266 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ | 
|  | 5267 | defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 5268 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) | 
|  | 5269 | { | 
|  | 5270 | if( ssl->conf->f_psk != NULL ) | 
|  | 5271 | { | 
|  | 5272 | /* If we've used a callback to select the PSK, | 
|  | 5273 | * the static configuration is irrelevant. */ | 
|  | 5274 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) | 
|  | 5275 | return( 1 ); | 
|  | 5276 |  | 
|  | 5277 | return( 0 ); | 
|  | 5278 | } | 
|  | 5279 |  | 
|  | 5280 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) | 
|  | 5281 | return( 1 ); | 
|  | 5282 |  | 
|  | 5283 | return( 0 ); | 
|  | 5284 | } | 
|  | 5285 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
|  | 5286 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
|  | 5287 |  | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5288 | /* | 
|  | 5289 | * Compute master secret if needed | 
|  | 5290 | * | 
|  | 5291 | * Parameters: | 
|  | 5292 | * [in/out] handshake | 
|  | 5293 | *          [in] resume, premaster, extended_ms, calc_verify, tls_prf | 
|  | 5294 | *               (PSA-PSK) ciphersuite_info, psk_opaque | 
|  | 5295 | *          [out] premaster (cleared) | 
|  | 5296 | * [out] master | 
|  | 5297 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK | 
|  | 5298 | *      debug: conf->f_dbg, conf->p_dbg | 
|  | 5299 | *      EMS: passed to calc_verify (debug + session_negotiate) | 
| Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5300 | *      PSA-PSA: conf | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5301 | */ | 
|  | 5302 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, | 
|  | 5303 | unsigned char *master, | 
|  | 5304 | const mbedtls_ssl_context *ssl ) | 
|  | 5305 | { | 
|  | 5306 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5307 |  | 
|  | 5308 | /* cf. RFC 5246, Section 8.1: | 
|  | 5309 | * "The master secret is always exactly 48 bytes in length." */ | 
|  | 5310 | size_t const master_secret_len = 48; | 
|  | 5311 |  | 
|  | 5312 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 5313 | unsigned char session_hash[48]; | 
|  | 5314 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
|  | 5315 |  | 
|  | 5316 | /* The label for the KDF used for key expansion. | 
|  | 5317 | * This is either "master secret" or "extended master secret" | 
|  | 5318 | * depending on whether the Extended Master Secret extension | 
|  | 5319 | * is used. */ | 
|  | 5320 | char const *lbl = "master secret"; | 
|  | 5321 |  | 
|  | 5322 | /* The salt for the KDF used for key expansion. | 
|  | 5323 | * - If the Extended Master Secret extension is not used, | 
|  | 5324 | *   this is ClientHello.Random + ServerHello.Random | 
|  | 5325 | *   (see Sect. 8.1 in RFC 5246). | 
|  | 5326 | * - If the Extended Master Secret extension is used, | 
|  | 5327 | *   this is the transcript of the handshake so far. | 
|  | 5328 | *   (see Sect. 4 in RFC 7627). */ | 
|  | 5329 | unsigned char const *salt = handshake->randbytes; | 
|  | 5330 | size_t salt_len = 64; | 
|  | 5331 |  | 
|  | 5332 | #if !defined(MBEDTLS_DEBUG_C) &&                    \ | 
|  | 5333 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ | 
|  | 5334 | !(defined(MBEDTLS_USE_PSA_CRYPTO) &&            \ | 
|  | 5335 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) | 
|  | 5336 | ssl = NULL; /* make sure we don't use it except for those cases */ | 
|  | 5337 | (void) ssl; | 
|  | 5338 | #endif | 
|  | 5339 |  | 
|  | 5340 | if( handshake->resume != 0 ) | 
|  | 5341 | { | 
|  | 5342 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); | 
|  | 5343 | return( 0 ); | 
|  | 5344 | } | 
|  | 5345 |  | 
|  | 5346 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 5347 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) | 
|  | 5348 | { | 
|  | 5349 | lbl  = "extended master secret"; | 
|  | 5350 | salt = session_hash; | 
|  | 5351 | handshake->calc_verify( ssl, session_hash, &salt_len ); | 
|  | 5352 |  | 
|  | 5353 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", | 
|  | 5354 | session_hash, salt_len ); | 
|  | 5355 | } | 
|  | 5356 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ | 
|  | 5357 |  | 
|  | 5358 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&          \ | 
|  | 5359 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 5360 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5361 | ssl_use_opaque_psk( ssl ) == 1 ) | 
|  | 5362 | { | 
|  | 5363 | /* Perform PSK-to-MS expansion in a single step. */ | 
|  | 5364 | psa_status_t status; | 
|  | 5365 | psa_algorithm_t alg; | 
|  | 5366 | mbedtls_svc_key_id_t psk; | 
|  | 5367 | psa_key_derivation_operation_t derivation = | 
|  | 5368 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 5369 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; | 
|  | 5370 |  | 
|  | 5371 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); | 
|  | 5372 |  | 
|  | 5373 | psk = mbedtls_ssl_get_opaque_psk( ssl ); | 
|  | 5374 |  | 
|  | 5375 | if( hash_alg == MBEDTLS_MD_SHA384 ) | 
|  | 5376 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); | 
|  | 5377 | else | 
|  | 5378 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); | 
|  | 5379 |  | 
|  | 5380 | status = setup_psa_key_derivation( &derivation, psk, alg, | 
|  | 5381 | salt, salt_len, | 
|  | 5382 | (unsigned char const *) lbl, | 
|  | 5383 | (size_t) strlen( lbl ), | 
|  | 5384 | master_secret_len ); | 
|  | 5385 | if( status != PSA_SUCCESS ) | 
|  | 5386 | { | 
|  | 5387 | psa_key_derivation_abort( &derivation ); | 
|  | 5388 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5389 | } | 
|  | 5390 |  | 
|  | 5391 | status = psa_key_derivation_output_bytes( &derivation, | 
|  | 5392 | master, | 
|  | 5393 | master_secret_len ); | 
|  | 5394 | if( status != PSA_SUCCESS ) | 
|  | 5395 | { | 
|  | 5396 | psa_key_derivation_abort( &derivation ); | 
|  | 5397 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5398 | } | 
|  | 5399 |  | 
|  | 5400 | status = psa_key_derivation_abort( &derivation ); | 
|  | 5401 | if( status != PSA_SUCCESS ) | 
|  | 5402 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 5403 | } | 
|  | 5404 | else | 
|  | 5405 | #endif | 
|  | 5406 | { | 
|  | 5407 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, | 
|  | 5408 | lbl, salt, salt_len, | 
|  | 5409 | master, | 
|  | 5410 | master_secret_len ); | 
|  | 5411 | if( ret != 0 ) | 
|  | 5412 | { | 
|  | 5413 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
|  | 5414 | return( ret ); | 
|  | 5415 | } | 
|  | 5416 |  | 
|  | 5417 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", | 
|  | 5418 | handshake->premaster, | 
|  | 5419 | handshake->pmslen ); | 
|  | 5420 |  | 
|  | 5421 | mbedtls_platform_zeroize( handshake->premaster, | 
|  | 5422 | sizeof(handshake->premaster) ); | 
|  | 5423 | } | 
|  | 5424 |  | 
|  | 5425 | return( 0 ); | 
|  | 5426 | } | 
|  | 5427 |  | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5428 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) | 
|  | 5429 | { | 
|  | 5430 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5431 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = | 
|  | 5432 | ssl->handshake->ciphersuite_info; | 
|  | 5433 |  | 
|  | 5434 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); | 
|  | 5435 |  | 
|  | 5436 | /* Set PRF, calc_verify and calc_finished function pointers */ | 
|  | 5437 | ret = ssl_set_handshake_prfs( ssl->handshake, | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5438 | ciphersuite_info->mac ); | 
|  | 5439 | if( ret != 0 ) | 
|  | 5440 | { | 
|  | 5441 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); | 
|  | 5442 | return( ret ); | 
|  | 5443 | } | 
|  | 5444 |  | 
|  | 5445 | /* Compute master secret if needed */ | 
|  | 5446 | ret = ssl_compute_master( ssl->handshake, | 
|  | 5447 | ssl->session_negotiate->master, | 
|  | 5448 | ssl ); | 
|  | 5449 | if( ret != 0 ) | 
|  | 5450 | { | 
|  | 5451 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); | 
|  | 5452 | return( ret ); | 
|  | 5453 | } | 
|  | 5454 |  | 
|  | 5455 | /* Swap the client and server random values: | 
|  | 5456 | * - MS derivation wanted client+server (RFC 5246 8.1) | 
|  | 5457 | * - key derivation wants server+client (RFC 5246 6.3) */ | 
|  | 5458 | { | 
|  | 5459 | unsigned char tmp[64]; | 
|  | 5460 | memcpy( tmp, ssl->handshake->randbytes, 64 ); | 
|  | 5461 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); | 
|  | 5462 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); | 
|  | 5463 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
|  | 5464 | } | 
|  | 5465 |  | 
|  | 5466 | /* Populate transform structure */ | 
|  | 5467 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, | 
|  | 5468 | ssl->session_negotiate->ciphersuite, | 
|  | 5469 | ssl->session_negotiate->master, | 
|  | 5470 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ | 
|  | 5471 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 5472 | ssl->session_negotiate->encrypt_then_mac, | 
|  | 5473 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && | 
|  | 5474 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 5475 | ssl->handshake->tls_prf, | 
|  | 5476 | ssl->handshake->randbytes, | 
|  | 5477 | ssl->minor_ver, | 
|  | 5478 | ssl->conf->endpoint, | 
|  | 5479 | ssl ); | 
|  | 5480 | if( ret != 0 ) | 
|  | 5481 | { | 
|  | 5482 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); | 
|  | 5483 | return( ret ); | 
|  | 5484 | } | 
|  | 5485 |  | 
|  | 5486 | /* We no longer need Server/ClientHello.random values */ | 
|  | 5487 | mbedtls_platform_zeroize( ssl->handshake->randbytes, | 
|  | 5488 | sizeof( ssl->handshake->randbytes ) ); | 
|  | 5489 |  | 
|  | 5490 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); | 
|  | 5491 |  | 
|  | 5492 | return( 0 ); | 
|  | 5493 | } | 
| Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5494 |  | 
| Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5495 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) | 
|  | 5496 | { | 
| Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5497 | switch( md ) | 
|  | 5498 | { | 
|  | 5499 | #if defined(MBEDTLS_SHA384_C) | 
|  | 5500 | case MBEDTLS_SSL_HASH_SHA384: | 
|  | 5501 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; | 
|  | 5502 | break; | 
|  | 5503 | #endif | 
|  | 5504 | #if defined(MBEDTLS_SHA256_C) | 
|  | 5505 | case MBEDTLS_SSL_HASH_SHA256: | 
|  | 5506 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; | 
|  | 5507 | break; | 
|  | 5508 | #endif | 
|  | 5509 | default: | 
|  | 5510 | return( -1 ); | 
|  | 5511 | } | 
|  | 5512 |  | 
| Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 5513 | return( 0 ); | 
| Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5514 | } | 
|  | 5515 |  | 
| Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5516 | #if defined(MBEDTLS_SHA256_C) | 
|  | 5517 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, | 
|  | 5518 | unsigned char *hash, | 
|  | 5519 | size_t *hlen ) | 
|  | 5520 | { | 
|  | 5521 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 5522 | size_t hash_size; | 
|  | 5523 | psa_status_t status; | 
|  | 5524 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); | 
|  | 5525 |  | 
|  | 5526 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); | 
|  | 5527 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); | 
|  | 5528 | if( status != PSA_SUCCESS ) | 
|  | 5529 | { | 
|  | 5530 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 5531 | return; | 
|  | 5532 | } | 
|  | 5533 |  | 
|  | 5534 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); | 
|  | 5535 | if( status != PSA_SUCCESS ) | 
|  | 5536 | { | 
|  | 5537 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 5538 | return; | 
|  | 5539 | } | 
|  | 5540 |  | 
|  | 5541 | *hlen = 32; | 
|  | 5542 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); | 
|  | 5543 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); | 
|  | 5544 | #else | 
|  | 5545 | mbedtls_sha256_context sha256; | 
|  | 5546 |  | 
|  | 5547 | mbedtls_sha256_init( &sha256 ); | 
|  | 5548 |  | 
|  | 5549 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); | 
|  | 5550 |  | 
|  | 5551 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
|  | 5552 | mbedtls_sha256_finish( &sha256, hash ); | 
|  | 5553 |  | 
|  | 5554 | *hlen = 32; | 
|  | 5555 |  | 
|  | 5556 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
|  | 5557 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
|  | 5558 |  | 
|  | 5559 | mbedtls_sha256_free( &sha256 ); | 
|  | 5560 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 5561 | return; | 
|  | 5562 | } | 
|  | 5563 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 5564 |  | 
| Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5565 | #if defined(MBEDTLS_SHA384_C) | 
|  | 5566 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, | 
|  | 5567 | unsigned char *hash, | 
|  | 5568 | size_t *hlen ) | 
|  | 5569 | { | 
|  | 5570 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 5571 | size_t hash_size; | 
|  | 5572 | psa_status_t status; | 
|  | 5573 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); | 
|  | 5574 |  | 
|  | 5575 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); | 
|  | 5576 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); | 
|  | 5577 | if( status != PSA_SUCCESS ) | 
|  | 5578 | { | 
|  | 5579 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 5580 | return; | 
|  | 5581 | } | 
|  | 5582 |  | 
|  | 5583 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); | 
|  | 5584 | if( status != PSA_SUCCESS ) | 
|  | 5585 | { | 
|  | 5586 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 5587 | return; | 
|  | 5588 | } | 
|  | 5589 |  | 
|  | 5590 | *hlen = 48; | 
|  | 5591 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); | 
|  | 5592 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); | 
|  | 5593 | #else | 
|  | 5594 | mbedtls_sha512_context sha512; | 
|  | 5595 |  | 
|  | 5596 | mbedtls_sha512_init( &sha512 ); | 
|  | 5597 |  | 
|  | 5598 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); | 
|  | 5599 |  | 
|  | 5600 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
|  | 5601 | mbedtls_sha512_finish( &sha512, hash ); | 
|  | 5602 |  | 
|  | 5603 | *hlen = 48; | 
|  | 5604 |  | 
|  | 5605 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
|  | 5606 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
|  | 5607 |  | 
|  | 5608 | mbedtls_sha512_free( &sha512 ); | 
|  | 5609 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 5610 | return; | 
|  | 5611 | } | 
|  | 5612 | #endif /* MBEDTLS_SHA384_C */ | 
|  | 5613 |  | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5614 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
|  | 5615 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) | 
|  | 5616 | { | 
|  | 5617 | unsigned char *p = ssl->handshake->premaster; | 
|  | 5618 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); | 
|  | 5619 | const unsigned char *psk = NULL; | 
|  | 5620 | size_t psk_len = 0; | 
|  | 5621 |  | 
|  | 5622 | if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) | 
|  | 5623 | == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) | 
|  | 5624 | { | 
|  | 5625 | /* | 
|  | 5626 | * This should never happen because the existence of a PSK is always | 
|  | 5627 | * checked before calling this function | 
|  | 5628 | */ | 
|  | 5629 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5630 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5631 | } | 
|  | 5632 |  | 
|  | 5633 | /* | 
|  | 5634 | * PMS = struct { | 
|  | 5635 | *     opaque other_secret<0..2^16-1>; | 
|  | 5636 | *     opaque psk<0..2^16-1>; | 
|  | 5637 | * }; | 
|  | 5638 | * with "other_secret" depending on the particular key exchange | 
|  | 5639 | */ | 
|  | 5640 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 5641 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) | 
|  | 5642 | { | 
|  | 5643 | if( end - p < 2 ) | 
|  | 5644 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5645 |  | 
|  | 5646 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); | 
|  | 5647 | p += 2; | 
|  | 5648 |  | 
|  | 5649 | if( end < p || (size_t)( end - p ) < psk_len ) | 
|  | 5650 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5651 |  | 
|  | 5652 | memset( p, 0, psk_len ); | 
|  | 5653 | p += psk_len; | 
|  | 5654 | } | 
|  | 5655 | else | 
|  | 5656 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
|  | 5657 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 5658 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
|  | 5659 | { | 
|  | 5660 | /* | 
|  | 5661 | * other_secret already set by the ClientKeyExchange message, | 
|  | 5662 | * and is 48 bytes long | 
|  | 5663 | */ | 
|  | 5664 | if( end - p < 2 ) | 
|  | 5665 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5666 |  | 
|  | 5667 | *p++ = 0; | 
|  | 5668 | *p++ = 48; | 
|  | 5669 | p += 48; | 
|  | 5670 | } | 
|  | 5671 | else | 
|  | 5672 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
|  | 5673 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 5674 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
|  | 5675 | { | 
|  | 5676 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5677 | size_t len; | 
|  | 5678 |  | 
|  | 5679 | /* Write length only when we know the actual value */ | 
|  | 5680 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | 
|  | 5681 | p + 2, end - ( p + 2 ), &len, | 
|  | 5682 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
|  | 5683 | { | 
|  | 5684 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
|  | 5685 | return( ret ); | 
|  | 5686 | } | 
|  | 5687 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); | 
|  | 5688 | p += 2 + len; | 
|  | 5689 |  | 
|  | 5690 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K  ); | 
|  | 5691 | } | 
|  | 5692 | else | 
|  | 5693 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
|  | 5694 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 5695 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
|  | 5696 | { | 
|  | 5697 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5698 | size_t zlen; | 
|  | 5699 |  | 
|  | 5700 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, | 
|  | 5701 | p + 2, end - ( p + 2 ), | 
|  | 5702 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
|  | 5703 | { | 
|  | 5704 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
|  | 5705 | return( ret ); | 
|  | 5706 | } | 
|  | 5707 |  | 
|  | 5708 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); | 
|  | 5709 | p += 2 + zlen; | 
|  | 5710 |  | 
|  | 5711 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 5712 | MBEDTLS_DEBUG_ECDH_Z ); | 
|  | 5713 | } | 
|  | 5714 | else | 
|  | 5715 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
|  | 5716 | { | 
|  | 5717 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5718 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5719 | } | 
|  | 5720 |  | 
|  | 5721 | /* opaque psk<0..2^16-1>; */ | 
|  | 5722 | if( end - p < 2 ) | 
|  | 5723 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5724 |  | 
|  | 5725 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); | 
|  | 5726 | p += 2; | 
|  | 5727 |  | 
|  | 5728 | if( end < p || (size_t)( end - p ) < psk_len ) | 
|  | 5729 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5730 |  | 
|  | 5731 | memcpy( p, psk, psk_len ); | 
|  | 5732 | p += psk_len; | 
|  | 5733 |  | 
|  | 5734 | ssl->handshake->pmslen = p - ssl->handshake->premaster; | 
|  | 5735 |  | 
|  | 5736 | return( 0 ); | 
|  | 5737 | } | 
|  | 5738 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 5739 |  | 
|  | 5740 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 5741 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); | 
|  | 5742 |  | 
|  | 5743 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5744 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) | 
|  | 5745 | { | 
|  | 5746 | /* If renegotiation is not enforced, retransmit until we would reach max | 
|  | 5747 | * timeout if we were using the usual handshake doubling scheme */ | 
|  | 5748 | if( ssl->conf->renego_max_records < 0 ) | 
|  | 5749 | { | 
|  | 5750 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; | 
|  | 5751 | unsigned char doublings = 1; | 
|  | 5752 |  | 
|  | 5753 | while( ratio != 0 ) | 
|  | 5754 | { | 
|  | 5755 | ++doublings; | 
|  | 5756 | ratio >>= 1; | 
|  | 5757 | } | 
|  | 5758 |  | 
|  | 5759 | if( ++ssl->renego_records_seen > doublings ) | 
|  | 5760 | { | 
|  | 5761 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); | 
|  | 5762 | return( 0 ); | 
|  | 5763 | } | 
|  | 5764 | } | 
|  | 5765 |  | 
|  | 5766 | return( ssl_write_hello_request( ssl ) ); | 
|  | 5767 | } | 
|  | 5768 | #endif | 
|  | 5769 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5770 |  | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5771 | /* | 
|  | 5772 | * Handshake functions | 
|  | 5773 | */ | 
|  | 5774 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 5775 | /* No certificate support -> dummy functions */ | 
|  | 5776 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | 
|  | 5777 | { | 
|  | 5778 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 5779 | ssl->handshake->ciphersuite_info; | 
|  | 5780 |  | 
|  | 5781 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | 
|  | 5782 |  | 
|  | 5783 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
|  | 5784 | { | 
|  | 5785 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
|  | 5786 | ssl->state++; | 
|  | 5787 | return( 0 ); | 
|  | 5788 | } | 
|  | 5789 |  | 
|  | 5790 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5791 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5792 | } | 
|  | 5793 |  | 
|  | 5794 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | 
|  | 5795 | { | 
|  | 5796 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 5797 | ssl->handshake->ciphersuite_info; | 
|  | 5798 |  | 
|  | 5799 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | 
|  | 5800 |  | 
|  | 5801 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
|  | 5802 | { | 
|  | 5803 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
|  | 5804 | ssl->state++; | 
|  | 5805 | return( 0 ); | 
|  | 5806 | } | 
|  | 5807 |  | 
|  | 5808 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5809 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5810 | } | 
|  | 5811 |  | 
|  | 5812 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 5813 | /* Some certificate support -> implement write and parse */ | 
|  | 5814 |  | 
|  | 5815 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | 
|  | 5816 | { | 
|  | 5817 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
|  | 5818 | size_t i, n; | 
|  | 5819 | const mbedtls_x509_crt *crt; | 
|  | 5820 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 5821 | ssl->handshake->ciphersuite_info; | 
|  | 5822 |  | 
|  | 5823 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | 
|  | 5824 |  | 
|  | 5825 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
|  | 5826 | { | 
|  | 5827 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
|  | 5828 | ssl->state++; | 
|  | 5829 | return( 0 ); | 
|  | 5830 | } | 
|  | 5831 |  | 
|  | 5832 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 5833 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 5834 | { | 
|  | 5835 | if( ssl->handshake->client_auth == 0 ) | 
|  | 5836 | { | 
|  | 5837 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
|  | 5838 | ssl->state++; | 
|  | 5839 | return( 0 ); | 
|  | 5840 | } | 
|  | 5841 | } | 
|  | 5842 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 5843 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 5844 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 5845 | { | 
|  | 5846 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) | 
|  | 5847 | { | 
|  | 5848 | /* Should never happen because we shouldn't have picked the | 
|  | 5849 | * ciphersuite if we don't have a certificate. */ | 
|  | 5850 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5851 | } | 
|  | 5852 | } | 
|  | 5853 | #endif | 
|  | 5854 |  | 
|  | 5855 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); | 
|  | 5856 |  | 
|  | 5857 | /* | 
|  | 5858 | *     0  .  0    handshake type | 
|  | 5859 | *     1  .  3    handshake length | 
|  | 5860 | *     4  .  6    length of all certs | 
|  | 5861 | *     7  .  9    length of cert. 1 | 
|  | 5862 | *    10  . n-1   peer certificate | 
|  | 5863 | *     n  . n+2   length of cert. 2 | 
|  | 5864 | *    n+3 . ...   upper level cert, etc. | 
|  | 5865 | */ | 
|  | 5866 | i = 7; | 
|  | 5867 | crt = mbedtls_ssl_own_cert( ssl ); | 
|  | 5868 |  | 
|  | 5869 | while( crt != NULL ) | 
|  | 5870 | { | 
|  | 5871 | n = crt->raw.len; | 
|  | 5872 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) | 
|  | 5873 | { | 
|  | 5874 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET | 
|  | 5875 | " > %" MBEDTLS_PRINTF_SIZET, | 
|  | 5876 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); | 
|  | 5877 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 5878 | } | 
|  | 5879 |  | 
|  | 5880 | ssl->out_msg[i    ] = MBEDTLS_BYTE_2( n ); | 
|  | 5881 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); | 
|  | 5882 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); | 
|  | 5883 |  | 
|  | 5884 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); | 
|  | 5885 | i += n; crt = crt->next; | 
|  | 5886 | } | 
|  | 5887 |  | 
|  | 5888 | ssl->out_msg[4]  = MBEDTLS_BYTE_2( i - 7 ); | 
|  | 5889 | ssl->out_msg[5]  = MBEDTLS_BYTE_1( i - 7 ); | 
|  | 5890 | ssl->out_msg[6]  = MBEDTLS_BYTE_0( i - 7 ); | 
|  | 5891 |  | 
|  | 5892 | ssl->out_msglen  = i; | 
|  | 5893 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 5894 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE; | 
|  | 5895 |  | 
|  | 5896 | ssl->state++; | 
|  | 5897 |  | 
|  | 5898 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
|  | 5899 | { | 
|  | 5900 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
|  | 5901 | return( ret ); | 
|  | 5902 | } | 
|  | 5903 |  | 
|  | 5904 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); | 
|  | 5905 |  | 
|  | 5906 | return( ret ); | 
|  | 5907 | } | 
|  | 5908 |  | 
|  | 5909 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 5910 |  | 
|  | 5911 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 5912 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, | 
|  | 5913 | unsigned char *crt_buf, | 
|  | 5914 | size_t crt_buf_len ) | 
|  | 5915 | { | 
|  | 5916 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; | 
|  | 5917 |  | 
|  | 5918 | if( peer_crt == NULL ) | 
|  | 5919 | return( -1 ); | 
|  | 5920 |  | 
|  | 5921 | if( peer_crt->raw.len != crt_buf_len ) | 
|  | 5922 | return( -1 ); | 
|  | 5923 |  | 
|  | 5924 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); | 
|  | 5925 | } | 
|  | 5926 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 5927 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, | 
|  | 5928 | unsigned char *crt_buf, | 
|  | 5929 | size_t crt_buf_len ) | 
|  | 5930 | { | 
|  | 5931 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5932 | unsigned char const * const peer_cert_digest = | 
|  | 5933 | ssl->session->peer_cert_digest; | 
|  | 5934 | mbedtls_md_type_t const peer_cert_digest_type = | 
|  | 5935 | ssl->session->peer_cert_digest_type; | 
|  | 5936 | mbedtls_md_info_t const * const digest_info = | 
|  | 5937 | mbedtls_md_info_from_type( peer_cert_digest_type ); | 
|  | 5938 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; | 
|  | 5939 | size_t digest_len; | 
|  | 5940 |  | 
|  | 5941 | if( peer_cert_digest == NULL || digest_info == NULL ) | 
|  | 5942 | return( -1 ); | 
|  | 5943 |  | 
|  | 5944 | digest_len = mbedtls_md_get_size( digest_info ); | 
|  | 5945 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) | 
|  | 5946 | return( -1 ); | 
|  | 5947 |  | 
|  | 5948 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); | 
|  | 5949 | if( ret != 0 ) | 
|  | 5950 | return( -1 ); | 
|  | 5951 |  | 
|  | 5952 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); | 
|  | 5953 | } | 
|  | 5954 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 5955 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
|  | 5956 |  | 
|  | 5957 | /* | 
|  | 5958 | * Once the certificate message is read, parse it into a cert chain and | 
|  | 5959 | * perform basic checks, but leave actual verification to the caller | 
|  | 5960 | */ | 
|  | 5961 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, | 
|  | 5962 | mbedtls_x509_crt *chain ) | 
|  | 5963 | { | 
|  | 5964 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 5965 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 5966 | int crt_cnt=0; | 
|  | 5967 | #endif | 
|  | 5968 | size_t i, n; | 
|  | 5969 | uint8_t alert; | 
|  | 5970 |  | 
|  | 5971 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
|  | 5972 | { | 
|  | 5973 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 5974 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5975 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 5976 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
|  | 5977 | } | 
|  | 5978 |  | 
|  | 5979 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE  ) | 
|  | 5980 | { | 
|  | 5981 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5982 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 5983 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
|  | 5984 | } | 
|  | 5985 |  | 
|  | 5986 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) | 
|  | 5987 | { | 
|  | 5988 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 5989 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5990 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 5991 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | 
|  | 5992 | } | 
|  | 5993 |  | 
|  | 5994 | i = mbedtls_ssl_hs_hdr_len( ssl ); | 
|  | 5995 |  | 
|  | 5996 | /* | 
|  | 5997 | * Same message structure as in mbedtls_ssl_write_certificate() | 
|  | 5998 | */ | 
|  | 5999 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; | 
|  | 6000 |  | 
|  | 6001 | if( ssl->in_msg[i] != 0 || | 
|  | 6002 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) | 
|  | 6003 | { | 
|  | 6004 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 6005 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6006 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 6007 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | 
|  | 6008 | } | 
|  | 6009 |  | 
|  | 6010 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ | 
|  | 6011 | i += 3; | 
|  | 6012 |  | 
|  | 6013 | /* Iterate through and parse the CRTs in the provided chain. */ | 
|  | 6014 | while( i < ssl->in_hslen ) | 
|  | 6015 | { | 
|  | 6016 | /* Check that there's room for the next CRT's length fields. */ | 
|  | 6017 | if ( i + 3 > ssl->in_hslen ) { | 
|  | 6018 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 6019 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 6020 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6021 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 6022 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | 
|  | 6023 | } | 
|  | 6024 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support | 
|  | 6025 | * anything beyond 2**16 ~ 64K. */ | 
|  | 6026 | if( ssl->in_msg[i] != 0 ) | 
|  | 6027 | { | 
|  | 6028 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 6029 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 6030 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6031 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); | 
|  | 6032 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); | 
|  | 6033 | } | 
|  | 6034 |  | 
|  | 6035 | /* Read length of the next CRT in the chain. */ | 
|  | 6036 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) | 
|  | 6037 | | (unsigned int) ssl->in_msg[i + 2]; | 
|  | 6038 | i += 3; | 
|  | 6039 |  | 
|  | 6040 | if( n < 128 || i + n > ssl->in_hslen ) | 
|  | 6041 | { | 
|  | 6042 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 6043 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 6044 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6045 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 6046 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | 
|  | 6047 | } | 
|  | 6048 |  | 
|  | 6049 | /* Check if we're handling the first CRT in the chain. */ | 
|  | 6050 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 6051 | if( crt_cnt++ == 0 && | 
|  | 6052 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 6053 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
|  | 6054 | { | 
|  | 6055 | /* During client-side renegotiation, check that the server's | 
|  | 6056 | * end-CRTs hasn't changed compared to the initial handshake, | 
|  | 6057 | * mitigating the triple handshake attack. On success, reuse | 
|  | 6058 | * the original end-CRT instead of parsing it again. */ | 
|  | 6059 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); | 
|  | 6060 | if( ssl_check_peer_crt_unchanged( ssl, | 
|  | 6061 | &ssl->in_msg[i], | 
|  | 6062 | n ) != 0 ) | 
|  | 6063 | { | 
|  | 6064 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); | 
|  | 6065 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 6066 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6067 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); | 
|  | 6068 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); | 
|  | 6069 | } | 
|  | 6070 |  | 
|  | 6071 | /* Now we can safely free the original chain. */ | 
|  | 6072 | ssl_clear_peer_cert( ssl->session ); | 
|  | 6073 | } | 
|  | 6074 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
|  | 6075 |  | 
|  | 6076 | /* Parse the next certificate in the chain. */ | 
|  | 6077 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 6078 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); | 
|  | 6079 | #else | 
|  | 6080 | /* If we don't need to store the CRT chain permanently, parse | 
|  | 6081 | * it in-place from the input buffer instead of making a copy. */ | 
|  | 6082 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); | 
|  | 6083 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 6084 | switch( ret ) | 
|  | 6085 | { | 
|  | 6086 | case 0: /*ok*/ | 
|  | 6087 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: | 
|  | 6088 | /* Ignore certificate with an unknown algorithm: maybe a | 
|  | 6089 | prior certificate was already trusted. */ | 
|  | 6090 | break; | 
|  | 6091 |  | 
|  | 6092 | case MBEDTLS_ERR_X509_ALLOC_FAILED: | 
|  | 6093 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; | 
|  | 6094 | goto crt_parse_der_failed; | 
|  | 6095 |  | 
|  | 6096 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: | 
|  | 6097 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6098 | goto crt_parse_der_failed; | 
|  | 6099 |  | 
|  | 6100 | default: | 
|  | 6101 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
|  | 6102 | crt_parse_der_failed: | 
|  | 6103 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); | 
|  | 6104 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); | 
|  | 6105 | return( ret ); | 
|  | 6106 | } | 
|  | 6107 |  | 
|  | 6108 | i += n; | 
|  | 6109 | } | 
|  | 6110 |  | 
|  | 6111 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); | 
|  | 6112 | return( 0 ); | 
|  | 6113 | } | 
|  | 6114 |  | 
|  | 6115 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6116 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) | 
|  | 6117 | { | 
|  | 6118 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6119 | return( -1 ); | 
|  | 6120 |  | 
|  | 6121 | if( ssl->in_hslen   == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && | 
|  | 6122 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE    && | 
|  | 6123 | ssl->in_msg[0]  == MBEDTLS_SSL_HS_CERTIFICATE   && | 
|  | 6124 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) | 
|  | 6125 | { | 
|  | 6126 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); | 
|  | 6127 | return( 0 ); | 
|  | 6128 | } | 
|  | 6129 | return( -1 ); | 
|  | 6130 | } | 
|  | 6131 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 6132 |  | 
|  | 6133 | /* Check if a certificate message is expected. | 
|  | 6134 | * Return either | 
|  | 6135 | * - SSL_CERTIFICATE_EXPECTED, or | 
|  | 6136 | * - SSL_CERTIFICATE_SKIP | 
|  | 6137 | * indicating whether a Certificate message is expected or not. | 
|  | 6138 | */ | 
|  | 6139 | #define SSL_CERTIFICATE_EXPECTED 0 | 
|  | 6140 | #define SSL_CERTIFICATE_SKIP     1 | 
|  | 6141 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, | 
|  | 6142 | int authmode ) | 
|  | 6143 | { | 
|  | 6144 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 6145 | ssl->handshake->ciphersuite_info; | 
|  | 6146 |  | 
|  | 6147 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
|  | 6148 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 6149 |  | 
|  | 6150 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6151 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 6152 | { | 
|  | 6153 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
|  | 6154 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 6155 |  | 
|  | 6156 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
|  | 6157 | { | 
|  | 6158 | ssl->session_negotiate->verify_result = | 
|  | 6159 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; | 
|  | 6160 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 6161 | } | 
|  | 6162 | } | 
|  | 6163 | #else | 
|  | 6164 | ((void) authmode); | 
|  | 6165 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 6166 |  | 
|  | 6167 | return( SSL_CERTIFICATE_EXPECTED ); | 
|  | 6168 | } | 
|  | 6169 |  | 
|  | 6170 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, | 
|  | 6171 | int authmode, | 
|  | 6172 | mbedtls_x509_crt *chain, | 
|  | 6173 | void *rs_ctx ) | 
|  | 6174 | { | 
|  | 6175 | int ret = 0; | 
|  | 6176 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 6177 | ssl->handshake->ciphersuite_info; | 
|  | 6178 | int have_ca_chain = 0; | 
|  | 6179 |  | 
|  | 6180 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); | 
|  | 6181 | void *p_vrfy; | 
|  | 6182 |  | 
|  | 6183 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
|  | 6184 | return( 0 ); | 
|  | 6185 |  | 
|  | 6186 | if( ssl->f_vrfy != NULL ) | 
|  | 6187 | { | 
|  | 6188 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); | 
|  | 6189 | f_vrfy = ssl->f_vrfy; | 
|  | 6190 | p_vrfy = ssl->p_vrfy; | 
|  | 6191 | } | 
|  | 6192 | else | 
|  | 6193 | { | 
|  | 6194 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); | 
|  | 6195 | f_vrfy = ssl->conf->f_vrfy; | 
|  | 6196 | p_vrfy = ssl->conf->p_vrfy; | 
|  | 6197 | } | 
|  | 6198 |  | 
|  | 6199 | /* | 
|  | 6200 | * Main check: verify certificate | 
|  | 6201 | */ | 
|  | 6202 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
|  | 6203 | if( ssl->conf->f_ca_cb != NULL ) | 
|  | 6204 | { | 
|  | 6205 | ((void) rs_ctx); | 
|  | 6206 | have_ca_chain = 1; | 
|  | 6207 |  | 
|  | 6208 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); | 
|  | 6209 | ret = mbedtls_x509_crt_verify_with_ca_cb( | 
|  | 6210 | chain, | 
|  | 6211 | ssl->conf->f_ca_cb, | 
|  | 6212 | ssl->conf->p_ca_cb, | 
|  | 6213 | ssl->conf->cert_profile, | 
|  | 6214 | ssl->hostname, | 
|  | 6215 | &ssl->session_negotiate->verify_result, | 
|  | 6216 | f_vrfy, p_vrfy ); | 
|  | 6217 | } | 
|  | 6218 | else | 
|  | 6219 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
|  | 6220 | { | 
|  | 6221 | mbedtls_x509_crt *ca_chain; | 
|  | 6222 | mbedtls_x509_crl *ca_crl; | 
|  | 6223 |  | 
|  | 6224 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 6225 | if( ssl->handshake->sni_ca_chain != NULL ) | 
|  | 6226 | { | 
|  | 6227 | ca_chain = ssl->handshake->sni_ca_chain; | 
|  | 6228 | ca_crl   = ssl->handshake->sni_ca_crl; | 
|  | 6229 | } | 
|  | 6230 | else | 
|  | 6231 | #endif | 
|  | 6232 | { | 
|  | 6233 | ca_chain = ssl->conf->ca_chain; | 
|  | 6234 | ca_crl   = ssl->conf->ca_crl; | 
|  | 6235 | } | 
|  | 6236 |  | 
|  | 6237 | if( ca_chain != NULL ) | 
|  | 6238 | have_ca_chain = 1; | 
|  | 6239 |  | 
|  | 6240 | ret = mbedtls_x509_crt_verify_restartable( | 
|  | 6241 | chain, | 
|  | 6242 | ca_chain, ca_crl, | 
|  | 6243 | ssl->conf->cert_profile, | 
|  | 6244 | ssl->hostname, | 
|  | 6245 | &ssl->session_negotiate->verify_result, | 
|  | 6246 | f_vrfy, p_vrfy, rs_ctx ); | 
|  | 6247 | } | 
|  | 6248 |  | 
|  | 6249 | if( ret != 0 ) | 
|  | 6250 | { | 
|  | 6251 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); | 
|  | 6252 | } | 
|  | 6253 |  | 
|  | 6254 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
|  | 6255 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 6256 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); | 
|  | 6257 | #endif | 
|  | 6258 |  | 
|  | 6259 | /* | 
|  | 6260 | * Secondary checks: always done, but change 'ret' only if it was 0 | 
|  | 6261 | */ | 
|  | 6262 |  | 
|  | 6263 | #if defined(MBEDTLS_ECP_C) | 
|  | 6264 | { | 
|  | 6265 | const mbedtls_pk_context *pk = &chain->pk; | 
|  | 6266 |  | 
|  | 6267 | /* If certificate uses an EC key, make sure the curve is OK */ | 
|  | 6268 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && | 
|  | 6269 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) | 
|  | 6270 | { | 
|  | 6271 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; | 
|  | 6272 |  | 
|  | 6273 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); | 
|  | 6274 | if( ret == 0 ) | 
|  | 6275 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | 
|  | 6276 | } | 
|  | 6277 | } | 
|  | 6278 | #endif /* MBEDTLS_ECP_C */ | 
|  | 6279 |  | 
|  | 6280 | if( mbedtls_ssl_check_cert_usage( chain, | 
|  | 6281 | ciphersuite_info, | 
|  | 6282 | ! ssl->conf->endpoint, | 
|  | 6283 | &ssl->session_negotiate->verify_result ) != 0 ) | 
|  | 6284 | { | 
|  | 6285 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); | 
|  | 6286 | if( ret == 0 ) | 
|  | 6287 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | 
|  | 6288 | } | 
|  | 6289 |  | 
|  | 6290 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a | 
|  | 6291 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, | 
|  | 6292 | * with details encoded in the verification flags. All other kinds | 
|  | 6293 | * of error codes, including those from the user provided f_vrfy | 
|  | 6294 | * functions, are treated as fatal and lead to a failure of | 
|  | 6295 | * ssl_parse_certificate even if verification was optional. */ | 
|  | 6296 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && | 
|  | 6297 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || | 
|  | 6298 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) | 
|  | 6299 | { | 
|  | 6300 | ret = 0; | 
|  | 6301 | } | 
|  | 6302 |  | 
|  | 6303 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) | 
|  | 6304 | { | 
|  | 6305 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); | 
|  | 6306 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; | 
|  | 6307 | } | 
|  | 6308 |  | 
|  | 6309 | if( ret != 0 ) | 
|  | 6310 | { | 
|  | 6311 | uint8_t alert; | 
|  | 6312 |  | 
|  | 6313 | /* The certificate may have been rejected for several reasons. | 
|  | 6314 | Pick one and send the corresponding alert. Which alert to send | 
|  | 6315 | may be a subject of debate in some cases. */ | 
|  | 6316 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) | 
|  | 6317 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; | 
|  | 6318 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) | 
|  | 6319 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
|  | 6320 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) | 
|  | 6321 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6322 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) | 
|  | 6323 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6324 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) | 
|  | 6325 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6326 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) | 
|  | 6327 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6328 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) | 
|  | 6329 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 6330 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) | 
|  | 6331 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; | 
|  | 6332 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) | 
|  | 6333 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; | 
|  | 6334 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) | 
|  | 6335 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; | 
|  | 6336 | else | 
|  | 6337 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; | 
|  | 6338 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6339 | alert ); | 
|  | 6340 | } | 
|  | 6341 |  | 
|  | 6342 | #if defined(MBEDTLS_DEBUG_C) | 
|  | 6343 | if( ssl->session_negotiate->verify_result != 0 ) | 
|  | 6344 | { | 
|  | 6345 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", | 
|  | 6346 | (unsigned int) ssl->session_negotiate->verify_result ) ); | 
|  | 6347 | } | 
|  | 6348 | else | 
|  | 6349 | { | 
|  | 6350 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); | 
|  | 6351 | } | 
|  | 6352 | #endif /* MBEDTLS_DEBUG_C */ | 
|  | 6353 |  | 
|  | 6354 | return( ret ); | 
|  | 6355 | } | 
|  | 6356 |  | 
|  | 6357 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 6358 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, | 
|  | 6359 | unsigned char *start, size_t len ) | 
|  | 6360 | { | 
|  | 6361 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6362 | /* Remember digest of the peer's end-CRT. */ | 
|  | 6363 | ssl->session_negotiate->peer_cert_digest = | 
|  | 6364 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); | 
|  | 6365 | if( ssl->session_negotiate->peer_cert_digest == NULL ) | 
|  | 6366 | { | 
|  | 6367 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | 
|  | 6368 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); | 
|  | 6369 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 6370 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6371 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 6372 |  | 
|  | 6373 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 6374 | } | 
|  | 6375 |  | 
|  | 6376 | ret = mbedtls_md( mbedtls_md_info_from_type( | 
|  | 6377 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), | 
|  | 6378 | start, len, | 
|  | 6379 | ssl->session_negotiate->peer_cert_digest ); | 
|  | 6380 |  | 
|  | 6381 | ssl->session_negotiate->peer_cert_digest_type = | 
|  | 6382 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; | 
|  | 6383 | ssl->session_negotiate->peer_cert_digest_len = | 
|  | 6384 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; | 
|  | 6385 |  | 
|  | 6386 | return( ret ); | 
|  | 6387 | } | 
|  | 6388 |  | 
|  | 6389 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, | 
|  | 6390 | unsigned char *start, size_t len ) | 
|  | 6391 | { | 
|  | 6392 | unsigned char *end = start + len; | 
|  | 6393 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6394 |  | 
|  | 6395 | /* Make a copy of the peer's raw public key. */ | 
|  | 6396 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); | 
|  | 6397 | ret = mbedtls_pk_parse_subpubkey( &start, end, | 
|  | 6398 | &ssl->handshake->peer_pubkey ); | 
|  | 6399 | if( ret != 0 ) | 
|  | 6400 | { | 
|  | 6401 | /* We should have parsed the public key before. */ | 
|  | 6402 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 6403 | } | 
|  | 6404 |  | 
|  | 6405 | return( 0 ); | 
|  | 6406 | } | 
|  | 6407 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 6408 |  | 
|  | 6409 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | 
|  | 6410 | { | 
|  | 6411 | int ret = 0; | 
|  | 6412 | int crt_expected; | 
|  | 6413 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 6414 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET | 
|  | 6415 | ? ssl->handshake->sni_authmode | 
|  | 6416 | : ssl->conf->authmode; | 
|  | 6417 | #else | 
|  | 6418 | const int authmode = ssl->conf->authmode; | 
|  | 6419 | #endif | 
|  | 6420 | void *rs_ctx = NULL; | 
|  | 6421 | mbedtls_x509_crt *chain = NULL; | 
|  | 6422 |  | 
|  | 6423 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | 
|  | 6424 |  | 
|  | 6425 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); | 
|  | 6426 | if( crt_expected == SSL_CERTIFICATE_SKIP ) | 
|  | 6427 | { | 
|  | 6428 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
|  | 6429 | goto exit; | 
|  | 6430 | } | 
|  | 6431 |  | 
|  | 6432 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
|  | 6433 | if( ssl->handshake->ecrs_enabled && | 
|  | 6434 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) | 
|  | 6435 | { | 
|  | 6436 | chain = ssl->handshake->ecrs_peer_cert; | 
|  | 6437 | ssl->handshake->ecrs_peer_cert = NULL; | 
|  | 6438 | goto crt_verify; | 
|  | 6439 | } | 
|  | 6440 | #endif | 
|  | 6441 |  | 
|  | 6442 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
|  | 6443 | { | 
|  | 6444 | /* mbedtls_ssl_read_record may have sent an alert already. We | 
|  | 6445 | let it decide whether to alert. */ | 
|  | 6446 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 6447 | goto exit; | 
|  | 6448 | } | 
|  | 6449 |  | 
|  | 6450 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6451 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) | 
|  | 6452 | { | 
|  | 6453 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; | 
|  | 6454 |  | 
|  | 6455 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) | 
|  | 6456 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; | 
|  | 6457 |  | 
|  | 6458 | goto exit; | 
|  | 6459 | } | 
|  | 6460 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 6461 |  | 
|  | 6462 | /* Clear existing peer CRT structure in case we tried to | 
|  | 6463 | * reuse a session but it failed, and allocate a new one. */ | 
|  | 6464 | ssl_clear_peer_cert( ssl->session_negotiate ); | 
|  | 6465 |  | 
|  | 6466 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); | 
|  | 6467 | if( chain == NULL ) | 
|  | 6468 | { | 
|  | 6469 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", | 
|  | 6470 | sizeof( mbedtls_x509_crt ) ) ); | 
|  | 6471 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 6472 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6473 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 6474 |  | 
|  | 6475 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 6476 | goto exit; | 
|  | 6477 | } | 
|  | 6478 | mbedtls_x509_crt_init( chain ); | 
|  | 6479 |  | 
|  | 6480 | ret = ssl_parse_certificate_chain( ssl, chain ); | 
|  | 6481 | if( ret != 0 ) | 
|  | 6482 | goto exit; | 
|  | 6483 |  | 
|  | 6484 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
|  | 6485 | if( ssl->handshake->ecrs_enabled) | 
|  | 6486 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; | 
|  | 6487 |  | 
|  | 6488 | crt_verify: | 
|  | 6489 | if( ssl->handshake->ecrs_enabled) | 
|  | 6490 | rs_ctx = &ssl->handshake->ecrs_ctx; | 
|  | 6491 | #endif | 
|  | 6492 |  | 
|  | 6493 | ret = ssl_parse_certificate_verify( ssl, authmode, | 
|  | 6494 | chain, rs_ctx ); | 
|  | 6495 | if( ret != 0 ) | 
|  | 6496 | goto exit; | 
|  | 6497 |  | 
|  | 6498 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 6499 | { | 
|  | 6500 | unsigned char *crt_start, *pk_start; | 
|  | 6501 | size_t crt_len, pk_len; | 
|  | 6502 |  | 
|  | 6503 | /* We parse the CRT chain without copying, so | 
|  | 6504 | * these pointers point into the input buffer, | 
|  | 6505 | * and are hence still valid after freeing the | 
|  | 6506 | * CRT chain. */ | 
|  | 6507 |  | 
|  | 6508 | crt_start = chain->raw.p; | 
|  | 6509 | crt_len   = chain->raw.len; | 
|  | 6510 |  | 
|  | 6511 | pk_start = chain->pk_raw.p; | 
|  | 6512 | pk_len   = chain->pk_raw.len; | 
|  | 6513 |  | 
|  | 6514 | /* Free the CRT structures before computing | 
|  | 6515 | * digest and copying the peer's public key. */ | 
|  | 6516 | mbedtls_x509_crt_free( chain ); | 
|  | 6517 | mbedtls_free( chain ); | 
|  | 6518 | chain = NULL; | 
|  | 6519 |  | 
|  | 6520 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); | 
|  | 6521 | if( ret != 0 ) | 
|  | 6522 | goto exit; | 
|  | 6523 |  | 
|  | 6524 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); | 
|  | 6525 | if( ret != 0 ) | 
|  | 6526 | goto exit; | 
|  | 6527 | } | 
|  | 6528 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 6529 | /* Pass ownership to session structure. */ | 
|  | 6530 | ssl->session_negotiate->peer_cert = chain; | 
|  | 6531 | chain = NULL; | 
|  | 6532 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 6533 |  | 
|  | 6534 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); | 
|  | 6535 |  | 
|  | 6536 | exit: | 
|  | 6537 |  | 
|  | 6538 | if( ret == 0 ) | 
|  | 6539 | ssl->state++; | 
|  | 6540 |  | 
|  | 6541 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
|  | 6542 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) | 
|  | 6543 | { | 
|  | 6544 | ssl->handshake->ecrs_peer_cert = chain; | 
|  | 6545 | chain = NULL; | 
|  | 6546 | } | 
|  | 6547 | #endif | 
|  | 6548 |  | 
|  | 6549 | if( chain != NULL ) | 
|  | 6550 | { | 
|  | 6551 | mbedtls_x509_crt_free( chain ); | 
|  | 6552 | mbedtls_free( chain ); | 
|  | 6553 | } | 
|  | 6554 |  | 
|  | 6555 | return( ret ); | 
|  | 6556 | } | 
|  | 6557 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 6558 |  | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 6559 | #if defined(MBEDTLS_SHA256_C) | 
|  | 6560 | static void ssl_calc_finished_tls_sha256( | 
|  | 6561 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
|  | 6562 | { | 
|  | 6563 | int len = 12; | 
|  | 6564 | const char *sender; | 
|  | 6565 | unsigned char padbuf[32]; | 
|  | 6566 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6567 | size_t hash_size; | 
|  | 6568 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; | 
|  | 6569 | psa_status_t status; | 
|  | 6570 | #else | 
|  | 6571 | mbedtls_sha256_context sha256; | 
|  | 6572 | #endif | 
|  | 6573 |  | 
|  | 6574 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
|  | 6575 | if( !session ) | 
|  | 6576 | session = ssl->session; | 
|  | 6577 |  | 
|  | 6578 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6579 | ? "client finished" | 
|  | 6580 | : "server finished"; | 
|  | 6581 |  | 
|  | 6582 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6583 | sha256_psa = psa_hash_operation_init(); | 
|  | 6584 |  | 
|  | 6585 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); | 
|  | 6586 |  | 
|  | 6587 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); | 
|  | 6588 | if( status != PSA_SUCCESS ) | 
|  | 6589 | { | 
|  | 6590 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 6591 | return; | 
|  | 6592 | } | 
|  | 6593 |  | 
|  | 6594 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); | 
|  | 6595 | if( status != PSA_SUCCESS ) | 
|  | 6596 | { | 
|  | 6597 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 6598 | return; | 
|  | 6599 | } | 
|  | 6600 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); | 
|  | 6601 | #else | 
|  | 6602 |  | 
|  | 6603 | mbedtls_sha256_init( &sha256 ); | 
|  | 6604 |  | 
|  | 6605 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha256" ) ); | 
|  | 6606 |  | 
|  | 6607 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
|  | 6608 |  | 
|  | 6609 | /* | 
|  | 6610 | * TLSv1.2: | 
|  | 6611 | *   hash = PRF( master, finished_label, | 
|  | 6612 | *               Hash( handshake ) )[0.11] | 
|  | 6613 | */ | 
|  | 6614 |  | 
|  | 6615 | #if !defined(MBEDTLS_SHA256_ALT) | 
|  | 6616 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) | 
|  | 6617 | sha256.state, sizeof( sha256.state ) ); | 
|  | 6618 | #endif | 
|  | 6619 |  | 
|  | 6620 | mbedtls_sha256_finish( &sha256, padbuf ); | 
|  | 6621 | mbedtls_sha256_free( &sha256 ); | 
|  | 6622 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 6623 |  | 
|  | 6624 | ssl->handshake->tls_prf( session->master, 48, sender, | 
|  | 6625 | padbuf, 32, buf, len ); | 
|  | 6626 |  | 
|  | 6627 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
|  | 6628 |  | 
|  | 6629 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
|  | 6630 |  | 
|  | 6631 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
|  | 6632 | } | 
|  | 6633 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 6634 |  | 
| Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 6635 |  | 
|  | 6636 | #if defined(MBEDTLS_SHA384_C) | 
|  | 6637 |  | 
|  | 6638 | static void ssl_calc_finished_tls_sha384( | 
|  | 6639 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
|  | 6640 | { | 
|  | 6641 | int len = 12; | 
|  | 6642 | const char *sender; | 
|  | 6643 | unsigned char padbuf[48]; | 
|  | 6644 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6645 | size_t hash_size; | 
|  | 6646 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; | 
|  | 6647 | psa_status_t status; | 
|  | 6648 | #else | 
|  | 6649 | mbedtls_sha512_context sha512; | 
|  | 6650 | #endif | 
|  | 6651 |  | 
|  | 6652 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
|  | 6653 | if( !session ) | 
|  | 6654 | session = ssl->session; | 
|  | 6655 |  | 
|  | 6656 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6657 | ? "client finished" | 
|  | 6658 | : "server finished"; | 
|  | 6659 |  | 
|  | 6660 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6661 | sha384_psa = psa_hash_operation_init(); | 
|  | 6662 |  | 
|  | 6663 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); | 
|  | 6664 |  | 
|  | 6665 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); | 
|  | 6666 | if( status != PSA_SUCCESS ) | 
|  | 6667 | { | 
|  | 6668 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 6669 | return; | 
|  | 6670 | } | 
|  | 6671 |  | 
|  | 6672 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); | 
|  | 6673 | if( status != PSA_SUCCESS ) | 
|  | 6674 | { | 
|  | 6675 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 6676 | return; | 
|  | 6677 | } | 
|  | 6678 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); | 
|  | 6679 | #else | 
|  | 6680 | mbedtls_sha512_init( &sha512 ); | 
|  | 6681 |  | 
|  | 6682 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha384" ) ); | 
|  | 6683 |  | 
|  | 6684 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
|  | 6685 |  | 
|  | 6686 | /* | 
|  | 6687 | * TLSv1.2: | 
|  | 6688 | *   hash = PRF( master, finished_label, | 
|  | 6689 | *               Hash( handshake ) )[0.11] | 
|  | 6690 | */ | 
|  | 6691 |  | 
|  | 6692 | #if !defined(MBEDTLS_SHA512_ALT) | 
|  | 6693 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) | 
|  | 6694 | sha512.state, sizeof( sha512.state ) ); | 
|  | 6695 | #endif | 
|  | 6696 | mbedtls_sha512_finish( &sha512, padbuf ); | 
|  | 6697 |  | 
|  | 6698 | mbedtls_sha512_free( &sha512 ); | 
|  | 6699 | #endif | 
|  | 6700 |  | 
|  | 6701 | ssl->handshake->tls_prf( session->master, 48, sender, | 
|  | 6702 | padbuf, 48, buf, len ); | 
|  | 6703 |  | 
|  | 6704 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
|  | 6705 |  | 
|  | 6706 | mbedtls_platform_zeroize(  padbuf, sizeof( padbuf ) ); | 
|  | 6707 |  | 
|  | 6708 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
|  | 6709 | } | 
|  | 6710 | #endif /* MBEDTLS_SHA384_C */ | 
|  | 6711 |  | 
| Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 6712 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) | 
|  | 6713 | { | 
|  | 6714 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); | 
|  | 6715 |  | 
|  | 6716 | /* | 
|  | 6717 | * Free our handshake params | 
|  | 6718 | */ | 
|  | 6719 | mbedtls_ssl_handshake_free( ssl ); | 
|  | 6720 | mbedtls_free( ssl->handshake ); | 
|  | 6721 | ssl->handshake = NULL; | 
|  | 6722 |  | 
|  | 6723 | /* | 
|  | 6724 | * Free the previous transform and swith in the current one | 
|  | 6725 | */ | 
|  | 6726 | if( ssl->transform ) | 
|  | 6727 | { | 
|  | 6728 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 6729 | mbedtls_free( ssl->transform ); | 
|  | 6730 | } | 
|  | 6731 | ssl->transform = ssl->transform_negotiate; | 
|  | 6732 | ssl->transform_negotiate = NULL; | 
|  | 6733 |  | 
|  | 6734 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); | 
|  | 6735 | } | 
|  | 6736 |  | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 6737 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) | 
|  | 6738 | { | 
|  | 6739 | int resume = ssl->handshake->resume; | 
|  | 6740 |  | 
|  | 6741 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); | 
|  | 6742 |  | 
|  | 6743 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6744 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
|  | 6745 | { | 
|  | 6746 | ssl->renego_status =  MBEDTLS_SSL_RENEGOTIATION_DONE; | 
|  | 6747 | ssl->renego_records_seen = 0; | 
|  | 6748 | } | 
|  | 6749 | #endif | 
|  | 6750 |  | 
|  | 6751 | /* | 
|  | 6752 | * Free the previous session and switch in the current one | 
|  | 6753 | */ | 
|  | 6754 | if( ssl->session ) | 
|  | 6755 | { | 
|  | 6756 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 6757 | /* RFC 7366 3.1: keep the EtM state */ | 
|  | 6758 | ssl->session_negotiate->encrypt_then_mac = | 
|  | 6759 | ssl->session->encrypt_then_mac; | 
|  | 6760 | #endif | 
|  | 6761 |  | 
|  | 6762 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 6763 | mbedtls_free( ssl->session ); | 
|  | 6764 | } | 
|  | 6765 | ssl->session = ssl->session_negotiate; | 
|  | 6766 | ssl->session_negotiate = NULL; | 
|  | 6767 |  | 
|  | 6768 | /* | 
|  | 6769 | * Add cache entry | 
|  | 6770 | */ | 
|  | 6771 | if( ssl->conf->f_set_cache != NULL && | 
|  | 6772 | ssl->session->id_len != 0 && | 
|  | 6773 | resume == 0 ) | 
|  | 6774 | { | 
|  | 6775 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, | 
|  | 6776 | ssl->session->id, | 
|  | 6777 | ssl->session->id_len, | 
|  | 6778 | ssl->session ) != 0 ) | 
|  | 6779 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); | 
|  | 6780 | } | 
|  | 6781 |  | 
|  | 6782 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6783 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 6784 | ssl->handshake->flight != NULL ) | 
|  | 6785 | { | 
|  | 6786 | /* Cancel handshake timer */ | 
|  | 6787 | mbedtls_ssl_set_timer( ssl, 0 ); | 
|  | 6788 |  | 
|  | 6789 | /* Keep last flight around in case we need to resend it: | 
|  | 6790 | * we need the handshake and transform structures for that */ | 
|  | 6791 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); | 
|  | 6792 | } | 
|  | 6793 | else | 
|  | 6794 | #endif | 
|  | 6795 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); | 
|  | 6796 |  | 
|  | 6797 | ssl->state++; | 
|  | 6798 |  | 
|  | 6799 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); | 
|  | 6800 | } | 
|  | 6801 |  | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 6802 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) | 
|  | 6803 | { | 
|  | 6804 | int ret, hash_len; | 
|  | 6805 |  | 
|  | 6806 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); | 
|  | 6807 |  | 
|  | 6808 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); | 
|  | 6809 |  | 
|  | 6810 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); | 
|  | 6811 |  | 
|  | 6812 | /* | 
|  | 6813 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites | 
|  | 6814 | * may define some other value. Currently (early 2016), no defined | 
|  | 6815 | * ciphersuite does this (and this is unlikely to change as activity has | 
|  | 6816 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. | 
|  | 6817 | */ | 
|  | 6818 | hash_len = 12; | 
|  | 6819 |  | 
|  | 6820 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6821 | ssl->verify_data_len = hash_len; | 
|  | 6822 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); | 
|  | 6823 | #endif | 
|  | 6824 |  | 
|  | 6825 | ssl->out_msglen  = 4 + hash_len; | 
|  | 6826 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 6827 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_FINISHED; | 
|  | 6828 |  | 
|  | 6829 | /* | 
|  | 6830 | * In case of session resuming, invert the client and server | 
|  | 6831 | * ChangeCipherSpec messages order. | 
|  | 6832 | */ | 
|  | 6833 | if( ssl->handshake->resume != 0 ) | 
|  | 6834 | { | 
|  | 6835 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 6836 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6837 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
|  | 6838 | #endif | 
|  | 6839 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6840 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 6841 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
|  | 6842 | #endif | 
|  | 6843 | } | 
|  | 6844 | else | 
|  | 6845 | ssl->state++; | 
|  | 6846 |  | 
|  | 6847 | /* | 
|  | 6848 | * Switch to our negotiated transform and session parameters for outbound | 
|  | 6849 | * data. | 
|  | 6850 | */ | 
|  | 6851 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); | 
|  | 6852 |  | 
|  | 6853 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6854 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 6855 | { | 
|  | 6856 | unsigned char i; | 
|  | 6857 |  | 
|  | 6858 | /* Remember current epoch settings for resending */ | 
|  | 6859 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
|  | 6860 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, | 
|  | 6861 | sizeof( ssl->handshake->alt_out_ctr ) ); | 
|  | 6862 |  | 
|  | 6863 | /* Set sequence_number to zero */ | 
|  | 6864 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); | 
|  | 6865 |  | 
|  | 6866 |  | 
|  | 6867 | /* Increment epoch */ | 
|  | 6868 | for( i = 2; i > 0; i-- ) | 
|  | 6869 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) | 
|  | 6870 | break; | 
|  | 6871 |  | 
|  | 6872 | /* The loop goes to its end iff the counter is wrapping */ | 
|  | 6873 | if( i == 0 ) | 
|  | 6874 | { | 
|  | 6875 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); | 
|  | 6876 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
|  | 6877 | } | 
|  | 6878 | } | 
|  | 6879 | else | 
|  | 6880 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 6881 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); | 
|  | 6882 |  | 
|  | 6883 | ssl->transform_out = ssl->transform_negotiate; | 
|  | 6884 | ssl->session_out = ssl->session_negotiate; | 
|  | 6885 |  | 
|  | 6886 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6887 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 6888 | mbedtls_ssl_send_flight_completed( ssl ); | 
|  | 6889 | #endif | 
|  | 6890 |  | 
|  | 6891 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
|  | 6892 | { | 
|  | 6893 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
|  | 6894 | return( ret ); | 
|  | 6895 | } | 
|  | 6896 |  | 
|  | 6897 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6898 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 6899 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 6900 | { | 
|  | 6901 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 6902 | return( ret ); | 
|  | 6903 | } | 
|  | 6904 | #endif | 
|  | 6905 |  | 
|  | 6906 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); | 
|  | 6907 |  | 
|  | 6908 | return( 0 ); | 
|  | 6909 | } | 
|  | 6910 |  | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 6911 | #define SSL_MAX_HASH_LEN 12 | 
|  | 6912 |  | 
|  | 6913 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) | 
|  | 6914 | { | 
|  | 6915 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6916 | unsigned int hash_len = 12; | 
|  | 6917 | unsigned char buf[SSL_MAX_HASH_LEN]; | 
|  | 6918 |  | 
|  | 6919 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); | 
|  | 6920 |  | 
|  | 6921 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); | 
|  | 6922 |  | 
|  | 6923 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
|  | 6924 | { | 
|  | 6925 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 6926 | goto exit; | 
|  | 6927 | } | 
|  | 6928 |  | 
|  | 6929 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
|  | 6930 | { | 
|  | 6931 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
|  | 6932 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6933 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 6934 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | 
|  | 6935 | goto exit; | 
|  | 6936 | } | 
|  | 6937 |  | 
|  | 6938 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED  ) | 
|  | 6939 | { | 
|  | 6940 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6941 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 6942 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | 
|  | 6943 | goto exit; | 
|  | 6944 | } | 
|  | 6945 |  | 
|  | 6946 | if( ssl->in_hslen  != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) | 
|  | 6947 | { | 
|  | 6948 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
|  | 6949 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6950 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 6951 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; | 
|  | 6952 | goto exit; | 
|  | 6953 | } | 
|  | 6954 |  | 
|  | 6955 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), | 
|  | 6956 | buf, hash_len ) != 0 ) | 
|  | 6957 | { | 
|  | 6958 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
|  | 6959 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6960 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); | 
|  | 6961 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; | 
|  | 6962 | goto exit; | 
|  | 6963 | } | 
|  | 6964 |  | 
|  | 6965 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6966 | ssl->verify_data_len = hash_len; | 
|  | 6967 | memcpy( ssl->peer_verify_data, buf, hash_len ); | 
|  | 6968 | #endif | 
|  | 6969 |  | 
|  | 6970 | if( ssl->handshake->resume != 0 ) | 
|  | 6971 | { | 
|  | 6972 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 6973 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6974 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
|  | 6975 | #endif | 
|  | 6976 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6977 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 6978 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
|  | 6979 | #endif | 
|  | 6980 | } | 
|  | 6981 | else | 
|  | 6982 | ssl->state++; | 
|  | 6983 |  | 
|  | 6984 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6985 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 6986 | mbedtls_ssl_recv_flight_completed( ssl ); | 
|  | 6987 | #endif | 
|  | 6988 |  | 
|  | 6989 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); | 
|  | 6990 |  | 
|  | 6991 | exit: | 
|  | 6992 | mbedtls_platform_zeroize( buf, hash_len ); | 
|  | 6993 | return( ret ); | 
|  | 6994 | } | 
|  | 6995 |  | 
| Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 6996 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
|  | 6997 | /* | 
|  | 6998 | * Helper to get TLS 1.2 PRF from ciphersuite | 
|  | 6999 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) | 
|  | 7000 | */ | 
|  | 7001 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) | 
|  | 7002 | { | 
|  | 7003 | #if defined(MBEDTLS_SHA384_C) | 
|  | 7004 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = | 
|  | 7005 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); | 
|  | 7006 |  | 
|  | 7007 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) | 
|  | 7008 | return( tls_prf_sha384 ); | 
|  | 7009 | #else | 
|  | 7010 | (void) ciphersuite_id; | 
|  | 7011 | #endif | 
|  | 7012 | return( tls_prf_sha256 ); | 
|  | 7013 | } | 
|  | 7014 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
| Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7015 |  | 
|  | 7016 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) | 
|  | 7017 | { | 
|  | 7018 | ((void) tls_prf); | 
|  | 7019 | #if defined(MBEDTLS_SHA384_C) | 
|  | 7020 | if( tls_prf == tls_prf_sha384 ) | 
|  | 7021 | { | 
|  | 7022 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); | 
|  | 7023 | } | 
|  | 7024 | else | 
|  | 7025 | #endif | 
|  | 7026 | #if defined(MBEDTLS_SHA256_C) | 
|  | 7027 | if( tls_prf == tls_prf_sha256 ) | 
|  | 7028 | { | 
|  | 7029 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); | 
|  | 7030 | } | 
|  | 7031 | else | 
|  | 7032 | #endif | 
|  | 7033 | return( MBEDTLS_SSL_TLS_PRF_NONE ); | 
|  | 7034 | } | 
|  | 7035 |  | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7036 | /* | 
|  | 7037 | * Populate a transform structure with session keys and all the other | 
|  | 7038 | * necessary information. | 
|  | 7039 | * | 
|  | 7040 | * Parameters: | 
|  | 7041 | * - [in/out]: transform: structure to populate | 
|  | 7042 | *      [in] must be just initialised with mbedtls_ssl_transform_init() | 
|  | 7043 | *      [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() | 
|  | 7044 | * - [in] ciphersuite | 
|  | 7045 | * - [in] master | 
|  | 7046 | * - [in] encrypt_then_mac | 
|  | 7047 | * - [in] compression | 
|  | 7048 | * - [in] tls_prf: pointer to PRF to use for key derivation | 
|  | 7049 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random | 
|  | 7050 | * - [in] minor_ver: SSL/TLS minor version | 
|  | 7051 | * - [in] endpoint: client or server | 
|  | 7052 | * - [in] ssl: used for: | 
|  | 7053 | *        - ssl->conf->{f,p}_export_keys | 
|  | 7054 | *      [in] optionally used for: | 
|  | 7055 | *        - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg | 
|  | 7056 | */ | 
|  | 7057 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, | 
|  | 7058 | int ciphersuite, | 
|  | 7059 | const unsigned char master[48], | 
|  | 7060 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ | 
|  | 7061 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 7062 | int encrypt_then_mac, | 
|  | 7063 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && | 
|  | 7064 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 7065 | ssl_tls_prf_t tls_prf, | 
|  | 7066 | const unsigned char randbytes[64], | 
|  | 7067 | int minor_ver, | 
|  | 7068 | unsigned endpoint, | 
|  | 7069 | const mbedtls_ssl_context *ssl ) | 
|  | 7070 | { | 
|  | 7071 | int ret = 0; | 
|  | 7072 | unsigned char keyblk[256]; | 
|  | 7073 | unsigned char *key1; | 
|  | 7074 | unsigned char *key2; | 
|  | 7075 | unsigned char *mac_enc; | 
|  | 7076 | unsigned char *mac_dec; | 
|  | 7077 | size_t mac_key_len = 0; | 
|  | 7078 | size_t iv_copy_len; | 
|  | 7079 | size_t keylen; | 
|  | 7080 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
|  | 7081 | const mbedtls_cipher_info_t *cipher_info; | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7082 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7083 | const mbedtls_md_info_t *md_info; | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7084 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7085 |  | 
|  | 7086 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 7087 | psa_key_type_t key_type; | 
|  | 7088 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 7089 | psa_algorithm_t alg; | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7090 | psa_algorithm_t mac_alg = 0; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7091 | size_t key_bits; | 
|  | 7092 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 7093 | #endif | 
|  | 7094 |  | 
|  | 7095 | #if !defined(MBEDTLS_DEBUG_C) && \ | 
|  | 7096 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 7097 | if( ssl->f_export_keys == NULL ) | 
|  | 7098 | { | 
|  | 7099 | ssl = NULL; /* make sure we don't use it except for these cases */ | 
|  | 7100 | (void) ssl; | 
|  | 7101 | } | 
|  | 7102 | #endif | 
|  | 7103 |  | 
|  | 7104 | /* | 
|  | 7105 | * Some data just needs copying into the structure | 
|  | 7106 | */ | 
|  | 7107 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ | 
|  | 7108 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
|  | 7109 | transform->encrypt_then_mac = encrypt_then_mac; | 
|  | 7110 | #endif | 
|  | 7111 | transform->minor_ver = minor_ver; | 
|  | 7112 |  | 
|  | 7113 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
|  | 7114 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); | 
|  | 7115 | #endif | 
|  | 7116 |  | 
|  | 7117 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 7118 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) | 
|  | 7119 | { | 
|  | 7120 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform | 
|  | 7121 | * generation separate. This should never happen. */ | 
|  | 7122 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 7123 | } | 
|  | 7124 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 7125 |  | 
|  | 7126 | /* | 
|  | 7127 | * Get various info structures | 
|  | 7128 | */ | 
|  | 7129 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); | 
|  | 7130 | if( ciphersuite_info == NULL ) | 
|  | 7131 | { | 
|  | 7132 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", | 
|  | 7133 | ciphersuite ) ); | 
|  | 7134 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7135 | } | 
|  | 7136 |  | 
|  | 7137 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); | 
|  | 7138 | if( cipher_info == NULL ) | 
|  | 7139 | { | 
|  | 7140 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", | 
|  | 7141 | ciphersuite_info->cipher ) ); | 
|  | 7142 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7143 | } | 
|  | 7144 |  | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7145 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 7146 | mac_alg = mbedtls_psa_translate_md( ciphersuite_info->mac ); | 
|  | 7147 | if( mac_alg == 0 ) | 
|  | 7148 | { | 
|  | 7149 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_psa_translate_md for %u not found", | 
|  | 7150 | (unsigned) ciphersuite_info->mac ) ); | 
|  | 7151 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7152 | } | 
|  | 7153 | #else | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7154 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); | 
|  | 7155 | if( md_info == NULL ) | 
|  | 7156 | { | 
|  | 7157 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", | 
|  | 7158 | (unsigned) ciphersuite_info->mac ) ); | 
|  | 7159 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7160 | } | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7161 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7162 |  | 
|  | 7163 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 7164 | /* Copy own and peer's CID if the use of the CID | 
|  | 7165 | * extension has been negotiated. */ | 
|  | 7166 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) | 
|  | 7167 | { | 
|  | 7168 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); | 
|  | 7169 |  | 
|  | 7170 | transform->in_cid_len = ssl->own_cid_len; | 
|  | 7171 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); | 
|  | 7172 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, | 
|  | 7173 | transform->in_cid_len ); | 
|  | 7174 |  | 
|  | 7175 | transform->out_cid_len = ssl->handshake->peer_cid_len; | 
|  | 7176 | memcpy( transform->out_cid, ssl->handshake->peer_cid, | 
|  | 7177 | ssl->handshake->peer_cid_len ); | 
|  | 7178 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, | 
|  | 7179 | transform->out_cid_len ); | 
|  | 7180 | } | 
|  | 7181 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 7182 |  | 
|  | 7183 | /* | 
|  | 7184 | * Compute key block using the PRF | 
|  | 7185 | */ | 
|  | 7186 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); | 
|  | 7187 | if( ret != 0 ) | 
|  | 7188 | { | 
|  | 7189 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
|  | 7190 | return( ret ); | 
|  | 7191 | } | 
|  | 7192 |  | 
|  | 7193 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", | 
|  | 7194 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); | 
|  | 7195 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); | 
|  | 7196 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); | 
|  | 7197 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); | 
|  | 7198 |  | 
|  | 7199 | /* | 
|  | 7200 | * Determine the appropriate key, IV and MAC length. | 
|  | 7201 | */ | 
|  | 7202 |  | 
|  | 7203 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; | 
|  | 7204 |  | 
|  | 7205 | #if defined(MBEDTLS_GCM_C) ||                           \ | 
|  | 7206 | defined(MBEDTLS_CCM_C) ||                           \ | 
|  | 7207 | defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 7208 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM || | 
|  | 7209 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM || | 
|  | 7210 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) | 
|  | 7211 | { | 
|  | 7212 | size_t explicit_ivlen; | 
|  | 7213 |  | 
|  | 7214 | transform->maclen = 0; | 
|  | 7215 | mac_key_len = 0; | 
|  | 7216 | transform->taglen = | 
|  | 7217 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; | 
|  | 7218 |  | 
|  | 7219 | /* All modes haves 96-bit IVs, but the length of the static parts vary | 
|  | 7220 | * with mode and version: | 
|  | 7221 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes | 
|  | 7222 | *   (to be concatenated with a dynamically chosen IV of 8 Bytes) | 
|  | 7223 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's | 
|  | 7224 | *   a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record | 
|  | 7225 | *   sequence number). | 
|  | 7226 | */ | 
|  | 7227 | transform->ivlen = 12; | 
|  | 7228 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) | 
|  | 7229 | transform->fixed_ivlen = 12; | 
|  | 7230 | else | 
|  | 7231 | transform->fixed_ivlen = 4; | 
|  | 7232 |  | 
|  | 7233 | /* Minimum length of encrypted record */ | 
|  | 7234 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; | 
|  | 7235 | transform->minlen = explicit_ivlen + transform->taglen; | 
|  | 7236 | } | 
|  | 7237 | else | 
|  | 7238 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ | 
|  | 7239 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
|  | 7240 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM || | 
|  | 7241 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) | 
|  | 7242 | { | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7243 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 7244 | /* Get MAC length */ | 
|  | 7245 | mac_key_len = PSA_HASH_LENGTH(mac_alg); | 
|  | 7246 | #else | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7247 | /* Initialize HMAC contexts */ | 
|  | 7248 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || | 
|  | 7249 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) | 
|  | 7250 | { | 
|  | 7251 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | 
|  | 7252 | goto end; | 
|  | 7253 | } | 
|  | 7254 |  | 
|  | 7255 | /* Get MAC length */ | 
|  | 7256 | mac_key_len = mbedtls_md_get_size( md_info ); | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7257 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7258 | transform->maclen = mac_key_len; | 
|  | 7259 |  | 
|  | 7260 | /* IV length */ | 
|  | 7261 | transform->ivlen = cipher_info->iv_size; | 
|  | 7262 |  | 
|  | 7263 | /* Minimum length */ | 
|  | 7264 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ) | 
|  | 7265 | transform->minlen = transform->maclen; | 
|  | 7266 | else | 
|  | 7267 | { | 
|  | 7268 | /* | 
|  | 7269 | * GenericBlockCipher: | 
|  | 7270 | * 1. if EtM is in use: one block plus MAC | 
|  | 7271 | *    otherwise: * first multiple of blocklen greater than maclen | 
|  | 7272 | * 2. IV | 
|  | 7273 | */ | 
|  | 7274 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 7275 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) | 
|  | 7276 | { | 
|  | 7277 | transform->minlen = transform->maclen | 
|  | 7278 | + cipher_info->block_size; | 
|  | 7279 | } | 
|  | 7280 | else | 
|  | 7281 | #endif | 
|  | 7282 | { | 
|  | 7283 | transform->minlen = transform->maclen | 
|  | 7284 | + cipher_info->block_size | 
|  | 7285 | - transform->maclen % cipher_info->block_size; | 
|  | 7286 | } | 
|  | 7287 |  | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7288 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 7289 | { | 
|  | 7290 | transform->minlen += transform->ivlen; | 
|  | 7291 | } | 
|  | 7292 | else | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7293 | { | 
|  | 7294 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 7295 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 7296 | goto end; | 
|  | 7297 | } | 
|  | 7298 | } | 
|  | 7299 | } | 
|  | 7300 | else | 
|  | 7301 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 7302 | { | 
|  | 7303 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 7304 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 7305 | } | 
|  | 7306 |  | 
|  | 7307 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", | 
|  | 7308 | (unsigned) keylen, | 
|  | 7309 | (unsigned) transform->minlen, | 
|  | 7310 | (unsigned) transform->ivlen, | 
|  | 7311 | (unsigned) transform->maclen ) ); | 
|  | 7312 |  | 
|  | 7313 | /* | 
|  | 7314 | * Finally setup the cipher contexts, IVs and MAC secrets. | 
|  | 7315 | */ | 
|  | 7316 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 7317 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 7318 | { | 
|  | 7319 | key1 = keyblk + mac_key_len * 2; | 
|  | 7320 | key2 = keyblk + mac_key_len * 2 + keylen; | 
|  | 7321 |  | 
|  | 7322 | mac_enc = keyblk; | 
|  | 7323 | mac_dec = keyblk + mac_key_len; | 
|  | 7324 |  | 
|  | 7325 | /* | 
|  | 7326 | * This is not used in TLS v1.1. | 
|  | 7327 | */ | 
|  | 7328 | iv_copy_len = ( transform->fixed_ivlen ) ? | 
|  | 7329 | transform->fixed_ivlen : transform->ivlen; | 
|  | 7330 | memcpy( transform->iv_enc, key2 + keylen,  iv_copy_len ); | 
|  | 7331 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, | 
|  | 7332 | iv_copy_len ); | 
|  | 7333 | } | 
|  | 7334 | else | 
|  | 7335 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 7336 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 7337 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 7338 | { | 
|  | 7339 | key1 = keyblk + mac_key_len * 2 + keylen; | 
|  | 7340 | key2 = keyblk + mac_key_len * 2; | 
|  | 7341 |  | 
|  | 7342 | mac_enc = keyblk + mac_key_len; | 
|  | 7343 | mac_dec = keyblk; | 
|  | 7344 |  | 
|  | 7345 | /* | 
|  | 7346 | * This is not used in TLS v1.1. | 
|  | 7347 | */ | 
|  | 7348 | iv_copy_len = ( transform->fixed_ivlen ) ? | 
|  | 7349 | transform->fixed_ivlen : transform->ivlen; | 
|  | 7350 | memcpy( transform->iv_dec, key1 + keylen,  iv_copy_len ); | 
|  | 7351 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, | 
|  | 7352 | iv_copy_len ); | 
|  | 7353 | } | 
|  | 7354 | else | 
|  | 7355 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 7356 | { | 
|  | 7357 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 7358 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 7359 | goto end; | 
|  | 7360 | } | 
|  | 7361 |  | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7362 | if( ssl != NULL && ssl->f_export_keys != NULL ) | 
|  | 7363 | { | 
|  | 7364 | ssl->f_export_keys( ssl->p_export_keys, | 
|  | 7365 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, | 
|  | 7366 | master, 48, | 
|  | 7367 | randbytes + 32, | 
|  | 7368 | randbytes, | 
|  | 7369 | tls_prf_get_type( tls_prf ) ); | 
|  | 7370 | } | 
|  | 7371 |  | 
|  | 7372 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 7373 | if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type, | 
|  | 7374 | transform->taglen, | 
|  | 7375 | &alg, | 
|  | 7376 | &key_type, | 
|  | 7377 | &key_bits ) ) != PSA_SUCCESS ) | 
|  | 7378 | { | 
|  | 7379 | ret = psa_ssl_status_to_mbedtls( status ); | 
|  | 7380 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); | 
|  | 7381 | goto end; | 
|  | 7382 | } | 
|  | 7383 |  | 
|  | 7384 | transform->psa_alg = alg; | 
|  | 7385 |  | 
|  | 7386 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) | 
|  | 7387 | { | 
|  | 7388 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); | 
|  | 7389 | psa_set_key_algorithm( &attributes, alg ); | 
|  | 7390 | psa_set_key_type( &attributes, key_type ); | 
|  | 7391 |  | 
|  | 7392 | if( ( status = psa_import_key( &attributes, | 
|  | 7393 | key1, | 
|  | 7394 | PSA_BITS_TO_BYTES( key_bits ), | 
|  | 7395 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) | 
|  | 7396 | { | 
|  | 7397 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); | 
|  | 7398 | ret = psa_ssl_status_to_mbedtls( status ); | 
|  | 7399 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); | 
|  | 7400 | goto end; | 
|  | 7401 | } | 
|  | 7402 |  | 
|  | 7403 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); | 
|  | 7404 |  | 
|  | 7405 | if( ( status = psa_import_key( &attributes, | 
|  | 7406 | key2, | 
|  | 7407 | PSA_BITS_TO_BYTES( key_bits ), | 
|  | 7408 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) | 
|  | 7409 | { | 
|  | 7410 | ret = psa_ssl_status_to_mbedtls( status ); | 
|  | 7411 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); | 
|  | 7412 | goto end; | 
|  | 7413 | } | 
|  | 7414 | } | 
|  | 7415 | #else | 
|  | 7416 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, | 
|  | 7417 | cipher_info ) ) != 0 ) | 
|  | 7418 | { | 
|  | 7419 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | 
|  | 7420 | goto end; | 
|  | 7421 | } | 
|  | 7422 |  | 
|  | 7423 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, | 
|  | 7424 | cipher_info ) ) != 0 ) | 
|  | 7425 | { | 
|  | 7426 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | 
|  | 7427 | goto end; | 
|  | 7428 | } | 
|  | 7429 |  | 
|  | 7430 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, | 
|  | 7431 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), | 
|  | 7432 | MBEDTLS_ENCRYPT ) ) != 0 ) | 
|  | 7433 | { | 
|  | 7434 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | 
|  | 7435 | goto end; | 
|  | 7436 | } | 
|  | 7437 |  | 
|  | 7438 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, | 
|  | 7439 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), | 
|  | 7440 | MBEDTLS_DECRYPT ) ) != 0 ) | 
|  | 7441 | { | 
|  | 7442 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | 
|  | 7443 | goto end; | 
|  | 7444 | } | 
|  | 7445 |  | 
|  | 7446 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
|  | 7447 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) | 
|  | 7448 | { | 
|  | 7449 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, | 
|  | 7450 | MBEDTLS_PADDING_NONE ) ) != 0 ) | 
|  | 7451 | { | 
|  | 7452 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); | 
|  | 7453 | goto end; | 
|  | 7454 | } | 
|  | 7455 |  | 
|  | 7456 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, | 
|  | 7457 | MBEDTLS_PADDING_NONE ) ) != 0 ) | 
|  | 7458 | { | 
|  | 7459 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); | 
|  | 7460 | goto end; | 
|  | 7461 | } | 
|  | 7462 | } | 
|  | 7463 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
|  | 7464 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 7465 |  | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7466 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
|  | 7467 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. | 
|  | 7468 | For AEAD-based ciphersuites, there is nothing to do here. */ | 
|  | 7469 | if( mac_key_len != 0 ) | 
|  | 7470 | { | 
|  | 7471 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7472 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7473 |  | 
|  | 7474 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7475 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7476 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); | 
|  | 7477 |  | 
|  | 7478 | if( ( status = psa_import_key( &attributes, | 
|  | 7479 | mac_enc, mac_key_len, | 
|  | 7480 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) | 
|  | 7481 | { | 
|  | 7482 | ret = psa_ssl_status_to_mbedtls( status ); | 
|  | 7483 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); | 
|  | 7484 | goto end; | 
|  | 7485 | } | 
|  | 7486 |  | 
| Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 7487 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || | 
|  | 7488 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) && | 
|  | 7489 | ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) ) | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7490 | /* mbedtls_ct_hmac() requires the key to be exportable */ | 
|  | 7491 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | | 
|  | 7492 | PSA_KEY_USAGE_VERIFY_HASH ); | 
|  | 7493 | else | 
|  | 7494 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); | 
|  | 7495 |  | 
|  | 7496 | if( ( status = psa_import_key( &attributes, | 
|  | 7497 | mac_dec, mac_key_len, | 
|  | 7498 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) | 
|  | 7499 | { | 
|  | 7500 | ret = psa_ssl_status_to_mbedtls( status ); | 
|  | 7501 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); | 
|  | 7502 | goto end; | 
|  | 7503 | } | 
|  | 7504 | #else | 
|  | 7505 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); | 
|  | 7506 | if( ret != 0 ) | 
|  | 7507 | goto end; | 
|  | 7508 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); | 
|  | 7509 | if( ret != 0 ) | 
|  | 7510 | goto end; | 
|  | 7511 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 7512 | } | 
|  | 7513 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 7514 |  | 
|  | 7515 | ((void) mac_dec); | 
|  | 7516 | ((void) mac_enc); | 
|  | 7517 |  | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7518 | end: | 
|  | 7519 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); | 
|  | 7520 | return( ret ); | 
|  | 7521 | } | 
|  | 7522 |  | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 7523 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 7524 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | 
|  | 7525 | unsigned char *hash, size_t *hashlen, | 
|  | 7526 | unsigned char *data, size_t data_len, | 
|  | 7527 | mbedtls_md_type_t md_alg ) | 
|  | 7528 | { | 
|  | 7529 | psa_status_t status; | 
|  | 7530 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; | 
|  | 7531 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); | 
|  | 7532 |  | 
|  | 7533 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); | 
|  | 7534 |  | 
|  | 7535 | if( ( status = psa_hash_setup( &hash_operation, | 
|  | 7536 | hash_alg ) ) != PSA_SUCCESS ) | 
|  | 7537 | { | 
|  | 7538 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); | 
|  | 7539 | goto exit; | 
|  | 7540 | } | 
|  | 7541 |  | 
|  | 7542 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, | 
|  | 7543 | 64 ) ) != PSA_SUCCESS ) | 
|  | 7544 | { | 
|  | 7545 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); | 
|  | 7546 | goto exit; | 
|  | 7547 | } | 
|  | 7548 |  | 
|  | 7549 | if( ( status = psa_hash_update( &hash_operation, | 
|  | 7550 | data, data_len ) ) != PSA_SUCCESS ) | 
|  | 7551 | { | 
|  | 7552 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); | 
|  | 7553 | goto exit; | 
|  | 7554 | } | 
|  | 7555 |  | 
|  | 7556 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, | 
|  | 7557 | hashlen ) ) != PSA_SUCCESS ) | 
|  | 7558 | { | 
|  | 7559 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); | 
|  | 7560 | goto exit; | 
|  | 7561 | } | 
|  | 7562 |  | 
|  | 7563 | exit: | 
|  | 7564 | if( status != PSA_SUCCESS ) | 
|  | 7565 | { | 
|  | 7566 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7567 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 7568 | switch( status ) | 
|  | 7569 | { | 
|  | 7570 | case PSA_ERROR_NOT_SUPPORTED: | 
|  | 7571 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); | 
|  | 7572 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ | 
|  | 7573 | case PSA_ERROR_BUFFER_TOO_SMALL: | 
|  | 7574 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); | 
|  | 7575 | case PSA_ERROR_INSUFFICIENT_MEMORY: | 
|  | 7576 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); | 
|  | 7577 | default: | 
|  | 7578 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); | 
|  | 7579 | } | 
|  | 7580 | } | 
|  | 7581 | return( 0 ); | 
|  | 7582 | } | 
|  | 7583 |  | 
|  | 7584 | #else | 
|  | 7585 |  | 
|  | 7586 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | 
|  | 7587 | unsigned char *hash, size_t *hashlen, | 
|  | 7588 | unsigned char *data, size_t data_len, | 
|  | 7589 | mbedtls_md_type_t md_alg ) | 
|  | 7590 | { | 
|  | 7591 | int ret = 0; | 
|  | 7592 | mbedtls_md_context_t ctx; | 
|  | 7593 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); | 
|  | 7594 | *hashlen = mbedtls_md_get_size( md_info ); | 
|  | 7595 |  | 
|  | 7596 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); | 
|  | 7597 |  | 
|  | 7598 | mbedtls_md_init( &ctx ); | 
|  | 7599 |  | 
|  | 7600 | /* | 
|  | 7601 | * digitally-signed struct { | 
|  | 7602 | *     opaque client_random[32]; | 
|  | 7603 | *     opaque server_random[32]; | 
|  | 7604 | *     ServerDHParams params; | 
|  | 7605 | * }; | 
|  | 7606 | */ | 
|  | 7607 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) | 
|  | 7608 | { | 
|  | 7609 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | 
|  | 7610 | goto exit; | 
|  | 7611 | } | 
|  | 7612 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) | 
|  | 7613 | { | 
|  | 7614 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); | 
|  | 7615 | goto exit; | 
|  | 7616 | } | 
|  | 7617 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 7618 | { | 
|  | 7619 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | 
|  | 7620 | goto exit; | 
|  | 7621 | } | 
|  | 7622 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) | 
|  | 7623 | { | 
|  | 7624 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | 
|  | 7625 | goto exit; | 
|  | 7626 | } | 
|  | 7627 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) | 
|  | 7628 | { | 
|  | 7629 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); | 
|  | 7630 | goto exit; | 
|  | 7631 | } | 
|  | 7632 |  | 
|  | 7633 | exit: | 
|  | 7634 | mbedtls_md_free( &ctx ); | 
|  | 7635 |  | 
|  | 7636 | if( ret != 0 ) | 
|  | 7637 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7638 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 7639 |  | 
|  | 7640 | return( ret ); | 
|  | 7641 | } | 
|  | 7642 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 7643 |  | 
| Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7644 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 7645 |  | 
|  | 7646 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ | 
|  | 7647 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 7648 | mbedtls_pk_type_t sig_alg ) | 
|  | 7649 | { | 
|  | 7650 | switch( sig_alg ) | 
|  | 7651 | { | 
|  | 7652 | case MBEDTLS_PK_RSA: | 
|  | 7653 | return( set->rsa ); | 
|  | 7654 | case MBEDTLS_PK_ECDSA: | 
|  | 7655 | return( set->ecdsa ); | 
|  | 7656 | default: | 
|  | 7657 | return( MBEDTLS_MD_NONE ); | 
|  | 7658 | } | 
|  | 7659 | } | 
|  | 7660 |  | 
|  | 7661 | /* Add a signature-hash-pair to a signature-hash set */ | 
|  | 7662 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 7663 | mbedtls_pk_type_t sig_alg, | 
|  | 7664 | mbedtls_md_type_t md_alg ) | 
|  | 7665 | { | 
|  | 7666 | switch( sig_alg ) | 
|  | 7667 | { | 
|  | 7668 | case MBEDTLS_PK_RSA: | 
|  | 7669 | if( set->rsa == MBEDTLS_MD_NONE ) | 
|  | 7670 | set->rsa = md_alg; | 
|  | 7671 | break; | 
|  | 7672 |  | 
|  | 7673 | case MBEDTLS_PK_ECDSA: | 
|  | 7674 | if( set->ecdsa == MBEDTLS_MD_NONE ) | 
|  | 7675 | set->ecdsa = md_alg; | 
|  | 7676 | break; | 
|  | 7677 |  | 
|  | 7678 | default: | 
|  | 7679 | break; | 
|  | 7680 | } | 
|  | 7681 | } | 
|  | 7682 |  | 
|  | 7683 | /* Allow exactly one hash algorithm for each signature. */ | 
|  | 7684 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 7685 | mbedtls_md_type_t md_alg ) | 
|  | 7686 | { | 
|  | 7687 | set->rsa   = md_alg; | 
|  | 7688 | set->ecdsa = md_alg; | 
|  | 7689 | } | 
|  | 7690 |  | 
|  | 7691 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 7692 |  | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7693 | /* Serialization of TLS 1.2 sessions: | 
|  | 7694 | * | 
|  | 7695 | * struct { | 
|  | 7696 | *    uint64 start_time; | 
|  | 7697 | *    uint8 ciphersuite[2];           // defined by the standard | 
|  | 7698 | *    uint8 compression;              // 0 or 1 | 
|  | 7699 | *    uint8 session_id_len;           // at most 32 | 
|  | 7700 | *    opaque session_id[32]; | 
|  | 7701 | *    opaque master[48];              // fixed length in the standard | 
|  | 7702 | *    uint32 verify_result; | 
|  | 7703 | *    opaque peer_cert<0..2^24-1>;    // length 0 means no peer cert | 
|  | 7704 | *    opaque ticket<0..2^24-1>;       // length 0 means no ticket | 
|  | 7705 | *    uint32 ticket_lifetime; | 
|  | 7706 | *    uint8 mfl_code;                 // up to 255 according to standard | 
|  | 7707 | *    uint8 encrypt_then_mac;         // 0 or 1 | 
|  | 7708 | * } serialized_session_tls12; | 
|  | 7709 | * | 
|  | 7710 | */ | 
|  | 7711 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, | 
|  | 7712 | unsigned char *buf, | 
|  | 7713 | size_t buf_len ) | 
|  | 7714 | { | 
|  | 7715 | unsigned char *p = buf; | 
|  | 7716 | size_t used = 0; | 
|  | 7717 |  | 
|  | 7718 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 7719 | uint64_t start; | 
|  | 7720 | #endif | 
|  | 7721 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7722 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7723 | size_t cert_len; | 
|  | 7724 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7725 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 7726 |  | 
|  | 7727 | /* | 
|  | 7728 | * Time | 
|  | 7729 | */ | 
|  | 7730 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 7731 | used += 8; | 
|  | 7732 |  | 
|  | 7733 | if( used <= buf_len ) | 
|  | 7734 | { | 
|  | 7735 | start = (uint64_t) session->start; | 
|  | 7736 |  | 
|  | 7737 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); | 
|  | 7738 | p += 8; | 
|  | 7739 | } | 
|  | 7740 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 7741 |  | 
|  | 7742 | /* | 
|  | 7743 | * Basic mandatory fields | 
|  | 7744 | */ | 
|  | 7745 | used += 2   /* ciphersuite */ | 
|  | 7746 | + 1   /* compression */ | 
|  | 7747 | + 1   /* id_len */ | 
|  | 7748 | + sizeof( session->id ) | 
|  | 7749 | + sizeof( session->master ) | 
|  | 7750 | + 4;  /* verify_result */ | 
|  | 7751 |  | 
|  | 7752 | if( used <= buf_len ) | 
|  | 7753 | { | 
|  | 7754 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); | 
|  | 7755 | p += 2; | 
|  | 7756 |  | 
|  | 7757 | *p++ = MBEDTLS_BYTE_0( session->compression ); | 
|  | 7758 |  | 
|  | 7759 | *p++ = MBEDTLS_BYTE_0( session->id_len ); | 
|  | 7760 | memcpy( p, session->id, 32 ); | 
|  | 7761 | p += 32; | 
|  | 7762 |  | 
|  | 7763 | memcpy( p, session->master, 48 ); | 
|  | 7764 | p += 48; | 
|  | 7765 |  | 
|  | 7766 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); | 
|  | 7767 | p += 4; | 
|  | 7768 | } | 
|  | 7769 |  | 
|  | 7770 | /* | 
|  | 7771 | * Peer's end-entity certificate | 
|  | 7772 | */ | 
|  | 7773 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7774 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7775 | if( session->peer_cert == NULL ) | 
|  | 7776 | cert_len = 0; | 
|  | 7777 | else | 
|  | 7778 | cert_len = session->peer_cert->raw.len; | 
|  | 7779 |  | 
|  | 7780 | used += 3 + cert_len; | 
|  | 7781 |  | 
|  | 7782 | if( used <= buf_len ) | 
|  | 7783 | { | 
|  | 7784 | *p++ = MBEDTLS_BYTE_2( cert_len ); | 
|  | 7785 | *p++ = MBEDTLS_BYTE_1( cert_len ); | 
|  | 7786 | *p++ = MBEDTLS_BYTE_0( cert_len ); | 
|  | 7787 |  | 
|  | 7788 | if( session->peer_cert != NULL ) | 
|  | 7789 | { | 
|  | 7790 | memcpy( p, session->peer_cert->raw.p, cert_len ); | 
|  | 7791 | p += cert_len; | 
|  | 7792 | } | 
|  | 7793 | } | 
|  | 7794 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7795 | if( session->peer_cert_digest != NULL ) | 
|  | 7796 | { | 
|  | 7797 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; | 
|  | 7798 | if( used <= buf_len ) | 
|  | 7799 | { | 
|  | 7800 | *p++ = (unsigned char) session->peer_cert_digest_type; | 
|  | 7801 | *p++ = (unsigned char) session->peer_cert_digest_len; | 
|  | 7802 | memcpy( p, session->peer_cert_digest, | 
|  | 7803 | session->peer_cert_digest_len ); | 
|  | 7804 | p += session->peer_cert_digest_len; | 
|  | 7805 | } | 
|  | 7806 | } | 
|  | 7807 | else | 
|  | 7808 | { | 
|  | 7809 | used += 2; | 
|  | 7810 | if( used <= buf_len ) | 
|  | 7811 | { | 
|  | 7812 | *p++ = (unsigned char) MBEDTLS_MD_NONE; | 
|  | 7813 | *p++ = 0; | 
|  | 7814 | } | 
|  | 7815 | } | 
|  | 7816 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7817 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 7818 |  | 
|  | 7819 | /* | 
|  | 7820 | * Session ticket if any, plus associated data | 
|  | 7821 | */ | 
|  | 7822 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 7823 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ | 
|  | 7824 |  | 
|  | 7825 | if( used <= buf_len ) | 
|  | 7826 | { | 
|  | 7827 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); | 
|  | 7828 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); | 
|  | 7829 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); | 
|  | 7830 |  | 
|  | 7831 | if( session->ticket != NULL ) | 
|  | 7832 | { | 
|  | 7833 | memcpy( p, session->ticket, session->ticket_len ); | 
|  | 7834 | p += session->ticket_len; | 
|  | 7835 | } | 
|  | 7836 |  | 
|  | 7837 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); | 
|  | 7838 | p += 4; | 
|  | 7839 | } | 
|  | 7840 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 7841 |  | 
|  | 7842 | /* | 
|  | 7843 | * Misc extension-related info | 
|  | 7844 | */ | 
|  | 7845 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 7846 | used += 1; | 
|  | 7847 |  | 
|  | 7848 | if( used <= buf_len ) | 
|  | 7849 | *p++ = session->mfl_code; | 
|  | 7850 | #endif | 
|  | 7851 |  | 
|  | 7852 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 7853 | used += 1; | 
|  | 7854 |  | 
|  | 7855 | if( used <= buf_len ) | 
|  | 7856 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); | 
|  | 7857 | #endif | 
|  | 7858 |  | 
|  | 7859 | return( used ); | 
|  | 7860 | } | 
|  | 7861 |  | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7862 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, | 
|  | 7863 | const unsigned char *buf, | 
|  | 7864 | size_t len ) | 
|  | 7865 | { | 
|  | 7866 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 7867 | uint64_t start; | 
|  | 7868 | #endif | 
|  | 7869 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7870 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7871 | size_t cert_len; | 
|  | 7872 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7873 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 7874 |  | 
|  | 7875 | const unsigned char *p = buf; | 
|  | 7876 | const unsigned char * const end = buf + len; | 
|  | 7877 |  | 
|  | 7878 | /* | 
|  | 7879 | * Time | 
|  | 7880 | */ | 
|  | 7881 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 7882 | if( 8 > (size_t)( end - p ) ) | 
|  | 7883 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7884 |  | 
|  | 7885 | start = ( (uint64_t) p[0] << 56 ) | | 
|  | 7886 | ( (uint64_t) p[1] << 48 ) | | 
|  | 7887 | ( (uint64_t) p[2] << 40 ) | | 
|  | 7888 | ( (uint64_t) p[3] << 32 ) | | 
|  | 7889 | ( (uint64_t) p[4] << 24 ) | | 
|  | 7890 | ( (uint64_t) p[5] << 16 ) | | 
|  | 7891 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 7892 | ( (uint64_t) p[7]       ); | 
|  | 7893 | p += 8; | 
|  | 7894 |  | 
|  | 7895 | session->start = (time_t) start; | 
|  | 7896 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 7897 |  | 
|  | 7898 | /* | 
|  | 7899 | * Basic mandatory fields | 
|  | 7900 | */ | 
|  | 7901 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) | 
|  | 7902 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7903 |  | 
|  | 7904 | session->ciphersuite = ( p[0] << 8 ) | p[1]; | 
|  | 7905 | p += 2; | 
|  | 7906 |  | 
|  | 7907 | session->compression = *p++; | 
|  | 7908 |  | 
|  | 7909 | session->id_len = *p++; | 
|  | 7910 | memcpy( session->id, p, 32 ); | 
|  | 7911 | p += 32; | 
|  | 7912 |  | 
|  | 7913 | memcpy( session->master, p, 48 ); | 
|  | 7914 | p += 48; | 
|  | 7915 |  | 
|  | 7916 | session->verify_result = ( (uint32_t) p[0] << 24 ) | | 
|  | 7917 | ( (uint32_t) p[1] << 16 ) | | 
|  | 7918 | ( (uint32_t) p[2] <<  8 ) | | 
|  | 7919 | ( (uint32_t) p[3]       ); | 
|  | 7920 | p += 4; | 
|  | 7921 |  | 
|  | 7922 | /* Immediately clear invalid pointer values that have been read, in case | 
|  | 7923 | * we exit early before we replaced them with valid ones. */ | 
|  | 7924 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7925 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7926 | session->peer_cert = NULL; | 
|  | 7927 | #else | 
|  | 7928 | session->peer_cert_digest = NULL; | 
|  | 7929 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7930 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 7931 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 7932 | session->ticket = NULL; | 
|  | 7933 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 7934 |  | 
|  | 7935 | /* | 
|  | 7936 | * Peer certificate | 
|  | 7937 | */ | 
|  | 7938 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7939 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7940 | /* Deserialize CRT from the end of the ticket. */ | 
|  | 7941 | if( 3 > (size_t)( end - p ) ) | 
|  | 7942 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7943 |  | 
|  | 7944 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; | 
|  | 7945 | p += 3; | 
|  | 7946 |  | 
|  | 7947 | if( cert_len != 0 ) | 
|  | 7948 | { | 
|  | 7949 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 7950 |  | 
|  | 7951 | if( cert_len > (size_t)( end - p ) ) | 
|  | 7952 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7953 |  | 
|  | 7954 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); | 
|  | 7955 |  | 
|  | 7956 | if( session->peer_cert == NULL ) | 
|  | 7957 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 7958 |  | 
|  | 7959 | mbedtls_x509_crt_init( session->peer_cert ); | 
|  | 7960 |  | 
|  | 7961 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, | 
|  | 7962 | p, cert_len ) ) != 0 ) | 
|  | 7963 | { | 
|  | 7964 | mbedtls_x509_crt_free( session->peer_cert ); | 
|  | 7965 | mbedtls_free( session->peer_cert ); | 
|  | 7966 | session->peer_cert = NULL; | 
|  | 7967 | return( ret ); | 
|  | 7968 | } | 
|  | 7969 |  | 
|  | 7970 | p += cert_len; | 
|  | 7971 | } | 
|  | 7972 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7973 | /* Deserialize CRT digest from the end of the ticket. */ | 
|  | 7974 | if( 2 > (size_t)( end - p ) ) | 
|  | 7975 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7976 |  | 
|  | 7977 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; | 
|  | 7978 | session->peer_cert_digest_len  = (size_t) *p++; | 
|  | 7979 |  | 
|  | 7980 | if( session->peer_cert_digest_len != 0 ) | 
|  | 7981 | { | 
|  | 7982 | const mbedtls_md_info_t *md_info = | 
|  | 7983 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); | 
|  | 7984 | if( md_info == NULL ) | 
|  | 7985 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7986 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) | 
|  | 7987 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7988 |  | 
|  | 7989 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) | 
|  | 7990 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7991 |  | 
|  | 7992 | session->peer_cert_digest = | 
|  | 7993 | mbedtls_calloc( 1, session->peer_cert_digest_len ); | 
|  | 7994 | if( session->peer_cert_digest == NULL ) | 
|  | 7995 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 7996 |  | 
|  | 7997 | memcpy( session->peer_cert_digest, p, | 
|  | 7998 | session->peer_cert_digest_len ); | 
|  | 7999 | p += session->peer_cert_digest_len; | 
|  | 8000 | } | 
|  | 8001 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 8002 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 8003 |  | 
|  | 8004 | /* | 
|  | 8005 | * Session ticket and associated data | 
|  | 8006 | */ | 
|  | 8007 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 8008 | if( 3 > (size_t)( end - p ) ) | 
|  | 8009 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8010 |  | 
|  | 8011 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; | 
|  | 8012 | p += 3; | 
|  | 8013 |  | 
|  | 8014 | if( session->ticket_len != 0 ) | 
|  | 8015 | { | 
|  | 8016 | if( session->ticket_len > (size_t)( end - p ) ) | 
|  | 8017 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8018 |  | 
|  | 8019 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); | 
|  | 8020 | if( session->ticket == NULL ) | 
|  | 8021 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 8022 |  | 
|  | 8023 | memcpy( session->ticket, p, session->ticket_len ); | 
|  | 8024 | p += session->ticket_len; | 
|  | 8025 | } | 
|  | 8026 |  | 
|  | 8027 | if( 4 > (size_t)( end - p ) ) | 
|  | 8028 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8029 |  | 
|  | 8030 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | | 
|  | 8031 | ( (uint32_t) p[1] << 16 ) | | 
|  | 8032 | ( (uint32_t) p[2] <<  8 ) | | 
|  | 8033 | ( (uint32_t) p[3]       ); | 
|  | 8034 | p += 4; | 
|  | 8035 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 8036 |  | 
|  | 8037 | /* | 
|  | 8038 | * Misc extension-related info | 
|  | 8039 | */ | 
|  | 8040 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 8041 | if( 1 > (size_t)( end - p ) ) | 
|  | 8042 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8043 |  | 
|  | 8044 | session->mfl_code = *p++; | 
|  | 8045 | #endif | 
|  | 8046 |  | 
|  | 8047 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 8048 | if( 1 > (size_t)( end - p ) ) | 
|  | 8049 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8050 |  | 
|  | 8051 | session->encrypt_then_mac = *p++; | 
|  | 8052 | #endif | 
|  | 8053 |  | 
|  | 8054 | /* Done, should have consumed entire buffer */ | 
|  | 8055 | if( p != end ) | 
|  | 8056 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8057 |  | 
|  | 8058 | return( 0 ); | 
|  | 8059 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 8060 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 8061 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8062 | #endif /* MBEDTLS_SSL_TLS_C */ |