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" |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 41 | #include "ssl_client.h" |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 42 | #include "ssl_debug_helpers.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 43 | #include "ssl_misc.h" |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 44 | |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 45 | #include "mbedtls/debug.h" |
46 | #include "mbedtls/error.h" | ||||
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 47 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 48 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 49 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 50 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 51 | #include <string.h> |
52 | |||||
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 53 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
54 | #include "mbedtls/psa_util.h" | ||||
55 | #include "psa/crypto.h" | ||||
56 | #endif | ||||
Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 57 | #include "mbedtls/legacy_or_psa.h" |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 58 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 59 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 60 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 61 | #endif |
62 | |||||
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 63 | #if defined(MBEDTLS_TEST_HOOKS) |
64 | static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args; | ||||
65 | |||||
66 | void mbedtls_ssl_set_chk_buf_ptr_fail_args( | ||||
67 | const uint8_t *cur, const uint8_t *end, size_t need ) | ||||
68 | { | ||||
69 | chk_buf_ptr_fail_args.cur = cur; | ||||
70 | chk_buf_ptr_fail_args.end = end; | ||||
71 | chk_buf_ptr_fail_args.need = need; | ||||
72 | } | ||||
73 | |||||
74 | void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void ) | ||||
75 | { | ||||
76 | memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) ); | ||||
77 | } | ||||
78 | |||||
79 | int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args ) | ||||
80 | { | ||||
81 | return( ( chk_buf_ptr_fail_args.cur != args->cur ) || | ||||
82 | ( chk_buf_ptr_fail_args.end != args->end ) || | ||||
83 | ( chk_buf_ptr_fail_args.need != args->need ) ); | ||||
84 | } | ||||
85 | #endif /* MBEDTLS_TEST_HOOKS */ | ||||
86 | |||||
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 87 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 88 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 89 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 90 | /* Top-level Connection ID API */ |
91 | |||||
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 92 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
93 | size_t len, | ||||
94 | int ignore_other_cid ) | ||||
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 95 | { |
96 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) | ||||
97 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
98 | |||||
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 99 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
100 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) | ||||
101 | { | ||||
102 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
103 | } | ||||
104 | |||||
105 | conf->ignore_unexpected_cid = ignore_other_cid; | ||||
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 106 | conf->cid_len = len; |
107 | return( 0 ); | ||||
108 | } | ||||
109 | |||||
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 110 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
111 | int enable, | ||||
112 | unsigned char const *own_cid, | ||||
113 | size_t own_cid_len ) | ||||
114 | { | ||||
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 115 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
116 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
117 | |||||
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 118 | ssl->negotiate_cid = enable; |
119 | if( enable == MBEDTLS_SSL_CID_DISABLED ) | ||||
120 | { | ||||
121 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); | ||||
122 | return( 0 ); | ||||
123 | } | ||||
124 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); | ||||
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 125 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 126 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 127 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 128 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 129 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
130 | (unsigned) own_cid_len, | ||||
131 | (unsigned) ssl->conf->cid_len ) ); | ||||
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 132 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
133 | } | ||||
134 | |||||
135 | memcpy( ssl->own_cid, own_cid, own_cid_len ); | ||||
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 136 | /* Truncation is not an issue here because |
137 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ | ||||
138 | ssl->own_cid_len = (uint8_t) own_cid_len; | ||||
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 139 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 140 | return( 0 ); |
141 | } | ||||
142 | |||||
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 143 | int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl, |
144 | int *enabled, | ||||
145 | unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], | ||||
146 | size_t *own_cid_len ) | ||||
147 | { | ||||
148 | *enabled = MBEDTLS_SSL_CID_DISABLED; | ||||
149 | |||||
150 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | ||||
151 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
152 | |||||
153 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is | ||||
154 | * zero as this is indistinguishable from not requesting to use | ||||
155 | * the CID extension. */ | ||||
156 | if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) | ||||
157 | return( 0 ); | ||||
158 | |||||
159 | if( own_cid_len != NULL ) | ||||
160 | { | ||||
161 | *own_cid_len = ssl->own_cid_len; | ||||
162 | if( own_cid != NULL ) | ||||
163 | memcpy( own_cid, ssl->own_cid, ssl->own_cid_len ); | ||||
164 | } | ||||
165 | |||||
166 | *enabled = MBEDTLS_SSL_CID_ENABLED; | ||||
167 | |||||
168 | return( 0 ); | ||||
169 | } | ||||
170 | |||||
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 171 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
172 | int *enabled, | ||||
173 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], | ||||
174 | size_t *peer_cid_len ) | ||||
175 | { | ||||
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 176 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 177 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 178 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 179 | mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 180 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 181 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 182 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 183 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 184 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
185 | * were used, but client and server requested the empty CID. | ||||
186 | * This is indistinguishable from not using the CID extension | ||||
187 | * in the first place. */ | ||||
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 188 | if( ssl->transform_in->in_cid_len == 0 && |
189 | ssl->transform_in->out_cid_len == 0 ) | ||||
190 | { | ||||
191 | return( 0 ); | ||||
192 | } | ||||
193 | |||||
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 194 | if( peer_cid_len != NULL ) |
195 | { | ||||
196 | *peer_cid_len = ssl->transform_in->out_cid_len; | ||||
197 | if( peer_cid != NULL ) | ||||
198 | { | ||||
199 | memcpy( peer_cid, ssl->transform_in->out_cid, | ||||
200 | ssl->transform_in->out_cid_len ); | ||||
201 | } | ||||
202 | } | ||||
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 203 | |
204 | *enabled = MBEDTLS_SSL_CID_ENABLED; | ||||
205 | |||||
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 206 | return( 0 ); |
207 | } | ||||
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 208 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 213 | /* |
214 | * Convert max_fragment_length codes to length. | ||||
215 | * RFC 6066 says: | ||||
216 | * enum{ | ||||
217 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) | ||||
218 | * } MaxFragmentLength; | ||||
219 | * and we add 0 -> extension unused | ||||
220 | */ | ||||
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 221 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 222 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 223 | switch( mfl ) |
224 | { | ||||
225 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: | ||||
226 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); | ||||
227 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: | ||||
228 | return 512; | ||||
229 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: | ||||
230 | return 1024; | ||||
231 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: | ||||
232 | return 2048; | ||||
233 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: | ||||
234 | return 4096; | ||||
235 | default: | ||||
236 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); | ||||
237 | } | ||||
238 | } | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 240 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 241 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
242 | const mbedtls_ssl_session *src ) | ||||
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 243 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | mbedtls_ssl_session_free( dst ); |
245 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); | ||||
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 246 | |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 247 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
248 | dst->ticket = NULL; | ||||
249 | #endif | ||||
250 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 252 | |
253 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 254 | if( src->peer_cert != NULL ) |
255 | { | ||||
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 256 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 257 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 258 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 259 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 260 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 261 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | 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] | 265 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 266 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 267 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 268 | dst->peer_cert = NULL; |
269 | return( ret ); | ||||
270 | } | ||||
271 | } | ||||
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 272 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 273 | if( src->peer_cert_digest != NULL ) |
274 | { | ||||
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 275 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 276 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 277 | if( dst->peer_cert_digest == NULL ) |
278 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | ||||
279 | |||||
280 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, | ||||
281 | src->peer_cert_digest_len ); | ||||
282 | dst->peer_cert_digest_type = src->peer_cert_digest_type; | ||||
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 283 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 284 | } |
285 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
286 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 288 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 289 | #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] | 290 | if( src->ticket != NULL ) |
291 | { | ||||
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 292 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 293 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 294 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 295 | |
296 | memcpy( dst->ticket, src->ticket, src->ticket_len ); | ||||
297 | } | ||||
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 298 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 299 | |
300 | return( 0 ); | ||||
301 | } | ||||
302 | |||||
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 303 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 304 | MBEDTLS_CHECK_RETURN_CRITICAL |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 305 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
306 | { | ||||
307 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); | ||||
308 | if( resized_buffer == NULL ) | ||||
309 | return -1; | ||||
310 | |||||
311 | /* We want to copy len_new bytes when downsizing the buffer, and | ||||
312 | * len_old bytes when upsizing, so we choose the smaller of two sizes, | ||||
313 | * to fit one buffer into another. Size checks, ensuring that no data is | ||||
314 | * lost, are done outside of this function. */ | ||||
315 | memcpy( resized_buffer, *buffer, | ||||
316 | ( len_new < *len_old ) ? len_new : *len_old ); | ||||
317 | mbedtls_platform_zeroize( *buffer, *len_old ); | ||||
318 | mbedtls_free( *buffer ); | ||||
319 | |||||
320 | *buffer = resized_buffer; | ||||
321 | *len_old = len_new; | ||||
322 | |||||
323 | return 0; | ||||
324 | } | ||||
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 325 | |
326 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, | ||||
Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 327 | size_t in_buf_new_len, |
328 | size_t out_buf_new_len ) | ||||
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 329 | { |
330 | int modified = 0; | ||||
331 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; | ||||
332 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; | ||||
333 | if( ssl->in_buf != NULL ) | ||||
334 | { | ||||
335 | written_in = ssl->in_msg - ssl->in_buf; | ||||
336 | iv_offset_in = ssl->in_iv - ssl->in_buf; | ||||
337 | len_offset_in = ssl->in_len - ssl->in_buf; | ||||
338 | if( downsizing ? | ||||
339 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : | ||||
340 | ssl->in_buf_len < in_buf_new_len ) | ||||
341 | { | ||||
342 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) | ||||
343 | { | ||||
344 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); | ||||
345 | } | ||||
346 | else | ||||
347 | { | ||||
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 348 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
349 | in_buf_new_len ) ); | ||||
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 350 | modified = 1; |
351 | } | ||||
352 | } | ||||
353 | } | ||||
354 | |||||
355 | if( ssl->out_buf != NULL ) | ||||
356 | { | ||||
357 | written_out = ssl->out_msg - ssl->out_buf; | ||||
358 | iv_offset_out = ssl->out_iv - ssl->out_buf; | ||||
359 | len_offset_out = ssl->out_len - ssl->out_buf; | ||||
360 | if( downsizing ? | ||||
361 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : | ||||
362 | ssl->out_buf_len < out_buf_new_len ) | ||||
363 | { | ||||
364 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) | ||||
365 | { | ||||
366 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); | ||||
367 | } | ||||
368 | else | ||||
369 | { | ||||
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 370 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
371 | out_buf_new_len ) ); | ||||
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 372 | modified = 1; |
373 | } | ||||
374 | } | ||||
375 | } | ||||
376 | if( modified ) | ||||
377 | { | ||||
378 | /* Update pointers here to avoid doing it twice. */ | ||||
379 | mbedtls_ssl_reset_in_out_pointers( ssl ); | ||||
380 | /* Fields below might not be properly updated with record | ||||
381 | * splitting or with CID, so they are manually updated here. */ | ||||
382 | ssl->out_msg = ssl->out_buf + written_out; | ||||
383 | ssl->out_len = ssl->out_buf + len_offset_out; | ||||
384 | ssl->out_iv = ssl->out_buf + iv_offset_out; | ||||
385 | |||||
386 | ssl->in_msg = ssl->in_buf + written_in; | ||||
387 | ssl->in_len = ssl->in_buf + len_offset_in; | ||||
388 | ssl->in_iv = ssl->in_buf + iv_offset_in; | ||||
389 | } | ||||
390 | } | ||||
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 391 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
392 | |||||
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 393 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 394 | |
395 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | ||||
396 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, | ||||
397 | const char *label, | ||||
398 | const unsigned char *random, size_t rlen, | ||||
399 | unsigned char *dstbuf, size_t dlen ); | ||||
400 | |||||
401 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ); | ||||
402 | |||||
403 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | ||||
404 | |||||
405 | /* Type for the TLS PRF */ | ||||
406 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, | ||||
407 | const unsigned char *, size_t, | ||||
408 | unsigned char *, size_t); | ||||
409 | |||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 410 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 411 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
412 | int ciphersuite, | ||||
413 | const unsigned char master[48], | ||||
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 414 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 415 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 416 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 417 | ssl_tls_prf_t tls_prf, |
418 | const unsigned char randbytes[64], | ||||
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 419 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 420 | unsigned endpoint, |
421 | const mbedtls_ssl_context *ssl ); | ||||
422 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 423 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 424 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 425 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
426 | const char *label, | ||||
427 | const unsigned char *random, size_t rlen, | ||||
428 | unsigned char *dstbuf, size_t dlen ); | ||||
429 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); | ||||
430 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); | ||||
431 | |||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 432 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 433 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 434 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 435 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 436 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
437 | const char *label, | ||||
438 | const unsigned char *random, size_t rlen, | ||||
439 | unsigned char *dstbuf, size_t dlen ); | ||||
440 | |||||
441 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); | ||||
442 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 443 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 444 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 445 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 446 | unsigned char *buf, |
447 | size_t buf_len ); | ||||
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 448 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 449 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 450 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 451 | const unsigned char *buf, |
452 | size_t len ); | ||||
453 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
454 | |||||
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 455 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
456 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 457 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 458 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 459 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 460 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 461 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 462 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 463 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 464 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 465 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
466 | const unsigned char *secret, size_t slen, | ||||
467 | const char *label, | ||||
468 | const unsigned char *random, size_t rlen, | ||||
469 | unsigned char *dstbuf, size_t dlen ) | ||||
470 | { | ||||
471 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; | ||||
472 | |||||
473 | switch( prf ) | ||||
474 | { | ||||
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 475 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 476 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 477 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
478 | tls_prf = tls_prf_sha384; | ||||
479 | break; | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 480 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 481 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 482 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
483 | tls_prf = tls_prf_sha256; | ||||
484 | break; | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 485 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 486 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 487 | default: |
488 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
489 | } | ||||
490 | |||||
491 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); | ||||
492 | } | ||||
493 | |||||
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 494 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
495 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) | ||||
496 | { | ||||
497 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
498 | if( session->peer_cert != NULL ) | ||||
499 | { | ||||
500 | mbedtls_x509_crt_free( session->peer_cert ); | ||||
501 | mbedtls_free( session->peer_cert ); | ||||
502 | session->peer_cert = NULL; | ||||
503 | } | ||||
504 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
505 | if( session->peer_cert_digest != NULL ) | ||||
506 | { | ||||
507 | /* Zeroization is not necessary. */ | ||||
508 | mbedtls_free( session->peer_cert_digest ); | ||||
509 | session->peer_cert_digest = NULL; | ||||
510 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; | ||||
511 | session->peer_cert_digest_len = 0; | ||||
512 | } | ||||
513 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
514 | } | ||||
515 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | ||||
516 | |||||
517 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, | ||||
518 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) | ||||
519 | { | ||||
520 | ((void) ciphersuite_info); | ||||
521 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 522 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 523 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
524 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; | ||||
525 | else | ||||
526 | #endif | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 527 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 528 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
529 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; | ||||
530 | else | ||||
531 | #endif | ||||
532 | { | ||||
533 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | ||||
534 | return; | ||||
535 | } | ||||
536 | } | ||||
537 | |||||
XiaokangQian | adab9a6 | 2022-07-18 07:41:26 +0000 | [diff] [blame] | 538 | void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
539 | unsigned hs_type, | ||||
540 | size_t total_hs_len ) | ||||
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 541 | { |
542 | unsigned char hs_hdr[4]; | ||||
543 | |||||
544 | /* Build HS header for checksum update. */ | ||||
545 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); | ||||
546 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); | ||||
547 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); | ||||
548 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); | ||||
549 | |||||
550 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); | ||||
551 | } | ||||
552 | |||||
553 | void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl, | ||||
554 | unsigned hs_type, | ||||
555 | unsigned char const *msg, | ||||
556 | size_t msg_len ) | ||||
557 | { | ||||
558 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len ); | ||||
559 | ssl->handshake->update_checksum( ssl, msg, msg_len ); | ||||
560 | } | ||||
561 | |||||
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 562 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
563 | { | ||||
564 | ((void) ssl); | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 565 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 566 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
567 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); | ||||
568 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); | ||||
569 | #else | ||||
570 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); | ||||
571 | #endif | ||||
572 | #endif | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 573 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 574 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
575 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); | ||||
576 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); | ||||
577 | #else | ||||
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 578 | mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 579 | #endif |
580 | #endif | ||||
581 | } | ||||
582 | |||||
583 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, | ||||
584 | const unsigned char *buf, size_t len ) | ||||
585 | { | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 586 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 587 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
588 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); | ||||
589 | #else | ||||
590 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); | ||||
591 | #endif | ||||
592 | #endif | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 593 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 594 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
595 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); | ||||
596 | #else | ||||
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 597 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 598 | #endif |
599 | #endif | ||||
600 | } | ||||
601 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 602 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 603 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
604 | const unsigned char *buf, size_t len ) | ||||
605 | { | ||||
606 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
607 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); | ||||
608 | #else | ||||
609 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); | ||||
610 | #endif | ||||
611 | } | ||||
612 | #endif | ||||
613 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 614 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 615 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
616 | const unsigned char *buf, size_t len ) | ||||
617 | { | ||||
618 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
619 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); | ||||
620 | #else | ||||
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 621 | mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len ); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 622 | #endif |
623 | } | ||||
624 | #endif | ||||
625 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 629 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 630 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 631 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
632 | handshake->fin_sha256_psa = psa_hash_operation_init(); | ||||
633 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); | ||||
634 | #else | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 636 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 637 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 638 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 639 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 640 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 641 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
642 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); | ||||
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 643 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 644 | mbedtls_sha512_init( &handshake->fin_sha384 ); |
645 | mbedtls_sha512_starts( &handshake->fin_sha384, 1 ); | ||||
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 646 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 647 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 648 | |
649 | handshake->update_checksum = ssl_update_checksum_start; | ||||
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 651 | #if defined(MBEDTLS_DHM_C) |
652 | mbedtls_dhm_init( &handshake->dhm_ctx ); | ||||
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 653 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 654 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 656 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 657 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 658 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 659 | #if defined(MBEDTLS_SSL_CLI_C) |
660 | handshake->ecjpake_cache = NULL; | ||||
661 | handshake->ecjpake_cache_len = 0; | ||||
662 | #endif | ||||
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 663 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 664 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 665 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 666 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 667 | #endif |
668 | |||||
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 669 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
670 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; | ||||
671 | #endif | ||||
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 672 | |
673 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | ||||
674 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
675 | mbedtls_pk_init( &handshake->peer_pubkey ); | ||||
676 | #endif | ||||
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 677 | } |
678 | |||||
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 679 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 680 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 681 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 682 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 683 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
684 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; | ||||
685 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; | ||||
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 686 | #else |
687 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); | ||||
688 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); | ||||
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 689 | #endif |
690 | |||||
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 691 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 692 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
693 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; | ||||
694 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; | ||||
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 695 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 696 | mbedtls_md_init( &transform->md_ctx_enc ); |
697 | mbedtls_md_init( &transform->md_ctx_dec ); | ||||
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 698 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 699 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 700 | } |
701 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 702 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 703 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 704 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 705 | } |
706 | |||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 707 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 709 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 710 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 711 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 712 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 713 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 714 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 715 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 716 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 717 | |
718 | /* | ||||
719 | * Either the pointers are now NULL or cleared properly and can be freed. | ||||
720 | * Now allocate missing structures. | ||||
721 | */ | ||||
722 | if( ssl->transform_negotiate == NULL ) | ||||
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 723 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 724 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 725 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 726 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 727 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 728 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 729 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 730 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 731 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 732 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 733 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 734 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 735 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 736 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
737 | /* If the buffers are too small - reallocate */ | ||||
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 738 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 739 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
740 | MBEDTLS_SSL_OUT_BUFFER_LEN ); | ||||
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 741 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 742 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 743 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 744 | if( ssl->handshake == NULL || |
745 | ssl->transform_negotiate == NULL || | ||||
746 | ssl->session_negotiate == NULL ) | ||||
747 | { | ||||
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 748 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 749 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 750 | mbedtls_free( ssl->handshake ); |
751 | mbedtls_free( ssl->transform_negotiate ); | ||||
752 | mbedtls_free( ssl->session_negotiate ); | ||||
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 753 | |
754 | ssl->handshake = NULL; | ||||
755 | ssl->transform_negotiate = NULL; | ||||
756 | ssl->session_negotiate = NULL; | ||||
757 | |||||
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 758 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 759 | } |
760 | |||||
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 761 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 762 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 763 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 764 | ssl_handshake_params_init( ssl->handshake ); |
765 | |||||
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 766 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
767 | defined(MBEDTLS_SSL_SRV_C) && \ | ||||
768 | defined(MBEDTLS_SSL_SESSION_TICKETS) | ||||
769 | ssl->handshake->new_session_tickets_count = | ||||
770 | ssl->conf->new_session_tickets_count ; | ||||
771 | #endif | ||||
772 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 773 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 774 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
775 | { | ||||
776 | ssl->handshake->alt_transform_out = ssl->transform_out; | ||||
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 777 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 778 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
779 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; | ||||
780 | else | ||||
781 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | ||||
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 782 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 783 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 784 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 785 | #endif |
786 | |||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 787 | /* |
788 | * curve_list is translated to IANA TLS group identifiers here because | ||||
789 | * mbedtls_ssl_conf_curves returns void and so can't return | ||||
790 | * any error codes. | ||||
791 | */ | ||||
792 | #if defined(MBEDTLS_ECP_C) | ||||
793 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
794 | /* Heap allocate and translate curve_list from internal to IANA group ids */ | ||||
795 | if ( ssl->conf->curve_list != NULL ) | ||||
796 | { | ||||
797 | size_t length; | ||||
798 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; | ||||
799 | |||||
800 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && | ||||
801 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} | ||||
802 | |||||
803 | /* Leave room for zero termination */ | ||||
804 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); | ||||
805 | if ( group_list == NULL ) | ||||
806 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | ||||
807 | |||||
808 | for( size_t i = 0; i < length; i++ ) | ||||
809 | { | ||||
810 | const mbedtls_ecp_curve_info *info = | ||||
811 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); | ||||
812 | if ( info == NULL ) | ||||
813 | { | ||||
814 | mbedtls_free( group_list ); | ||||
815 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | ||||
816 | } | ||||
817 | group_list[i] = info->tls_id; | ||||
818 | } | ||||
819 | |||||
820 | group_list[length] = 0; | ||||
821 | |||||
822 | ssl->handshake->group_list = group_list; | ||||
823 | ssl->handshake->group_list_heap_allocated = 1; | ||||
824 | } | ||||
825 | else | ||||
826 | { | ||||
827 | ssl->handshake->group_list = ssl->conf->group_list; | ||||
828 | ssl->handshake->group_list_heap_allocated = 0; | ||||
829 | } | ||||
830 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | ||||
831 | #endif /* MBEDTLS_ECP_C */ | ||||
832 | |||||
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 833 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
834 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 835 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 836 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
837 | signature algorithms IANA identifiers. */ | ||||
838 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && | ||||
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 839 | ssl->conf->sig_hashes != NULL ) |
840 | { | ||||
841 | const int *md; | ||||
842 | const int *sig_hashes = ssl->conf->sig_hashes; | ||||
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 843 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 844 | uint16_t *p; |
845 | |||||
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 846 | #if defined(static_assert) |
847 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN | ||||
848 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), | ||||
849 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); | ||||
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 850 | #endif |
851 | |||||
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 852 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
853 | { | ||||
854 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) | ||||
855 | continue; | ||||
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 856 | #if defined(MBEDTLS_ECDSA_C) |
857 | sig_algs_len += sizeof( uint16_t ); | ||||
858 | #endif | ||||
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 859 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 860 | #if defined(MBEDTLS_RSA_C) |
861 | sig_algs_len += sizeof( uint16_t ); | ||||
862 | #endif | ||||
863 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) | ||||
864 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | ||||
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 865 | } |
866 | |||||
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 867 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 868 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
869 | |||||
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 870 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
871 | sizeof( uint16_t )); | ||||
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 872 | if( ssl->handshake->sig_algs == NULL ) |
873 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | ||||
874 | |||||
875 | p = (uint16_t *)ssl->handshake->sig_algs; | ||||
876 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) | ||||
877 | { | ||||
878 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); | ||||
879 | if( hash == MBEDTLS_SSL_HASH_NONE ) | ||||
880 | continue; | ||||
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 881 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 882 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
883 | p++; | ||||
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 884 | #endif |
885 | #if defined(MBEDTLS_RSA_C) | ||||
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 886 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
887 | p++; | ||||
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 888 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 889 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 890 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 891 | ssl->handshake->sig_algs_heap_allocated = 1; |
892 | } | ||||
893 | else | ||||
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 894 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 895 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 896 | ssl->handshake->sig_algs_heap_allocated = 0; |
897 | } | ||||
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 898 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 899 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 900 | return( 0 ); |
901 | } | ||||
902 | |||||
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 903 | #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] | 904 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 905 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 906 | static int ssl_cookie_write_dummy( void *ctx, |
907 | unsigned char **p, unsigned char *end, | ||||
908 | const unsigned char *cli_id, size_t cli_id_len ) | ||||
909 | { | ||||
910 | ((void) ctx); | ||||
911 | ((void) p); | ||||
912 | ((void) end); | ||||
913 | ((void) cli_id); | ||||
914 | ((void) cli_id_len); | ||||
915 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 916 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 917 | } |
918 | |||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 919 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 920 | static int ssl_cookie_check_dummy( void *ctx, |
921 | const unsigned char *cookie, size_t cookie_len, | ||||
922 | const unsigned char *cli_id, size_t cli_id_len ) | ||||
923 | { | ||||
924 | ((void) ctx); | ||||
925 | ((void) cookie); | ||||
926 | ((void) cookie_len); | ||||
927 | ((void) cli_id); | ||||
928 | ((void) cli_id_len); | ||||
929 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 930 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 931 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 932 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 933 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 934 | /* |
935 | * Initialize an SSL context | ||||
936 | */ | ||||
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 937 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
938 | { | ||||
939 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); | ||||
940 | } | ||||
941 | |||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 942 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 943 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
944 | { | ||||
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 945 | const mbedtls_ssl_config *conf = ssl->conf; |
946 | |||||
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 947 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 948 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
949 | { | ||||
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 950 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
951 | { | ||||
952 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); | ||||
953 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
954 | } | ||||
955 | |||||
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 956 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
957 | return( 0 ); | ||||
958 | } | ||||
959 | #endif | ||||
960 | |||||
961 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 962 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 963 | { |
964 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); | ||||
965 | return( 0 ); | ||||
966 | } | ||||
967 | #endif | ||||
968 | |||||
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 969 | #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] | 970 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 971 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 972 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
973 | { | ||||
974 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); | ||||
975 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
976 | } | ||||
977 | |||||
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 978 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
979 | { | ||||
980 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); | ||||
981 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
982 | } | ||||
983 | |||||
984 | |||||
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 985 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
986 | return( 0 ); | ||||
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 987 | } |
988 | #endif | ||||
989 | |||||
990 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); | ||||
991 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | ||||
992 | } | ||||
993 | |||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 994 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 995 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
996 | { | ||||
997 | int ret; | ||||
998 | ret = ssl_conf_version_check( ssl ); | ||||
999 | if( ret != 0 ) | ||||
1000 | return( ret ); | ||||
1001 | |||||
1002 | /* Space for further checks */ | ||||
1003 | |||||
1004 | return( 0 ); | ||||
1005 | } | ||||
1006 | |||||
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1007 | /* |
1008 | * Setup an SSL context | ||||
1009 | */ | ||||
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1010 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1011 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 1012 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1013 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1014 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1015 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
1016 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1017 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1018 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1019 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1020 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
1021 | return( ret ); | ||||
1022 | |||||
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1023 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1024 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1025 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1026 | |
1027 | /* Set to NULL in case of an error condition */ | ||||
1028 | ssl->out_buf = NULL; | ||||
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1029 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1030 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
1031 | ssl->in_buf_len = in_buf_len; | ||||
1032 | #endif | ||||
1033 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); | ||||
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1034 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1035 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1036 | 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] | 1037 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1038 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1039 | } |
1040 | |||||
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1041 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
1042 | ssl->out_buf_len = out_buf_len; | ||||
1043 | #endif | ||||
1044 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); | ||||
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1045 | if( ssl->out_buf == NULL ) |
1046 | { | ||||
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1047 | 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] | 1048 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1049 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1050 | } |
1051 | |||||
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1052 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1053 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1054 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1055 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1056 | #endif |
1057 | |||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1058 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1059 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1060 | |
1061 | return( 0 ); | ||||
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1062 | |
1063 | error: | ||||
1064 | mbedtls_free( ssl->in_buf ); | ||||
1065 | mbedtls_free( ssl->out_buf ); | ||||
1066 | |||||
1067 | ssl->conf = NULL; | ||||
1068 | |||||
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1069 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
1070 | ssl->in_buf_len = 0; | ||||
1071 | ssl->out_buf_len = 0; | ||||
1072 | #endif | ||||
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1073 | ssl->in_buf = NULL; |
1074 | ssl->out_buf = NULL; | ||||
1075 | |||||
1076 | ssl->in_hdr = NULL; | ||||
1077 | ssl->in_ctr = NULL; | ||||
1078 | ssl->in_len = NULL; | ||||
1079 | ssl->in_iv = NULL; | ||||
1080 | ssl->in_msg = NULL; | ||||
1081 | |||||
1082 | ssl->out_hdr = NULL; | ||||
1083 | ssl->out_ctr = NULL; | ||||
1084 | ssl->out_len = NULL; | ||||
1085 | ssl->out_iv = NULL; | ||||
1086 | ssl->out_msg = NULL; | ||||
1087 | |||||
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1088 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1089 | } |
1090 | |||||
1091 | /* | ||||
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1092 | * Reset an initialized and used SSL context for re-use while retaining |
1093 | * all application-set variables, function pointers and data. | ||||
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1094 | * |
1095 | * If partial is non-zero, keep data in the input buffer and client ID. | ||||
1096 | * (Use when a DTLS client reconnects from the same port.) | ||||
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1097 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1098 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
1099 | int partial ) | ||||
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1100 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1101 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
1102 | size_t in_buf_len = ssl->in_buf_len; | ||||
1103 | size_t out_buf_len = ssl->out_buf_len; | ||||
1104 | #else | ||||
1105 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | ||||
1106 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | ||||
1107 | #endif | ||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1108 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1109 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
1110 | partial = 0; | ||||
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1111 | #endif |
1112 | |||||
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1113 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1114 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1115 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1116 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
1117 | |||||
1118 | /* Reset incoming message parsing */ | ||||
1119 | ssl->in_offt = NULL; | ||||
1120 | ssl->nb_zero = 0; | ||||
1121 | ssl->in_msgtype = 0; | ||||
1122 | ssl->in_msglen = 0; | ||||
1123 | ssl->in_hslen = 0; | ||||
1124 | ssl->keep_current_message = 0; | ||||
1125 | ssl->transform_in = NULL; | ||||
1126 | |||||
1127 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
1128 | ssl->next_record_offset = 0; | ||||
1129 | ssl->in_epoch = 0; | ||||
1130 | #endif | ||||
1131 | |||||
1132 | /* Keep current datagram if partial == 1 */ | ||||
1133 | if( partial == 0 ) | ||||
1134 | { | ||||
1135 | ssl->in_left = 0; | ||||
1136 | memset( ssl->in_buf, 0, in_buf_len ); | ||||
1137 | } | ||||
1138 | |||||
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1139 | ssl->send_alert = 0; |
1140 | |||||
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1141 | /* Reset outgoing message writing */ |
1142 | ssl->out_msgtype = 0; | ||||
1143 | ssl->out_msglen = 0; | ||||
1144 | ssl->out_left = 0; | ||||
1145 | memset( ssl->out_buf, 0, out_buf_len ); | ||||
1146 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); | ||||
1147 | ssl->transform_out = NULL; | ||||
1148 | |||||
1149 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | ||||
1150 | mbedtls_ssl_dtls_replay_reset( ssl ); | ||||
1151 | #endif | ||||
1152 | |||||
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1153 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1154 | if( ssl->transform ) |
1155 | { | ||||
1156 | mbedtls_ssl_transform_free( ssl->transform ); | ||||
1157 | mbedtls_free( ssl->transform ); | ||||
1158 | ssl->transform = NULL; | ||||
1159 | } | ||||
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1160 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
1161 | |||||
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1162 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
1163 | mbedtls_ssl_transform_free( ssl->transform_application ); | ||||
1164 | mbedtls_free( ssl->transform_application ); | ||||
1165 | ssl->transform_application = NULL; | ||||
1166 | |||||
1167 | if( ssl->handshake != NULL ) | ||||
1168 | { | ||||
1169 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); | ||||
1170 | mbedtls_free( ssl->handshake->transform_earlydata ); | ||||
1171 | ssl->handshake->transform_earlydata = NULL; | ||||
1172 | |||||
1173 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); | ||||
1174 | mbedtls_free( ssl->handshake->transform_handshake ); | ||||
1175 | ssl->handshake->transform_handshake = NULL; | ||||
1176 | } | ||||
1177 | |||||
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1178 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1179 | } |
1180 | |||||
1181 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) | ||||
1182 | { | ||||
1183 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
1184 | |||||
1185 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | ||||
1186 | |||||
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1187 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1188 | |
1189 | /* Reset renegotiation state */ | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1190 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
1191 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; | ||||
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1192 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1193 | |
1194 | ssl->verify_data_len = 0; | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1195 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
1196 | 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] | 1197 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1199 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1200 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1201 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1202 | if( ssl->session ) |
1203 | { | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1204 | mbedtls_ssl_session_free( ssl->session ); |
1205 | mbedtls_free( ssl->session ); | ||||
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1206 | ssl->session = NULL; |
1207 | } | ||||
1208 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1209 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1210 | ssl->alpn_chosen = NULL; |
1211 | #endif | ||||
1212 | |||||
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1213 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1214 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1215 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1216 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1217 | { |
1218 | mbedtls_free( ssl->cli_id ); | ||||
1219 | ssl->cli_id = NULL; | ||||
1220 | ssl->cli_id_len = 0; | ||||
1221 | } | ||||
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1222 | #endif |
1223 | |||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1224 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
1225 | return( ret ); | ||||
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1226 | |
1227 | return( 0 ); | ||||
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1228 | } |
1229 | |||||
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1230 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1231 | * Reset an initialized and used SSL context for re-use while retaining |
1232 | * all application-set variables, function pointers and data. | ||||
1233 | */ | ||||
1234 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) | ||||
1235 | { | ||||
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1236 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1237 | } |
1238 | |||||
1239 | /* | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1240 | * SSL set accessors |
1241 | */ | ||||
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1242 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1243 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1244 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1245 | } |
1246 | |||||
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1247 | 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] | 1248 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1249 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1250 | } |
1251 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1252 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1253 | 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] | 1254 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1255 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1256 | } |
1257 | #endif | ||||
1258 | |||||
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1259 | 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] | 1260 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1261 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1262 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1264 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1265 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1266 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
1267 | unsigned allow_packing ) | ||||
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1268 | { |
1269 | ssl->disable_datagram_packing = !allow_packing; | ||||
1270 | } | ||||
1271 | |||||
1272 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, | ||||
1273 | uint32_t min, uint32_t max ) | ||||
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1274 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1275 | conf->hs_timeout_min = min; |
1276 | conf->hs_timeout_max = max; | ||||
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1277 | } |
1278 | #endif | ||||
1279 | |||||
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1280 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1281 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1282 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1283 | } |
1284 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1285 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1286 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1287 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1288 | void *p_vrfy ) |
1289 | { | ||||
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1290 | conf->f_vrfy = f_vrfy; |
1291 | conf->p_vrfy = p_vrfy; | ||||
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1292 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1293 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1294 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1295 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1296 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1297 | void *p_rng ) |
1298 | { | ||||
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1299 | conf->f_rng = f_rng; |
1300 | conf->p_rng = p_rng; | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1301 | } |
1302 | |||||
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1303 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1304 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1305 | void *p_dbg ) |
1306 | { | ||||
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1307 | conf->f_dbg = f_dbg; |
1308 | conf->p_dbg = p_dbg; | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1309 | } |
1310 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1311 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1312 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1313 | mbedtls_ssl_send_t *f_send, |
1314 | mbedtls_ssl_recv_t *f_recv, | ||||
1315 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) | ||||
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1316 | { |
1317 | ssl->p_bio = p_bio; | ||||
1318 | ssl->f_send = f_send; | ||||
1319 | ssl->f_recv = f_recv; | ||||
1320 | ssl->f_recv_timeout = f_recv_timeout; | ||||
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1321 | } |
1322 | |||||
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1323 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
1324 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) | ||||
1325 | { | ||||
1326 | ssl->mtu = mtu; | ||||
1327 | } | ||||
1328 | #endif | ||||
1329 | |||||
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1330 | 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] | 1331 | { |
1332 | conf->read_timeout = timeout; | ||||
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1333 | } |
1334 | |||||
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1335 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
1336 | void *p_timer, | ||||
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1337 | mbedtls_ssl_set_timer_t *f_set_timer, |
1338 | mbedtls_ssl_get_timer_t *f_get_timer ) | ||||
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1339 | { |
1340 | ssl->p_timer = p_timer; | ||||
1341 | ssl->f_set_timer = f_set_timer; | ||||
1342 | ssl->f_get_timer = f_get_timer; | ||||
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1343 | |
1344 | /* Make sure we start with no timer running */ | ||||
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1345 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1346 | } |
1347 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1348 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1349 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1350 | void *p_cache, |
1351 | mbedtls_ssl_cache_get_t *f_get_cache, | ||||
1352 | mbedtls_ssl_cache_set_t *f_set_cache ) | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1353 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1354 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1355 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1356 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1357 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1358 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1360 | #if defined(MBEDTLS_SSL_CLI_C) |
1361 | 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] | 1362 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1363 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1364 | |
1365 | if( ssl == NULL || | ||||
1366 | session == NULL || | ||||
1367 | ssl->session_negotiate == NULL || | ||||
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1368 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1369 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1370 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1371 | } |
1372 | |||||
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1373 | if( ssl->handshake->resume == 1 ) |
1374 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
1375 | |||||
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1376 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
1377 | session ) ) != 0 ) | ||||
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1378 | return( ret ); |
1379 | |||||
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1380 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1381 | |
1382 | return( 0 ); | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1383 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1384 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1385 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1386 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
1387 | const int *ciphersuites ) | ||||
1388 | { | ||||
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1389 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1390 | } |
1391 | |||||
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1392 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1393 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1394 | const int kex_modes ) |
1395 | { | ||||
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1396 | 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] | 1397 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1398 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1399 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1400 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1401 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1402 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1403 | { |
1404 | conf->cert_profile = profile; | ||||
1405 | } | ||||
1406 | |||||
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1407 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
1408 | { | ||||
1409 | mbedtls_ssl_key_cert *cur = key_cert, *next; | ||||
1410 | |||||
1411 | while( cur != NULL ) | ||||
1412 | { | ||||
1413 | next = cur->next; | ||||
1414 | mbedtls_free( cur ); | ||||
1415 | cur = next; | ||||
1416 | } | ||||
1417 | } | ||||
1418 | |||||
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1419 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1420 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1421 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
1422 | mbedtls_x509_crt *cert, | ||||
1423 | mbedtls_pk_context *key ) | ||||
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1424 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1425 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1426 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1427 | if( cert == NULL ) |
1428 | { | ||||
1429 | /* Free list if cert is null */ | ||||
1430 | ssl_key_cert_free( *head ); | ||||
1431 | *head = NULL; | ||||
1432 | return( 0 ); | ||||
1433 | } | ||||
1434 | |||||
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1435 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
1436 | if( new_cert == NULL ) | ||||
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1437 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1438 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1439 | new_cert->cert = cert; |
1440 | new_cert->key = key; | ||||
1441 | new_cert->next = NULL; | ||||
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1442 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1443 | /* 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] | 1444 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1445 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1446 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1447 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1448 | else |
1449 | { | ||||
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1450 | mbedtls_ssl_key_cert *cur = *head; |
1451 | while( cur->next != NULL ) | ||||
1452 | cur = cur->next; | ||||
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1453 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1454 | } |
1455 | |||||
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1456 | return( 0 ); |
1457 | } | ||||
1458 | |||||
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1459 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1460 | mbedtls_x509_crt *own_cert, |
1461 | mbedtls_pk_context *pk_key ) | ||||
1462 | { | ||||
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1463 | 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] | 1464 | } |
1465 | |||||
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1466 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1467 | mbedtls_x509_crt *ca_chain, |
1468 | mbedtls_x509_crl *ca_crl ) | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1469 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1470 | conf->ca_chain = ca_chain; |
1471 | conf->ca_crl = ca_crl; | ||||
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1472 | |
1473 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | ||||
1474 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() | ||||
1475 | * cannot be used together. */ | ||||
1476 | conf->f_ca_cb = NULL; | ||||
1477 | conf->p_ca_cb = NULL; | ||||
1478 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1479 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1480 | |
1481 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | ||||
1482 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, | ||||
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1483 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1484 | void *p_ca_cb ) |
1485 | { | ||||
1486 | conf->f_ca_cb = f_ca_cb; | ||||
1487 | conf->p_ca_cb = p_ca_cb; | ||||
1488 | |||||
1489 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() | ||||
1490 | * cannot be used together. */ | ||||
1491 | conf->ca_chain = NULL; | ||||
1492 | conf->ca_crl = NULL; | ||||
1493 | } | ||||
1494 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1495 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1496 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1497 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1498 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
1499 | size_t *name_len ) | ||||
1500 | { | ||||
1501 | *name_len = ssl->handshake->sni_name_len; | ||||
1502 | return( ssl->handshake->sni_name ); | ||||
1503 | } | ||||
1504 | |||||
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1505 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
1506 | mbedtls_x509_crt *own_cert, | ||||
1507 | mbedtls_pk_context *pk_key ) | ||||
1508 | { | ||||
1509 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, | ||||
1510 | own_cert, pk_key ) ); | ||||
1511 | } | ||||
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1512 | |
1513 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, | ||||
1514 | mbedtls_x509_crt *ca_chain, | ||||
1515 | mbedtls_x509_crl *ca_crl ) | ||||
1516 | { | ||||
1517 | ssl->handshake->sni_ca_chain = ca_chain; | ||||
1518 | ssl->handshake->sni_ca_crl = ca_crl; | ||||
1519 | } | ||||
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1520 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1521 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
1522 | void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl, | ||||
1523 | const mbedtls_x509_crt *crt) | ||||
1524 | { | ||||
1525 | ssl->handshake->dn_hints = crt; | ||||
1526 | } | ||||
1527 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | ||||
1528 | |||||
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1529 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
1530 | int authmode ) | ||||
1531 | { | ||||
1532 | ssl->handshake->sni_authmode = authmode; | ||||
1533 | } | ||||
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1534 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
1535 | |||||
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1536 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
1537 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, | ||||
1538 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | ||||
1539 | void *p_vrfy ) | ||||
1540 | { | ||||
1541 | ssl->f_vrfy = f_vrfy; | ||||
1542 | ssl->p_vrfy = p_vrfy; | ||||
1543 | } | ||||
1544 | #endif | ||||
1545 | |||||
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1546 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1547 | /* |
1548 | * Set EC J-PAKE password for current handshake | ||||
1549 | */ | ||||
1550 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, | ||||
1551 | const unsigned char *pw, | ||||
1552 | size_t pw_len ) | ||||
1553 | { | ||||
1554 | mbedtls_ecjpake_role role; | ||||
1555 | |||||
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1556 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1557 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
1558 | |||||
1559 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | ||||
1560 | role = MBEDTLS_ECJPAKE_SERVER; | ||||
1561 | else | ||||
1562 | role = MBEDTLS_ECJPAKE_CLIENT; | ||||
1563 | |||||
1564 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, | ||||
1565 | role, | ||||
1566 | MBEDTLS_MD_SHA256, | ||||
1567 | MBEDTLS_ECP_DP_SECP256R1, | ||||
1568 | pw, pw_len ) ); | ||||
1569 | } | ||||
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1570 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1571 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1572 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1573 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1574 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1575 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
1576 | { | ||||
1577 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
1578 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) | ||||
1579 | return( 1 ); | ||||
1580 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1581 | if( conf->psk != NULL ) |
1582 | return( 1 ); | ||||
1583 | |||||
1584 | return( 0 ); | ||||
1585 | } | ||||
1586 | |||||
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1587 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
1588 | { | ||||
1589 | /* Remove reference to existing PSK, if any. */ | ||||
1590 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1591 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1592 | { |
1593 | /* The maintenance of the PSK key slot is the | ||||
1594 | * user's responsibility. */ | ||||
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1595 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1596 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1597 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
1598 | if( conf->psk != NULL ) | ||||
1599 | { | ||||
1600 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | ||||
1601 | |||||
1602 | mbedtls_free( conf->psk ); | ||||
1603 | conf->psk = NULL; | ||||
1604 | conf->psk_len = 0; | ||||
1605 | } | ||||
1606 | |||||
1607 | /* Remove reference to PSK identity, if any. */ | ||||
1608 | if( conf->psk_identity != NULL ) | ||||
1609 | { | ||||
1610 | mbedtls_free( conf->psk_identity ); | ||||
1611 | conf->psk_identity = NULL; | ||||
1612 | conf->psk_identity_len = 0; | ||||
1613 | } | ||||
1614 | } | ||||
1615 | |||||
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1616 | /* This function assumes that PSK identity in the SSL config is unset. |
1617 | * It checks that the provided identity is well-formed and attempts | ||||
1618 | * to make a copy of it in the SSL config. | ||||
1619 | * On failure, the PSK identity in the config remains unset. */ | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1620 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1621 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
1622 | unsigned char const *psk_identity, | ||||
1623 | size_t psk_identity_len ) | ||||
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1624 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1625 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1626 | if( psk_identity == NULL || |
1627 | ( psk_identity_len >> 16 ) != 0 || | ||||
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1628 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1629 | { |
1630 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
1631 | } | ||||
1632 | |||||
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1633 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
1634 | if( conf->psk_identity == NULL ) | ||||
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1635 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1636 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1637 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1638 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1639 | |
1640 | return( 0 ); | ||||
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1641 | } |
1642 | |||||
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1643 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
1644 | const unsigned char *psk, size_t psk_len, | ||||
1645 | const unsigned char *psk_identity, size_t psk_identity_len ) | ||||
1646 | { | ||||
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1647 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1648 | |
1649 | /* We currently only support one PSK, raw or opaque. */ | ||||
1650 | if( ssl_conf_psk_is_configured( conf ) ) | ||||
1651 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1652 | |
1653 | /* Check and set raw PSK */ | ||||
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1654 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1655 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1656 | if( psk_len == 0 ) |
1657 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
1658 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | ||||
1659 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
1660 | |||||
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1661 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
1662 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | ||||
1663 | conf->psk_len = psk_len; | ||||
1664 | memcpy( conf->psk, psk, conf->psk_len ); | ||||
1665 | |||||
1666 | /* Check and set PSK Identity */ | ||||
1667 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); | ||||
1668 | if( ret != 0 ) | ||||
1669 | ssl_conf_remove_psk( conf ); | ||||
1670 | |||||
1671 | return( ret ); | ||||
1672 | } | ||||
1673 | |||||
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1674 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
1675 | { | ||||
1676 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1677 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1678 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1679 | /* The maintenance of the external PSK key slot is the |
1680 | * user's responsibility. */ | ||||
1681 | if( ssl->handshake->psk_opaque_is_internal ) | ||||
1682 | { | ||||
1683 | psa_destroy_key( ssl->handshake->psk_opaque ); | ||||
1684 | ssl->handshake->psk_opaque_is_internal = 0; | ||||
1685 | } | ||||
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1686 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1687 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1688 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1689 | if( ssl->handshake->psk != NULL ) |
1690 | { | ||||
1691 | mbedtls_platform_zeroize( ssl->handshake->psk, | ||||
1692 | ssl->handshake->psk_len ); | ||||
1693 | mbedtls_free( ssl->handshake->psk ); | ||||
1694 | ssl->handshake->psk_len = 0; | ||||
1695 | } | ||||
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1696 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1697 | } |
1698 | |||||
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1699 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
1700 | const unsigned char *psk, size_t psk_len ) | ||||
1701 | { | ||||
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1702 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
1703 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); | ||||
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1704 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 1705 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 1706 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1707 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
1708 | |||||
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1709 | if( psk == NULL || ssl->handshake == NULL ) |
1710 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
1711 | |||||
1712 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | ||||
1713 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
1714 | |||||
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1715 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1716 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1717 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1718 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
1719 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) | ||||
1720 | { | ||||
Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 1721 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1722 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 ); |
1723 | else | ||||
1724 | alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 ); | ||||
1725 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); | ||||
1726 | } | ||||
1727 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1728 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 1729 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 1730 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
1731 | { | ||||
1732 | alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH ); | ||||
1733 | psa_set_key_usage_flags( &key_attributes, | ||||
1734 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT ); | ||||
1735 | } | ||||
1736 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | ||||
1737 | |||||
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1738 | psa_set_key_algorithm( &key_attributes, alg ); |
1739 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); | ||||
1740 | |||||
1741 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); | ||||
1742 | if( status != PSA_SUCCESS ) | ||||
1743 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
1744 | |||||
1745 | /* Allow calling psa_destroy_key() on psk remove */ | ||||
1746 | ssl->handshake->psk_opaque_is_internal = 1; | ||||
1747 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); | ||||
1748 | #else | ||||
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1749 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1750 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1751 | |
1752 | ssl->handshake->psk_len = psk_len; | ||||
1753 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); | ||||
1754 | |||||
1755 | return( 0 ); | ||||
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1756 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1757 | } |
1758 | |||||
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1759 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
1760 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, | ||||
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1761 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1762 | const unsigned char *psk_identity, |
1763 | size_t psk_identity_len ) | ||||
1764 | { | ||||
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1765 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1766 | |
1767 | /* We currently only support one PSK, raw or opaque. */ | ||||
1768 | if( ssl_conf_psk_is_configured( conf ) ) | ||||
1769 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1770 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1771 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1772 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1773 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1774 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1775 | |
1776 | /* Check and set PSK Identity */ | ||||
1777 | ret = ssl_conf_set_psk_identity( conf, psk_identity, | ||||
1778 | psk_identity_len ); | ||||
1779 | if( ret != 0 ) | ||||
1780 | ssl_conf_remove_psk( conf ); | ||||
1781 | |||||
1782 | return( ret ); | ||||
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1783 | } |
1784 | |||||
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1785 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
1786 | mbedtls_svc_key_id_t psk ) | ||||
1787 | { | ||||
1788 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || | ||||
1789 | ( ssl->handshake == NULL ) ) | ||||
1790 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
1791 | |||||
1792 | ssl_remove_psk( ssl ); | ||||
1793 | ssl->handshake->psk_opaque = psk; | ||||
1794 | return( 0 ); | ||||
1795 | } | ||||
1796 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
1797 | |||||
1798 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, | ||||
1799 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, | ||||
1800 | size_t), | ||||
1801 | void *p_psk ) | ||||
1802 | { | ||||
1803 | conf->f_psk = f_psk; | ||||
1804 | conf->p_psk = p_psk; | ||||
1805 | } | ||||
1806 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | ||||
1807 | |||||
1808 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1809 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
1810 | psa_algorithm_t alg ) | ||||
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1811 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1812 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1813 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1814 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1815 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1816 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1817 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1818 | return( MBEDTLS_SSL_MODE_STREAM ); |
1819 | } | ||||
1820 | |||||
1821 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
1822 | |||||
1823 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( | ||||
1824 | mbedtls_cipher_mode_t mode ) | ||||
1825 | { | ||||
1826 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | ||||
1827 | if( mode == MBEDTLS_MODE_CBC ) | ||||
1828 | return( MBEDTLS_SSL_MODE_CBC ); | ||||
1829 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | ||||
1830 | |||||
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1831 | #if defined(MBEDTLS_GCM_C) || \ |
1832 | defined(MBEDTLS_CCM_C) || \ | ||||
1833 | defined(MBEDTLS_CHACHAPOLY_C) | ||||
1834 | if( mode == MBEDTLS_MODE_GCM || | ||||
1835 | mode == MBEDTLS_MODE_CCM || | ||||
1836 | mode == MBEDTLS_MODE_CHACHAPOLY ) | ||||
1837 | return( MBEDTLS_SSL_MODE_AEAD ); | ||||
1838 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ | ||||
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1839 | |
1840 | return( MBEDTLS_SSL_MODE_STREAM ); | ||||
1841 | } | ||||
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1842 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1843 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1844 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
1845 | mbedtls_ssl_mode_t base_mode, | ||||
1846 | int encrypt_then_mac ) | ||||
1847 | { | ||||
1848 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | ||||
1849 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && | ||||
1850 | base_mode == MBEDTLS_SSL_MODE_CBC ) | ||||
1851 | { | ||||
1852 | return( MBEDTLS_SSL_MODE_CBC_ETM ); | ||||
1853 | } | ||||
1854 | #else | ||||
1855 | (void) encrypt_then_mac; | ||||
1856 | #endif | ||||
1857 | return( base_mode ); | ||||
1858 | } | ||||
1859 | |||||
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1860 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1861 | const mbedtls_ssl_transform *transform ) |
1862 | { | ||||
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1863 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1864 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1865 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1866 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1867 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
1868 | #endif | ||||
1869 | ); | ||||
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1870 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1871 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1872 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1873 | encrypt_then_mac = transform->encrypt_then_mac; |
1874 | #endif | ||||
1875 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); | ||||
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1876 | } |
1877 | |||||
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1878 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1879 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1880 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1881 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1882 | const mbedtls_ssl_ciphersuite_t *suite ) |
1883 | { | ||||
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1884 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
1885 | |||||
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1886 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
1887 | psa_status_t status; | ||||
1888 | psa_algorithm_t alg; | ||||
1889 | psa_key_type_t type; | ||||
1890 | size_t size; | ||||
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1891 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
1892 | if( status == PSA_SUCCESS ) | ||||
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1893 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1894 | #else |
1895 | const mbedtls_cipher_info_t *cipher = | ||||
1896 | mbedtls_cipher_info_from_type( suite->cipher ); | ||||
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1897 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1898 | { |
1899 | base_mode = | ||||
1900 | mbedtls_ssl_get_base_mode( | ||||
1901 | mbedtls_cipher_info_get_mode( cipher ) ); | ||||
1902 | } | ||||
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1903 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
1904 | |||||
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1905 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
1906 | int encrypt_then_mac = 0; | ||||
1907 | #endif | ||||
1908 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); | ||||
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1909 | } |
1910 | |||||
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 1911 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 1912 | |
1913 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | ||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1914 | /* Serialization of TLS 1.3 sessions: |
1915 | * | ||||
1916 | * struct { | ||||
1917 | * uint64 ticket_received; | ||||
1918 | * uint32 ticket_lifetime; | ||||
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1919 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1920 | * } ClientOnlyData; |
1921 | * | ||||
1922 | * struct { | ||||
1923 | * uint8 endpoint; | ||||
1924 | * uint8 ciphersuite[2]; | ||||
1925 | * uint32 ticket_age_add; | ||||
1926 | * uint8 ticket_flags; | ||||
1927 | * opaque resumption_key<0..255>; | ||||
1928 | * select ( endpoint ) { | ||||
1929 | * case client: ClientOnlyData; | ||||
1930 | * case server: uint64 start_time; | ||||
1931 | * }; | ||||
1932 | * } serialized_session_tls13; | ||||
1933 | * | ||||
1934 | */ | ||||
1935 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | ||||
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1936 | MBEDTLS_CHECK_RETURN_CRITICAL |
1937 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, | ||||
1938 | unsigned char *buf, | ||||
1939 | size_t buf_len, | ||||
1940 | size_t *olen ) | ||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1941 | { |
1942 | unsigned char *p = buf; | ||||
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1943 | size_t needed = 1 /* endpoint */ |
1944 | + 2 /* ciphersuite */ | ||||
1945 | + 4 /* ticket_age_add */ | ||||
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1946 | + 1 /* ticket_flags */ |
1947 | + 1; /* resumption_key length */ | ||||
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1948 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1949 | |
1950 | if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN ) | ||||
1951 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
1952 | needed += session->resumption_key_len; /* resumption_key */ | ||||
1953 | |||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1954 | #if defined(MBEDTLS_HAVE_TIME) |
1955 | needed += 8; /* start_time or ticket_received */ | ||||
1956 | #endif | ||||
1957 | |||||
1958 | #if defined(MBEDTLS_SSL_CLI_C) | ||||
1959 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) | ||||
1960 | { | ||||
1961 | needed += 4 /* ticket_lifetime */ | ||||
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1962 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1963 | |
1964 | /* Check size_t overflow */ | ||||
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1965 | if( session->ticket_len > SIZE_MAX - needed ) |
1966 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 1967 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 1968 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1969 | } |
1970 | #endif /* MBEDTLS_SSL_CLI_C */ | ||||
1971 | |||||
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1972 | *olen = needed; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1973 | if( needed > buf_len ) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 1974 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1975 | |
1976 | p[0] = session->endpoint; | ||||
1977 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 ); | ||||
1978 | MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 ); | ||||
1979 | p[7] = session->ticket_flags; | ||||
1980 | |||||
1981 | /* save resumption_key */ | ||||
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1982 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1983 | p += 9; |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1984 | memcpy( p, session->resumption_key, session->resumption_key_len ); |
1985 | p += session->resumption_key_len; | ||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 1986 | |
1987 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) | ||||
1988 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) | ||||
1989 | { | ||||
1990 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 ); | ||||
1991 | p += 8; | ||||
1992 | } | ||||
1993 | #endif /* MBEDTLS_HAVE_TIME */ | ||||
1994 | |||||
1995 | #if defined(MBEDTLS_SSL_CLI_C) | ||||
1996 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) | ||||
1997 | { | ||||
1998 | #if defined(MBEDTLS_HAVE_TIME) | ||||
1999 | MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 ); | ||||
2000 | p += 8; | ||||
2001 | #endif | ||||
2002 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); | ||||
2003 | p += 4; | ||||
2004 | |||||
2005 | MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 ); | ||||
2006 | p += 2; | ||||
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2007 | |
2008 | if( session->ticket != NULL && session->ticket_len > 0 ) | ||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2009 | { |
2010 | memcpy( p, session->ticket, session->ticket_len ); | ||||
2011 | p += session->ticket_len; | ||||
2012 | } | ||||
2013 | } | ||||
2014 | #endif /* MBEDTLS_SSL_CLI_C */ | ||||
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2015 | return( 0 ); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2016 | } |
2017 | |||||
2018 | MBEDTLS_CHECK_RETURN_CRITICAL | ||||
2019 | static int ssl_tls13_session_load( mbedtls_ssl_session *session, | ||||
2020 | const unsigned char *buf, | ||||
2021 | size_t len ) | ||||
2022 | { | ||||
2023 | const unsigned char *p = buf; | ||||
2024 | const unsigned char *end = buf + len; | ||||
2025 | |||||
2026 | if( end - p < 9 ) | ||||
2027 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
2028 | session->endpoint = p[0]; | ||||
2029 | session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 ); | ||||
2030 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 ); | ||||
2031 | session->ticket_flags = p[7]; | ||||
2032 | |||||
2033 | /* load resumption_key */ | ||||
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2034 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2035 | p += 9; |
2036 | |||||
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2037 | if( end - p < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2038 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
2039 | |||||
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2040 | if( sizeof( session->resumption_key ) < session->resumption_key_len ) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2041 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2042 | memcpy( session->resumption_key, p, session->resumption_key_len ); |
2043 | p += session->resumption_key_len; | ||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2044 | |
2045 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) | ||||
2046 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) | ||||
2047 | { | ||||
2048 | if( end - p < 8 ) | ||||
2049 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
2050 | session->start = MBEDTLS_GET_UINT64_BE( p, 0 ); | ||||
2051 | p += 8; | ||||
2052 | } | ||||
2053 | #endif /* MBEDTLS_HAVE_TIME */ | ||||
2054 | |||||
2055 | #if defined(MBEDTLS_SSL_CLI_C) | ||||
2056 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) | ||||
2057 | { | ||||
2058 | #if defined(MBEDTLS_HAVE_TIME) | ||||
2059 | if( end - p < 8 ) | ||||
2060 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
2061 | session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 ); | ||||
2062 | p += 8; | ||||
2063 | #endif | ||||
2064 | if( end - p < 4 ) | ||||
2065 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
2066 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 ); | ||||
2067 | p += 4; | ||||
2068 | |||||
2069 | if( end - p < 2 ) | ||||
2070 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
2071 | session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 ); | ||||
2072 | p += 2; | ||||
2073 | |||||
2074 | if( end - p < ( long int )session->ticket_len ) | ||||
2075 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
2076 | if( session->ticket_len > 0 ) | ||||
2077 | { | ||||
2078 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); | ||||
2079 | if( session->ticket == NULL ) | ||||
2080 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | ||||
2081 | memcpy( session->ticket, p, session->ticket_len ); | ||||
2082 | p += session->ticket_len; | ||||
2083 | } | ||||
2084 | } | ||||
2085 | #endif /* MBEDTLS_SSL_CLI_C */ | ||||
2086 | |||||
2087 | return( 0 ); | ||||
2088 | |||||
2089 | } | ||||
2090 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ | ||||
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2091 | MBEDTLS_CHECK_RETURN_CRITICAL |
2092 | static int ssl_tls13_session_save( const mbedtls_ssl_session *session, | ||||
2093 | unsigned char *buf, | ||||
2094 | size_t buf_len, | ||||
2095 | size_t *olen ) | ||||
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2096 | { |
2097 | ((void) session); | ||||
2098 | ((void) buf); | ||||
2099 | ((void) buf_len); | ||||
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2100 | *olen = 0; |
2101 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2102 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2103 | |
2104 | static int ssl_tls13_session_load( const mbedtls_ssl_session *session, | ||||
2105 | unsigned char *buf, | ||||
2106 | size_t buf_len ) | ||||
2107 | { | ||||
2108 | ((void) session); | ||||
2109 | ((void) buf); | ||||
2110 | ((void) buf_len); | ||||
2111 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
2112 | } | ||||
2113 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ | ||||
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2114 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
2115 | |||||
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 2116 | 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] | 2117 | size_t taglen, |
2118 | psa_algorithm_t *alg, | ||||
2119 | psa_key_type_t *key_type, | ||||
2120 | size_t *key_size ) | ||||
2121 | { | ||||
2122 | switch ( mbedtls_cipher_type ) | ||||
2123 | { | ||||
2124 | case MBEDTLS_CIPHER_AES_128_CBC: | ||||
2125 | *alg = PSA_ALG_CBC_NO_PADDING; | ||||
2126 | *key_type = PSA_KEY_TYPE_AES; | ||||
2127 | *key_size = 128; | ||||
2128 | break; | ||||
2129 | case MBEDTLS_CIPHER_AES_128_CCM: | ||||
2130 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | ||||
2131 | *key_type = PSA_KEY_TYPE_AES; | ||||
2132 | *key_size = 128; | ||||
2133 | break; | ||||
2134 | case MBEDTLS_CIPHER_AES_128_GCM: | ||||
2135 | *alg = PSA_ALG_GCM; | ||||
2136 | *key_type = PSA_KEY_TYPE_AES; | ||||
2137 | *key_size = 128; | ||||
2138 | break; | ||||
2139 | case MBEDTLS_CIPHER_AES_192_CCM: | ||||
2140 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | ||||
2141 | *key_type = PSA_KEY_TYPE_AES; | ||||
2142 | *key_size = 192; | ||||
2143 | break; | ||||
2144 | case MBEDTLS_CIPHER_AES_192_GCM: | ||||
2145 | *alg = PSA_ALG_GCM; | ||||
2146 | *key_type = PSA_KEY_TYPE_AES; | ||||
2147 | *key_size = 192; | ||||
2148 | break; | ||||
2149 | case MBEDTLS_CIPHER_AES_256_CBC: | ||||
2150 | *alg = PSA_ALG_CBC_NO_PADDING; | ||||
2151 | *key_type = PSA_KEY_TYPE_AES; | ||||
2152 | *key_size = 256; | ||||
2153 | break; | ||||
2154 | case MBEDTLS_CIPHER_AES_256_CCM: | ||||
2155 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | ||||
2156 | *key_type = PSA_KEY_TYPE_AES; | ||||
2157 | *key_size = 256; | ||||
2158 | break; | ||||
2159 | case MBEDTLS_CIPHER_AES_256_GCM: | ||||
2160 | *alg = PSA_ALG_GCM; | ||||
2161 | *key_type = PSA_KEY_TYPE_AES; | ||||
2162 | *key_size = 256; | ||||
2163 | break; | ||||
2164 | case MBEDTLS_CIPHER_ARIA_128_CBC: | ||||
2165 | *alg = PSA_ALG_CBC_NO_PADDING; | ||||
2166 | *key_type = PSA_KEY_TYPE_ARIA; | ||||
2167 | *key_size = 128; | ||||
2168 | break; | ||||
2169 | case MBEDTLS_CIPHER_ARIA_128_CCM: | ||||
2170 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | ||||
2171 | *key_type = PSA_KEY_TYPE_ARIA; | ||||
2172 | *key_size = 128; | ||||
2173 | break; | ||||
2174 | case MBEDTLS_CIPHER_ARIA_128_GCM: | ||||
2175 | *alg = PSA_ALG_GCM; | ||||
2176 | *key_type = PSA_KEY_TYPE_ARIA; | ||||
2177 | *key_size = 128; | ||||
2178 | break; | ||||
2179 | case MBEDTLS_CIPHER_ARIA_192_CCM: | ||||
2180 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | ||||
2181 | *key_type = PSA_KEY_TYPE_ARIA; | ||||
2182 | *key_size = 192; | ||||
2183 | break; | ||||
2184 | case MBEDTLS_CIPHER_ARIA_192_GCM: | ||||
2185 | *alg = PSA_ALG_GCM; | ||||
2186 | *key_type = PSA_KEY_TYPE_ARIA; | ||||
2187 | *key_size = 192; | ||||
2188 | break; | ||||
2189 | case MBEDTLS_CIPHER_ARIA_256_CBC: | ||||
2190 | *alg = PSA_ALG_CBC_NO_PADDING; | ||||
2191 | *key_type = PSA_KEY_TYPE_ARIA; | ||||
2192 | *key_size = 256; | ||||
2193 | break; | ||||
2194 | case MBEDTLS_CIPHER_ARIA_256_CCM: | ||||
2195 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | ||||
2196 | *key_type = PSA_KEY_TYPE_ARIA; | ||||
2197 | *key_size = 256; | ||||
2198 | break; | ||||
2199 | case MBEDTLS_CIPHER_ARIA_256_GCM: | ||||
2200 | *alg = PSA_ALG_GCM; | ||||
2201 | *key_type = PSA_KEY_TYPE_ARIA; | ||||
2202 | *key_size = 256; | ||||
2203 | break; | ||||
2204 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: | ||||
2205 | *alg = PSA_ALG_CBC_NO_PADDING; | ||||
2206 | *key_type = PSA_KEY_TYPE_CAMELLIA; | ||||
2207 | *key_size = 128; | ||||
2208 | break; | ||||
2209 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: | ||||
2210 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | ||||
2211 | *key_type = PSA_KEY_TYPE_CAMELLIA; | ||||
2212 | *key_size = 128; | ||||
2213 | break; | ||||
2214 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: | ||||
2215 | *alg = PSA_ALG_GCM; | ||||
2216 | *key_type = PSA_KEY_TYPE_CAMELLIA; | ||||
2217 | *key_size = 128; | ||||
2218 | break; | ||||
2219 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: | ||||
2220 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | ||||
2221 | *key_type = PSA_KEY_TYPE_CAMELLIA; | ||||
2222 | *key_size = 192; | ||||
2223 | break; | ||||
2224 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: | ||||
2225 | *alg = PSA_ALG_GCM; | ||||
2226 | *key_type = PSA_KEY_TYPE_CAMELLIA; | ||||
2227 | *key_size = 192; | ||||
2228 | break; | ||||
2229 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: | ||||
2230 | *alg = PSA_ALG_CBC_NO_PADDING; | ||||
2231 | *key_type = PSA_KEY_TYPE_CAMELLIA; | ||||
2232 | *key_size = 256; | ||||
2233 | break; | ||||
2234 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: | ||||
2235 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; | ||||
2236 | *key_type = PSA_KEY_TYPE_CAMELLIA; | ||||
2237 | *key_size = 256; | ||||
2238 | break; | ||||
2239 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: | ||||
2240 | *alg = PSA_ALG_GCM; | ||||
2241 | *key_type = PSA_KEY_TYPE_CAMELLIA; | ||||
2242 | *key_size = 256; | ||||
2243 | break; | ||||
2244 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: | ||||
2245 | *alg = PSA_ALG_CHACHA20_POLY1305; | ||||
2246 | *key_type = PSA_KEY_TYPE_CHACHA20; | ||||
2247 | *key_size = 256; | ||||
2248 | break; | ||||
2249 | case MBEDTLS_CIPHER_NULL: | ||||
2250 | *alg = MBEDTLS_SSL_NULL_CIPHER; | ||||
2251 | *key_type = 0; | ||||
2252 | *key_size = 0; | ||||
2253 | break; | ||||
2254 | default: | ||||
2255 | return PSA_ERROR_NOT_SUPPORTED; | ||||
2256 | } | ||||
2257 | |||||
2258 | return PSA_SUCCESS; | ||||
2259 | } | ||||
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2260 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2261 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2262 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2263 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
2264 | const unsigned char *dhm_P, size_t P_len, | ||||
2265 | const unsigned char *dhm_G, size_t G_len ) | ||||
2266 | { | ||||
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2267 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2268 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2269 | mbedtls_mpi_free( &conf->dhm_P ); |
2270 | mbedtls_mpi_free( &conf->dhm_G ); | ||||
2271 | |||||
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2272 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
2273 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) | ||||
2274 | { | ||||
2275 | mbedtls_mpi_free( &conf->dhm_P ); | ||||
2276 | mbedtls_mpi_free( &conf->dhm_G ); | ||||
2277 | return( ret ); | ||||
2278 | } | ||||
2279 | |||||
2280 | return( 0 ); | ||||
2281 | } | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2282 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2283 | 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] | 2284 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2285 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2286 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2287 | mbedtls_mpi_free( &conf->dhm_P ); |
2288 | mbedtls_mpi_free( &conf->dhm_G ); | ||||
2289 | |||||
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2290 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
2291 | &conf->dhm_P ) ) != 0 || | ||||
2292 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, | ||||
2293 | &conf->dhm_G ) ) != 0 ) | ||||
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2294 | { |
2295 | mbedtls_mpi_free( &conf->dhm_P ); | ||||
2296 | mbedtls_mpi_free( &conf->dhm_G ); | ||||
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2297 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2298 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2299 | |
2300 | return( 0 ); | ||||
2301 | } | ||||
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2302 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2303 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2304 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
2305 | /* | ||||
2306 | * Set the minimum length for Diffie-Hellman parameters | ||||
2307 | */ | ||||
2308 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, | ||||
2309 | unsigned int bitlen ) | ||||
2310 | { | ||||
2311 | conf->dhm_min_bitlen = bitlen; | ||||
2312 | } | ||||
2313 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ | ||||
2314 | |||||
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2315 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2316 | #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] | 2317 | /* |
2318 | * Set allowed/preferred hashes for handshake signatures | ||||
2319 | */ | ||||
2320 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, | ||||
2321 | const int *hashes ) | ||||
2322 | { | ||||
2323 | conf->sig_hashes = hashes; | ||||
2324 | } | ||||
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2325 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2326 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2327 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2328 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
2329 | const uint16_t* sig_algs ) | ||||
2330 | { | ||||
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2331 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
2332 | conf->sig_hashes = NULL; | ||||
2333 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
2334 | conf->sig_algs = sig_algs; | ||||
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2335 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2336 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2337 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2338 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2339 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2340 | /* |
2341 | * Set the allowed elliptic curves | ||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2342 | * |
2343 | * mbedtls_ssl_setup() takes the provided list | ||||
2344 | * and translates it to a list of IANA TLS group identifiers, | ||||
2345 | * stored in ssl->handshake->group_list. | ||||
2346 | * | ||||
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2347 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2348 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2349 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2350 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2351 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2352 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2353 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2354 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2355 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2356 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2357 | /* |
2358 | * Set the allowed groups | ||||
2359 | */ | ||||
2360 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, | ||||
2361 | const uint16_t *group_list ) | ||||
2362 | { | ||||
2363 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
2364 | conf->curve_list = NULL; | ||||
2365 | #endif | ||||
2366 | conf->group_list = group_list; | ||||
2367 | } | ||||
2368 | |||||
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2369 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2370 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2371 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2372 | /* Initialize to suppress unnecessary compiler warning */ |
2373 | size_t hostname_len = 0; | ||||
2374 | |||||
2375 | /* Check if new hostname is valid before | ||||
2376 | * making any change to current one */ | ||||
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2377 | if( hostname != NULL ) |
2378 | { | ||||
2379 | hostname_len = strlen( hostname ); | ||||
2380 | |||||
2381 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) | ||||
2382 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
2383 | } | ||||
2384 | |||||
2385 | /* Now it's clear that we will overwrite the old hostname, | ||||
2386 | * so we can free it safely */ | ||||
2387 | |||||
2388 | if( ssl->hostname != NULL ) | ||||
2389 | { | ||||
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2390 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2391 | mbedtls_free( ssl->hostname ); |
2392 | } | ||||
2393 | |||||
2394 | /* Passing NULL as hostname shall clear the old one */ | ||||
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2395 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2396 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2397 | { |
2398 | ssl->hostname = NULL; | ||||
2399 | } | ||||
2400 | else | ||||
2401 | { | ||||
2402 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); | ||||
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2403 | if( ssl->hostname == NULL ) |
2404 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | ||||
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2405 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2406 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2407 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2408 | ssl->hostname[hostname_len] = '\0'; |
2409 | } | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2410 | |
2411 | return( 0 ); | ||||
2412 | } | ||||
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2413 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2414 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2415 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2416 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2417 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2418 | const unsigned char *, size_t), |
2419 | void *p_sni ) | ||||
2420 | { | ||||
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2421 | conf->f_sni = f_sni; |
2422 | conf->p_sni = p_sni; | ||||
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2423 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2424 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2425 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2426 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2427 | 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] | 2428 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2429 | size_t cur_len, tot_len; |
2430 | const char **p; | ||||
2431 | |||||
2432 | /* | ||||
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2433 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
2434 | * MUST NOT be truncated." | ||||
2435 | * We check lengths now rather than later. | ||||
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2436 | */ |
2437 | tot_len = 0; | ||||
2438 | for( p = protos; *p != NULL; p++ ) | ||||
2439 | { | ||||
2440 | cur_len = strlen( *p ); | ||||
2441 | tot_len += cur_len; | ||||
2442 | |||||
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2443 | if( ( cur_len == 0 ) || |
2444 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || | ||||
2445 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2446 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2447 | } |
2448 | |||||
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2449 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2450 | |
2451 | return( 0 ); | ||||
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2452 | } |
2453 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2454 | 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] | 2455 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2456 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2457 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2458 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2459 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2460 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2461 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
2462 | int support_mki_value ) | ||||
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2463 | { |
2464 | conf->dtls_srtp_mki_support = support_mki_value; | ||||
2465 | } | ||||
2466 | |||||
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2467 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
2468 | unsigned char *mki_value, | ||||
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2469 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2470 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2471 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2472 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2473 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2474 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2475 | |
2476 | 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] | 2477 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2478 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2479 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2480 | |
2481 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); | ||||
2482 | ssl->dtls_srtp_info.mki_len = mki_len; | ||||
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2483 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2484 | } |
2485 | |||||
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2486 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2487 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2488 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2489 | const mbedtls_ssl_srtp_profile *p; |
2490 | size_t list_size = 0; | ||||
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2491 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2492 | /* check the profiles list: all entry must be valid, |
2493 | * 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] | 2494 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
2495 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; | ||||
2496 | p++ ) | ||||
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2497 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2498 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2499 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2500 | list_size++; |
2501 | } | ||||
2502 | else | ||||
2503 | { | ||||
2504 | /* unsupported value, stop parsing and set the size to an error value */ | ||||
2505 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; | ||||
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2506 | } |
2507 | } | ||||
2508 | |||||
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2509 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2510 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2511 | conf->dtls_srtp_profile_list = NULL; |
2512 | conf->dtls_srtp_profile_list_len = 0; | ||||
2513 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
2514 | } | ||||
2515 | |||||
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2516 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2517 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2518 | |
2519 | return( 0 ); | ||||
2520 | } | ||||
2521 | |||||
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2522 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
2523 | mbedtls_dtls_srtp_info *dtls_srtp_info ) | ||||
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2524 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2525 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
2526 | /* do not copy the mki value if there is no chosen profile */ | ||||
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2527 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2528 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2529 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2530 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2531 | else |
2532 | { | ||||
2533 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; | ||||
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2534 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
2535 | ssl->dtls_srtp_info.mki_len ); | ||||
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2536 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2537 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2538 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
2539 | |||||
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2540 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2541 | 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] | 2542 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2543 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2544 | } |
2545 | |||||
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2546 | 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] | 2547 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2548 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2549 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2550 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2551 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2552 | #if defined(MBEDTLS_SSL_SRV_C) |
2553 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, | ||||
2554 | char cert_req_ca_list ) | ||||
2555 | { | ||||
2556 | conf->cert_req_ca_list = cert_req_ca_list; | ||||
2557 | } | ||||
2558 | #endif | ||||
2559 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2560 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2561 | 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] | 2562 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2563 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2564 | } |
2565 | #endif | ||||
2566 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2567 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2568 | 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] | 2569 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2570 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2571 | } |
2572 | #endif | ||||
2573 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2574 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2575 | 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] | 2576 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2577 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2578 | 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] | 2579 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2580 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2581 | } |
2582 | |||||
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2583 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2584 | |
2585 | return( 0 ); | ||||
2586 | } | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2587 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2588 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2589 | 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] | 2590 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2591 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2592 | } |
2593 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2594 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2595 | 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] | 2596 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2597 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2598 | } |
2599 | |||||
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2600 | 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] | 2601 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2602 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2603 | } |
2604 | |||||
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2605 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2606 | const unsigned char period[8] ) |
2607 | { | ||||
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2608 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2609 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2610 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2611 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2612 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2613 | #if defined(MBEDTLS_SSL_CLI_C) |
2614 | 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] | 2615 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2616 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2617 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2618 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2619 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2620 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2621 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2622 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2623 | void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf, |
2624 | uint16_t num_tickets ) | ||||
2625 | { | ||||
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 2626 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 2627 | } |
2628 | #endif | ||||
2629 | |||||
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2630 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
2631 | mbedtls_ssl_ticket_write_t *f_ticket_write, | ||||
2632 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, | ||||
2633 | void *p_ticket ) | ||||
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2634 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2635 | conf->f_ticket_write = f_ticket_write; |
2636 | conf->f_ticket_parse = f_ticket_parse; | ||||
2637 | conf->p_ticket = p_ticket; | ||||
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2638 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2639 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2640 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2641 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2642 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
2643 | mbedtls_ssl_export_keys_t *f_export_keys, | ||||
2644 | void *p_export_keys ) | ||||
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2645 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2646 | ssl->f_export_keys = f_export_keys; |
2647 | ssl->p_export_keys = p_export_keys; | ||||
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2648 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2649 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2650 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2651 | void mbedtls_ssl_conf_async_private_cb( |
2652 | mbedtls_ssl_config *conf, | ||||
2653 | mbedtls_ssl_async_sign_t *f_async_sign, | ||||
2654 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, | ||||
2655 | mbedtls_ssl_async_resume_t *f_async_resume, | ||||
2656 | mbedtls_ssl_async_cancel_t *f_async_cancel, | ||||
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2657 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2658 | { |
2659 | conf->f_async_sign_start = f_async_sign; | ||||
2660 | conf->f_async_decrypt_start = f_async_decrypt; | ||||
2661 | conf->f_async_resume = f_async_resume; | ||||
2662 | conf->f_async_cancel = f_async_cancel; | ||||
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2663 | conf->p_async_config_data = async_config_data; |
2664 | } | ||||
2665 | |||||
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2666 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
2667 | { | ||||
2668 | return( conf->p_async_config_data ); | ||||
2669 | } | ||||
2670 | |||||
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2671 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2672 | { |
2673 | if( ssl->handshake == NULL ) | ||||
2674 | return( NULL ); | ||||
2675 | else | ||||
2676 | return( ssl->handshake->user_async_ctx ); | ||||
2677 | } | ||||
2678 | |||||
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2679 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2680 | void *ctx ) |
2681 | { | ||||
2682 | if( ssl->handshake != NULL ) | ||||
2683 | ssl->handshake->user_async_ctx = ctx; | ||||
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2684 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2685 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2686 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2687 | /* |
2688 | * SSL get accessors | ||||
2689 | */ | ||||
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2690 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2691 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2692 | if( ssl->session != NULL ) |
2693 | return( ssl->session->verify_result ); | ||||
2694 | |||||
2695 | if( ssl->session_negotiate != NULL ) | ||||
2696 | return( ssl->session_negotiate->verify_result ); | ||||
2697 | |||||
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2698 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2699 | } |
2700 | |||||
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2701 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
2702 | { | ||||
2703 | if( ssl == NULL || ssl->session == NULL ) | ||||
2704 | return( 0 ); | ||||
2705 | |||||
2706 | return( ssl->session->ciphersuite ); | ||||
2707 | } | ||||
2708 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2709 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2710 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2711 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2712 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2713 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2714 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2715 | } |
2716 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2717 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2718 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2719 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2720 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2721 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2722 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2723 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2724 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2725 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2726 | default: |
2727 | return( "unknown (DTLS)" ); | ||||
2728 | } | ||||
2729 | } | ||||
2730 | #endif | ||||
2731 | |||||
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2732 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2733 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2734 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2735 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2736 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2737 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2738 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2739 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2740 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2741 | } |
2742 | |||||
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2743 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2744 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
2745 | { | ||||
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2746 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2747 | size_t read_mfl; |
2748 | |||||
2749 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ | ||||
2750 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | ||||
2751 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) | ||||
2752 | { | ||||
2753 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); | ||||
2754 | } | ||||
2755 | |||||
2756 | /* Check if a smaller max length was negotiated */ | ||||
2757 | if( ssl->session_out != NULL ) | ||||
2758 | { | ||||
2759 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); | ||||
2760 | if( read_mfl < max_len ) | ||||
2761 | { | ||||
2762 | max_len = read_mfl; | ||||
2763 | } | ||||
2764 | } | ||||
2765 | |||||
2766 | // During a handshake, use the value being negotiated | ||||
2767 | if( ssl->session_negotiate != NULL ) | ||||
2768 | { | ||||
2769 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); | ||||
2770 | if( read_mfl < max_len ) | ||||
2771 | { | ||||
2772 | max_len = read_mfl; | ||||
2773 | } | ||||
2774 | } | ||||
2775 | |||||
2776 | return( max_len ); | ||||
2777 | } | ||||
2778 | |||||
2779 | 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] | 2780 | { |
2781 | size_t max_len; | ||||
2782 | |||||
2783 | /* | ||||
2784 | * Assume mfl_code is correct since it was checked when set | ||||
2785 | */ | ||||
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2786 | 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] | 2787 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2788 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2789 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2790 | 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] | 2791 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2792 | 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] | 2793 | } |
2794 | |||||
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2795 | /* During a handshake, use the value being negotiated */ |
2796 | if( ssl->session_negotiate != NULL && | ||||
2797 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) | ||||
2798 | { | ||||
2799 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); | ||||
2800 | } | ||||
2801 | |||||
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2802 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2803 | } |
2804 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | ||||
2805 | |||||
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2806 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2807 | 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] | 2808 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2809 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
2810 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | ||||
2811 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || | ||||
2812 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) | ||||
2813 | return ( 0 ); | ||||
2814 | |||||
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2815 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
2816 | return( ssl->mtu ); | ||||
2817 | |||||
2818 | if( ssl->mtu == 0 ) | ||||
2819 | return( ssl->handshake->mtu ); | ||||
2820 | |||||
2821 | return( ssl->mtu < ssl->handshake->mtu ? | ||||
2822 | ssl->mtu : ssl->handshake->mtu ); | ||||
2823 | } | ||||
2824 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | ||||
2825 | |||||
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2826 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
2827 | { | ||||
2828 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; | ||||
2829 | |||||
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2830 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
2831 | !defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
2832 | (void) ssl; | ||||
2833 | #endif | ||||
2834 | |||||
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2835 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2836 | 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] | 2837 | |
2838 | if( max_len > mfl ) | ||||
2839 | max_len = mfl; | ||||
2840 | #endif | ||||
2841 | |||||
2842 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2843 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2844 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2845 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2846 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
2847 | const size_t overhead = (size_t) ret; | ||||
2848 | |||||
2849 | if( ret < 0 ) | ||||
2850 | return( ret ); | ||||
2851 | |||||
2852 | if( mtu <= overhead ) | ||||
2853 | { | ||||
2854 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); | ||||
2855 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
2856 | } | ||||
2857 | |||||
2858 | if( max_len > mtu - overhead ) | ||||
2859 | max_len = mtu - overhead; | ||||
2860 | } | ||||
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2861 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2862 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2863 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
2864 | !defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
2865 | ((void) ssl); | ||||
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2866 | #endif |
2867 | |||||
2868 | return( (int) max_len ); | ||||
2869 | } | ||||
2870 | |||||
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2871 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
2872 | { | ||||
2873 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; | ||||
2874 | |||||
2875 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | ||||
2876 | (void) ssl; | ||||
2877 | #endif | ||||
2878 | |||||
2879 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | ||||
2880 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); | ||||
2881 | |||||
2882 | if( max_len > mfl ) | ||||
2883 | max_len = mfl; | ||||
2884 | #endif | ||||
2885 | |||||
2886 | return( (int) max_len ); | ||||
2887 | } | ||||
2888 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2889 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
2890 | 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] | 2891 | { |
2892 | if( ssl == NULL || ssl->session == NULL ) | ||||
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2893 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2894 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2895 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2896 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2897 | #else |
2898 | return( NULL ); | ||||
2899 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2900 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2901 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2903 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2904 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
2905 | mbedtls_ssl_session *dst ) | ||||
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2906 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2907 | int ret; |
2908 | |||||
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2909 | if( ssl == NULL || |
2910 | dst == NULL || | ||||
2911 | ssl->session == NULL || | ||||
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2912 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2913 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2914 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2915 | } |
2916 | |||||
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2917 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
2918 | * idempotent: Each session can only be exported once. | ||||
2919 | * | ||||
2920 | * (This is in preparation for TLS 1.3 support where we will | ||||
2921 | * need the ability to export multiple sessions (aka tickets), | ||||
2922 | * which will be achieved by calling mbedtls_ssl_get_session() | ||||
2923 | * multiple times until it fails.) | ||||
2924 | * | ||||
2925 | * Check whether we have already exported the current session, | ||||
2926 | * and fail if so. | ||||
2927 | */ | ||||
2928 | if( ssl->session->exported == 1 ) | ||||
2929 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
2930 | |||||
2931 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); | ||||
2932 | if( ret != 0 ) | ||||
2933 | return( ret ); | ||||
2934 | |||||
2935 | /* Remember that we've exported the session. */ | ||||
2936 | ssl->session->exported = 1; | ||||
2937 | return( 0 ); | ||||
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2938 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2939 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2940 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2941 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2942 | * Define ticket header determining Mbed TLS version |
2943 | * and structure of the ticket. | ||||
2944 | */ | ||||
2945 | |||||
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2946 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2947 | * Define bitflag determining compile-time settings influencing |
2948 | * structure of serialized SSL sessions. | ||||
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2949 | */ |
2950 | |||||
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2951 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2952 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2953 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2954 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2955 | #endif /* MBEDTLS_HAVE_TIME */ |
2956 | |||||
2957 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | ||||
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2958 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2959 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2960 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2961 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
2962 | |||||
2963 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) | ||||
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2964 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2965 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2966 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2967 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
2968 | |||||
2969 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | ||||
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2970 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2971 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2972 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2973 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
2974 | |||||
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2975 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2976 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2977 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2978 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2979 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
2980 | |||||
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2981 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
2982 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 | ||||
2983 | #else | ||||
2984 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 | ||||
2985 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | ||||
2986 | |||||
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2987 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
2988 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 | ||||
2989 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 | ||||
2990 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 | ||||
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 2991 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
2992 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 | ||||
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2993 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2994 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2995 | ( (uint16_t) ( \ |
2996 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ | ||||
2997 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ | ||||
2998 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ | ||||
2999 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ | ||||
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3000 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 3001 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3002 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3003 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3004 | MBEDTLS_VERSION_MAJOR, |
3005 | MBEDTLS_VERSION_MINOR, | ||||
3006 | MBEDTLS_VERSION_PATCH, | ||||
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3007 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
3008 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), | ||||
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3009 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3010 | |
3011 | /* | ||||
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3012 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3013 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3014 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3015 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3016 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3017 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
3018 | * opaque session_format[2]; // library-version specific 16-bit field | ||||
3019 | * // determining the format of the remaining | ||||
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3020 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3021 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3022 | * Note: When updating the format, remember to keep |
3023 | * these version+format bytes. | ||||
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3024 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3025 | * // In this version, `session_format` determines |
3026 | * // the setting of those compile-time | ||||
3027 | * // configuration options which influence | ||||
3028 | * // the structure of mbedtls_ssl_session. | ||||
3029 | * | ||||
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3030 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
3031 | * // - TLS 1.2 (3) | ||||
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3032 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3033 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3034 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3035 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3036 | * serialized_session_tls12 data; |
3037 | * | ||||
3038 | * }; | ||||
3039 | * | ||||
3040 | * } serialized_session; | ||||
3041 | * | ||||
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3042 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3043 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3044 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3045 | static int ssl_session_save( const mbedtls_ssl_session *session, |
3046 | unsigned char omit_header, | ||||
3047 | unsigned char *buf, | ||||
3048 | size_t buf_len, | ||||
3049 | size_t *olen ) | ||||
3050 | { | ||||
3051 | unsigned char *p = buf; | ||||
3052 | size_t used = 0; | ||||
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3053 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3054 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
3055 | size_t out_len; | ||||
3056 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
3057 | #endif | ||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3058 | if( session == NULL ) |
3059 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
3060 | |||||
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3061 | if( !omit_header ) |
3062 | { | ||||
3063 | /* | ||||
3064 | * Add Mbed TLS version identifier | ||||
3065 | */ | ||||
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3066 | used += sizeof( ssl_serialized_session_header ); |
3067 | |||||
3068 | if( used <= buf_len ) | ||||
3069 | { | ||||
3070 | memcpy( p, ssl_serialized_session_header, | ||||
3071 | sizeof( ssl_serialized_session_header ) ); | ||||
3072 | p += sizeof( ssl_serialized_session_header ); | ||||
3073 | } | ||||
3074 | } | ||||
3075 | |||||
3076 | /* | ||||
3077 | * TLS version identifier | ||||
3078 | */ | ||||
3079 | used += 1; | ||||
3080 | if( used <= buf_len ) | ||||
3081 | { | ||||
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3082 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3083 | } |
3084 | |||||
3085 | /* Forward to version-specific serialization routine. */ | ||||
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3086 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3087 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3088 | { |
3089 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3090 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3091 | used += ssl_tls12_session_save( session, p, remaining_len ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3092 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3093 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
3094 | |||||
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3095 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
3096 | case MBEDTLS_SSL_VERSION_TLS1_3: | ||||
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3097 | ret = ssl_tls13_session_save( session, p, remaining_len, &out_len ); |
3098 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) | ||||
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3099 | return( ret ); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3100 | used += out_len; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3101 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3102 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
3103 | |||||
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3104 | default: |
3105 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
3106 | } | ||||
3107 | |||||
3108 | *olen = used; | ||||
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 3109 | if( used > buf_len ) |
3110 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | ||||
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3111 | |
3112 | return( 0 ); | ||||
3113 | } | ||||
3114 | |||||
3115 | /* | ||||
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3116 | * Public wrapper for ssl_session_save() |
3117 | */ | ||||
3118 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, | ||||
3119 | unsigned char *buf, | ||||
3120 | size_t buf_len, | ||||
3121 | size_t *olen ) | ||||
3122 | { | ||||
3123 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); | ||||
3124 | } | ||||
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3125 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3126 | /* |
3127 | * Deserialize session, see mbedtls_ssl_session_save() for format. | ||||
3128 | * | ||||
3129 | * This internal version is wrapped by a public function that cleans up in | ||||
3130 | * case of error, and has an extra option omit_header. | ||||
3131 | */ | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3132 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3133 | static int ssl_session_load( mbedtls_ssl_session *session, |
3134 | unsigned char omit_header, | ||||
3135 | const unsigned char *buf, | ||||
3136 | size_t len ) | ||||
3137 | { | ||||
3138 | const unsigned char *p = buf; | ||||
3139 | const unsigned char * const end = buf + len; | ||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3140 | size_t remaining_len; |
3141 | |||||
3142 | |||||
3143 | if( session == NULL ) | ||||
3144 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3145 | |
3146 | if( !omit_header ) | ||||
3147 | { | ||||
3148 | /* | ||||
3149 | * Check Mbed TLS version identifier | ||||
3150 | */ | ||||
3151 | |||||
3152 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) | ||||
3153 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
3154 | |||||
3155 | if( memcmp( p, ssl_serialized_session_header, | ||||
3156 | sizeof( ssl_serialized_session_header ) ) != 0 ) | ||||
3157 | { | ||||
3158 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | ||||
3159 | } | ||||
3160 | p += sizeof( ssl_serialized_session_header ); | ||||
3161 | } | ||||
3162 | |||||
3163 | /* | ||||
3164 | * TLS version identifier | ||||
3165 | */ | ||||
3166 | if( 1 > (size_t)( end - p ) ) | ||||
3167 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3168 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3169 | |
3170 | /* Dispatch according to TLS version. */ | ||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3171 | remaining_len = ( end - p ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3172 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3173 | { |
3174 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3175 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3176 | return( ssl_tls12_session_load( session, p, remaining_len ) ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3177 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
3178 | |||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3179 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
3180 | case MBEDTLS_SSL_VERSION_TLS1_3: | ||||
3181 | return( ssl_tls13_session_load( session, p, remaining_len ) ); | ||||
3182 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | ||||
3183 | |||||
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3184 | default: |
3185 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
3186 | } | ||||
3187 | } | ||||
3188 | |||||
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3189 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3190 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3191 | */ |
3192 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, | ||||
3193 | const unsigned char *buf, | ||||
3194 | size_t len ) | ||||
3195 | { | ||||
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3196 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3197 | |
3198 | if( ret != 0 ) | ||||
3199 | mbedtls_ssl_session_free( session ); | ||||
3200 | |||||
3201 | return( ret ); | ||||
3202 | } | ||||
3203 | |||||
3204 | /* | ||||
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3205 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3206 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3207 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3208 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
3209 | { | ||||
3210 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
3211 | |||||
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3212 | /* |
3213 | * 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] | 3214 | * that were written into the output buffer by the previous handshake step, |
3215 | * if the write to the network callback returned with the | ||||
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3216 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
3217 | * We proceed to the next handshake step only when all data from the | ||||
3218 | * previous one have been sent to the peer, thus we make sure that this is | ||||
3219 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may | ||||
3220 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case | ||||
3221 | * we have to wait before to go ahead. | ||||
3222 | * In the case of TLS 1.3, handshake step handlers do not send data to the | ||||
3223 | * peer. Data are only sent here and through | ||||
3224 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an | ||||
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3225 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3226 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3227 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
3228 | return( ret ); | ||||
3229 | |||||
3230 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
3231 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | ||||
3232 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | ||||
3233 | { | ||||
3234 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | ||||
3235 | return( ret ); | ||||
3236 | } | ||||
3237 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | ||||
3238 | |||||
3239 | return( ret ); | ||||
3240 | } | ||||
3241 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3242 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3243 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3244 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3245 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3246 | if( ssl == NULL || |
3247 | ssl->conf == NULL || | ||||
3248 | ssl->handshake == NULL || | ||||
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3249 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3250 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3251 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3252 | } |
3253 | |||||
3254 | ret = ssl_prepare_handshake_step( ssl ); | ||||
3255 | if( ret != 0 ) | ||||
3256 | return( ret ); | ||||
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3257 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3258 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
3259 | if( ret != 0 ) | ||||
3260 | goto cleanup; | ||||
3261 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3262 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3263 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3264 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 3265 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
3266 | mbedtls_ssl_states_str( ssl->state ) ) ); | ||||
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3267 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3268 | switch( ssl->state ) |
3269 | { | ||||
3270 | case MBEDTLS_SSL_HELLO_REQUEST: | ||||
3271 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; | ||||
3272 | break; | ||||
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3273 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3274 | case MBEDTLS_SSL_CLIENT_HELLO: |
3275 | ret = mbedtls_ssl_write_client_hello( ssl ); | ||||
3276 | break; | ||||
3277 | |||||
3278 | default: | ||||
3279 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | ||||
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3280 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3281 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
3282 | else | ||||
3283 | ret = mbedtls_ssl_handshake_client_step( ssl ); | ||||
3284 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
3285 | ret = mbedtls_ssl_handshake_client_step( ssl ); | ||||
3286 | #else | ||||
3287 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); | ||||
3288 | #endif | ||||
3289 | } | ||||
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3290 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3291 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3292 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3293 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3294 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3295 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3296 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 3297 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3298 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3299 | |
3300 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
3301 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) | ||||
3302 | ret = mbedtls_ssl_handshake_server_step( ssl ); | ||||
3303 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
3304 | } | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3305 | #endif |
3306 | |||||
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3307 | if( ret != 0 ) |
3308 | { | ||||
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3309 | /* handshake_step return error. And it is same |
3310 | * with alert_reason. | ||||
3311 | */ | ||||
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3312 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3313 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3314 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3315 | goto cleanup; |
3316 | } | ||||
3317 | } | ||||
3318 | |||||
3319 | cleanup: | ||||
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3320 | return( ret ); |
3321 | } | ||||
3322 | |||||
3323 | /* | ||||
3324 | * Perform the SSL handshake | ||||
3325 | */ | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3326 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3327 | { |
3328 | int ret = 0; | ||||
3329 | |||||
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3330 | /* Sanity checks */ |
3331 | |||||
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3332 | if( ssl == NULL || ssl->conf == NULL ) |
3333 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
3334 | |||||
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3335 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
3336 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | ||||
3337 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) | ||||
3338 | { | ||||
3339 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " | ||||
3340 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); | ||||
3341 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
3342 | } | ||||
3343 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | ||||
3344 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3345 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3346 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3347 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3348 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3350 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3351 | |
3352 | if( ret != 0 ) | ||||
3353 | break; | ||||
3354 | } | ||||
3355 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3356 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3357 | |
3358 | return( ret ); | ||||
3359 | } | ||||
3360 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3361 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
3362 | #if defined(MBEDTLS_SSL_SRV_C) | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3363 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3364 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3365 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3366 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3367 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3368 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3369 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3370 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3371 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3372 | |
3373 | ssl->out_msglen = 4; | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3374 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
3375 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; | ||||
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3376 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3377 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3378 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3379 | 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] | 3380 | return( ret ); |
3381 | } | ||||
3382 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3383 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3384 | |
3385 | return( 0 ); | ||||
3386 | } | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3387 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3388 | |
3389 | /* | ||||
3390 | * Actually renegotiate current connection, triggered by either: | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3391 | * - any side: calling mbedtls_ssl_renegotiate(), |
3392 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), | ||||
3393 | * - 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] | 3394 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3395 | * 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] | 3396 | * 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] | 3397 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3398 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3399 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3400 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3401 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3402 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3403 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3404 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
3405 | return( ret ); | ||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3406 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3407 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
3408 | * the ServerHello will have message_seq = 1" */ | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3409 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3410 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3411 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3412 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3413 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3414 | ssl->handshake->out_msg_seq = 1; |
3415 | else | ||||
3416 | ssl->handshake->in_msg_seq = 1; | ||||
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3417 | } |
3418 | #endif | ||||
3419 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3420 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
3421 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; | ||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3422 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3423 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3424 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3425 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3426 | return( ret ); |
3427 | } | ||||
3428 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3429 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3430 | |
3431 | return( 0 ); | ||||
3432 | } | ||||
3433 | |||||
3434 | /* | ||||
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3435 | * Renegotiate current connection on client, |
3436 | * or request renegotiation on server | ||||
3437 | */ | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3438 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3439 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3440 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3441 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3442 | if( ssl == NULL || ssl->conf == NULL ) |
3443 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
3444 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3445 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3446 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3447 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3448 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3449 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3450 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3451 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3452 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3453 | |
3454 | /* Did we already try/start sending HelloRequest? */ | ||||
3455 | if( ssl->out_left != 0 ) | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3456 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3457 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3458 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3459 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3460 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3461 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3462 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3463 | /* |
3464 | * On client, either start the renegotiation process or, | ||||
3465 | * if already in progress, continue the handshake | ||||
3466 | */ | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3467 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3468 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3469 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3470 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3471 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3472 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3473 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3474 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3475 | return( ret ); |
3476 | } | ||||
3477 | } | ||||
3478 | else | ||||
3479 | { | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3480 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3481 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3482 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3483 | return( ret ); |
3484 | } | ||||
3485 | } | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3486 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3487 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3488 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3489 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3490 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3491 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3492 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3493 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3494 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
3495 | |||||
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3496 | if( handshake == NULL ) |
3497 | return; | ||||
3498 | |||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3499 | #if defined(MBEDTLS_ECP_C) |
3500 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
3501 | if ( ssl->handshake->group_list_heap_allocated ) | ||||
3502 | mbedtls_free( (void*) handshake->group_list ); | ||||
3503 | handshake->group_list = NULL; | ||||
3504 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | ||||
3505 | #endif /* MBEDTLS_ECP_C */ | ||||
3506 | |||||
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3507 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
3508 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
3509 | if ( ssl->handshake->sig_algs_heap_allocated ) | ||||
3510 | mbedtls_free( (void*) handshake->sig_algs ); | ||||
3511 | handshake->sig_algs = NULL; | ||||
3512 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | ||||
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3513 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
3514 | if( ssl->handshake->certificate_request_context ) | ||||
3515 | { | ||||
3516 | mbedtls_free( (void*) handshake->certificate_request_context ); | ||||
3517 | } | ||||
3518 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | ||||
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3519 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
3520 | |||||
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3521 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
3522 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) | ||||
3523 | { | ||||
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3524 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3525 | handshake->async_in_progress = 0; |
3526 | } | ||||
3527 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | ||||
3528 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3529 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3530 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
3531 | psa_hash_abort( &handshake->fin_sha256_psa ); | ||||
3532 | #else | ||||
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3533 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
3534 | #endif | ||||
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3535 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 3536 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3537 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3538 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3539 | #else |
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 3540 | mbedtls_sha512_free( &handshake->fin_sha384 ); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3541 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3542 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3543 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3544 | #if defined(MBEDTLS_DHM_C) |
3545 | mbedtls_dhm_free( &handshake->dhm_ctx ); | ||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3546 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3547 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3548 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3549 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3550 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3551 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3552 | #if defined(MBEDTLS_SSL_CLI_C) |
3553 | mbedtls_free( handshake->ecjpake_cache ); | ||||
3554 | handshake->ecjpake_cache = NULL; | ||||
3555 | handshake->ecjpake_cache_len = 0; | ||||
3556 | #endif | ||||
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3557 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3558 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3559 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
3560 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | ||||
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3561 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3562 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3563 | #endif |
3564 | |||||
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3565 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 3566 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
3567 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) | ||||
3568 | { | ||||
3569 | /* The maintenance of the external PSK key slot is the | ||||
3570 | * user's responsibility. */ | ||||
3571 | if( ssl->handshake->psk_opaque_is_internal ) | ||||
3572 | { | ||||
3573 | psa_destroy_key( ssl->handshake->psk_opaque ); | ||||
3574 | ssl->handshake->psk_opaque_is_internal = 0; | ||||
3575 | } | ||||
3576 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | ||||
3577 | } | ||||
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3578 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3579 | if( handshake->psk != NULL ) |
3580 | { | ||||
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3581 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3582 | mbedtls_free( handshake->psk ); |
3583 | } | ||||
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3584 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3585 | #endif |
3586 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3587 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
3588 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | ||||
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3589 | /* |
3590 | * Free only the linked list wrapper, not the keys themselves | ||||
3591 | * since the belong to the SNI callback | ||||
3592 | */ | ||||
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3593 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3594 | #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] | 3595 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3596 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3597 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3598 | if( handshake->ecrs_peer_cert != NULL ) |
3599 | { | ||||
3600 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); | ||||
3601 | mbedtls_free( handshake->ecrs_peer_cert ); | ||||
3602 | } | ||||
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3603 | #endif |
3604 | |||||
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3605 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
3606 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
3607 | mbedtls_pk_free( &handshake->peer_pubkey ); | ||||
3608 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
3609 | |||||
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3610 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
3611 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) | ||||
3612 | mbedtls_free( handshake->cookie ); | ||||
3613 | #endif /* MBEDTLS_SSL_CLI_C && | ||||
3614 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ | ||||
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3615 | |
3616 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3617 | mbedtls_ssl_flight_free( handshake->flight ); |
3618 | mbedtls_ssl_buffering_free( ssl ); | ||||
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3619 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3620 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3621 | #if defined(MBEDTLS_ECDH_C) && \ |
3622 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) | ||||
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3623 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3624 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3625 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
3626 | |||||
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3627 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3628 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
3629 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); | ||||
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3630 | mbedtls_free( handshake->transform_earlydata ); |
3631 | mbedtls_free( handshake->transform_handshake ); | ||||
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3632 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3633 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3634 | |
3635 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | ||||
3636 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS | ||||
3637 | * processes datagrams and the fact that a datagram is allowed to have | ||||
3638 | * several records in it, it is possible that the I/O buffers are not | ||||
3639 | * empty at this stage */ | ||||
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3640 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
3641 | mbedtls_ssl_get_output_buflen( ssl ) ); | ||||
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3642 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3643 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3644 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
3645 | mbedtls_platform_zeroize( handshake, | ||||
3646 | sizeof( mbedtls_ssl_handshake_params ) ); | ||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3647 | } |
3648 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3649 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3650 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3651 | if( session == NULL ) |
3652 | return; | ||||
3653 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3654 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3655 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3656 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3657 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3658 | #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] | 3659 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3660 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3661 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3662 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3663 | } |
3664 | |||||
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3665 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3666 | |
3667 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | ||||
3668 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u | ||||
3669 | #else | ||||
3670 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u | ||||
3671 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | ||||
3672 | |||||
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3673 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3674 | |
3675 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | ||||
3676 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u | ||||
3677 | #else | ||||
3678 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u | ||||
3679 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | ||||
3680 | |||||
3681 | #if defined(MBEDTLS_SSL_ALPN) | ||||
3682 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u | ||||
3683 | #else | ||||
3684 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u | ||||
3685 | #endif /* MBEDTLS_SSL_ALPN */ | ||||
3686 | |||||
3687 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 | ||||
3688 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 | ||||
3689 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 | ||||
3690 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 | ||||
3691 | |||||
3692 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ | ||||
3693 | ( (uint32_t) ( \ | ||||
3694 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ | ||||
3695 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ | ||||
3696 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ | ||||
3697 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ | ||||
3698 | 0u ) ) | ||||
3699 | |||||
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3700 | static unsigned char ssl_serialized_context_header[] = { |
3701 | MBEDTLS_VERSION_MAJOR, | ||||
3702 | MBEDTLS_VERSION_MINOR, | ||||
3703 | MBEDTLS_VERSION_PATCH, | ||||
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3704 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
3705 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), | ||||
3706 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), | ||||
3707 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), | ||||
3708 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), | ||||
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3709 | }; |
3710 | |||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3711 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3712 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3713 | * |
3714 | * The format of the serialized data is: | ||||
3715 | * (in the presentation language of TLS, RFC 8446 section 3) | ||||
3716 | * | ||||
3717 | * // header | ||||
3718 | * opaque mbedtls_version[3]; // major, minor, patch | ||||
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3719 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3720 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3721 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3722 | * Note: When updating the format, remember to keep these |
3723 | * 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] | 3724 | * |
3725 | * // session sub-structure | ||||
3726 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() | ||||
3727 | * // transform sub-structure | ||||
3728 | * uint8 random[64]; // ServerHello.random+ClientHello.random | ||||
3729 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value | ||||
3730 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use | ||||
3731 | * // fields from ssl_context | ||||
3732 | * uint32 badmac_seen; // DTLS: number of records with failing MAC | ||||
3733 | * uint64 in_window_top; // DTLS: last validated record seq_num | ||||
3734 | * uint64 in_window; // DTLS: bitmask for replay protection | ||||
3735 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram | ||||
3736 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number | ||||
3737 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) | ||||
3738 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol | ||||
3739 | * | ||||
3740 | * Note that many fields of the ssl_context or sub-structures are not | ||||
3741 | * serialized, as they fall in one of the following categories: | ||||
3742 | * | ||||
3743 | * 1. forced value (eg in_left must be 0) | ||||
3744 | * 2. pointer to dynamically-allocated memory (eg session, transform) | ||||
3745 | * 3. value can be re-derived from other data (eg session keys from MS) | ||||
3746 | * 4. value was temporary (eg content of input buffer) | ||||
3747 | * 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] | 3748 | */ |
3749 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, | ||||
3750 | unsigned char *buf, | ||||
3751 | size_t buf_len, | ||||
3752 | size_t *olen ) | ||||
3753 | { | ||||
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3754 | unsigned char *p = buf; |
3755 | size_t used = 0; | ||||
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3756 | size_t session_len; |
3757 | int ret = 0; | ||||
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3758 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3759 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3760 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
3761 | * this function's documentation. | ||||
3762 | * | ||||
3763 | * These are due to assumptions/limitations in the implementation. Some of | ||||
3764 | * them are likely to stay (no handshake in progress) some might go away | ||||
3765 | * (only DTLS) but are currently used to simplify the implementation. | ||||
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3766 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3767 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3768 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3769 | { |
3770 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); | ||||
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3771 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3772 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3773 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3774 | { |
3775 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); | ||||
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3776 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3777 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3778 | /* Double-check that sub-structures are indeed ready */ |
3779 | if( ssl->transform == NULL || ssl->session == NULL ) | ||||
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3780 | { |
3781 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); | ||||
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3782 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3783 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3784 | /* There must be no pending incoming or outgoing data */ |
3785 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) | ||||
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3786 | { |
3787 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); | ||||
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3788 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3789 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3790 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3791 | { |
3792 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); | ||||
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3793 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3794 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3795 | /* Protocol must be DLTS, not TLS */ |
3796 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | ||||
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3797 | { |
3798 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); | ||||
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3799 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3800 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3801 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3802 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3803 | { |
3804 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); | ||||
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3805 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3806 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3807 | /* We must be using an AEAD ciphersuite */ |
3808 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) | ||||
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3809 | { |
3810 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); | ||||
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3811 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3812 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3813 | /* Renegotiation must not be enabled */ |
3814 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | ||||
3815 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) | ||||
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3816 | { |
3817 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); | ||||
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3818 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3819 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3820 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3821 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3822 | /* |
3823 | * Version and format identifier | ||||
3824 | */ | ||||
3825 | used += sizeof( ssl_serialized_context_header ); | ||||
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3826 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3827 | if( used <= buf_len ) |
3828 | { | ||||
3829 | memcpy( p, ssl_serialized_context_header, | ||||
3830 | sizeof( ssl_serialized_context_header ) ); | ||||
3831 | p += sizeof( ssl_serialized_context_header ); | ||||
3832 | } | ||||
3833 | |||||
3834 | /* | ||||
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3835 | * Session (length + data) |
3836 | */ | ||||
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3837 | 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] | 3838 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
3839 | return( ret ); | ||||
3840 | |||||
3841 | used += 4 + session_len; | ||||
3842 | if( used <= buf_len ) | ||||
3843 | { | ||||
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3844 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
3845 | p += 4; | ||||
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3846 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3847 | ret = ssl_session_save( ssl->session, 1, |
3848 | p, session_len, &session_len ); | ||||
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3849 | if( ret != 0 ) |
3850 | return( ret ); | ||||
3851 | |||||
3852 | p += session_len; | ||||
3853 | } | ||||
3854 | |||||
3855 | /* | ||||
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3856 | * Transform |
3857 | */ | ||||
3858 | used += sizeof( ssl->transform->randbytes ); | ||||
3859 | if( used <= buf_len ) | ||||
3860 | { | ||||
3861 | memcpy( p, ssl->transform->randbytes, | ||||
3862 | sizeof( ssl->transform->randbytes ) ); | ||||
3863 | p += sizeof( ssl->transform->randbytes ); | ||||
3864 | } | ||||
3865 | |||||
3866 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | ||||
3867 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; | ||||
3868 | if( used <= buf_len ) | ||||
3869 | { | ||||
3870 | *p++ = ssl->transform->in_cid_len; | ||||
3871 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); | ||||
3872 | p += ssl->transform->in_cid_len; | ||||
3873 | |||||
3874 | *p++ = ssl->transform->out_cid_len; | ||||
3875 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); | ||||
3876 | p += ssl->transform->out_cid_len; | ||||
3877 | } | ||||
3878 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | ||||
3879 | |||||
3880 | /* | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3881 | * Saved fields from top-level ssl_context structure |
3882 | */ | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3883 | used += 4; |
3884 | if( used <= buf_len ) | ||||
3885 | { | ||||
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3886 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
3887 | p += 4; | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3888 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3889 | |
3890 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | ||||
3891 | used += 16; | ||||
3892 | if( used <= buf_len ) | ||||
3893 | { | ||||
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3894 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
3895 | p += 8; | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3896 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3897 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
3898 | p += 8; | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3899 | } |
3900 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | ||||
3901 | |||||
3902 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
3903 | used += 1; | ||||
3904 | if( used <= buf_len ) | ||||
3905 | { | ||||
3906 | *p++ = ssl->disable_datagram_packing; | ||||
3907 | } | ||||
3908 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | ||||
3909 | |||||
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3910 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3911 | if( used <= buf_len ) |
3912 | { | ||||
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3913 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
3914 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3915 | } |
3916 | |||||
3917 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
3918 | used += 2; | ||||
3919 | if( used <= buf_len ) | ||||
3920 | { | ||||
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3921 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
3922 | p += 2; | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3923 | } |
3924 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | ||||
3925 | |||||
3926 | #if defined(MBEDTLS_SSL_ALPN) | ||||
3927 | { | ||||
3928 | const uint8_t alpn_len = ssl->alpn_chosen | ||||
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 3929 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3930 | : 0; |
3931 | |||||
3932 | used += 1 + alpn_len; | ||||
3933 | if( used <= buf_len ) | ||||
3934 | { | ||||
3935 | *p++ = alpn_len; | ||||
3936 | |||||
3937 | if( ssl->alpn_chosen != NULL ) | ||||
3938 | { | ||||
3939 | memcpy( p, ssl->alpn_chosen, alpn_len ); | ||||
3940 | p += alpn_len; | ||||
3941 | } | ||||
3942 | } | ||||
3943 | } | ||||
3944 | #endif /* MBEDTLS_SSL_ALPN */ | ||||
3945 | |||||
3946 | /* | ||||
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3947 | * Done |
3948 | */ | ||||
3949 | *olen = used; | ||||
3950 | |||||
3951 | if( used > buf_len ) | ||||
3952 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | ||||
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3953 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3954 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
3955 | |||||
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3956 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3957 | } |
3958 | |||||
3959 | /* | ||||
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3960 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3961 | * |
3962 | * This internal version is wrapped by a public function that cleans up in | ||||
3963 | * case of error. | ||||
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3964 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3965 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3966 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
3967 | const unsigned char *buf, | ||||
3968 | size_t len ) | ||||
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3969 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3970 | const unsigned char *p = buf; |
3971 | const unsigned char * const end = buf + len; | ||||
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3972 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3973 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3974 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3975 | /* |
3976 | * The context should have been freshly setup or reset. | ||||
3977 | * Give the user an error in case of obvious misuse. | ||||
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 3978 | * (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] | 3979 | * renegotiating, or if the user mistakenly loaded a session first.) |
3980 | */ | ||||
3981 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || | ||||
3982 | ssl->session != NULL ) | ||||
3983 | { | ||||
3984 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
3985 | } | ||||
3986 | |||||
3987 | /* | ||||
3988 | * We can't check that the config matches the initial one, but we can at | ||||
3989 | * least check it matches the requirements for serializing. | ||||
3990 | */ | ||||
3991 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | ||||
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3992 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
3993 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || | ||||
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3994 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3995 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3996 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3997 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3998 | { |
3999 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4000 | } | ||||
4001 | |||||
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4002 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
4003 | |||||
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4004 | /* |
4005 | * Check version identifier | ||||
4006 | */ | ||||
4007 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) | ||||
4008 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4009 | |||||
4010 | if( memcmp( p, ssl_serialized_context_header, | ||||
4011 | sizeof( ssl_serialized_context_header ) ) != 0 ) | ||||
4012 | { | ||||
4013 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | ||||
4014 | } | ||||
4015 | p += sizeof( ssl_serialized_context_header ); | ||||
4016 | |||||
4017 | /* | ||||
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4018 | * Session |
4019 | */ | ||||
4020 | if( (size_t)( end - p ) < 4 ) | ||||
4021 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4022 | |||||
4023 | session_len = ( (size_t) p[0] << 24 ) | | ||||
4024 | ( (size_t) p[1] << 16 ) | | ||||
4025 | ( (size_t) p[2] << 8 ) | | ||||
4026 | ( (size_t) p[3] ); | ||||
4027 | p += 4; | ||||
4028 | |||||
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4029 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4030 | * 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] | 4031 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4032 | ssl->session_in = ssl->session; |
4033 | ssl->session_out = ssl->session; | ||||
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4034 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4035 | |
4036 | if( (size_t)( end - p ) < session_len ) | ||||
4037 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4038 | |||||
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4039 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4040 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4041 | { |
4042 | mbedtls_ssl_session_free( ssl->session ); | ||||
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4043 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4044 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4045 | |
4046 | p += session_len; | ||||
4047 | |||||
4048 | /* | ||||
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4049 | * Transform |
4050 | */ | ||||
4051 | |||||
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4052 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4053 | * 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] | 4054 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4055 | ssl->transform_in = ssl->transform; |
4056 | ssl->transform_out = ssl->transform; | ||||
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4057 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4058 | |
4059 | /* Read random bytes and populate structure */ | ||||
4060 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) | ||||
4061 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4062 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 4063 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4064 | ssl->session->ciphersuite, |
4065 | ssl->session->master, | ||||
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4066 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4067 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4068 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4069 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
4070 | p, /* currently pointing to randbytes */ | ||||
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4071 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4072 | ssl->conf->endpoint, |
4073 | ssl ); | ||||
4074 | if( ret != 0 ) | ||||
4075 | return( ret ); | ||||
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4076 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4077 | p += sizeof( ssl->transform->randbytes ); |
4078 | |||||
4079 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | ||||
4080 | /* Read connection IDs and store them */ | ||||
4081 | if( (size_t)( end - p ) < 1 ) | ||||
4082 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4083 | |||||
4084 | ssl->transform->in_cid_len = *p++; | ||||
4085 | |||||
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 4086 | 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] | 4087 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
4088 | |||||
4089 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); | ||||
4090 | p += ssl->transform->in_cid_len; | ||||
4091 | |||||
4092 | ssl->transform->out_cid_len = *p++; | ||||
4093 | |||||
4094 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) | ||||
4095 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4096 | |||||
4097 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); | ||||
4098 | p += ssl->transform->out_cid_len; | ||||
4099 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | ||||
4100 | |||||
4101 | /* | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4102 | * Saved fields from top-level ssl_context structure |
4103 | */ | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4104 | if( (size_t)( end - p ) < 4 ) |
4105 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4106 | |||||
4107 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | | ||||
4108 | ( (uint32_t) p[1] << 16 ) | | ||||
4109 | ( (uint32_t) p[2] << 8 ) | | ||||
4110 | ( (uint32_t) p[3] ); | ||||
4111 | p += 4; | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4112 | |
4113 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | ||||
4114 | if( (size_t)( end - p ) < 16 ) | ||||
4115 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4116 | |||||
4117 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | | ||||
4118 | ( (uint64_t) p[1] << 48 ) | | ||||
4119 | ( (uint64_t) p[2] << 40 ) | | ||||
4120 | ( (uint64_t) p[3] << 32 ) | | ||||
4121 | ( (uint64_t) p[4] << 24 ) | | ||||
4122 | ( (uint64_t) p[5] << 16 ) | | ||||
4123 | ( (uint64_t) p[6] << 8 ) | | ||||
4124 | ( (uint64_t) p[7] ); | ||||
4125 | p += 8; | ||||
4126 | |||||
4127 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | | ||||
4128 | ( (uint64_t) p[1] << 48 ) | | ||||
4129 | ( (uint64_t) p[2] << 40 ) | | ||||
4130 | ( (uint64_t) p[3] << 32 ) | | ||||
4131 | ( (uint64_t) p[4] << 24 ) | | ||||
4132 | ( (uint64_t) p[5] << 16 ) | | ||||
4133 | ( (uint64_t) p[6] << 8 ) | | ||||
4134 | ( (uint64_t) p[7] ); | ||||
4135 | p += 8; | ||||
4136 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | ||||
4137 | |||||
4138 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
4139 | if( (size_t)( end - p ) < 1 ) | ||||
4140 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4141 | |||||
4142 | ssl->disable_datagram_packing = *p++; | ||||
4143 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | ||||
4144 | |||||
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4145 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4146 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 4147 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
4148 | p += sizeof( ssl->cur_out_ctr ); | ||||
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4149 | |
4150 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
4151 | if( (size_t)( end - p ) < 2 ) | ||||
4152 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4153 | |||||
4154 | ssl->mtu = ( p[0] << 8 ) | p[1]; | ||||
4155 | p += 2; | ||||
4156 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | ||||
4157 | |||||
4158 | #if defined(MBEDTLS_SSL_ALPN) | ||||
4159 | { | ||||
4160 | uint8_t alpn_len; | ||||
4161 | const char **cur; | ||||
4162 | |||||
4163 | if( (size_t)( end - p ) < 1 ) | ||||
4164 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4165 | |||||
4166 | alpn_len = *p++; | ||||
4167 | |||||
4168 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) | ||||
4169 | { | ||||
4170 | /* alpn_chosen should point to an item in the configured list */ | ||||
4171 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) | ||||
4172 | { | ||||
4173 | if( strlen( *cur ) == alpn_len && | ||||
4174 | memcmp( p, cur, alpn_len ) == 0 ) | ||||
4175 | { | ||||
4176 | ssl->alpn_chosen = *cur; | ||||
4177 | break; | ||||
4178 | } | ||||
4179 | } | ||||
4180 | } | ||||
4181 | |||||
4182 | /* can only happen on conf mismatch */ | ||||
4183 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) | ||||
4184 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
4185 | |||||
4186 | p += alpn_len; | ||||
4187 | } | ||||
4188 | #endif /* MBEDTLS_SSL_ALPN */ | ||||
4189 | |||||
4190 | /* | ||||
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4191 | * Forced fields from top-level ssl_context structure |
4192 | * | ||||
4193 | * Most of them already set to the correct value by mbedtls_ssl_init() and | ||||
4194 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. | ||||
4195 | */ | ||||
4196 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; | ||||
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4197 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4198 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4199 | /* Adjust pointers for header fields of outgoing records to |
4200 | * the given transform, accounting for explicit IV and CID. */ | ||||
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4201 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4202 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4203 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
4204 | ssl->in_epoch = 1; | ||||
4205 | #endif | ||||
4206 | |||||
4207 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, | ||||
4208 | * 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] | 4209 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
4210 | * inappropriately. */ | ||||
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4211 | if( ssl->handshake != NULL ) |
4212 | { | ||||
4213 | mbedtls_ssl_handshake_free( ssl ); | ||||
4214 | mbedtls_free( ssl->handshake ); | ||||
4215 | ssl->handshake = NULL; | ||||
4216 | } | ||||
4217 | |||||
4218 | /* | ||||
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4219 | * Done - should have consumed entire buffer |
4220 | */ | ||||
4221 | if( p != end ) | ||||
4222 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4223 | |
4224 | return( 0 ); | ||||
4225 | } | ||||
4226 | |||||
4227 | /* | ||||
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4228 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4229 | */ |
4230 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, | ||||
4231 | const unsigned char *buf, | ||||
4232 | size_t len ) | ||||
4233 | { | ||||
4234 | int ret = ssl_context_load( context, buf, len ); | ||||
4235 | |||||
4236 | if( ret != 0 ) | ||||
4237 | mbedtls_ssl_free( context ); | ||||
4238 | |||||
4239 | return( ret ); | ||||
4240 | } | ||||
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4241 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4242 | |
4243 | /* | ||||
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4244 | * Free an SSL context |
4245 | */ | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4246 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4247 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4248 | if( ssl == NULL ) |
4249 | return; | ||||
4250 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4251 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4252 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4253 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4254 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4255 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
4256 | size_t out_buf_len = ssl->out_buf_len; | ||||
4257 | #else | ||||
4258 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | ||||
4259 | #endif | ||||
4260 | |||||
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4261 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4262 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4263 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4264 | } |
4265 | |||||
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 4266 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4267 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4268 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
4269 | size_t in_buf_len = ssl->in_buf_len; | ||||
4270 | #else | ||||
4271 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | ||||
4272 | #endif | ||||
4273 | |||||
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4274 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4275 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4276 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4277 | } |
4278 | |||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4279 | if( ssl->transform ) |
4280 | { | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4281 | mbedtls_ssl_transform_free( ssl->transform ); |
4282 | mbedtls_free( ssl->transform ); | ||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4283 | } |
4284 | |||||
4285 | if( ssl->handshake ) | ||||
4286 | { | ||||
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4287 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4288 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
4289 | mbedtls_ssl_session_free( ssl->session_negotiate ); | ||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4291 | mbedtls_free( ssl->handshake ); |
4292 | mbedtls_free( ssl->transform_negotiate ); | ||||
4293 | mbedtls_free( ssl->session_negotiate ); | ||||
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4294 | } |
4295 | |||||
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4296 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4297 | mbedtls_ssl_transform_free( ssl->transform_application ); |
4298 | mbedtls_free( ssl->transform_application ); | ||||
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4299 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4300 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4301 | if( ssl->session ) |
4302 | { | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4303 | mbedtls_ssl_session_free( ssl->session ); |
4304 | mbedtls_free( ssl->session ); | ||||
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4305 | } |
4306 | |||||
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4307 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4308 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4309 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4310 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4311 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4312 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4313 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4314 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4315 | #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] | 4316 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4317 | #endif |
4318 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4319 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4320 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4321 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4322 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4323 | } |
4324 | |||||
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4325 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4326 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4327 | */ |
4328 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) | ||||
4329 | { | ||||
4330 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); | ||||
4331 | } | ||||
4332 | |||||
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4333 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
4334 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: | ||||
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4335 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4336 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4337 | * about this list. |
4338 | */ | ||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4339 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4340 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4341 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4342 | #endif |
4343 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | ||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4344 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4345 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4346 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4347 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4348 | #endif |
4349 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) | ||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4350 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4351 | #endif |
4352 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) | ||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4353 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4354 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4355 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4356 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4357 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4358 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4359 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4360 | #endif |
4361 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) | ||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4362 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4363 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4364 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4365 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4366 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4367 | static int ssl_preset_suiteb_ciphersuites[] = { |
4368 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | ||||
4369 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | ||||
4370 | 0 | ||||
4371 | }; | ||||
4372 | |||||
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4373 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4374 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4375 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4376 | * 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] | 4377 | * rules SHOULD be upheld. |
4378 | * - No duplicate entries. | ||||
4379 | * - But if there is a good reason, do not change the order of the algorithms. | ||||
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4380 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4381 | * - 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] | 4382 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4383 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4384 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4385 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4386 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
4387 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4388 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA && |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4389 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4390 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4391 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4392 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
4393 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4394 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4395 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
4396 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4397 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4398 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
4399 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4400 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4401 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4402 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4403 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4404 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4405 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4406 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4407 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4408 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4409 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4410 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4411 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4412 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4413 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4414 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4415 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 4416 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
4417 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ | ||||
4418 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4419 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 4420 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
4421 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ | ||||
4422 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4423 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 4424 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
4425 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ | ||||
4426 | |||||
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4427 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4428 | }; |
4429 | |||||
4430 | /* NOTICE: see above */ | ||||
4431 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
4432 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4433 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4434 | #if defined(MBEDTLS_ECDSA_C) |
4435 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ), | ||||
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4436 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4437 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
4438 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, | ||||
4439 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ | ||||
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4440 | #if defined(MBEDTLS_RSA_C) |
4441 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), | ||||
4442 | #endif | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4443 | #endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4444 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4445 | #if defined(MBEDTLS_ECDSA_C) |
4446 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ), | ||||
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 4447 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4448 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
4449 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, | ||||
4450 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ | ||||
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4451 | #if defined(MBEDTLS_RSA_C) |
4452 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), | ||||
4453 | #endif | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4454 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4455 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4456 | #if defined(MBEDTLS_ECDSA_C) |
4457 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ), | ||||
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 4458 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4459 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
4460 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, | ||||
4461 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ | ||||
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4462 | #if defined(MBEDTLS_RSA_C) |
4463 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), | ||||
4464 | #endif | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4465 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4466 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4467 | }; |
4468 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
4469 | /* NOTICE: see above */ | ||||
4470 | static uint16_t ssl_preset_suiteb_sig_algs[] = { | ||||
4471 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4472 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4473 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
4474 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4475 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4476 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
4477 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4478 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4479 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
4480 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4481 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4482 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4483 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4484 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4485 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4486 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4487 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4488 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4489 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4490 | #endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4491 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4492 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4493 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4494 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4495 | /* NOTICE: see above */ |
4496 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
4497 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4498 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4499 | #if defined(MBEDTLS_ECDSA_C) |
4500 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ), | ||||
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4501 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4502 | #if defined(MBEDTLS_RSA_C) |
4503 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), | ||||
4504 | #endif | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4505 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4506 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4507 | #if defined(MBEDTLS_ECDSA_C) |
4508 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ), | ||||
Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 4509 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4510 | #if defined(MBEDTLS_RSA_C) |
4511 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), | ||||
4512 | #endif | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4513 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4514 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4515 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4516 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
4517 | |||||
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4518 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4519 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4520 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4521 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4522 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4523 | #endif |
4524 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | ||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4525 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4526 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4527 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4528 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4529 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4530 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4531 | /* 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] | 4532 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4533 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4534 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4535 | { |
4536 | size_t i, j; | ||||
4537 | int ret = 0; | ||||
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4538 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4539 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4540 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4541 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4542 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4543 | if( sig_algs[i] != sig_algs[j] ) |
4544 | continue; | ||||
4545 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET | ||||
4546 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", | ||||
4547 | sig_algs[i], j, i ); | ||||
4548 | ret = -1; | ||||
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4549 | } |
4550 | } | ||||
4551 | return( ret ); | ||||
4552 | } | ||||
4553 | |||||
4554 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | ||||
4555 | |||||
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4556 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4557 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4558 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4559 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4560 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4561 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4562 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4563 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4564 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4565 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4566 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4567 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4568 | { |
4569 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); | ||||
4570 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | ||||
4571 | } | ||||
4572 | |||||
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4573 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4574 | { |
4575 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); | ||||
4576 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); | ||||
4577 | } | ||||
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4578 | |
4579 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4580 | 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] | 4581 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4582 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4583 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
4584 | } | ||||
4585 | |||||
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4586 | 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] | 4587 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4588 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4589 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
4590 | } | ||||
4591 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4592 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
4593 | |||||
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4594 | /* Use the functions here so that they are covered in tests, |
4595 | * but otherwise access member directly for efficiency */ | ||||
4596 | mbedtls_ssl_conf_endpoint( conf, endpoint ); | ||||
4597 | mbedtls_ssl_conf_transport( conf, transport ); | ||||
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4598 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4599 | /* |
4600 | * Things that are common to all presets | ||||
4601 | */ | ||||
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4602 | #if defined(MBEDTLS_SSL_CLI_C) |
4603 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | ||||
4604 | { | ||||
4605 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; | ||||
4606 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | ||||
4607 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; | ||||
4608 | #endif | ||||
4609 | } | ||||
4610 | #endif | ||||
4611 | |||||
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4612 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
4613 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | ||||
4614 | #endif | ||||
4615 | |||||
4616 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | ||||
4617 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | ||||
4618 | #endif | ||||
4619 | |||||
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4620 | #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] | 4621 | conf->f_cookie_write = ssl_cookie_write_dummy; |
4622 | conf->f_cookie_check = ssl_cookie_check_dummy; | ||||
4623 | #endif | ||||
4624 | |||||
4625 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | ||||
4626 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; | ||||
4627 | #endif | ||||
4628 | |||||
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4629 | #if defined(MBEDTLS_SSL_SRV_C) |
4630 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; | ||||
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4631 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4632 | #endif |
4633 | |||||
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4634 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
4635 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; | ||||
4636 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; | ||||
4637 | #endif | ||||
4638 | |||||
4639 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | ||||
4640 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; | ||||
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4641 | memset( conf->renego_period, 0x00, 2 ); |
4642 | memset( conf->renego_period + 2, 0xFF, 6 ); | ||||
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4643 | #endif |
4644 | |||||
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4645 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4646 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
4647 | { | ||||
4648 | const unsigned char dhm_p[] = | ||||
4649 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; | ||||
4650 | const unsigned char dhm_g[] = | ||||
4651 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; | ||||
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4652 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4653 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
4654 | dhm_p, sizeof( dhm_p ), | ||||
4655 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) | ||||
4656 | { | ||||
4657 | return( ret ); | ||||
4658 | } | ||||
4659 | } | ||||
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4660 | #endif |
4661 | |||||
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4662 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 4663 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 4664 | mbedtls_ssl_conf_new_session_tickets( |
4665 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS ); | ||||
4666 | #endif | ||||
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4667 | /* |
4668 | * Allow all TLS 1.3 key exchange modes by default. | ||||
4669 | */ | ||||
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4670 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4671 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4672 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4673 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
4674 | { | ||||
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4675 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4676 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4677 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4678 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4679 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4680 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4681 | } |
4682 | else | ||||
4683 | { | ||||
4684 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | ||||
4685 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | ||||
4686 | { | ||||
4687 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | ||||
4688 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; | ||||
4689 | } | ||||
4690 | else | ||||
4691 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ | ||||
4692 | { | ||||
4693 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | ||||
4694 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | ||||
4695 | } | ||||
4696 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) | ||||
4697 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; | ||||
4698 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; | ||||
4699 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
4700 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | ||||
4701 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | ||||
4702 | #else | ||||
4703 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | ||||
4704 | #endif | ||||
4705 | } | ||||
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4706 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4707 | /* |
4708 | * Preset-specific defaults | ||||
4709 | */ | ||||
4710 | switch( preset ) | ||||
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4711 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4712 | /* |
4713 | * NSA Suite B | ||||
4714 | */ | ||||
4715 | case MBEDTLS_SSL_PRESET_SUITEB: | ||||
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4716 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4717 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4718 | |
4719 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | ||||
4720 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; | ||||
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4721 | #endif |
4722 | |||||
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4723 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4724 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
4725 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) | ||||
4726 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; | ||||
4727 | else | ||||
4728 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
4729 | conf->sig_algs = ssl_preset_suiteb_sig_algs; | ||||
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4730 | #endif |
4731 | |||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4732 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
4733 | conf->curve_list = NULL; | ||||
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4734 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4735 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4736 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4737 | |
4738 | /* | ||||
4739 | * Default | ||||
4740 | */ | ||||
4741 | default: | ||||
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 4742 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4743 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4744 | |
4745 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | ||||
4746 | conf->cert_profile = &mbedtls_x509_crt_profile_default; | ||||
4747 | #endif | ||||
4748 | |||||
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4749 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4750 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
4751 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) | ||||
4752 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; | ||||
4753 | else | ||||
4754 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
4755 | conf->sig_algs = ssl_preset_default_sig_algs; | ||||
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 4756 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4757 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4758 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
4759 | conf->curve_list = NULL; | ||||
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4760 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4761 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4762 | |
4763 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | ||||
4764 | conf->dhm_min_bitlen = 1024; | ||||
4765 | #endif | ||||
4766 | } | ||||
4767 | |||||
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4768 | return( 0 ); |
4769 | } | ||||
4770 | |||||
4771 | /* | ||||
4772 | * Free mbedtls_ssl_config | ||||
4773 | */ | ||||
4774 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) | ||||
4775 | { | ||||
4776 | #if defined(MBEDTLS_DHM_C) | ||||
4777 | mbedtls_mpi_free( &conf->dhm_P ); | ||||
4778 | mbedtls_mpi_free( &conf->dhm_G ); | ||||
4779 | #endif | ||||
4780 | |||||
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4781 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4782 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
4783 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) | ||||
4784 | { | ||||
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4785 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
4786 | } | ||||
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4787 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4788 | if( conf->psk != NULL ) |
4789 | { | ||||
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4790 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4791 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4792 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4793 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4794 | } |
4795 | |||||
4796 | if( conf->psk_identity != NULL ) | ||||
4797 | { | ||||
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4798 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4799 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4800 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4801 | conf->psk_identity_len = 0; |
4802 | } | ||||
4803 | #endif | ||||
4804 | |||||
4805 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | ||||
4806 | ssl_key_cert_free( conf->key_cert ); | ||||
4807 | #endif | ||||
4808 | |||||
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4809 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4810 | } |
4811 | |||||
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4812 | #if defined(MBEDTLS_PK_C) && \ |
4813 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) | ||||
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4814 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4815 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4816 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4817 | 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] | 4818 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4819 | #if defined(MBEDTLS_RSA_C) |
4820 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) | ||||
4821 | return( MBEDTLS_SSL_SIG_RSA ); | ||||
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4822 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4823 | #if defined(MBEDTLS_ECDSA_C) |
4824 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) | ||||
4825 | return( MBEDTLS_SSL_SIG_ECDSA ); | ||||
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4826 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4827 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4828 | } |
4829 | |||||
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4830 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
4831 | { | ||||
4832 | switch( type ) { | ||||
4833 | case MBEDTLS_PK_RSA: | ||||
4834 | return( MBEDTLS_SSL_SIG_RSA ); | ||||
4835 | case MBEDTLS_PK_ECDSA: | ||||
4836 | case MBEDTLS_PK_ECKEY: | ||||
4837 | return( MBEDTLS_SSL_SIG_ECDSA ); | ||||
4838 | default: | ||||
4839 | return( MBEDTLS_SSL_SIG_ANON ); | ||||
4840 | } | ||||
4841 | } | ||||
4842 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4843 | 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] | 4844 | { |
4845 | switch( sig ) | ||||
4846 | { | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4847 | #if defined(MBEDTLS_RSA_C) |
4848 | case MBEDTLS_SSL_SIG_RSA: | ||||
4849 | return( MBEDTLS_PK_RSA ); | ||||
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4850 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4851 | #if defined(MBEDTLS_ECDSA_C) |
4852 | case MBEDTLS_SSL_SIG_ECDSA: | ||||
4853 | return( MBEDTLS_PK_ECDSA ); | ||||
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4854 | #endif |
4855 | default: | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4856 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4857 | } |
4858 | } | ||||
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4859 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4860 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4861 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4862 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4863 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4864 | 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] | 4865 | { |
4866 | switch( hash ) | ||||
4867 | { | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4868 | #if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4869 | case MBEDTLS_SSL_HASH_MD5: |
4870 | return( MBEDTLS_MD_MD5 ); | ||||
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4871 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4872 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4873 | case MBEDTLS_SSL_HASH_SHA1: |
4874 | return( MBEDTLS_MD_SHA1 ); | ||||
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4875 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4876 | #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4877 | case MBEDTLS_SSL_HASH_SHA224: |
4878 | return( MBEDTLS_MD_SHA224 ); | ||||
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4879 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4880 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4881 | case MBEDTLS_SSL_HASH_SHA256: |
4882 | return( MBEDTLS_MD_SHA256 ); | ||||
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4883 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4884 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4885 | case MBEDTLS_SSL_HASH_SHA384: |
4886 | return( MBEDTLS_MD_SHA384 ); | ||||
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4887 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4888 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4889 | case MBEDTLS_SSL_HASH_SHA512: |
4890 | return( MBEDTLS_MD_SHA512 ); | ||||
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4891 | #endif |
4892 | default: | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4893 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4894 | } |
4895 | } | ||||
4896 | |||||
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4897 | /* |
4898 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX | ||||
4899 | */ | ||||
4900 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) | ||||
4901 | { | ||||
4902 | switch( md ) | ||||
4903 | { | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4904 | #if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4905 | case MBEDTLS_MD_MD5: |
4906 | return( MBEDTLS_SSL_HASH_MD5 ); | ||||
4907 | #endif | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4908 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4909 | case MBEDTLS_MD_SHA1: |
4910 | return( MBEDTLS_SSL_HASH_SHA1 ); | ||||
4911 | #endif | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4912 | #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4913 | case MBEDTLS_MD_SHA224: |
4914 | return( MBEDTLS_SSL_HASH_SHA224 ); | ||||
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4915 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4916 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4917 | case MBEDTLS_MD_SHA256: |
4918 | return( MBEDTLS_SSL_HASH_SHA256 ); | ||||
4919 | #endif | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4920 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4921 | case MBEDTLS_MD_SHA384: |
4922 | return( MBEDTLS_SSL_HASH_SHA384 ); | ||||
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4923 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4924 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4925 | case MBEDTLS_MD_SHA512: |
4926 | return( MBEDTLS_SSL_HASH_SHA512 ); | ||||
4927 | #endif | ||||
4928 | default: | ||||
4929 | return( MBEDTLS_SSL_HASH_NONE ); | ||||
4930 | } | ||||
4931 | } | ||||
4932 | |||||
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4933 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4934 | * 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] | 4935 | * 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] | 4936 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4937 | 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] | 4938 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4939 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4940 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4941 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4942 | return( -1 ); |
4943 | |||||
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4944 | for( ; *group_list != 0; group_list++ ) |
4945 | { | ||||
4946 | if( *group_list == tls_id ) | ||||
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4947 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4948 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4949 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4950 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4951 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4952 | |
4953 | #if defined(MBEDTLS_ECP_C) | ||||
4954 | /* | ||||
4955 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. | ||||
4956 | */ | ||||
4957 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) | ||||
4958 | { | ||||
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 4959 | const mbedtls_ecp_curve_info *grp_info = |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 4960 | mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 4961 | |
Tom Cosgrove | bcc13c9 | 2022-08-24 15:08:16 +0100 | [diff] [blame] | 4962 | if ( grp_info == NULL ) |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 4963 | return -1; |
4964 | |||||
4965 | uint16_t tls_id = grp_info->tls_id; | ||||
4966 | |||||
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4967 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
4968 | } | ||||
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4969 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4970 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4971 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
4972 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, | ||||
4973 | const mbedtls_ssl_ciphersuite_t *ciphersuite, | ||||
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4974 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4975 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4976 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4977 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4978 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4979 | const char *ext_oid; |
4980 | size_t ext_len; | ||||
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4982 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4983 | { |
4984 | /* Server part of the key exchange */ | ||||
4985 | switch( ciphersuite->key_exchange ) | ||||
4986 | { | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4987 | case MBEDTLS_KEY_EXCHANGE_RSA: |
4988 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: | ||||
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4989 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4990 | break; |
4991 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4992 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
4993 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: | ||||
4994 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: | ||||
4995 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | ||||
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4996 | break; |
4997 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4998 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
4999 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: | ||||
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5000 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5001 | break; |
5002 | |||||
5003 | /* 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] | 5004 | case MBEDTLS_KEY_EXCHANGE_NONE: |
5005 | case MBEDTLS_KEY_EXCHANGE_PSK: | ||||
5006 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: | ||||
5007 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: | ||||
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5008 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5009 | usage = 0; |
5010 | } | ||||
5011 | } | ||||
5012 | else | ||||
5013 | { | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5014 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
5015 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | ||||
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5016 | } |
5017 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5018 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5019 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5020 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5021 | ret = -1; |
5022 | } | ||||
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5023 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5024 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5025 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5026 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
5027 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); | ||||
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5028 | } |
5029 | else | ||||
5030 | { | ||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5031 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
5032 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); | ||||
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5033 | } |
5034 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5035 | 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] | 5036 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5037 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5038 | ret = -1; |
5039 | } | ||||
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5040 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5041 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5042 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5043 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5044 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5045 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
5046 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, | ||||
5047 | const mbedtls_md_type_t md, | ||||
5048 | unsigned char *dst, | ||||
5049 | size_t dst_len, | ||||
5050 | size_t *olen ) | ||||
5051 | { | ||||
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5052 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
5053 | psa_hash_operation_t *hash_operation_to_clone; | ||||
5054 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); | ||||
5055 | |||||
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5056 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5057 | |
5058 | switch( md ) | ||||
5059 | { | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5060 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5061 | case MBEDTLS_MD_SHA384: |
5062 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; | ||||
5063 | break; | ||||
5064 | #endif | ||||
5065 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5066 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5067 | case MBEDTLS_MD_SHA256: |
5068 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; | ||||
5069 | break; | ||||
5070 | #endif | ||||
5071 | |||||
5072 | default: | ||||
5073 | goto exit; | ||||
5074 | } | ||||
5075 | |||||
5076 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); | ||||
5077 | if( status != PSA_SUCCESS ) | ||||
5078 | goto exit; | ||||
5079 | |||||
5080 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); | ||||
5081 | if( status != PSA_SUCCESS ) | ||||
5082 | goto exit; | ||||
5083 | |||||
5084 | exit: | ||||
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 5085 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5086 | } |
5087 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
5088 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5089 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5090 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5091 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
5092 | unsigned char *dst, | ||||
5093 | size_t dst_len, | ||||
5094 | size_t *olen ) | ||||
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5095 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5096 | int ret; |
5097 | mbedtls_sha512_context sha512; | ||||
5098 | |||||
5099 | if( dst_len < 48 ) | ||||
5100 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
5101 | |||||
5102 | mbedtls_sha512_init( &sha512 ); | ||||
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5103 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5104 | |
5105 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) | ||||
5106 | { | ||||
5107 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); | ||||
5108 | goto exit; | ||||
5109 | } | ||||
5110 | |||||
5111 | *olen = 48; | ||||
5112 | |||||
5113 | exit: | ||||
5114 | |||||
5115 | mbedtls_sha512_free( &sha512 ); | ||||
5116 | return( ret ); | ||||
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5117 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5118 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5119 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5120 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5121 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5122 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
5123 | unsigned char *dst, | ||||
5124 | size_t dst_len, | ||||
5125 | size_t *olen ) | ||||
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5126 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5127 | int ret; |
5128 | mbedtls_sha256_context sha256; | ||||
5129 | |||||
5130 | if( dst_len < 32 ) | ||||
5131 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
5132 | |||||
5133 | mbedtls_sha256_init( &sha256 ); | ||||
5134 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | ||||
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5135 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5136 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
5137 | { | ||||
5138 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); | ||||
5139 | goto exit; | ||||
5140 | } | ||||
5141 | |||||
5142 | *olen = 32; | ||||
5143 | |||||
5144 | exit: | ||||
5145 | |||||
5146 | mbedtls_sha256_free( &sha256 ); | ||||
5147 | return( ret ); | ||||
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5148 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5149 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5150 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 5151 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
5152 | const mbedtls_md_type_t md, | ||||
5153 | unsigned char *dst, | ||||
5154 | size_t dst_len, | ||||
5155 | size_t *olen ) | ||||
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5156 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5157 | switch( md ) |
5158 | { | ||||
5159 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5160 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5161 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5162 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5163 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5164 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5165 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5166 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5167 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5168 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5169 | |
5170 | default: | ||||
5171 | break; | ||||
5172 | } | ||||
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5173 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5174 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5175 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5176 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5177 | |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5178 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
5179 | /* mbedtls_ssl_parse_sig_alg_ext() | ||||
5180 | * | ||||
5181 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` | ||||
5182 | * value (TLS 1.3 RFC8446): | ||||
5183 | * enum { | ||||
5184 | * .... | ||||
5185 | * ecdsa_secp256r1_sha256( 0x0403 ), | ||||
5186 | * ecdsa_secp384r1_sha384( 0x0503 ), | ||||
5187 | * ecdsa_secp521r1_sha512( 0x0603 ), | ||||
5188 | * .... | ||||
5189 | * } SignatureScheme; | ||||
5190 | * | ||||
5191 | * struct { | ||||
5192 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; | ||||
5193 | * } SignatureSchemeList; | ||||
5194 | * | ||||
5195 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` | ||||
5196 | * value (TLS 1.2 RFC5246): | ||||
5197 | * enum { | ||||
5198 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), | ||||
5199 | * sha512(6), (255) | ||||
5200 | * } HashAlgorithm; | ||||
5201 | * | ||||
5202 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } | ||||
5203 | * SignatureAlgorithm; | ||||
5204 | * | ||||
5205 | * struct { | ||||
5206 | * HashAlgorithm hash; | ||||
5207 | * SignatureAlgorithm signature; | ||||
5208 | * } SignatureAndHashAlgorithm; | ||||
5209 | * | ||||
5210 | * SignatureAndHashAlgorithm | ||||
5211 | * supported_signature_algorithms<2..2^16-2>; | ||||
5212 | * | ||||
5213 | * The TLS 1.3 signature algorithm extension was defined to be a compatible | ||||
5214 | * generalization of the TLS 1.2 signature algorithm extension. | ||||
5215 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by | ||||
5216 | * `SignatureScheme` field of TLS 1.3 | ||||
5217 | * | ||||
5218 | */ | ||||
5219 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, | ||||
5220 | const unsigned char *buf, | ||||
5221 | const unsigned char *end ) | ||||
5222 | { | ||||
5223 | const unsigned char *p = buf; | ||||
5224 | size_t supported_sig_algs_len = 0; | ||||
5225 | const unsigned char *supported_sig_algs_end; | ||||
5226 | uint16_t sig_alg; | ||||
5227 | uint32_t common_idx = 0; | ||||
5228 | |||||
5229 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); | ||||
5230 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); | ||||
5231 | p += 2; | ||||
5232 | |||||
5233 | memset( ssl->handshake->received_sig_algs, 0, | ||||
5234 | sizeof(ssl->handshake->received_sig_algs) ); | ||||
5235 | |||||
5236 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); | ||||
5237 | supported_sig_algs_end = p + supported_sig_algs_len; | ||||
5238 | while( p < supported_sig_algs_end ) | ||||
5239 | { | ||||
5240 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); | ||||
5241 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); | ||||
5242 | p += 2; | ||||
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5243 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s", |
5244 | sig_alg, | ||||
5245 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); | ||||
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5246 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 52b7d92 | 2022-07-01 18:03:31 +0800 | [diff] [blame] | 5247 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5248 | ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) && |
5249 | mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) ) | ||||
5250 | { | ||||
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5251 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5252 | } |
5253 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5254 | |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 5255 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s", |
5256 | mbedtls_ssl_sig_alg_to_str( sig_alg ) ) ); | ||||
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5257 | |
5258 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) | ||||
5259 | { | ||||
5260 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; | ||||
5261 | common_idx += 1; | ||||
5262 | } | ||||
5263 | } | ||||
5264 | /* Check that we consumed all the message. */ | ||||
5265 | if( p != end ) | ||||
5266 | { | ||||
5267 | MBEDTLS_SSL_DEBUG_MSG( 1, | ||||
5268 | ( "Signature algorithms extension length misaligned" ) ); | ||||
5269 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, | ||||
5270 | MBEDTLS_ERR_SSL_DECODE_ERROR ); | ||||
5271 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | ||||
5272 | } | ||||
5273 | |||||
5274 | if( common_idx == 0 ) | ||||
5275 | { | ||||
5276 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); | ||||
5277 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, | ||||
5278 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); | ||||
5279 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); | ||||
5280 | } | ||||
5281 | |||||
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5282 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5283 | return( 0 ); |
5284 | } | ||||
5285 | |||||
5286 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | ||||
5287 | |||||
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5288 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
5289 | |||||
5290 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
5291 | |||||
5292 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, | ||||
5293 | mbedtls_svc_key_id_t key, | ||||
5294 | psa_algorithm_t alg, | ||||
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5295 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5296 | const unsigned char* seed, size_t seed_length, |
5297 | const unsigned char* label, size_t label_length, | ||||
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5298 | const unsigned char* other_secret, |
5299 | size_t other_secret_length, | ||||
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5300 | size_t capacity ) |
5301 | { | ||||
5302 | psa_status_t status; | ||||
5303 | |||||
5304 | status = psa_key_derivation_setup( derivation, alg ); | ||||
5305 | if( status != PSA_SUCCESS ) | ||||
5306 | return( status ); | ||||
5307 | |||||
5308 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) | ||||
5309 | { | ||||
5310 | status = psa_key_derivation_input_bytes( derivation, | ||||
5311 | PSA_KEY_DERIVATION_INPUT_SEED, | ||||
5312 | seed, seed_length ); | ||||
5313 | if( status != PSA_SUCCESS ) | ||||
5314 | return( status ); | ||||
5315 | |||||
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5316 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5317 | { |
5318 | status = psa_key_derivation_input_bytes( derivation, | ||||
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5319 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
5320 | other_secret, other_secret_length ); | ||||
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5321 | if( status != PSA_SUCCESS ) |
5322 | return( status ); | ||||
5323 | } | ||||
5324 | |||||
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5325 | if( mbedtls_svc_key_id_is_null( key ) ) |
5326 | { | ||||
5327 | status = psa_key_derivation_input_bytes( | ||||
5328 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, | ||||
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5329 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5330 | } |
5331 | else | ||||
5332 | { | ||||
5333 | status = psa_key_derivation_input_key( | ||||
5334 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); | ||||
5335 | } | ||||
5336 | if( status != PSA_SUCCESS ) | ||||
5337 | return( status ); | ||||
5338 | |||||
5339 | status = psa_key_derivation_input_bytes( derivation, | ||||
5340 | PSA_KEY_DERIVATION_INPUT_LABEL, | ||||
5341 | label, label_length ); | ||||
5342 | if( status != PSA_SUCCESS ) | ||||
5343 | return( status ); | ||||
5344 | } | ||||
5345 | else | ||||
5346 | { | ||||
5347 | return( PSA_ERROR_NOT_SUPPORTED ); | ||||
5348 | } | ||||
5349 | |||||
5350 | status = psa_key_derivation_set_capacity( derivation, capacity ); | ||||
5351 | if( status != PSA_SUCCESS ) | ||||
5352 | return( status ); | ||||
5353 | |||||
5354 | return( PSA_SUCCESS ); | ||||
5355 | } | ||||
5356 | |||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5357 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5358 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
5359 | const unsigned char *secret, size_t slen, | ||||
5360 | const char *label, | ||||
5361 | const unsigned char *random, size_t rlen, | ||||
5362 | unsigned char *dstbuf, size_t dlen ) | ||||
5363 | { | ||||
5364 | psa_status_t status; | ||||
5365 | psa_algorithm_t alg; | ||||
5366 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; | ||||
5367 | psa_key_derivation_operation_t derivation = | ||||
5368 | PSA_KEY_DERIVATION_OPERATION_INIT; | ||||
5369 | |||||
5370 | if( md_type == MBEDTLS_MD_SHA384 ) | ||||
5371 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); | ||||
5372 | else | ||||
5373 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); | ||||
5374 | |||||
5375 | /* Normally a "secret" should be long enough to be impossible to | ||||
5376 | * find by brute force, and in particular should not be empty. But | ||||
5377 | * this PRF is also used to derive an IV, in particular in EAP-TLS, | ||||
5378 | * and for this use case it makes sense to have a 0-length "secret". | ||||
5379 | * Since the key API doesn't allow importing a key of length 0, | ||||
5380 | * keep master_key=0, which setup_psa_key_derivation() understands | ||||
5381 | * to mean a 0-length "secret" input. */ | ||||
5382 | if( slen != 0 ) | ||||
5383 | { | ||||
5384 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); | ||||
5385 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); | ||||
5386 | psa_set_key_algorithm( &key_attributes, alg ); | ||||
5387 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); | ||||
5388 | |||||
5389 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); | ||||
5390 | if( status != PSA_SUCCESS ) | ||||
5391 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
5392 | } | ||||
5393 | |||||
5394 | status = setup_psa_key_derivation( &derivation, | ||||
5395 | master_key, alg, | ||||
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5396 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5397 | random, rlen, |
5398 | (unsigned char const *) label, | ||||
5399 | (size_t) strlen( label ), | ||||
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5400 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5401 | dlen ); |
5402 | if( status != PSA_SUCCESS ) | ||||
5403 | { | ||||
5404 | psa_key_derivation_abort( &derivation ); | ||||
5405 | psa_destroy_key( master_key ); | ||||
5406 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
5407 | } | ||||
5408 | |||||
5409 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); | ||||
5410 | if( status != PSA_SUCCESS ) | ||||
5411 | { | ||||
5412 | psa_key_derivation_abort( &derivation ); | ||||
5413 | psa_destroy_key( master_key ); | ||||
5414 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
5415 | } | ||||
5416 | |||||
5417 | status = psa_key_derivation_abort( &derivation ); | ||||
5418 | if( status != PSA_SUCCESS ) | ||||
5419 | { | ||||
5420 | psa_destroy_key( master_key ); | ||||
5421 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
5422 | } | ||||
5423 | |||||
5424 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) | ||||
5425 | status = psa_destroy_key( master_key ); | ||||
5426 | if( status != PSA_SUCCESS ) | ||||
5427 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
5428 | |||||
5429 | return( 0 ); | ||||
5430 | } | ||||
5431 | |||||
5432 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
5433 | |||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5434 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5435 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
5436 | const unsigned char *secret, size_t slen, | ||||
5437 | const char *label, | ||||
5438 | const unsigned char *random, size_t rlen, | ||||
5439 | unsigned char *dstbuf, size_t dlen ) | ||||
5440 | { | ||||
5441 | size_t nb; | ||||
5442 | size_t i, j, k, md_len; | ||||
5443 | unsigned char *tmp; | ||||
5444 | size_t tmp_len = 0; | ||||
5445 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; | ||||
5446 | const mbedtls_md_info_t *md_info; | ||||
5447 | mbedtls_md_context_t md_ctx; | ||||
5448 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
5449 | |||||
5450 | mbedtls_md_init( &md_ctx ); | ||||
5451 | |||||
5452 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) | ||||
5453 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
5454 | |||||
5455 | md_len = mbedtls_md_get_size( md_info ); | ||||
5456 | |||||
5457 | tmp_len = md_len + strlen( label ) + rlen; | ||||
5458 | tmp = mbedtls_calloc( 1, tmp_len ); | ||||
5459 | if( tmp == NULL ) | ||||
5460 | { | ||||
5461 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | ||||
5462 | goto exit; | ||||
5463 | } | ||||
5464 | |||||
5465 | nb = strlen( label ); | ||||
5466 | memcpy( tmp + md_len, label, nb ); | ||||
5467 | memcpy( tmp + md_len + nb, random, rlen ); | ||||
5468 | nb += rlen; | ||||
5469 | |||||
5470 | /* | ||||
5471 | * Compute P_<hash>(secret, label + random)[0..dlen] | ||||
5472 | */ | ||||
5473 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) | ||||
5474 | goto exit; | ||||
5475 | |||||
5476 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); | ||||
5477 | if( ret != 0 ) | ||||
5478 | goto exit; | ||||
5479 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); | ||||
5480 | if( ret != 0 ) | ||||
5481 | goto exit; | ||||
5482 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); | ||||
5483 | if( ret != 0 ) | ||||
5484 | goto exit; | ||||
5485 | |||||
5486 | for( i = 0; i < dlen; i += md_len ) | ||||
5487 | { | ||||
5488 | ret = mbedtls_md_hmac_reset ( &md_ctx ); | ||||
5489 | if( ret != 0 ) | ||||
5490 | goto exit; | ||||
5491 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); | ||||
5492 | if( ret != 0 ) | ||||
5493 | goto exit; | ||||
5494 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); | ||||
5495 | if( ret != 0 ) | ||||
5496 | goto exit; | ||||
5497 | |||||
5498 | ret = mbedtls_md_hmac_reset ( &md_ctx ); | ||||
5499 | if( ret != 0 ) | ||||
5500 | goto exit; | ||||
5501 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); | ||||
5502 | if( ret != 0 ) | ||||
5503 | goto exit; | ||||
5504 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); | ||||
5505 | if( ret != 0 ) | ||||
5506 | goto exit; | ||||
5507 | |||||
5508 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; | ||||
5509 | |||||
5510 | for( j = 0; j < k; j++ ) | ||||
5511 | dstbuf[i + j] = h_i[j]; | ||||
5512 | } | ||||
5513 | |||||
5514 | exit: | ||||
5515 | mbedtls_md_free( &md_ctx ); | ||||
5516 | |||||
5517 | mbedtls_platform_zeroize( tmp, tmp_len ); | ||||
5518 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); | ||||
5519 | |||||
5520 | mbedtls_free( tmp ); | ||||
5521 | |||||
5522 | return( ret ); | ||||
5523 | } | ||||
5524 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
5525 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5526 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5527 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5528 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
5529 | const char *label, | ||||
5530 | const unsigned char *random, size_t rlen, | ||||
5531 | unsigned char *dstbuf, size_t dlen ) | ||||
5532 | { | ||||
5533 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, | ||||
5534 | label, random, rlen, dstbuf, dlen ) ); | ||||
5535 | } | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5536 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5537 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5538 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5539 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5540 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
5541 | const char *label, | ||||
5542 | const unsigned char *random, size_t rlen, | ||||
5543 | unsigned char *dstbuf, size_t dlen ) | ||||
5544 | { | ||||
5545 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, | ||||
5546 | label, random, rlen, dstbuf, dlen ) ); | ||||
5547 | } | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5548 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5549 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5550 | /* |
5551 | * Set appropriate PRF function and other SSL / TLS1.2 functions | ||||
5552 | * | ||||
5553 | * Inputs: | ||||
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5554 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
5555 | * | ||||
5556 | * Outputs: | ||||
5557 | * - the tls_prf, calc_verify and calc_finished members of handshake structure | ||||
5558 | */ | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5559 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5560 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5561 | mbedtls_md_type_t hash ) |
5562 | { | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5563 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5564 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5565 | { |
5566 | handshake->tls_prf = tls_prf_sha384; | ||||
5567 | handshake->calc_verify = ssl_calc_verify_tls_sha384; | ||||
5568 | handshake->calc_finished = ssl_calc_finished_tls_sha384; | ||||
5569 | } | ||||
5570 | else | ||||
5571 | #endif | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5572 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5573 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5574 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5575 | handshake->tls_prf = tls_prf_sha256; |
5576 | handshake->calc_verify = ssl_calc_verify_tls_sha256; | ||||
5577 | handshake->calc_finished = ssl_calc_finished_tls_sha256; | ||||
5578 | } | ||||
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5579 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5580 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5581 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5582 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5583 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
5584 | } | ||||
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5585 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5586 | |
5587 | return( 0 ); | ||||
5588 | } | ||||
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5589 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5590 | /* |
5591 | * Compute master secret if needed | ||||
5592 | * | ||||
5593 | * Parameters: | ||||
5594 | * [in/out] handshake | ||||
5595 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf | ||||
5596 | * (PSA-PSK) ciphersuite_info, psk_opaque | ||||
5597 | * [out] premaster (cleared) | ||||
5598 | * [out] master | ||||
5599 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK | ||||
5600 | * debug: conf->f_dbg, conf->p_dbg | ||||
5601 | * EMS: passed to calc_verify (debug + session_negotiate) | ||||
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5602 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5603 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5604 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5605 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
5606 | unsigned char *master, | ||||
5607 | const mbedtls_ssl_context *ssl ) | ||||
5608 | { | ||||
5609 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
5610 | |||||
5611 | /* cf. RFC 5246, Section 8.1: | ||||
5612 | * "The master secret is always exactly 48 bytes in length." */ | ||||
5613 | size_t const master_secret_len = 48; | ||||
5614 | |||||
5615 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | ||||
5616 | unsigned char session_hash[48]; | ||||
5617 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | ||||
5618 | |||||
5619 | /* The label for the KDF used for key expansion. | ||||
5620 | * This is either "master secret" or "extended master secret" | ||||
5621 | * depending on whether the Extended Master Secret extension | ||||
5622 | * is used. */ | ||||
5623 | char const *lbl = "master secret"; | ||||
5624 | |||||
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5625 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5626 | * - If the Extended Master Secret extension is not used, |
5627 | * this is ClientHello.Random + ServerHello.Random | ||||
5628 | * (see Sect. 8.1 in RFC 5246). | ||||
5629 | * - If the Extended Master Secret extension is used, | ||||
5630 | * this is the transcript of the handshake so far. | ||||
5631 | * (see Sect. 4 in RFC 7627). */ | ||||
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5632 | unsigned char const *seed = handshake->randbytes; |
5633 | size_t seed_len = 64; | ||||
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5634 | |
5635 | #if !defined(MBEDTLS_DEBUG_C) && \ | ||||
5636 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ | ||||
5637 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ | ||||
5638 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) | ||||
5639 | ssl = NULL; /* make sure we don't use it except for those cases */ | ||||
5640 | (void) ssl; | ||||
5641 | #endif | ||||
5642 | |||||
5643 | if( handshake->resume != 0 ) | ||||
5644 | { | ||||
5645 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); | ||||
5646 | return( 0 ); | ||||
5647 | } | ||||
5648 | |||||
5649 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | ||||
5650 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) | ||||
5651 | { | ||||
5652 | lbl = "extended master secret"; | ||||
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5653 | seed = session_hash; |
5654 | handshake->calc_verify( ssl, session_hash, &seed_len ); | ||||
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5655 | |
5656 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", | ||||
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5657 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5658 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 5659 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5660 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 5661 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 5662 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 5663 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5664 | { |
5665 | /* Perform PSK-to-MS expansion in a single step. */ | ||||
5666 | psa_status_t status; | ||||
5667 | psa_algorithm_t alg; | ||||
5668 | mbedtls_svc_key_id_t psk; | ||||
5669 | psa_key_derivation_operation_t derivation = | ||||
5670 | PSA_KEY_DERIVATION_OPERATION_INIT; | ||||
5671 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; | ||||
5672 | |||||
5673 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); | ||||
5674 | |||||
5675 | psk = mbedtls_ssl_get_opaque_psk( ssl ); | ||||
5676 | |||||
5677 | if( hash_alg == MBEDTLS_MD_SHA384 ) | ||||
5678 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); | ||||
5679 | else | ||||
5680 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); | ||||
5681 | |||||
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5682 | size_t other_secret_len = 0; |
5683 | unsigned char* other_secret = NULL; | ||||
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5684 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5685 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5686 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5687 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5688 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5689 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5690 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5691 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5692 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5693 | other_secret_len = 48; |
5694 | other_secret = handshake->premaster + 2; | ||||
5695 | break; | ||||
5696 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: | ||||
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5697 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5698 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
5699 | other_secret = handshake->premaster + 2; | ||||
5700 | break; | ||||
5701 | default: | ||||
5702 | break; | ||||
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5703 | } |
5704 | |||||
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5705 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5706 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5707 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5708 | (unsigned char const *) lbl, |
5709 | (size_t) strlen( lbl ), | ||||
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5710 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5711 | master_secret_len ); |
5712 | if( status != PSA_SUCCESS ) | ||||
5713 | { | ||||
5714 | psa_key_derivation_abort( &derivation ); | ||||
5715 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
5716 | } | ||||
5717 | |||||
5718 | status = psa_key_derivation_output_bytes( &derivation, | ||||
5719 | master, | ||||
5720 | master_secret_len ); | ||||
5721 | if( status != PSA_SUCCESS ) | ||||
5722 | { | ||||
5723 | psa_key_derivation_abort( &derivation ); | ||||
5724 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
5725 | } | ||||
5726 | |||||
5727 | status = psa_key_derivation_abort( &derivation ); | ||||
5728 | if( status != PSA_SUCCESS ) | ||||
5729 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
5730 | } | ||||
5731 | else | ||||
5732 | #endif | ||||
5733 | { | ||||
5734 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, | ||||
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5735 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5736 | master, |
5737 | master_secret_len ); | ||||
5738 | if( ret != 0 ) | ||||
5739 | { | ||||
5740 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | ||||
5741 | return( ret ); | ||||
5742 | } | ||||
5743 | |||||
5744 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", | ||||
5745 | handshake->premaster, | ||||
5746 | handshake->pmslen ); | ||||
5747 | |||||
5748 | mbedtls_platform_zeroize( handshake->premaster, | ||||
5749 | sizeof(handshake->premaster) ); | ||||
5750 | } | ||||
5751 | |||||
5752 | return( 0 ); | ||||
5753 | } | ||||
5754 | |||||
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5755 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
5756 | { | ||||
5757 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
5758 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = | ||||
5759 | ssl->handshake->ciphersuite_info; | ||||
5760 | |||||
5761 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); | ||||
5762 | |||||
5763 | /* Set PRF, calc_verify and calc_finished function pointers */ | ||||
5764 | ret = ssl_set_handshake_prfs( ssl->handshake, | ||||
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5765 | ciphersuite_info->mac ); |
5766 | if( ret != 0 ) | ||||
5767 | { | ||||
5768 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); | ||||
5769 | return( ret ); | ||||
5770 | } | ||||
5771 | |||||
5772 | /* Compute master secret if needed */ | ||||
5773 | ret = ssl_compute_master( ssl->handshake, | ||||
5774 | ssl->session_negotiate->master, | ||||
5775 | ssl ); | ||||
5776 | if( ret != 0 ) | ||||
5777 | { | ||||
5778 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); | ||||
5779 | return( ret ); | ||||
5780 | } | ||||
5781 | |||||
5782 | /* Swap the client and server random values: | ||||
5783 | * - MS derivation wanted client+server (RFC 5246 8.1) | ||||
5784 | * - key derivation wants server+client (RFC 5246 6.3) */ | ||||
5785 | { | ||||
5786 | unsigned char tmp[64]; | ||||
5787 | memcpy( tmp, ssl->handshake->randbytes, 64 ); | ||||
5788 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); | ||||
5789 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); | ||||
5790 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | ||||
5791 | } | ||||
5792 | |||||
5793 | /* Populate transform structure */ | ||||
5794 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, | ||||
5795 | ssl->session_negotiate->ciphersuite, | ||||
5796 | ssl->session_negotiate->master, | ||||
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5797 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5798 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5799 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5800 | ssl->handshake->tls_prf, |
5801 | ssl->handshake->randbytes, | ||||
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5802 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5803 | ssl->conf->endpoint, |
5804 | ssl ); | ||||
5805 | if( ret != 0 ) | ||||
5806 | { | ||||
5807 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); | ||||
5808 | return( ret ); | ||||
5809 | } | ||||
5810 | |||||
5811 | /* We no longer need Server/ClientHello.random values */ | ||||
5812 | mbedtls_platform_zeroize( ssl->handshake->randbytes, | ||||
5813 | sizeof( ssl->handshake->randbytes ) ); | ||||
5814 | |||||
5815 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); | ||||
5816 | |||||
5817 | return( 0 ); | ||||
5818 | } | ||||
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5819 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5820 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
5821 | { | ||||
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5822 | switch( md ) |
5823 | { | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5824 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5825 | case MBEDTLS_SSL_HASH_SHA384: |
5826 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; | ||||
5827 | break; | ||||
5828 | #endif | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5829 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5830 | case MBEDTLS_SSL_HASH_SHA256: |
5831 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; | ||||
5832 | break; | ||||
5833 | #endif | ||||
5834 | default: | ||||
5835 | return( -1 ); | ||||
5836 | } | ||||
5837 | |||||
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 5838 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5839 | } |
5840 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5841 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5842 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
5843 | unsigned char *hash, | ||||
5844 | size_t *hlen ) | ||||
5845 | { | ||||
5846 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
5847 | size_t hash_size; | ||||
5848 | psa_status_t status; | ||||
5849 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); | ||||
5850 | |||||
5851 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); | ||||
5852 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); | ||||
5853 | if( status != PSA_SUCCESS ) | ||||
5854 | { | ||||
5855 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | ||||
5856 | return; | ||||
5857 | } | ||||
5858 | |||||
5859 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); | ||||
5860 | if( status != PSA_SUCCESS ) | ||||
5861 | { | ||||
5862 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | ||||
5863 | return; | ||||
5864 | } | ||||
5865 | |||||
5866 | *hlen = 32; | ||||
5867 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); | ||||
5868 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); | ||||
5869 | #else | ||||
5870 | mbedtls_sha256_context sha256; | ||||
5871 | |||||
5872 | mbedtls_sha256_init( &sha256 ); | ||||
5873 | |||||
5874 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); | ||||
5875 | |||||
5876 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | ||||
5877 | mbedtls_sha256_finish( &sha256, hash ); | ||||
5878 | |||||
5879 | *hlen = 32; | ||||
5880 | |||||
5881 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | ||||
5882 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | ||||
5883 | |||||
5884 | mbedtls_sha256_free( &sha256 ); | ||||
5885 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
5886 | return; | ||||
5887 | } | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5888 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5889 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5890 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5891 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
5892 | unsigned char *hash, | ||||
5893 | size_t *hlen ) | ||||
5894 | { | ||||
5895 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
5896 | size_t hash_size; | ||||
5897 | psa_status_t status; | ||||
5898 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); | ||||
5899 | |||||
5900 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); | ||||
5901 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); | ||||
5902 | if( status != PSA_SUCCESS ) | ||||
5903 | { | ||||
5904 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | ||||
5905 | return; | ||||
5906 | } | ||||
5907 | |||||
5908 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); | ||||
5909 | if( status != PSA_SUCCESS ) | ||||
5910 | { | ||||
5911 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | ||||
5912 | return; | ||||
5913 | } | ||||
5914 | |||||
5915 | *hlen = 48; | ||||
5916 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); | ||||
5917 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); | ||||
5918 | #else | ||||
5919 | mbedtls_sha512_context sha512; | ||||
5920 | |||||
5921 | mbedtls_sha512_init( &sha512 ); | ||||
5922 | |||||
5923 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); | ||||
5924 | |||||
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 5925 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5926 | mbedtls_sha512_finish( &sha512, hash ); |
5927 | |||||
5928 | *hlen = 48; | ||||
5929 | |||||
5930 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | ||||
5931 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | ||||
5932 | |||||
5933 | mbedtls_sha512_free( &sha512 ); | ||||
5934 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
5935 | return; | ||||
5936 | } | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5937 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5938 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 5939 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
5940 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | ||||
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5941 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
5942 | { | ||||
5943 | unsigned char *p = ssl->handshake->premaster; | ||||
5944 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); | ||||
5945 | const unsigned char *psk = NULL; | ||||
5946 | size_t psk_len = 0; | ||||
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5947 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5948 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5949 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5950 | { |
5951 | /* | ||||
5952 | * This should never happen because the existence of a PSK is always | ||||
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5953 | * checked before calling this function. |
5954 | * | ||||
5955 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with | ||||
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5956 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5957 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5958 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
5959 | { | ||||
5960 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | ||||
5961 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
5962 | } | ||||
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5963 | } |
5964 | |||||
5965 | /* | ||||
5966 | * PMS = struct { | ||||
5967 | * opaque other_secret<0..2^16-1>; | ||||
5968 | * opaque psk<0..2^16-1>; | ||||
5969 | * }; | ||||
5970 | * with "other_secret" depending on the particular key exchange | ||||
5971 | */ | ||||
5972 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | ||||
5973 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) | ||||
5974 | { | ||||
5975 | if( end - p < 2 ) | ||||
5976 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
5977 | |||||
5978 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); | ||||
5979 | p += 2; | ||||
5980 | |||||
5981 | if( end < p || (size_t)( end - p ) < psk_len ) | ||||
5982 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
5983 | |||||
5984 | memset( p, 0, psk_len ); | ||||
5985 | p += psk_len; | ||||
5986 | } | ||||
5987 | else | ||||
5988 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | ||||
5989 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | ||||
5990 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | ||||
5991 | { | ||||
5992 | /* | ||||
5993 | * other_secret already set by the ClientKeyExchange message, | ||||
5994 | * and is 48 bytes long | ||||
5995 | */ | ||||
5996 | if( end - p < 2 ) | ||||
5997 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
5998 | |||||
5999 | *p++ = 0; | ||||
6000 | *p++ = 48; | ||||
6001 | p += 48; | ||||
6002 | } | ||||
6003 | else | ||||
6004 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | ||||
6005 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | ||||
6006 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | ||||
6007 | { | ||||
6008 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
6009 | size_t len; | ||||
6010 | |||||
6011 | /* Write length only when we know the actual value */ | ||||
6012 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | ||||
6013 | p + 2, end - ( p + 2 ), &len, | ||||
6014 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | ||||
6015 | { | ||||
6016 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | ||||
6017 | return( ret ); | ||||
6018 | } | ||||
6019 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); | ||||
6020 | p += 2 + len; | ||||
6021 | |||||
6022 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); | ||||
6023 | } | ||||
6024 | else | ||||
6025 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | ||||
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6026 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6027 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
6028 | { | ||||
6029 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
6030 | size_t zlen; | ||||
6031 | |||||
6032 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, | ||||
6033 | p + 2, end - ( p + 2 ), | ||||
6034 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | ||||
6035 | { | ||||
6036 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | ||||
6037 | return( ret ); | ||||
6038 | } | ||||
6039 | |||||
6040 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); | ||||
6041 | p += 2 + zlen; | ||||
6042 | |||||
6043 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | ||||
6044 | MBEDTLS_DEBUG_ECDH_Z ); | ||||
6045 | } | ||||
6046 | else | ||||
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6047 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6048 | { |
6049 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | ||||
6050 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
6051 | } | ||||
6052 | |||||
6053 | /* opaque psk<0..2^16-1>; */ | ||||
6054 | if( end - p < 2 ) | ||||
6055 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
6056 | |||||
6057 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); | ||||
6058 | p += 2; | ||||
6059 | |||||
6060 | if( end < p || (size_t)( end - p ) < psk_len ) | ||||
6061 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
6062 | |||||
6063 | memcpy( p, psk, psk_len ); | ||||
6064 | p += psk_len; | ||||
6065 | |||||
6066 | ssl->handshake->pmslen = p - ssl->handshake->premaster; | ||||
6067 | |||||
6068 | return( 0 ); | ||||
6069 | } | ||||
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6070 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6071 | |
6072 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6073 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6074 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
6075 | |||||
6076 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
6077 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) | ||||
6078 | { | ||||
6079 | /* If renegotiation is not enforced, retransmit until we would reach max | ||||
6080 | * timeout if we were using the usual handshake doubling scheme */ | ||||
6081 | if( ssl->conf->renego_max_records < 0 ) | ||||
6082 | { | ||||
6083 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; | ||||
6084 | unsigned char doublings = 1; | ||||
6085 | |||||
6086 | while( ratio != 0 ) | ||||
6087 | { | ||||
6088 | ++doublings; | ||||
6089 | ratio >>= 1; | ||||
6090 | } | ||||
6091 | |||||
6092 | if( ++ssl->renego_records_seen > doublings ) | ||||
6093 | { | ||||
6094 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); | ||||
6095 | return( 0 ); | ||||
6096 | } | ||||
6097 | } | ||||
6098 | |||||
6099 | return( ssl_write_hello_request( ssl ) ); | ||||
6100 | } | ||||
6101 | #endif | ||||
6102 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | ||||
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6103 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6104 | /* |
6105 | * Handshake functions | ||||
6106 | */ | ||||
6107 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | ||||
6108 | /* No certificate support -> dummy functions */ | ||||
6109 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | ||||
6110 | { | ||||
6111 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | ||||
6112 | ssl->handshake->ciphersuite_info; | ||||
6113 | |||||
6114 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | ||||
6115 | |||||
6116 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | ||||
6117 | { | ||||
6118 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | ||||
6119 | ssl->state++; | ||||
6120 | return( 0 ); | ||||
6121 | } | ||||
6122 | |||||
6123 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | ||||
6124 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
6125 | } | ||||
6126 | |||||
6127 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | ||||
6128 | { | ||||
6129 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | ||||
6130 | ssl->handshake->ciphersuite_info; | ||||
6131 | |||||
6132 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | ||||
6133 | |||||
6134 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | ||||
6135 | { | ||||
6136 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | ||||
6137 | ssl->state++; | ||||
6138 | return( 0 ); | ||||
6139 | } | ||||
6140 | |||||
6141 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | ||||
6142 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
6143 | } | ||||
6144 | |||||
6145 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | ||||
6146 | /* Some certificate support -> implement write and parse */ | ||||
6147 | |||||
6148 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | ||||
6149 | { | ||||
6150 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | ||||
6151 | size_t i, n; | ||||
6152 | const mbedtls_x509_crt *crt; | ||||
6153 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | ||||
6154 | ssl->handshake->ciphersuite_info; | ||||
6155 | |||||
6156 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | ||||
6157 | |||||
6158 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | ||||
6159 | { | ||||
6160 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | ||||
6161 | ssl->state++; | ||||
6162 | return( 0 ); | ||||
6163 | } | ||||
6164 | |||||
6165 | #if defined(MBEDTLS_SSL_CLI_C) | ||||
6166 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | ||||
6167 | { | ||||
6168 | if( ssl->handshake->client_auth == 0 ) | ||||
6169 | { | ||||
6170 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | ||||
6171 | ssl->state++; | ||||
6172 | return( 0 ); | ||||
6173 | } | ||||
6174 | } | ||||
6175 | #endif /* MBEDTLS_SSL_CLI_C */ | ||||
6176 | #if defined(MBEDTLS_SSL_SRV_C) | ||||
6177 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | ||||
6178 | { | ||||
6179 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) | ||||
6180 | { | ||||
6181 | /* Should never happen because we shouldn't have picked the | ||||
6182 | * ciphersuite if we don't have a certificate. */ | ||||
6183 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
6184 | } | ||||
6185 | } | ||||
6186 | #endif | ||||
6187 | |||||
6188 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); | ||||
6189 | |||||
6190 | /* | ||||
6191 | * 0 . 0 handshake type | ||||
6192 | * 1 . 3 handshake length | ||||
6193 | * 4 . 6 length of all certs | ||||
6194 | * 7 . 9 length of cert. 1 | ||||
6195 | * 10 . n-1 peer certificate | ||||
6196 | * n . n+2 length of cert. 2 | ||||
6197 | * n+3 . ... upper level cert, etc. | ||||
6198 | */ | ||||
6199 | i = 7; | ||||
6200 | crt = mbedtls_ssl_own_cert( ssl ); | ||||
6201 | |||||
6202 | while( crt != NULL ) | ||||
6203 | { | ||||
6204 | n = crt->raw.len; | ||||
6205 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) | ||||
6206 | { | ||||
6207 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET | ||||
6208 | " > %" MBEDTLS_PRINTF_SIZET, | ||||
6209 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); | ||||
6210 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | ||||
6211 | } | ||||
6212 | |||||
6213 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); | ||||
6214 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); | ||||
6215 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); | ||||
6216 | |||||
6217 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); | ||||
6218 | i += n; crt = crt->next; | ||||
6219 | } | ||||
6220 | |||||
6221 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); | ||||
6222 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); | ||||
6223 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); | ||||
6224 | |||||
6225 | ssl->out_msglen = i; | ||||
6226 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | ||||
6227 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; | ||||
6228 | |||||
6229 | ssl->state++; | ||||
6230 | |||||
6231 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | ||||
6232 | { | ||||
6233 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | ||||
6234 | return( ret ); | ||||
6235 | } | ||||
6236 | |||||
6237 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); | ||||
6238 | |||||
6239 | return( ret ); | ||||
6240 | } | ||||
6241 | |||||
6242 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | ||||
6243 | |||||
6244 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6245 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6246 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
6247 | unsigned char *crt_buf, | ||||
6248 | size_t crt_buf_len ) | ||||
6249 | { | ||||
6250 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; | ||||
6251 | |||||
6252 | if( peer_crt == NULL ) | ||||
6253 | return( -1 ); | ||||
6254 | |||||
6255 | if( peer_crt->raw.len != crt_buf_len ) | ||||
6256 | return( -1 ); | ||||
6257 | |||||
6258 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); | ||||
6259 | } | ||||
6260 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6261 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6262 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
6263 | unsigned char *crt_buf, | ||||
6264 | size_t crt_buf_len ) | ||||
6265 | { | ||||
6266 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
6267 | unsigned char const * const peer_cert_digest = | ||||
6268 | ssl->session->peer_cert_digest; | ||||
6269 | mbedtls_md_type_t const peer_cert_digest_type = | ||||
6270 | ssl->session->peer_cert_digest_type; | ||||
6271 | mbedtls_md_info_t const * const digest_info = | ||||
6272 | mbedtls_md_info_from_type( peer_cert_digest_type ); | ||||
6273 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; | ||||
6274 | size_t digest_len; | ||||
6275 | |||||
6276 | if( peer_cert_digest == NULL || digest_info == NULL ) | ||||
6277 | return( -1 ); | ||||
6278 | |||||
6279 | digest_len = mbedtls_md_get_size( digest_info ); | ||||
6280 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) | ||||
6281 | return( -1 ); | ||||
6282 | |||||
6283 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); | ||||
6284 | if( ret != 0 ) | ||||
6285 | return( -1 ); | ||||
6286 | |||||
6287 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); | ||||
6288 | } | ||||
6289 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
6290 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | ||||
6291 | |||||
6292 | /* | ||||
6293 | * Once the certificate message is read, parse it into a cert chain and | ||||
6294 | * perform basic checks, but leave actual verification to the caller | ||||
6295 | */ | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6296 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6297 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
6298 | mbedtls_x509_crt *chain ) | ||||
6299 | { | ||||
6300 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
6301 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | ||||
6302 | int crt_cnt=0; | ||||
6303 | #endif | ||||
6304 | size_t i, n; | ||||
6305 | uint8_t alert; | ||||
6306 | |||||
6307 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | ||||
6308 | { | ||||
6309 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | ||||
6310 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6311 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | ||||
6312 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | ||||
6313 | } | ||||
6314 | |||||
6315 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) | ||||
6316 | { | ||||
6317 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6318 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | ||||
6319 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | ||||
6320 | } | ||||
6321 | |||||
6322 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) | ||||
6323 | { | ||||
6324 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | ||||
6325 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6326 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | ||||
6327 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | ||||
6328 | } | ||||
6329 | |||||
6330 | i = mbedtls_ssl_hs_hdr_len( ssl ); | ||||
6331 | |||||
6332 | /* | ||||
6333 | * Same message structure as in mbedtls_ssl_write_certificate() | ||||
6334 | */ | ||||
6335 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; | ||||
6336 | |||||
6337 | if( ssl->in_msg[i] != 0 || | ||||
6338 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) | ||||
6339 | { | ||||
6340 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | ||||
6341 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6342 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | ||||
6343 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | ||||
6344 | } | ||||
6345 | |||||
6346 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ | ||||
6347 | i += 3; | ||||
6348 | |||||
6349 | /* Iterate through and parse the CRTs in the provided chain. */ | ||||
6350 | while( i < ssl->in_hslen ) | ||||
6351 | { | ||||
6352 | /* Check that there's room for the next CRT's length fields. */ | ||||
6353 | if ( i + 3 > ssl->in_hslen ) { | ||||
6354 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | ||||
6355 | mbedtls_ssl_send_alert_message( ssl, | ||||
6356 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6357 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | ||||
6358 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | ||||
6359 | } | ||||
6360 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support | ||||
6361 | * anything beyond 2**16 ~ 64K. */ | ||||
6362 | if( ssl->in_msg[i] != 0 ) | ||||
6363 | { | ||||
6364 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | ||||
6365 | mbedtls_ssl_send_alert_message( ssl, | ||||
6366 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6367 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); | ||||
6368 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); | ||||
6369 | } | ||||
6370 | |||||
6371 | /* Read length of the next CRT in the chain. */ | ||||
6372 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) | ||||
6373 | | (unsigned int) ssl->in_msg[i + 2]; | ||||
6374 | i += 3; | ||||
6375 | |||||
6376 | if( n < 128 || i + n > ssl->in_hslen ) | ||||
6377 | { | ||||
6378 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | ||||
6379 | mbedtls_ssl_send_alert_message( ssl, | ||||
6380 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6381 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | ||||
6382 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); | ||||
6383 | } | ||||
6384 | |||||
6385 | /* Check if we're handling the first CRT in the chain. */ | ||||
6386 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | ||||
6387 | if( crt_cnt++ == 0 && | ||||
6388 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | ||||
6389 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | ||||
6390 | { | ||||
6391 | /* During client-side renegotiation, check that the server's | ||||
6392 | * end-CRTs hasn't changed compared to the initial handshake, | ||||
6393 | * mitigating the triple handshake attack. On success, reuse | ||||
6394 | * the original end-CRT instead of parsing it again. */ | ||||
6395 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); | ||||
6396 | if( ssl_check_peer_crt_unchanged( ssl, | ||||
6397 | &ssl->in_msg[i], | ||||
6398 | n ) != 0 ) | ||||
6399 | { | ||||
6400 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); | ||||
6401 | mbedtls_ssl_send_alert_message( ssl, | ||||
6402 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6403 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); | ||||
6404 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); | ||||
6405 | } | ||||
6406 | |||||
6407 | /* Now we can safely free the original chain. */ | ||||
6408 | ssl_clear_peer_cert( ssl->session ); | ||||
6409 | } | ||||
6410 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | ||||
6411 | |||||
6412 | /* Parse the next certificate in the chain. */ | ||||
6413 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
6414 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); | ||||
6415 | #else | ||||
6416 | /* If we don't need to store the CRT chain permanently, parse | ||||
6417 | * it in-place from the input buffer instead of making a copy. */ | ||||
6418 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); | ||||
6419 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
6420 | switch( ret ) | ||||
6421 | { | ||||
6422 | case 0: /*ok*/ | ||||
6423 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: | ||||
6424 | /* Ignore certificate with an unknown algorithm: maybe a | ||||
6425 | prior certificate was already trusted. */ | ||||
6426 | break; | ||||
6427 | |||||
6428 | case MBEDTLS_ERR_X509_ALLOC_FAILED: | ||||
6429 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; | ||||
6430 | goto crt_parse_der_failed; | ||||
6431 | |||||
6432 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: | ||||
6433 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | ||||
6434 | goto crt_parse_der_failed; | ||||
6435 | |||||
6436 | default: | ||||
6437 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | ||||
6438 | crt_parse_der_failed: | ||||
6439 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); | ||||
6440 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); | ||||
6441 | return( ret ); | ||||
6442 | } | ||||
6443 | |||||
6444 | i += n; | ||||
6445 | } | ||||
6446 | |||||
6447 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); | ||||
6448 | return( 0 ); | ||||
6449 | } | ||||
6450 | |||||
6451 | #if defined(MBEDTLS_SSL_SRV_C) | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6452 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6453 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
6454 | { | ||||
6455 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | ||||
6456 | return( -1 ); | ||||
6457 | |||||
6458 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && | ||||
6459 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | ||||
6460 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && | ||||
6461 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) | ||||
6462 | { | ||||
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame] | 6463 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6464 | return( 0 ); |
6465 | } | ||||
6466 | return( -1 ); | ||||
6467 | } | ||||
6468 | #endif /* MBEDTLS_SSL_SRV_C */ | ||||
6469 | |||||
6470 | /* Check if a certificate message is expected. | ||||
6471 | * Return either | ||||
6472 | * - SSL_CERTIFICATE_EXPECTED, or | ||||
6473 | * - SSL_CERTIFICATE_SKIP | ||||
6474 | * indicating whether a Certificate message is expected or not. | ||||
6475 | */ | ||||
6476 | #define SSL_CERTIFICATE_EXPECTED 0 | ||||
6477 | #define SSL_CERTIFICATE_SKIP 1 | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6478 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6479 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
6480 | int authmode ) | ||||
6481 | { | ||||
6482 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | ||||
6483 | ssl->handshake->ciphersuite_info; | ||||
6484 | |||||
6485 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | ||||
6486 | return( SSL_CERTIFICATE_SKIP ); | ||||
6487 | |||||
6488 | #if defined(MBEDTLS_SSL_SRV_C) | ||||
6489 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | ||||
6490 | { | ||||
6491 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | ||||
6492 | return( SSL_CERTIFICATE_SKIP ); | ||||
6493 | |||||
6494 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) | ||||
6495 | { | ||||
6496 | ssl->session_negotiate->verify_result = | ||||
6497 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; | ||||
6498 | return( SSL_CERTIFICATE_SKIP ); | ||||
6499 | } | ||||
6500 | } | ||||
6501 | #else | ||||
6502 | ((void) authmode); | ||||
6503 | #endif /* MBEDTLS_SSL_SRV_C */ | ||||
6504 | |||||
6505 | return( SSL_CERTIFICATE_EXPECTED ); | ||||
6506 | } | ||||
6507 | |||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6508 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6509 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
6510 | int authmode, | ||||
6511 | mbedtls_x509_crt *chain, | ||||
6512 | void *rs_ctx ) | ||||
6513 | { | ||||
6514 | int ret = 0; | ||||
6515 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | ||||
6516 | ssl->handshake->ciphersuite_info; | ||||
6517 | int have_ca_chain = 0; | ||||
6518 | |||||
6519 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); | ||||
6520 | void *p_vrfy; | ||||
6521 | |||||
6522 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) | ||||
6523 | return( 0 ); | ||||
6524 | |||||
6525 | if( ssl->f_vrfy != NULL ) | ||||
6526 | { | ||||
6527 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); | ||||
6528 | f_vrfy = ssl->f_vrfy; | ||||
6529 | p_vrfy = ssl->p_vrfy; | ||||
6530 | } | ||||
6531 | else | ||||
6532 | { | ||||
6533 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); | ||||
6534 | f_vrfy = ssl->conf->f_vrfy; | ||||
6535 | p_vrfy = ssl->conf->p_vrfy; | ||||
6536 | } | ||||
6537 | |||||
6538 | /* | ||||
6539 | * Main check: verify certificate | ||||
6540 | */ | ||||
6541 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | ||||
6542 | if( ssl->conf->f_ca_cb != NULL ) | ||||
6543 | { | ||||
6544 | ((void) rs_ctx); | ||||
6545 | have_ca_chain = 1; | ||||
6546 | |||||
6547 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); | ||||
6548 | ret = mbedtls_x509_crt_verify_with_ca_cb( | ||||
6549 | chain, | ||||
6550 | ssl->conf->f_ca_cb, | ||||
6551 | ssl->conf->p_ca_cb, | ||||
6552 | ssl->conf->cert_profile, | ||||
6553 | ssl->hostname, | ||||
6554 | &ssl->session_negotiate->verify_result, | ||||
6555 | f_vrfy, p_vrfy ); | ||||
6556 | } | ||||
6557 | else | ||||
6558 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | ||||
6559 | { | ||||
6560 | mbedtls_x509_crt *ca_chain; | ||||
6561 | mbedtls_x509_crl *ca_crl; | ||||
6562 | |||||
6563 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | ||||
6564 | if( ssl->handshake->sni_ca_chain != NULL ) | ||||
6565 | { | ||||
6566 | ca_chain = ssl->handshake->sni_ca_chain; | ||||
6567 | ca_crl = ssl->handshake->sni_ca_crl; | ||||
6568 | } | ||||
6569 | else | ||||
6570 | #endif | ||||
6571 | { | ||||
6572 | ca_chain = ssl->conf->ca_chain; | ||||
6573 | ca_crl = ssl->conf->ca_crl; | ||||
6574 | } | ||||
6575 | |||||
6576 | if( ca_chain != NULL ) | ||||
6577 | have_ca_chain = 1; | ||||
6578 | |||||
6579 | ret = mbedtls_x509_crt_verify_restartable( | ||||
6580 | chain, | ||||
6581 | ca_chain, ca_crl, | ||||
6582 | ssl->conf->cert_profile, | ||||
6583 | ssl->hostname, | ||||
6584 | &ssl->session_negotiate->verify_result, | ||||
6585 | f_vrfy, p_vrfy, rs_ctx ); | ||||
6586 | } | ||||
6587 | |||||
6588 | if( ret != 0 ) | ||||
6589 | { | ||||
6590 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); | ||||
6591 | } | ||||
6592 | |||||
6593 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | ||||
6594 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | ||||
6595 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); | ||||
6596 | #endif | ||||
6597 | |||||
6598 | /* | ||||
6599 | * Secondary checks: always done, but change 'ret' only if it was 0 | ||||
6600 | */ | ||||
6601 | |||||
6602 | #if defined(MBEDTLS_ECP_C) | ||||
6603 | { | ||||
6604 | const mbedtls_pk_context *pk = &chain->pk; | ||||
6605 | |||||
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 6606 | /* If certificate uses an EC key, make sure the curve is OK. |
6607 | * This is a public key, so it can't be opaque, so can_do() is a good | ||||
6608 | * enough check to ensure pk_ec() is safe to use here. */ | ||||
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6609 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6610 | { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6611 | /* and in the unlikely case the above assumption no longer holds |
6612 | * we are making sure that pk_ec() here does not return a NULL | ||||
6613 | */ | ||||
6614 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk ); | ||||
6615 | if( ec == NULL ) | ||||
6616 | { | ||||
Tom Cosgrove | 20c1137 | 2022-08-24 15:06:13 +0100 | [diff] [blame] | 6617 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) ); |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6618 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
6619 | } | ||||
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6620 | |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6621 | if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 ) |
6622 | { | ||||
6623 | ssl->session_negotiate->verify_result |= | ||||
6624 | MBEDTLS_X509_BADCERT_BAD_KEY; | ||||
6625 | |||||
6626 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); | ||||
6627 | if( ret == 0 ) | ||||
6628 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | ||||
6629 | } | ||||
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6630 | } |
6631 | } | ||||
6632 | #endif /* MBEDTLS_ECP_C */ | ||||
6633 | |||||
6634 | if( mbedtls_ssl_check_cert_usage( chain, | ||||
6635 | ciphersuite_info, | ||||
6636 | ! ssl->conf->endpoint, | ||||
6637 | &ssl->session_negotiate->verify_result ) != 0 ) | ||||
6638 | { | ||||
6639 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); | ||||
6640 | if( ret == 0 ) | ||||
6641 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | ||||
6642 | } | ||||
6643 | |||||
6644 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a | ||||
6645 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, | ||||
6646 | * with details encoded in the verification flags. All other kinds | ||||
6647 | * of error codes, including those from the user provided f_vrfy | ||||
6648 | * functions, are treated as fatal and lead to a failure of | ||||
6649 | * ssl_parse_certificate even if verification was optional. */ | ||||
6650 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && | ||||
6651 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || | ||||
6652 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) | ||||
6653 | { | ||||
6654 | ret = 0; | ||||
6655 | } | ||||
6656 | |||||
6657 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) | ||||
6658 | { | ||||
6659 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); | ||||
6660 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; | ||||
6661 | } | ||||
6662 | |||||
6663 | if( ret != 0 ) | ||||
6664 | { | ||||
6665 | uint8_t alert; | ||||
6666 | |||||
6667 | /* The certificate may have been rejected for several reasons. | ||||
6668 | Pick one and send the corresponding alert. Which alert to send | ||||
6669 | may be a subject of debate in some cases. */ | ||||
6670 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) | ||||
6671 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; | ||||
6672 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) | ||||
6673 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | ||||
6674 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) | ||||
6675 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | ||||
6676 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) | ||||
6677 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | ||||
6678 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) | ||||
6679 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | ||||
6680 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) | ||||
6681 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | ||||
6682 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) | ||||
6683 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | ||||
6684 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) | ||||
6685 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; | ||||
6686 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) | ||||
6687 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; | ||||
6688 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) | ||||
6689 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; | ||||
6690 | else | ||||
6691 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; | ||||
6692 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6693 | alert ); | ||||
6694 | } | ||||
6695 | |||||
6696 | #if defined(MBEDTLS_DEBUG_C) | ||||
6697 | if( ssl->session_negotiate->verify_result != 0 ) | ||||
6698 | { | ||||
6699 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", | ||||
6700 | (unsigned int) ssl->session_negotiate->verify_result ) ); | ||||
6701 | } | ||||
6702 | else | ||||
6703 | { | ||||
6704 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); | ||||
6705 | } | ||||
6706 | #endif /* MBEDTLS_DEBUG_C */ | ||||
6707 | |||||
6708 | return( ret ); | ||||
6709 | } | ||||
6710 | |||||
6711 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6712 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6713 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
6714 | unsigned char *start, size_t len ) | ||||
6715 | { | ||||
6716 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
6717 | /* Remember digest of the peer's end-CRT. */ | ||||
6718 | ssl->session_negotiate->peer_cert_digest = | ||||
6719 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); | ||||
6720 | if( ssl->session_negotiate->peer_cert_digest == NULL ) | ||||
6721 | { | ||||
6722 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | ||||
6723 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); | ||||
6724 | mbedtls_ssl_send_alert_message( ssl, | ||||
6725 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6726 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | ||||
6727 | |||||
6728 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | ||||
6729 | } | ||||
6730 | |||||
6731 | ret = mbedtls_md( mbedtls_md_info_from_type( | ||||
6732 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), | ||||
6733 | start, len, | ||||
6734 | ssl->session_negotiate->peer_cert_digest ); | ||||
6735 | |||||
6736 | ssl->session_negotiate->peer_cert_digest_type = | ||||
6737 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; | ||||
6738 | ssl->session_negotiate->peer_cert_digest_len = | ||||
6739 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; | ||||
6740 | |||||
6741 | return( ret ); | ||||
6742 | } | ||||
6743 | |||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6744 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6745 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
6746 | unsigned char *start, size_t len ) | ||||
6747 | { | ||||
6748 | unsigned char *end = start + len; | ||||
6749 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
6750 | |||||
6751 | /* Make a copy of the peer's raw public key. */ | ||||
6752 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); | ||||
6753 | ret = mbedtls_pk_parse_subpubkey( &start, end, | ||||
6754 | &ssl->handshake->peer_pubkey ); | ||||
6755 | if( ret != 0 ) | ||||
6756 | { | ||||
6757 | /* We should have parsed the public key before. */ | ||||
6758 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
6759 | } | ||||
6760 | |||||
6761 | return( 0 ); | ||||
6762 | } | ||||
6763 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
6764 | |||||
6765 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | ||||
6766 | { | ||||
6767 | int ret = 0; | ||||
6768 | int crt_expected; | ||||
6769 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | ||||
6770 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET | ||||
6771 | ? ssl->handshake->sni_authmode | ||||
6772 | : ssl->conf->authmode; | ||||
6773 | #else | ||||
6774 | const int authmode = ssl->conf->authmode; | ||||
6775 | #endif | ||||
6776 | void *rs_ctx = NULL; | ||||
6777 | mbedtls_x509_crt *chain = NULL; | ||||
6778 | |||||
6779 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | ||||
6780 | |||||
6781 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); | ||||
6782 | if( crt_expected == SSL_CERTIFICATE_SKIP ) | ||||
6783 | { | ||||
6784 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | ||||
6785 | goto exit; | ||||
6786 | } | ||||
6787 | |||||
6788 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | ||||
6789 | if( ssl->handshake->ecrs_enabled && | ||||
6790 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) | ||||
6791 | { | ||||
6792 | chain = ssl->handshake->ecrs_peer_cert; | ||||
6793 | ssl->handshake->ecrs_peer_cert = NULL; | ||||
6794 | goto crt_verify; | ||||
6795 | } | ||||
6796 | #endif | ||||
6797 | |||||
6798 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | ||||
6799 | { | ||||
6800 | /* mbedtls_ssl_read_record may have sent an alert already. We | ||||
6801 | let it decide whether to alert. */ | ||||
6802 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | ||||
6803 | goto exit; | ||||
6804 | } | ||||
6805 | |||||
6806 | #if defined(MBEDTLS_SSL_SRV_C) | ||||
6807 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) | ||||
6808 | { | ||||
6809 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; | ||||
6810 | |||||
6811 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) | ||||
6812 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; | ||||
6813 | |||||
6814 | goto exit; | ||||
6815 | } | ||||
6816 | #endif /* MBEDTLS_SSL_SRV_C */ | ||||
6817 | |||||
6818 | /* Clear existing peer CRT structure in case we tried to | ||||
6819 | * reuse a session but it failed, and allocate a new one. */ | ||||
6820 | ssl_clear_peer_cert( ssl->session_negotiate ); | ||||
6821 | |||||
6822 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); | ||||
6823 | if( chain == NULL ) | ||||
6824 | { | ||||
6825 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", | ||||
6826 | sizeof( mbedtls_x509_crt ) ) ); | ||||
6827 | mbedtls_ssl_send_alert_message( ssl, | ||||
6828 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
6829 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | ||||
6830 | |||||
6831 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | ||||
6832 | goto exit; | ||||
6833 | } | ||||
6834 | mbedtls_x509_crt_init( chain ); | ||||
6835 | |||||
6836 | ret = ssl_parse_certificate_chain( ssl, chain ); | ||||
6837 | if( ret != 0 ) | ||||
6838 | goto exit; | ||||
6839 | |||||
6840 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | ||||
6841 | if( ssl->handshake->ecrs_enabled) | ||||
6842 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; | ||||
6843 | |||||
6844 | crt_verify: | ||||
6845 | if( ssl->handshake->ecrs_enabled) | ||||
6846 | rs_ctx = &ssl->handshake->ecrs_ctx; | ||||
6847 | #endif | ||||
6848 | |||||
6849 | ret = ssl_parse_certificate_verify( ssl, authmode, | ||||
6850 | chain, rs_ctx ); | ||||
6851 | if( ret != 0 ) | ||||
6852 | goto exit; | ||||
6853 | |||||
6854 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
6855 | { | ||||
6856 | unsigned char *crt_start, *pk_start; | ||||
6857 | size_t crt_len, pk_len; | ||||
6858 | |||||
6859 | /* We parse the CRT chain without copying, so | ||||
6860 | * these pointers point into the input buffer, | ||||
6861 | * and are hence still valid after freeing the | ||||
6862 | * CRT chain. */ | ||||
6863 | |||||
6864 | crt_start = chain->raw.p; | ||||
6865 | crt_len = chain->raw.len; | ||||
6866 | |||||
6867 | pk_start = chain->pk_raw.p; | ||||
6868 | pk_len = chain->pk_raw.len; | ||||
6869 | |||||
6870 | /* Free the CRT structures before computing | ||||
6871 | * digest and copying the peer's public key. */ | ||||
6872 | mbedtls_x509_crt_free( chain ); | ||||
6873 | mbedtls_free( chain ); | ||||
6874 | chain = NULL; | ||||
6875 | |||||
6876 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); | ||||
6877 | if( ret != 0 ) | ||||
6878 | goto exit; | ||||
6879 | |||||
6880 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); | ||||
6881 | if( ret != 0 ) | ||||
6882 | goto exit; | ||||
6883 | } | ||||
6884 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
6885 | /* Pass ownership to session structure. */ | ||||
6886 | ssl->session_negotiate->peer_cert = chain; | ||||
6887 | chain = NULL; | ||||
6888 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
6889 | |||||
6890 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); | ||||
6891 | |||||
6892 | exit: | ||||
6893 | |||||
6894 | if( ret == 0 ) | ||||
6895 | ssl->state++; | ||||
6896 | |||||
6897 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | ||||
6898 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) | ||||
6899 | { | ||||
6900 | ssl->handshake->ecrs_peer_cert = chain; | ||||
6901 | chain = NULL; | ||||
6902 | } | ||||
6903 | #endif | ||||
6904 | |||||
6905 | if( chain != NULL ) | ||||
6906 | { | ||||
6907 | mbedtls_x509_crt_free( chain ); | ||||
6908 | mbedtls_free( chain ); | ||||
6909 | } | ||||
6910 | |||||
6911 | return( ret ); | ||||
6912 | } | ||||
6913 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | ||||
6914 | |||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6915 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 6916 | static void ssl_calc_finished_tls_sha256( |
6917 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | ||||
6918 | { | ||||
6919 | int len = 12; | ||||
6920 | const char *sender; | ||||
6921 | unsigned char padbuf[32]; | ||||
6922 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
6923 | size_t hash_size; | ||||
6924 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; | ||||
6925 | psa_status_t status; | ||||
6926 | #else | ||||
6927 | mbedtls_sha256_context sha256; | ||||
6928 | #endif | ||||
6929 | |||||
6930 | mbedtls_ssl_session *session = ssl->session_negotiate; | ||||
6931 | if( !session ) | ||||
6932 | session = ssl->session; | ||||
6933 | |||||
6934 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | ||||
6935 | ? "client finished" | ||||
6936 | : "server finished"; | ||||
6937 | |||||
6938 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
6939 | sha256_psa = psa_hash_operation_init(); | ||||
6940 | |||||
6941 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); | ||||
6942 | |||||
6943 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); | ||||
6944 | if( status != PSA_SUCCESS ) | ||||
6945 | { | ||||
6946 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | ||||
6947 | return; | ||||
6948 | } | ||||
6949 | |||||
6950 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); | ||||
6951 | if( status != PSA_SUCCESS ) | ||||
6952 | { | ||||
6953 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | ||||
6954 | return; | ||||
6955 | } | ||||
6956 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); | ||||
6957 | #else | ||||
6958 | |||||
6959 | mbedtls_sha256_init( &sha256 ); | ||||
6960 | |||||
6961 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); | ||||
6962 | |||||
6963 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | ||||
6964 | |||||
6965 | /* | ||||
6966 | * TLSv1.2: | ||||
6967 | * hash = PRF( master, finished_label, | ||||
6968 | * Hash( handshake ) )[0.11] | ||||
6969 | */ | ||||
6970 | |||||
6971 | #if !defined(MBEDTLS_SHA256_ALT) | ||||
6972 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) | ||||
6973 | sha256.state, sizeof( sha256.state ) ); | ||||
6974 | #endif | ||||
6975 | |||||
6976 | mbedtls_sha256_finish( &sha256, padbuf ); | ||||
6977 | mbedtls_sha256_free( &sha256 ); | ||||
6978 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
6979 | |||||
6980 | ssl->handshake->tls_prf( session->master, 48, sender, | ||||
6981 | padbuf, 32, buf, len ); | ||||
6982 | |||||
6983 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | ||||
6984 | |||||
6985 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); | ||||
6986 | |||||
6987 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); | ||||
6988 | } | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6989 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 6990 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 6991 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6992 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 6993 | static void ssl_calc_finished_tls_sha384( |
6994 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | ||||
6995 | { | ||||
6996 | int len = 12; | ||||
6997 | const char *sender; | ||||
6998 | unsigned char padbuf[48]; | ||||
6999 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
7000 | size_t hash_size; | ||||
7001 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; | ||||
7002 | psa_status_t status; | ||||
7003 | #else | ||||
7004 | mbedtls_sha512_context sha512; | ||||
7005 | #endif | ||||
7006 | |||||
7007 | mbedtls_ssl_session *session = ssl->session_negotiate; | ||||
7008 | if( !session ) | ||||
7009 | session = ssl->session; | ||||
7010 | |||||
7011 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | ||||
7012 | ? "client finished" | ||||
7013 | : "server finished"; | ||||
7014 | |||||
7015 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
7016 | sha384_psa = psa_hash_operation_init(); | ||||
7017 | |||||
7018 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); | ||||
7019 | |||||
7020 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); | ||||
7021 | if( status != PSA_SUCCESS ) | ||||
7022 | { | ||||
7023 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | ||||
7024 | return; | ||||
7025 | } | ||||
7026 | |||||
7027 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); | ||||
7028 | if( status != PSA_SUCCESS ) | ||||
7029 | { | ||||
7030 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | ||||
7031 | return; | ||||
7032 | } | ||||
7033 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); | ||||
7034 | #else | ||||
7035 | mbedtls_sha512_init( &sha512 ); | ||||
7036 | |||||
7037 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); | ||||
7038 | |||||
Andrzej Kurek | a242e83 | 2022-08-11 10:03:14 -0400 | [diff] [blame] | 7039 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 ); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7040 | |
7041 | /* | ||||
7042 | * TLSv1.2: | ||||
7043 | * hash = PRF( master, finished_label, | ||||
7044 | * Hash( handshake ) )[0.11] | ||||
7045 | */ | ||||
7046 | |||||
7047 | #if !defined(MBEDTLS_SHA512_ALT) | ||||
7048 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) | ||||
7049 | sha512.state, sizeof( sha512.state ) ); | ||||
7050 | #endif | ||||
7051 | mbedtls_sha512_finish( &sha512, padbuf ); | ||||
7052 | |||||
7053 | mbedtls_sha512_free( &sha512 ); | ||||
7054 | #endif | ||||
7055 | |||||
7056 | ssl->handshake->tls_prf( session->master, 48, sender, | ||||
7057 | padbuf, 48, buf, len ); | ||||
7058 | |||||
7059 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | ||||
7060 | |||||
7061 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); | ||||
7062 | |||||
7063 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); | ||||
7064 | } | ||||
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7065 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7066 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7067 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
7068 | { | ||||
7069 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); | ||||
7070 | |||||
7071 | /* | ||||
7072 | * Free our handshake params | ||||
7073 | */ | ||||
7074 | mbedtls_ssl_handshake_free( ssl ); | ||||
7075 | mbedtls_free( ssl->handshake ); | ||||
7076 | ssl->handshake = NULL; | ||||
7077 | |||||
7078 | /* | ||||
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7079 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7080 | */ |
7081 | if( ssl->transform ) | ||||
7082 | { | ||||
7083 | mbedtls_ssl_transform_free( ssl->transform ); | ||||
7084 | mbedtls_free( ssl->transform ); | ||||
7085 | } | ||||
7086 | ssl->transform = ssl->transform_negotiate; | ||||
7087 | ssl->transform_negotiate = NULL; | ||||
7088 | |||||
7089 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); | ||||
7090 | } | ||||
7091 | |||||
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7092 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
7093 | { | ||||
7094 | int resume = ssl->handshake->resume; | ||||
7095 | |||||
7096 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); | ||||
7097 | |||||
7098 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | ||||
7099 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | ||||
7100 | { | ||||
7101 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; | ||||
7102 | ssl->renego_records_seen = 0; | ||||
7103 | } | ||||
7104 | #endif | ||||
7105 | |||||
7106 | /* | ||||
7107 | * Free the previous session and switch in the current one | ||||
7108 | */ | ||||
7109 | if( ssl->session ) | ||||
7110 | { | ||||
7111 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | ||||
7112 | /* RFC 7366 3.1: keep the EtM state */ | ||||
7113 | ssl->session_negotiate->encrypt_then_mac = | ||||
7114 | ssl->session->encrypt_then_mac; | ||||
7115 | #endif | ||||
7116 | |||||
7117 | mbedtls_ssl_session_free( ssl->session ); | ||||
7118 | mbedtls_free( ssl->session ); | ||||
7119 | } | ||||
7120 | ssl->session = ssl->session_negotiate; | ||||
7121 | ssl->session_negotiate = NULL; | ||||
7122 | |||||
7123 | /* | ||||
7124 | * Add cache entry | ||||
7125 | */ | ||||
7126 | if( ssl->conf->f_set_cache != NULL && | ||||
7127 | ssl->session->id_len != 0 && | ||||
7128 | resume == 0 ) | ||||
7129 | { | ||||
7130 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, | ||||
7131 | ssl->session->id, | ||||
7132 | ssl->session->id_len, | ||||
7133 | ssl->session ) != 0 ) | ||||
7134 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); | ||||
7135 | } | ||||
7136 | |||||
7137 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
7138 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | ||||
7139 | ssl->handshake->flight != NULL ) | ||||
7140 | { | ||||
7141 | /* Cancel handshake timer */ | ||||
7142 | mbedtls_ssl_set_timer( ssl, 0 ); | ||||
7143 | |||||
7144 | /* Keep last flight around in case we need to resend it: | ||||
7145 | * we need the handshake and transform structures for that */ | ||||
7146 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); | ||||
7147 | } | ||||
7148 | else | ||||
7149 | #endif | ||||
7150 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); | ||||
7151 | |||||
7152 | ssl->state++; | ||||
7153 | |||||
7154 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); | ||||
7155 | } | ||||
7156 | |||||
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7157 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
7158 | { | ||||
7159 | int ret, hash_len; | ||||
7160 | |||||
7161 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); | ||||
7162 | |||||
7163 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); | ||||
7164 | |||||
7165 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); | ||||
7166 | |||||
7167 | /* | ||||
7168 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites | ||||
7169 | * may define some other value. Currently (early 2016), no defined | ||||
7170 | * ciphersuite does this (and this is unlikely to change as activity has | ||||
7171 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. | ||||
7172 | */ | ||||
7173 | hash_len = 12; | ||||
7174 | |||||
7175 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | ||||
7176 | ssl->verify_data_len = hash_len; | ||||
7177 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); | ||||
7178 | #endif | ||||
7179 | |||||
7180 | ssl->out_msglen = 4 + hash_len; | ||||
7181 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | ||||
7182 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; | ||||
7183 | |||||
7184 | /* | ||||
7185 | * In case of session resuming, invert the client and server | ||||
7186 | * ChangeCipherSpec messages order. | ||||
7187 | */ | ||||
7188 | if( ssl->handshake->resume != 0 ) | ||||
7189 | { | ||||
7190 | #if defined(MBEDTLS_SSL_CLI_C) | ||||
7191 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | ||||
7192 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | ||||
7193 | #endif | ||||
7194 | #if defined(MBEDTLS_SSL_SRV_C) | ||||
7195 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | ||||
7196 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | ||||
7197 | #endif | ||||
7198 | } | ||||
7199 | else | ||||
7200 | ssl->state++; | ||||
7201 | |||||
7202 | /* | ||||
7203 | * Switch to our negotiated transform and session parameters for outbound | ||||
7204 | * data. | ||||
7205 | */ | ||||
7206 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); | ||||
7207 | |||||
7208 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
7209 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | ||||
7210 | { | ||||
7211 | unsigned char i; | ||||
7212 | |||||
7213 | /* Remember current epoch settings for resending */ | ||||
7214 | ssl->handshake->alt_transform_out = ssl->transform_out; | ||||
7215 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, | ||||
7216 | sizeof( ssl->handshake->alt_out_ctr ) ); | ||||
7217 | |||||
7218 | /* Set sequence_number to zero */ | ||||
7219 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); | ||||
7220 | |||||
7221 | |||||
7222 | /* Increment epoch */ | ||||
7223 | for( i = 2; i > 0; i-- ) | ||||
7224 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) | ||||
7225 | break; | ||||
7226 | |||||
7227 | /* The loop goes to its end iff the counter is wrapping */ | ||||
7228 | if( i == 0 ) | ||||
7229 | { | ||||
7230 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); | ||||
7231 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | ||||
7232 | } | ||||
7233 | } | ||||
7234 | else | ||||
7235 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | ||||
7236 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); | ||||
7237 | |||||
7238 | ssl->transform_out = ssl->transform_negotiate; | ||||
7239 | ssl->session_out = ssl->session_negotiate; | ||||
7240 | |||||
7241 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
7242 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | ||||
7243 | mbedtls_ssl_send_flight_completed( ssl ); | ||||
7244 | #endif | ||||
7245 | |||||
7246 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | ||||
7247 | { | ||||
7248 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | ||||
7249 | return( ret ); | ||||
7250 | } | ||||
7251 | |||||
7252 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
7253 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | ||||
7254 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | ||||
7255 | { | ||||
7256 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | ||||
7257 | return( ret ); | ||||
7258 | } | ||||
7259 | #endif | ||||
7260 | |||||
7261 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); | ||||
7262 | |||||
7263 | return( 0 ); | ||||
7264 | } | ||||
7265 | |||||
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7266 | #define SSL_MAX_HASH_LEN 12 |
7267 | |||||
7268 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) | ||||
7269 | { | ||||
7270 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
7271 | unsigned int hash_len = 12; | ||||
7272 | unsigned char buf[SSL_MAX_HASH_LEN]; | ||||
7273 | |||||
7274 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); | ||||
7275 | |||||
7276 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); | ||||
7277 | |||||
7278 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | ||||
7279 | { | ||||
7280 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | ||||
7281 | goto exit; | ||||
7282 | } | ||||
7283 | |||||
7284 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | ||||
7285 | { | ||||
7286 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | ||||
7287 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
7288 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | ||||
7289 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | ||||
7290 | goto exit; | ||||
7291 | } | ||||
7292 | |||||
7293 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) | ||||
7294 | { | ||||
7295 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
7296 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | ||||
7297 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | ||||
7298 | goto exit; | ||||
7299 | } | ||||
7300 | |||||
7301 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) | ||||
7302 | { | ||||
7303 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | ||||
7304 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
7305 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | ||||
7306 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; | ||||
7307 | goto exit; | ||||
7308 | } | ||||
7309 | |||||
7310 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), | ||||
7311 | buf, hash_len ) != 0 ) | ||||
7312 | { | ||||
7313 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | ||||
7314 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
7315 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); | ||||
7316 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; | ||||
7317 | goto exit; | ||||
7318 | } | ||||
7319 | |||||
7320 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | ||||
7321 | ssl->verify_data_len = hash_len; | ||||
7322 | memcpy( ssl->peer_verify_data, buf, hash_len ); | ||||
7323 | #endif | ||||
7324 | |||||
7325 | if( ssl->handshake->resume != 0 ) | ||||
7326 | { | ||||
7327 | #if defined(MBEDTLS_SSL_CLI_C) | ||||
7328 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | ||||
7329 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | ||||
7330 | #endif | ||||
7331 | #if defined(MBEDTLS_SSL_SRV_C) | ||||
7332 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | ||||
7333 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | ||||
7334 | #endif | ||||
7335 | } | ||||
7336 | else | ||||
7337 | ssl->state++; | ||||
7338 | |||||
7339 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | ||||
7340 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | ||||
7341 | mbedtls_ssl_recv_flight_completed( ssl ); | ||||
7342 | #endif | ||||
7343 | |||||
7344 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); | ||||
7345 | |||||
7346 | exit: | ||||
7347 | mbedtls_platform_zeroize( buf, hash_len ); | ||||
7348 | return( ret ); | ||||
7349 | } | ||||
7350 | |||||
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7351 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
7352 | /* | ||||
7353 | * Helper to get TLS 1.2 PRF from ciphersuite | ||||
7354 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) | ||||
7355 | */ | ||||
7356 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) | ||||
7357 | { | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7358 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7359 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
7360 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); | ||||
7361 | |||||
Leonid Rozenboim | e9d8dcd | 2022-08-08 15:57:48 -0700 | [diff] [blame] | 7362 | if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7363 | return( tls_prf_sha384 ); |
7364 | #else | ||||
7365 | (void) ciphersuite_id; | ||||
7366 | #endif | ||||
7367 | return( tls_prf_sha256 ); | ||||
7368 | } | ||||
7369 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | ||||
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7370 | |
7371 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) | ||||
7372 | { | ||||
7373 | ((void) tls_prf); | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7374 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7375 | if( tls_prf == tls_prf_sha384 ) |
7376 | { | ||||
7377 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); | ||||
7378 | } | ||||
7379 | else | ||||
7380 | #endif | ||||
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7381 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7382 | if( tls_prf == tls_prf_sha256 ) |
7383 | { | ||||
7384 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); | ||||
7385 | } | ||||
7386 | else | ||||
7387 | #endif | ||||
7388 | return( MBEDTLS_SSL_TLS_PRF_NONE ); | ||||
7389 | } | ||||
7390 | |||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7391 | /* |
7392 | * Populate a transform structure with session keys and all the other | ||||
7393 | * necessary information. | ||||
7394 | * | ||||
7395 | * Parameters: | ||||
7396 | * - [in/out]: transform: structure to populate | ||||
7397 | * [in] must be just initialised with mbedtls_ssl_transform_init() | ||||
7398 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() | ||||
7399 | * - [in] ciphersuite | ||||
7400 | * - [in] master | ||||
7401 | * - [in] encrypt_then_mac | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7402 | * - [in] tls_prf: pointer to PRF to use for key derivation |
7403 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random | ||||
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7404 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7405 | * - [in] endpoint: client or server |
7406 | * - [in] ssl: used for: | ||||
7407 | * - ssl->conf->{f,p}_export_keys | ||||
7408 | * [in] optionally used for: | ||||
7409 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg | ||||
7410 | */ | ||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7411 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7412 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
7413 | int ciphersuite, | ||||
7414 | const unsigned char master[48], | ||||
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7415 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7416 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7417 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7418 | ssl_tls_prf_t tls_prf, |
7419 | const unsigned char randbytes[64], | ||||
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7420 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7421 | unsigned endpoint, |
7422 | const mbedtls_ssl_context *ssl ) | ||||
7423 | { | ||||
7424 | int ret = 0; | ||||
7425 | unsigned char keyblk[256]; | ||||
7426 | unsigned char *key1; | ||||
7427 | unsigned char *key2; | ||||
7428 | unsigned char *mac_enc; | ||||
7429 | unsigned char *mac_dec; | ||||
7430 | size_t mac_key_len = 0; | ||||
7431 | size_t iv_copy_len; | ||||
7432 | size_t keylen; | ||||
7433 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | ||||
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7434 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7435 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7436 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7437 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7438 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7439 | |
7440 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
7441 | psa_key_type_t key_type; | ||||
7442 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | ||||
7443 | psa_algorithm_t alg; | ||||
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7444 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7445 | size_t key_bits; |
7446 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | ||||
7447 | #endif | ||||
7448 | |||||
7449 | #if !defined(MBEDTLS_DEBUG_C) && \ | ||||
7450 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | ||||
7451 | if( ssl->f_export_keys == NULL ) | ||||
7452 | { | ||||
7453 | ssl = NULL; /* make sure we don't use it except for these cases */ | ||||
7454 | (void) ssl; | ||||
7455 | } | ||||
7456 | #endif | ||||
7457 | |||||
7458 | /* | ||||
7459 | * Some data just needs copying into the structure | ||||
7460 | */ | ||||
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7461 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7462 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7463 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7464 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7465 | |
7466 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | ||||
7467 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); | ||||
7468 | #endif | ||||
7469 | |||||
7470 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | ||||
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7471 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7472 | { |
7473 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform | ||||
7474 | * generation separate. This should never happen. */ | ||||
7475 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
7476 | } | ||||
7477 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | ||||
7478 | |||||
7479 | /* | ||||
7480 | * Get various info structures | ||||
7481 | */ | ||||
7482 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); | ||||
7483 | if( ciphersuite_info == NULL ) | ||||
7484 | { | ||||
7485 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", | ||||
7486 | ciphersuite ) ); | ||||
7487 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
7488 | } | ||||
7489 | |||||
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 7490 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7491 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7492 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7493 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7494 | ciphersuite_info ); |
7495 | |||||
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7496 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
7497 | transform->taglen = | ||||
7498 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; | ||||
7499 | |||||
7500 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
7501 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, | ||||
7502 | transform->taglen, | ||||
7503 | &alg, | ||||
7504 | &key_type, | ||||
7505 | &key_bits ) ) != PSA_SUCCESS ) | ||||
7506 | { | ||||
7507 | ret = psa_ssl_status_to_mbedtls( status ); | ||||
7508 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); | ||||
7509 | goto end; | ||||
7510 | } | ||||
7511 | #else | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7512 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
7513 | if( cipher_info == NULL ) | ||||
7514 | { | ||||
7515 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", | ||||
7516 | ciphersuite_info->cipher ) ); | ||||
7517 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
7518 | } | ||||
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7519 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7520 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7521 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7522 | mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7523 | if( mac_alg == 0 ) |
7524 | { | ||||
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7525 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found", |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7526 | (unsigned) ciphersuite_info->mac ) ); |
7527 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
7528 | } | ||||
7529 | #else | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7530 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
7531 | if( md_info == NULL ) | ||||
7532 | { | ||||
7533 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", | ||||
7534 | (unsigned) ciphersuite_info->mac ) ); | ||||
7535 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
7536 | } | ||||
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7537 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7538 | |
7539 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | ||||
7540 | /* Copy own and peer's CID if the use of the CID | ||||
7541 | * extension has been negotiated. */ | ||||
7542 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) | ||||
7543 | { | ||||
7544 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); | ||||
7545 | |||||
7546 | transform->in_cid_len = ssl->own_cid_len; | ||||
7547 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); | ||||
7548 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, | ||||
7549 | transform->in_cid_len ); | ||||
7550 | |||||
7551 | transform->out_cid_len = ssl->handshake->peer_cid_len; | ||||
7552 | memcpy( transform->out_cid, ssl->handshake->peer_cid, | ||||
7553 | ssl->handshake->peer_cid_len ); | ||||
7554 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, | ||||
7555 | transform->out_cid_len ); | ||||
7556 | } | ||||
7557 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | ||||
7558 | |||||
7559 | /* | ||||
7560 | * Compute key block using the PRF | ||||
7561 | */ | ||||
7562 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); | ||||
7563 | if( ret != 0 ) | ||||
7564 | { | ||||
7565 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | ||||
7566 | return( ret ); | ||||
7567 | } | ||||
7568 | |||||
7569 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", | ||||
7570 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); | ||||
7571 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); | ||||
7572 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); | ||||
7573 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); | ||||
7574 | |||||
7575 | /* | ||||
7576 | * Determine the appropriate key, IV and MAC length. | ||||
7577 | */ | ||||
7578 | |||||
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7579 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
7580 | keylen = PSA_BITS_TO_BYTES(key_bits); | ||||
7581 | #else | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7582 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7583 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7584 | |
7585 | #if defined(MBEDTLS_GCM_C) || \ | ||||
7586 | defined(MBEDTLS_CCM_C) || \ | ||||
7587 | defined(MBEDTLS_CHACHAPOLY_C) | ||||
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7588 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7589 | { |
7590 | size_t explicit_ivlen; | ||||
7591 | |||||
7592 | transform->maclen = 0; | ||||
7593 | mac_key_len = 0; | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7594 | |
7595 | /* All modes haves 96-bit IVs, but the length of the static parts vary | ||||
7596 | * with mode and version: | ||||
7597 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes | ||||
7598 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) | ||||
7599 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's | ||||
7600 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record | ||||
7601 | * sequence number). | ||||
7602 | */ | ||||
7603 | transform->ivlen = 12; | ||||
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7604 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
7605 | if( key_type == PSA_KEY_TYPE_CHACHA20 ) | ||||
7606 | #else | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7607 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7608 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7609 | transform->fixed_ivlen = 12; |
7610 | else | ||||
7611 | transform->fixed_ivlen = 4; | ||||
7612 | |||||
7613 | /* Minimum length of encrypted record */ | ||||
7614 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; | ||||
7615 | transform->minlen = explicit_ivlen + transform->taglen; | ||||
7616 | } | ||||
7617 | else | ||||
7618 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ | ||||
7619 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | ||||
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7620 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
7621 | ssl_mode == MBEDTLS_SSL_MODE_CBC || | ||||
7622 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7623 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7624 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 7625 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7626 | #else |
7627 | size_t block_size = cipher_info->block_size; | ||||
7628 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
7629 | |||||
7630 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7631 | /* Get MAC length */ |
7632 | mac_key_len = PSA_HASH_LENGTH(mac_alg); | ||||
7633 | #else | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7634 | /* Initialize HMAC contexts */ |
7635 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || | ||||
7636 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) | ||||
7637 | { | ||||
7638 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | ||||
7639 | goto end; | ||||
7640 | } | ||||
7641 | |||||
7642 | /* Get MAC length */ | ||||
7643 | mac_key_len = mbedtls_md_get_size( md_info ); | ||||
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7644 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7645 | transform->maclen = mac_key_len; |
7646 | |||||
7647 | /* IV length */ | ||||
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7648 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 7649 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7650 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7651 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7652 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7653 | |
7654 | /* Minimum length */ | ||||
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7655 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7656 | transform->minlen = transform->maclen; |
7657 | else | ||||
7658 | { | ||||
7659 | /* | ||||
7660 | * GenericBlockCipher: | ||||
7661 | * 1. if EtM is in use: one block plus MAC | ||||
7662 | * otherwise: * first multiple of blocklen greater than maclen | ||||
7663 | * 2. IV | ||||
7664 | */ | ||||
7665 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | ||||
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7666 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7667 | { |
7668 | transform->minlen = transform->maclen | ||||
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7669 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7670 | } |
7671 | else | ||||
7672 | #endif | ||||
7673 | { | ||||
7674 | transform->minlen = transform->maclen | ||||
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7675 | + block_size |
7676 | - transform->maclen % block_size; | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7677 | } |
7678 | |||||
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7679 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7680 | { |
7681 | transform->minlen += transform->ivlen; | ||||
7682 | } | ||||
7683 | else | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7684 | { |
7685 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | ||||
7686 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | ||||
7687 | goto end; | ||||
7688 | } | ||||
7689 | } | ||||
7690 | } | ||||
7691 | else | ||||
7692 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | ||||
7693 | { | ||||
7694 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | ||||
7695 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
7696 | } | ||||
7697 | |||||
7698 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", | ||||
7699 | (unsigned) keylen, | ||||
7700 | (unsigned) transform->minlen, | ||||
7701 | (unsigned) transform->ivlen, | ||||
7702 | (unsigned) transform->maclen ) ); | ||||
7703 | |||||
7704 | /* | ||||
7705 | * Finally setup the cipher contexts, IVs and MAC secrets. | ||||
7706 | */ | ||||
7707 | #if defined(MBEDTLS_SSL_CLI_C) | ||||
7708 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | ||||
7709 | { | ||||
7710 | key1 = keyblk + mac_key_len * 2; | ||||
7711 | key2 = keyblk + mac_key_len * 2 + keylen; | ||||
7712 | |||||
7713 | mac_enc = keyblk; | ||||
7714 | mac_dec = keyblk + mac_key_len; | ||||
7715 | |||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7716 | iv_copy_len = ( transform->fixed_ivlen ) ? |
7717 | transform->fixed_ivlen : transform->ivlen; | ||||
7718 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); | ||||
7719 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, | ||||
7720 | iv_copy_len ); | ||||
7721 | } | ||||
7722 | else | ||||
7723 | #endif /* MBEDTLS_SSL_CLI_C */ | ||||
7724 | #if defined(MBEDTLS_SSL_SRV_C) | ||||
7725 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) | ||||
7726 | { | ||||
7727 | key1 = keyblk + mac_key_len * 2 + keylen; | ||||
7728 | key2 = keyblk + mac_key_len * 2; | ||||
7729 | |||||
7730 | mac_enc = keyblk + mac_key_len; | ||||
7731 | mac_dec = keyblk; | ||||
7732 | |||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7733 | iv_copy_len = ( transform->fixed_ivlen ) ? |
7734 | transform->fixed_ivlen : transform->ivlen; | ||||
7735 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); | ||||
7736 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, | ||||
7737 | iv_copy_len ); | ||||
7738 | } | ||||
7739 | else | ||||
7740 | #endif /* MBEDTLS_SSL_SRV_C */ | ||||
7741 | { | ||||
7742 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | ||||
7743 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | ||||
7744 | goto end; | ||||
7745 | } | ||||
7746 | |||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7747 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
7748 | { | ||||
7749 | ssl->f_export_keys( ssl->p_export_keys, | ||||
7750 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, | ||||
7751 | master, 48, | ||||
7752 | randbytes + 32, | ||||
7753 | randbytes, | ||||
7754 | tls_prf_get_type( tls_prf ) ); | ||||
7755 | } | ||||
7756 | |||||
7757 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7758 | transform->psa_alg = alg; |
7759 | |||||
7760 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) | ||||
7761 | { | ||||
7762 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); | ||||
7763 | psa_set_key_algorithm( &attributes, alg ); | ||||
7764 | psa_set_key_type( &attributes, key_type ); | ||||
7765 | |||||
7766 | if( ( status = psa_import_key( &attributes, | ||||
7767 | key1, | ||||
7768 | PSA_BITS_TO_BYTES( key_bits ), | ||||
7769 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) | ||||
7770 | { | ||||
7771 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); | ||||
7772 | ret = psa_ssl_status_to_mbedtls( status ); | ||||
7773 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); | ||||
7774 | goto end; | ||||
7775 | } | ||||
7776 | |||||
7777 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); | ||||
7778 | |||||
7779 | if( ( status = psa_import_key( &attributes, | ||||
7780 | key2, | ||||
7781 | PSA_BITS_TO_BYTES( key_bits ), | ||||
7782 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) | ||||
7783 | { | ||||
7784 | ret = psa_ssl_status_to_mbedtls( status ); | ||||
7785 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); | ||||
7786 | goto end; | ||||
7787 | } | ||||
7788 | } | ||||
7789 | #else | ||||
7790 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, | ||||
7791 | cipher_info ) ) != 0 ) | ||||
7792 | { | ||||
7793 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | ||||
7794 | goto end; | ||||
7795 | } | ||||
7796 | |||||
7797 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, | ||||
7798 | cipher_info ) ) != 0 ) | ||||
7799 | { | ||||
7800 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | ||||
7801 | goto end; | ||||
7802 | } | ||||
7803 | |||||
7804 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, | ||||
7805 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), | ||||
7806 | MBEDTLS_ENCRYPT ) ) != 0 ) | ||||
7807 | { | ||||
7808 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | ||||
7809 | goto end; | ||||
7810 | } | ||||
7811 | |||||
7812 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, | ||||
7813 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), | ||||
7814 | MBEDTLS_DECRYPT ) ) != 0 ) | ||||
7815 | { | ||||
7816 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | ||||
7817 | goto end; | ||||
7818 | } | ||||
7819 | |||||
7820 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | ||||
7821 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) | ||||
7822 | { | ||||
7823 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, | ||||
7824 | MBEDTLS_PADDING_NONE ) ) != 0 ) | ||||
7825 | { | ||||
7826 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); | ||||
7827 | goto end; | ||||
7828 | } | ||||
7829 | |||||
7830 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, | ||||
7831 | MBEDTLS_PADDING_NONE ) ) != 0 ) | ||||
7832 | { | ||||
7833 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); | ||||
7834 | goto end; | ||||
7835 | } | ||||
7836 | } | ||||
7837 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | ||||
7838 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
7839 | |||||
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7840 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
7841 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. | ||||
7842 | For AEAD-based ciphersuites, there is nothing to do here. */ | ||||
7843 | if( mac_key_len != 0 ) | ||||
7844 | { | ||||
7845 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7846 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7847 | |
7848 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); | ||||
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7849 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7850 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
7851 | |||||
7852 | if( ( status = psa_import_key( &attributes, | ||||
7853 | mac_enc, mac_key_len, | ||||
7854 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) | ||||
7855 | { | ||||
7856 | ret = psa_ssl_status_to_mbedtls( status ); | ||||
7857 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); | ||||
7858 | goto end; | ||||
7859 | } | ||||
7860 | |||||
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 7861 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 7862 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) |
7863 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | ||||
7864 | && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) | ||||
7865 | #endif | ||||
7866 | ) ) | ||||
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7867 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
7868 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | | ||||
7869 | PSA_KEY_USAGE_VERIFY_HASH ); | ||||
7870 | else | ||||
7871 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); | ||||
7872 | |||||
7873 | if( ( status = psa_import_key( &attributes, | ||||
7874 | mac_dec, mac_key_len, | ||||
7875 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) | ||||
7876 | { | ||||
7877 | ret = psa_ssl_status_to_mbedtls( status ); | ||||
7878 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); | ||||
7879 | goto end; | ||||
7880 | } | ||||
7881 | #else | ||||
7882 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); | ||||
7883 | if( ret != 0 ) | ||||
7884 | goto end; | ||||
7885 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); | ||||
7886 | if( ret != 0 ) | ||||
7887 | goto end; | ||||
7888 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
7889 | } | ||||
7890 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | ||||
7891 | |||||
7892 | ((void) mac_dec); | ||||
7893 | ((void) mac_enc); | ||||
7894 | |||||
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7895 | end: |
7896 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); | ||||
7897 | return( ret ); | ||||
7898 | } | ||||
7899 | |||||
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 7900 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
7901 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | ||||
7902 | unsigned char *hash, size_t *hashlen, | ||||
7903 | unsigned char *data, size_t data_len, | ||||
7904 | mbedtls_md_type_t md_alg ) | ||||
7905 | { | ||||
7906 | psa_status_t status; | ||||
7907 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; | ||||
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 7908 | psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg ); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 7909 | |
7910 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); | ||||
7911 | |||||
7912 | if( ( status = psa_hash_setup( &hash_operation, | ||||
7913 | hash_alg ) ) != PSA_SUCCESS ) | ||||
7914 | { | ||||
7915 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); | ||||
7916 | goto exit; | ||||
7917 | } | ||||
7918 | |||||
7919 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, | ||||
7920 | 64 ) ) != PSA_SUCCESS ) | ||||
7921 | { | ||||
7922 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); | ||||
7923 | goto exit; | ||||
7924 | } | ||||
7925 | |||||
7926 | if( ( status = psa_hash_update( &hash_operation, | ||||
7927 | data, data_len ) ) != PSA_SUCCESS ) | ||||
7928 | { | ||||
7929 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); | ||||
7930 | goto exit; | ||||
7931 | } | ||||
7932 | |||||
7933 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, | ||||
7934 | hashlen ) ) != PSA_SUCCESS ) | ||||
7935 | { | ||||
7936 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); | ||||
7937 | goto exit; | ||||
7938 | } | ||||
7939 | |||||
7940 | exit: | ||||
7941 | if( status != PSA_SUCCESS ) | ||||
7942 | { | ||||
7943 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
7944 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | ||||
7945 | switch( status ) | ||||
7946 | { | ||||
7947 | case PSA_ERROR_NOT_SUPPORTED: | ||||
7948 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); | ||||
7949 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ | ||||
7950 | case PSA_ERROR_BUFFER_TOO_SMALL: | ||||
7951 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); | ||||
7952 | case PSA_ERROR_INSUFFICIENT_MEMORY: | ||||
7953 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); | ||||
7954 | default: | ||||
7955 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); | ||||
7956 | } | ||||
7957 | } | ||||
7958 | return( 0 ); | ||||
7959 | } | ||||
7960 | |||||
7961 | #else | ||||
7962 | |||||
7963 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | ||||
7964 | unsigned char *hash, size_t *hashlen, | ||||
7965 | unsigned char *data, size_t data_len, | ||||
7966 | mbedtls_md_type_t md_alg ) | ||||
7967 | { | ||||
7968 | int ret = 0; | ||||
7969 | mbedtls_md_context_t ctx; | ||||
7970 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); | ||||
7971 | *hashlen = mbedtls_md_get_size( md_info ); | ||||
7972 | |||||
7973 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); | ||||
7974 | |||||
7975 | mbedtls_md_init( &ctx ); | ||||
7976 | |||||
7977 | /* | ||||
7978 | * digitally-signed struct { | ||||
7979 | * opaque client_random[32]; | ||||
7980 | * opaque server_random[32]; | ||||
7981 | * ServerDHParams params; | ||||
7982 | * }; | ||||
7983 | */ | ||||
7984 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) | ||||
7985 | { | ||||
7986 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | ||||
7987 | goto exit; | ||||
7988 | } | ||||
7989 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) | ||||
7990 | { | ||||
7991 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); | ||||
7992 | goto exit; | ||||
7993 | } | ||||
7994 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) | ||||
7995 | { | ||||
7996 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | ||||
7997 | goto exit; | ||||
7998 | } | ||||
7999 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) | ||||
8000 | { | ||||
8001 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | ||||
8002 | goto exit; | ||||
8003 | } | ||||
8004 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) | ||||
8005 | { | ||||
8006 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); | ||||
8007 | goto exit; | ||||
8008 | } | ||||
8009 | |||||
8010 | exit: | ||||
8011 | mbedtls_md_free( &ctx ); | ||||
8012 | |||||
8013 | if( ret != 0 ) | ||||
8014 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
8015 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | ||||
8016 | |||||
8017 | return( ret ); | ||||
8018 | } | ||||
8019 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
8020 | |||||
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8021 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
8022 | |||||
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8023 | /* Find the preferred hash for a given signature algorithm. */ |
8024 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( | ||||
8025 | mbedtls_ssl_context *ssl, | ||||
8026 | unsigned int sig_alg ) | ||||
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8027 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8028 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8029 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8030 | |
8031 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) | ||||
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8032 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8033 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8034 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8035 | { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8036 | unsigned int hash_alg_received = |
8037 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( | ||||
8038 | received_sig_algs[i] ); | ||||
8039 | unsigned int sig_alg_received = | ||||
8040 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( | ||||
8041 | received_sig_algs[i] ); | ||||
8042 | |||||
8043 | if( sig_alg == sig_alg_received ) | ||||
8044 | { | ||||
8045 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
8046 | if( ssl->handshake->key_cert && ssl->handshake->key_cert->key ) | ||||
8047 | { | ||||
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8048 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 8049 | mbedtls_hash_info_psa_from_md( hash_alg_received ); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8050 | |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8051 | if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
8052 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, | ||||
8053 | PSA_ALG_ECDSA( psa_hash_alg ), | ||||
8054 | PSA_KEY_USAGE_SIGN_HASH ) ) | ||||
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8055 | continue; |
8056 | |||||
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8057 | if( sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
Neil Armstrong | 971f30d | 2022-07-01 16:23:50 +0200 | [diff] [blame] | 8058 | ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key, |
8059 | PSA_ALG_RSA_PKCS1V15_SIGN( | ||||
8060 | psa_hash_alg ), | ||||
8061 | PSA_KEY_USAGE_SIGN_HASH ) ) | ||||
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8062 | continue; |
8063 | } | ||||
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8064 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8065 | |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8066 | return( hash_alg_received ); |
8067 | } | ||||
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8068 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8069 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8070 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8071 | } |
8072 | |||||
8073 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | ||||
8074 | |||||
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8075 | /* Serialization of TLS 1.2 sessions: |
8076 | * | ||||
8077 | * struct { | ||||
8078 | * uint64 start_time; | ||||
8079 | * uint8 ciphersuite[2]; // defined by the standard | ||||
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8080 | * uint8 session_id_len; // at most 32 |
8081 | * opaque session_id[32]; | ||||
8082 | * opaque master[48]; // fixed length in the standard | ||||
8083 | * uint32 verify_result; | ||||
8084 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert | ||||
8085 | * opaque ticket<0..2^24-1>; // length 0 means no ticket | ||||
8086 | * uint32 ticket_lifetime; | ||||
8087 | * uint8 mfl_code; // up to 255 according to standard | ||||
8088 | * uint8 encrypt_then_mac; // 0 or 1 | ||||
8089 | * } serialized_session_tls12; | ||||
8090 | * | ||||
8091 | */ | ||||
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8092 | static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8093 | unsigned char *buf, |
8094 | size_t buf_len ) | ||||
8095 | { | ||||
8096 | unsigned char *p = buf; | ||||
8097 | size_t used = 0; | ||||
8098 | |||||
8099 | #if defined(MBEDTLS_HAVE_TIME) | ||||
8100 | uint64_t start; | ||||
8101 | #endif | ||||
8102 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | ||||
8103 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
8104 | size_t cert_len; | ||||
8105 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
8106 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | ||||
8107 | |||||
8108 | /* | ||||
8109 | * Time | ||||
8110 | */ | ||||
8111 | #if defined(MBEDTLS_HAVE_TIME) | ||||
8112 | used += 8; | ||||
8113 | |||||
8114 | if( used <= buf_len ) | ||||
8115 | { | ||||
8116 | start = (uint64_t) session->start; | ||||
8117 | |||||
8118 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); | ||||
8119 | p += 8; | ||||
8120 | } | ||||
8121 | #endif /* MBEDTLS_HAVE_TIME */ | ||||
8122 | |||||
8123 | /* | ||||
8124 | * Basic mandatory fields | ||||
8125 | */ | ||||
8126 | used += 2 /* ciphersuite */ | ||||
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8127 | + 1 /* id_len */ |
8128 | + sizeof( session->id ) | ||||
8129 | + sizeof( session->master ) | ||||
8130 | + 4; /* verify_result */ | ||||
8131 | |||||
8132 | if( used <= buf_len ) | ||||
8133 | { | ||||
8134 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); | ||||
8135 | p += 2; | ||||
8136 | |||||
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8137 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
8138 | memcpy( p, session->id, 32 ); | ||||
8139 | p += 32; | ||||
8140 | |||||
8141 | memcpy( p, session->master, 48 ); | ||||
8142 | p += 48; | ||||
8143 | |||||
8144 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); | ||||
8145 | p += 4; | ||||
8146 | } | ||||
8147 | |||||
8148 | /* | ||||
8149 | * Peer's end-entity certificate | ||||
8150 | */ | ||||
8151 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | ||||
8152 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
8153 | if( session->peer_cert == NULL ) | ||||
8154 | cert_len = 0; | ||||
8155 | else | ||||
8156 | cert_len = session->peer_cert->raw.len; | ||||
8157 | |||||
8158 | used += 3 + cert_len; | ||||
8159 | |||||
8160 | if( used <= buf_len ) | ||||
8161 | { | ||||
8162 | *p++ = MBEDTLS_BYTE_2( cert_len ); | ||||
8163 | *p++ = MBEDTLS_BYTE_1( cert_len ); | ||||
8164 | *p++ = MBEDTLS_BYTE_0( cert_len ); | ||||
8165 | |||||
8166 | if( session->peer_cert != NULL ) | ||||
8167 | { | ||||
8168 | memcpy( p, session->peer_cert->raw.p, cert_len ); | ||||
8169 | p += cert_len; | ||||
8170 | } | ||||
8171 | } | ||||
8172 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
8173 | if( session->peer_cert_digest != NULL ) | ||||
8174 | { | ||||
8175 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; | ||||
8176 | if( used <= buf_len ) | ||||
8177 | { | ||||
8178 | *p++ = (unsigned char) session->peer_cert_digest_type; | ||||
8179 | *p++ = (unsigned char) session->peer_cert_digest_len; | ||||
8180 | memcpy( p, session->peer_cert_digest, | ||||
8181 | session->peer_cert_digest_len ); | ||||
8182 | p += session->peer_cert_digest_len; | ||||
8183 | } | ||||
8184 | } | ||||
8185 | else | ||||
8186 | { | ||||
8187 | used += 2; | ||||
8188 | if( used <= buf_len ) | ||||
8189 | { | ||||
8190 | *p++ = (unsigned char) MBEDTLS_MD_NONE; | ||||
8191 | *p++ = 0; | ||||
8192 | } | ||||
8193 | } | ||||
8194 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
8195 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | ||||
8196 | |||||
8197 | /* | ||||
8198 | * Session ticket if any, plus associated data | ||||
8199 | */ | ||||
8200 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | ||||
8201 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ | ||||
8202 | |||||
8203 | if( used <= buf_len ) | ||||
8204 | { | ||||
8205 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); | ||||
8206 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); | ||||
8207 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); | ||||
8208 | |||||
8209 | if( session->ticket != NULL ) | ||||
8210 | { | ||||
8211 | memcpy( p, session->ticket, session->ticket_len ); | ||||
8212 | p += session->ticket_len; | ||||
8213 | } | ||||
8214 | |||||
8215 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); | ||||
8216 | p += 4; | ||||
8217 | } | ||||
8218 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | ||||
8219 | |||||
8220 | /* | ||||
8221 | * Misc extension-related info | ||||
8222 | */ | ||||
8223 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | ||||
8224 | used += 1; | ||||
8225 | |||||
8226 | if( used <= buf_len ) | ||||
8227 | *p++ = session->mfl_code; | ||||
8228 | #endif | ||||
8229 | |||||
8230 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | ||||
8231 | used += 1; | ||||
8232 | |||||
8233 | if( used <= buf_len ) | ||||
8234 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); | ||||
8235 | #endif | ||||
8236 | |||||
8237 | return( used ); | ||||
8238 | } | ||||
8239 | |||||
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8240 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 8241 | static int ssl_tls12_session_load( mbedtls_ssl_session *session, |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8242 | const unsigned char *buf, |
8243 | size_t len ) | ||||
8244 | { | ||||
8245 | #if defined(MBEDTLS_HAVE_TIME) | ||||
8246 | uint64_t start; | ||||
8247 | #endif | ||||
8248 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | ||||
8249 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
8250 | size_t cert_len; | ||||
8251 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
8252 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | ||||
8253 | |||||
8254 | const unsigned char *p = buf; | ||||
8255 | const unsigned char * const end = buf + len; | ||||
8256 | |||||
8257 | /* | ||||
8258 | * Time | ||||
8259 | */ | ||||
8260 | #if defined(MBEDTLS_HAVE_TIME) | ||||
8261 | if( 8 > (size_t)( end - p ) ) | ||||
8262 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8263 | |||||
8264 | start = ( (uint64_t) p[0] << 56 ) | | ||||
8265 | ( (uint64_t) p[1] << 48 ) | | ||||
8266 | ( (uint64_t) p[2] << 40 ) | | ||||
8267 | ( (uint64_t) p[3] << 32 ) | | ||||
8268 | ( (uint64_t) p[4] << 24 ) | | ||||
8269 | ( (uint64_t) p[5] << 16 ) | | ||||
8270 | ( (uint64_t) p[6] << 8 ) | | ||||
8271 | ( (uint64_t) p[7] ); | ||||
8272 | p += 8; | ||||
8273 | |||||
8274 | session->start = (time_t) start; | ||||
8275 | #endif /* MBEDTLS_HAVE_TIME */ | ||||
8276 | |||||
8277 | /* | ||||
8278 | * Basic mandatory fields | ||||
8279 | */ | ||||
Thomas Daubney | 20f89a9 | 2022-06-20 15:12:19 +0100 | [diff] [blame] | 8280 | if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8281 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
8282 | |||||
8283 | session->ciphersuite = ( p[0] << 8 ) | p[1]; | ||||
8284 | p += 2; | ||||
8285 | |||||
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8286 | session->id_len = *p++; |
8287 | memcpy( session->id, p, 32 ); | ||||
8288 | p += 32; | ||||
8289 | |||||
8290 | memcpy( session->master, p, 48 ); | ||||
8291 | p += 48; | ||||
8292 | |||||
8293 | session->verify_result = ( (uint32_t) p[0] << 24 ) | | ||||
8294 | ( (uint32_t) p[1] << 16 ) | | ||||
8295 | ( (uint32_t) p[2] << 8 ) | | ||||
8296 | ( (uint32_t) p[3] ); | ||||
8297 | p += 4; | ||||
8298 | |||||
8299 | /* Immediately clear invalid pointer values that have been read, in case | ||||
8300 | * we exit early before we replaced them with valid ones. */ | ||||
8301 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | ||||
8302 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
8303 | session->peer_cert = NULL; | ||||
8304 | #else | ||||
8305 | session->peer_cert_digest = NULL; | ||||
8306 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
8307 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | ||||
8308 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | ||||
8309 | session->ticket = NULL; | ||||
8310 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | ||||
8311 | |||||
8312 | /* | ||||
8313 | * Peer certificate | ||||
8314 | */ | ||||
8315 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | ||||
8316 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | ||||
8317 | /* Deserialize CRT from the end of the ticket. */ | ||||
8318 | if( 3 > (size_t)( end - p ) ) | ||||
8319 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8320 | |||||
8321 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; | ||||
8322 | p += 3; | ||||
8323 | |||||
8324 | if( cert_len != 0 ) | ||||
8325 | { | ||||
8326 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
8327 | |||||
8328 | if( cert_len > (size_t)( end - p ) ) | ||||
8329 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8330 | |||||
8331 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); | ||||
8332 | |||||
8333 | if( session->peer_cert == NULL ) | ||||
8334 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | ||||
8335 | |||||
8336 | mbedtls_x509_crt_init( session->peer_cert ); | ||||
8337 | |||||
8338 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, | ||||
8339 | p, cert_len ) ) != 0 ) | ||||
8340 | { | ||||
8341 | mbedtls_x509_crt_free( session->peer_cert ); | ||||
8342 | mbedtls_free( session->peer_cert ); | ||||
8343 | session->peer_cert = NULL; | ||||
8344 | return( ret ); | ||||
8345 | } | ||||
8346 | |||||
8347 | p += cert_len; | ||||
8348 | } | ||||
8349 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
8350 | /* Deserialize CRT digest from the end of the ticket. */ | ||||
8351 | if( 2 > (size_t)( end - p ) ) | ||||
8352 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8353 | |||||
8354 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; | ||||
8355 | session->peer_cert_digest_len = (size_t) *p++; | ||||
8356 | |||||
8357 | if( session->peer_cert_digest_len != 0 ) | ||||
8358 | { | ||||
8359 | const mbedtls_md_info_t *md_info = | ||||
8360 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); | ||||
8361 | if( md_info == NULL ) | ||||
8362 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8363 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) | ||||
8364 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8365 | |||||
8366 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) | ||||
8367 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8368 | |||||
8369 | session->peer_cert_digest = | ||||
8370 | mbedtls_calloc( 1, session->peer_cert_digest_len ); | ||||
8371 | if( session->peer_cert_digest == NULL ) | ||||
8372 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | ||||
8373 | |||||
8374 | memcpy( session->peer_cert_digest, p, | ||||
8375 | session->peer_cert_digest_len ); | ||||
8376 | p += session->peer_cert_digest_len; | ||||
8377 | } | ||||
8378 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | ||||
8379 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | ||||
8380 | |||||
8381 | /* | ||||
8382 | * Session ticket and associated data | ||||
8383 | */ | ||||
8384 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | ||||
8385 | if( 3 > (size_t)( end - p ) ) | ||||
8386 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8387 | |||||
8388 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; | ||||
8389 | p += 3; | ||||
8390 | |||||
8391 | if( session->ticket_len != 0 ) | ||||
8392 | { | ||||
8393 | if( session->ticket_len > (size_t)( end - p ) ) | ||||
8394 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8395 | |||||
8396 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); | ||||
8397 | if( session->ticket == NULL ) | ||||
8398 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | ||||
8399 | |||||
8400 | memcpy( session->ticket, p, session->ticket_len ); | ||||
8401 | p += session->ticket_len; | ||||
8402 | } | ||||
8403 | |||||
8404 | if( 4 > (size_t)( end - p ) ) | ||||
8405 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8406 | |||||
8407 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | | ||||
8408 | ( (uint32_t) p[1] << 16 ) | | ||||
8409 | ( (uint32_t) p[2] << 8 ) | | ||||
8410 | ( (uint32_t) p[3] ); | ||||
8411 | p += 4; | ||||
8412 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | ||||
8413 | |||||
8414 | /* | ||||
8415 | * Misc extension-related info | ||||
8416 | */ | ||||
8417 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | ||||
8418 | if( 1 > (size_t)( end - p ) ) | ||||
8419 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8420 | |||||
8421 | session->mfl_code = *p++; | ||||
8422 | #endif | ||||
8423 | |||||
8424 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | ||||
8425 | if( 1 > (size_t)( end - p ) ) | ||||
8426 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8427 | |||||
8428 | session->encrypt_then_mac = *p++; | ||||
8429 | #endif | ||||
8430 | |||||
8431 | /* Done, should have consumed entire buffer */ | ||||
8432 | if( p != end ) | ||||
8433 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
8434 | |||||
8435 | return( 0 ); | ||||
8436 | } | ||||
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 8437 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
8438 | |||||
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8439 | int mbedtls_ssl_validate_ciphersuite( |
8440 | const mbedtls_ssl_context *ssl, | ||||
8441 | const mbedtls_ssl_ciphersuite_t *suite_info, | ||||
8442 | mbedtls_ssl_protocol_version min_tls_version, | ||||
8443 | mbedtls_ssl_protocol_version max_tls_version ) | ||||
8444 | { | ||||
8445 | (void) ssl; | ||||
8446 | |||||
8447 | if( suite_info == NULL ) | ||||
8448 | return( -1 ); | ||||
8449 | |||||
8450 | if( ( suite_info->min_tls_version > max_tls_version ) || | ||||
8451 | ( suite_info->max_tls_version < min_tls_version ) ) | ||||
8452 | { | ||||
8453 | return( -1 ); | ||||
8454 | } | ||||
8455 | |||||
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 8456 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8457 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
8458 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && | ||||
8459 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) | ||||
8460 | { | ||||
8461 | return( -1 ); | ||||
8462 | } | ||||
8463 | #endif | ||||
8464 | |||||
8465 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ | ||||
8466 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | ||||
8467 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && | ||||
8468 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) | ||||
8469 | { | ||||
8470 | return( -1 ); | ||||
8471 | } | ||||
8472 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | ||||
8473 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
8474 | |||||
8475 | return( 0 ); | ||||
8476 | } | ||||
8477 | |||||
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8478 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
8479 | /* | ||||
8480 | * Function for writing a signature algorithm extension. | ||||
8481 | * | ||||
8482 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` | ||||
8483 | * value (TLS 1.3 RFC8446): | ||||
8484 | * enum { | ||||
8485 | * .... | ||||
8486 | * ecdsa_secp256r1_sha256( 0x0403 ), | ||||
8487 | * ecdsa_secp384r1_sha384( 0x0503 ), | ||||
8488 | * ecdsa_secp521r1_sha512( 0x0603 ), | ||||
8489 | * .... | ||||
8490 | * } SignatureScheme; | ||||
8491 | * | ||||
8492 | * struct { | ||||
8493 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; | ||||
8494 | * } SignatureSchemeList; | ||||
8495 | * | ||||
8496 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` | ||||
8497 | * value (TLS 1.2 RFC5246): | ||||
8498 | * enum { | ||||
8499 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), | ||||
8500 | * sha512(6), (255) | ||||
8501 | * } HashAlgorithm; | ||||
8502 | * | ||||
8503 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } | ||||
8504 | * SignatureAlgorithm; | ||||
8505 | * | ||||
8506 | * struct { | ||||
8507 | * HashAlgorithm hash; | ||||
8508 | * SignatureAlgorithm signature; | ||||
8509 | * } SignatureAndHashAlgorithm; | ||||
8510 | * | ||||
8511 | * SignatureAndHashAlgorithm | ||||
8512 | * supported_signature_algorithms<2..2^16-2>; | ||||
8513 | * | ||||
8514 | * The TLS 1.3 signature algorithm extension was defined to be a compatible | ||||
8515 | * generalization of the TLS 1.2 signature algorithm extension. | ||||
8516 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by | ||||
8517 | * `SignatureScheme` field of TLS 1.3 | ||||
8518 | * | ||||
8519 | */ | ||||
8520 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, | ||||
8521 | const unsigned char *end, size_t *out_len ) | ||||
8522 | { | ||||
8523 | unsigned char *p = buf; | ||||
8524 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ | ||||
8525 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ | ||||
8526 | |||||
8527 | *out_len = 0; | ||||
8528 | |||||
8529 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); | ||||
8530 | |||||
8531 | /* Check if we have space for header and length field: | ||||
8532 | * - extension_type (2 bytes) | ||||
8533 | * - extension_data_length (2 bytes) | ||||
8534 | * - supported_signature_algorithms_length (2 bytes) | ||||
8535 | */ | ||||
8536 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); | ||||
8537 | p += 6; | ||||
8538 | |||||
8539 | /* | ||||
8540 | * Write supported_signature_algorithms | ||||
8541 | */ | ||||
8542 | supported_sig_alg = p; | ||||
8543 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); | ||||
8544 | if( sig_alg == NULL ) | ||||
8545 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); | ||||
8546 | |||||
8547 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) | ||||
8548 | { | ||||
Jerry Yu | 53f5c15 | 2022-06-22 20:24:38 +0800 | [diff] [blame] | 8549 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s", |
8550 | *sig_alg, | ||||
8551 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); | ||||
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8552 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
8553 | continue; | ||||
8554 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); | ||||
8555 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); | ||||
8556 | p += 2; | ||||
Jerry Yu | 80dd5db | 2022-06-22 19:30:32 +0800 | [diff] [blame] | 8557 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s", |
Jerry Yu | f3b46b5 | 2022-06-19 16:52:27 +0800 | [diff] [blame] | 8558 | *sig_alg, |
8559 | mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) ); | ||||
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8560 | } |
8561 | |||||
8562 | /* Length of supported_signature_algorithms */ | ||||
8563 | supported_sig_alg_len = p - supported_sig_alg; | ||||
8564 | if( supported_sig_alg_len == 0 ) | ||||
8565 | { | ||||
8566 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); | ||||
8567 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | ||||
8568 | } | ||||
8569 | |||||
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8570 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8571 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8572 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
8573 | |||||
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8574 | *out_len = p - buf; |
8575 | |||||
8576 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | ||||
8577 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; | ||||
8578 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | ||||
8579 | return( 0 ); | ||||
8580 | } | ||||
8581 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | ||||
8582 | |||||
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8583 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8584 | /* |
8585 | * mbedtls_ssl_parse_server_name_ext | ||||
8586 | * | ||||
8587 | * Structure of server_name extension: | ||||
8588 | * | ||||
8589 | * enum { | ||||
8590 | * host_name(0), (255) | ||||
8591 | * } NameType; | ||||
8592 | * opaque HostName<1..2^16-1>; | ||||
8593 | * | ||||
8594 | * struct { | ||||
8595 | * NameType name_type; | ||||
8596 | * select (name_type) { | ||||
8597 | * case host_name: HostName; | ||||
8598 | * } name; | ||||
8599 | * } ServerName; | ||||
8600 | * struct { | ||||
8601 | * ServerName server_name_list<1..2^16-1> | ||||
8602 | * } ServerNameList; | ||||
8603 | */ | ||||
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8604 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8605 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
8606 | const unsigned char *buf, | ||||
8607 | const unsigned char *end ) | ||||
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8608 | { |
8609 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||||
8610 | const unsigned char *p = buf; | ||||
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8611 | size_t server_name_list_len, hostname_len; |
8612 | const unsigned char *server_name_list_end; | ||||
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8613 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8614 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8615 | |
8616 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); | ||||
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8617 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8618 | p += 2; |
8619 | |||||
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8620 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
8621 | server_name_list_end = p + server_name_list_len; | ||||
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8622 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8623 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8624 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8625 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8626 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
8627 | hostname_len + 3 ); | ||||
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8628 | |
8629 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) | ||||
8630 | { | ||||
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8631 | /* sni_name is intended to be used only during the parsing of the |
8632 | * ClientHello message (it is reset to NULL before the end of | ||||
8633 | * the message parsing). Thus it is ok to just point to the | ||||
8634 | * reception buffer and not make a copy of it. | ||||
8635 | */ | ||||
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8636 | ssl->handshake->sni_name = p + 3; |
8637 | ssl->handshake->sni_name_len = hostname_len; | ||||
8638 | if( ssl->conf->f_sni == NULL ) | ||||
8639 | return( 0 ); | ||||
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8640 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8641 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8642 | if( ret != 0 ) |
8643 | { | ||||
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8644 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 8645 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
8646 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); | ||||
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8647 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
8648 | } | ||||
8649 | return( 0 ); | ||||
8650 | } | ||||
8651 | |||||
8652 | p += hostname_len + 3; | ||||
8653 | } | ||||
8654 | |||||
8655 | return( 0 ); | ||||
8656 | } | ||||
8657 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | ||||
8658 | |||||
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8659 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 8660 | MBEDTLS_CHECK_RETURN_CRITICAL |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8661 | int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
8662 | const unsigned char *buf, | ||||
8663 | const unsigned char *end ) | ||||
8664 | { | ||||
8665 | const unsigned char *p = buf; | ||||
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8666 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8667 | const unsigned char *protocol_name_list; |
8668 | const unsigned char *protocol_name_list_end; | ||||
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8669 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8670 | |
8671 | /* If ALPN not configured, just ignore the extension */ | ||||
8672 | if( ssl->conf->alpn_list == NULL ) | ||||
8673 | return( 0 ); | ||||
8674 | |||||
8675 | /* | ||||
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8676 | * RFC7301, section 3.1 |
8677 | * opaque ProtocolName<1..2^8-1>; | ||||
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8678 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8679 | * struct { |
8680 | * ProtocolName protocol_name_list<2..2^16-1> | ||||
8681 | * } ProtocolNameList; | ||||
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8682 | */ |
8683 | |||||
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8684 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8685 | * protocol_name_list_len 2 bytes |
8686 | * protocol_name_len 1 bytes | ||||
8687 | * protocol_name >=1 byte | ||||
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8688 | */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8689 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 ); |
8690 | |||||
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8691 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8692 | p += 2; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8693 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8694 | protocol_name_list = p; |
8695 | protocol_name_list_end = p + protocol_name_list_len; | ||||
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8696 | |
8697 | /* Validate peer's list (lengths) */ | ||||
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8698 | while( p < protocol_name_list_end ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8699 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8700 | protocol_name_len = *p++; |
8701 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end, | ||||
8702 | protocol_name_len ); | ||||
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8703 | if( protocol_name_len == 0 ) |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8704 | { |
8705 | MBEDTLS_SSL_PEND_FATAL_ALERT( | ||||
8706 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, | ||||
8707 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); | ||||
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8708 | return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8709 | } |
8710 | |||||
8711 | p += protocol_name_len; | ||||
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8712 | } |
8713 | |||||
8714 | /* Use our order of preference */ | ||||
8715 | for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ ) | ||||
8716 | { | ||||
8717 | size_t const alpn_len = strlen( *alpn ); | ||||
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8718 | p = protocol_name_list; |
8719 | while( p < protocol_name_list_end ) | ||||
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8720 | { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8721 | protocol_name_len = *p++; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8722 | if( protocol_name_len == alpn_len && |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8723 | memcmp( p, *alpn, alpn_len ) == 0 ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8724 | { |
8725 | ssl->alpn_chosen = *alpn; | ||||
8726 | return( 0 ); | ||||
8727 | } | ||||
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8728 | |
8729 | p += protocol_name_len; | ||||
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8730 | } |
8731 | } | ||||
8732 | |||||
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8733 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8734 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
8735 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, | ||||
8736 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); | ||||
8737 | return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL ); | ||||
8738 | } | ||||
8739 | |||||
8740 | int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl, | ||||
8741 | unsigned char *buf, | ||||
8742 | unsigned char *end, | ||||
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8743 | size_t *out_len ) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8744 | { |
8745 | unsigned char *p = buf; | ||||
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8746 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 8747 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8748 | |
8749 | if( ssl->alpn_chosen == NULL ) | ||||
8750 | { | ||||
8751 | return( 0 ); | ||||
8752 | } | ||||
8753 | |||||
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8754 | protocol_name_len = strlen( ssl->alpn_chosen ); |
8755 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len ); | ||||
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8756 | |
8757 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) ); | ||||
8758 | /* | ||||
8759 | * 0 . 1 ext identifier | ||||
8760 | * 2 . 3 ext length | ||||
8761 | * 4 . 5 protocol list length | ||||
8762 | * 6 . 6 protocol name length | ||||
8763 | * 7 . 7+n protocol name | ||||
8764 | */ | ||||
8765 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); | ||||
8766 | |||||
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8767 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8768 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8769 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 ); |
8770 | MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 ); | ||||
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 8771 | /* Note: the length of the chosen protocol has been checked to be less |
8772 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. | ||||
8773 | */ | ||||
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8774 | p[6] = MBEDTLS_BYTE_0( protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8775 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 8776 | memcpy( p + 7, ssl->alpn_chosen, protocol_name_len ); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 8777 | return ( 0 ); |
8778 | } | ||||
8779 | #endif /* MBEDTLS_SSL_ALPN */ | ||||
8780 | |||||
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8781 | #endif /* MBEDTLS_SSL_TLS_C */ |