Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 shared functions |
| 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 | /* |
| 20 | * The SSL 3.0 specification was drafted by Netscape in 1996, |
| 21 | * and became an IETF standard in 1999. |
| 22 | * |
| 23 | * http://wp.netscape.com/eng/ssl3/ |
| 24 | * http://www.ietf.org/rfc/rfc2246.txt |
| 25 | * http://www.ietf.org/rfc/rfc4346.txt |
| 26 | */ |
| 27 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 28 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 32 | #if defined(MBEDTLS_PLATFORM_C) |
| 33 | #include "mbedtls/platform.h" |
| 34 | #else |
| 35 | #include <stdlib.h> |
| 36 | #define mbedtls_calloc calloc |
| 37 | #define mbedtls_free free |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #endif |
| 39 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 41 | #include "mbedtls/ssl_internal.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 42 | #include "mbedtls/debug.h" |
| 43 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 44 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 45 | #include "mbedtls/version.h" |
Gabor Mezei | e24dea8 | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 46 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 47 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 48 | #include <string.h> |
| 49 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 50 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 51 | #include "mbedtls/psa_util.h" |
| 52 | #include "psa/crypto.h" |
| 53 | #endif |
| 54 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 55 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 56 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 57 | #endif |
| 58 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 60 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 61 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 62 | /* Top-level Connection ID API */ |
| 63 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 64 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 65 | size_t len, |
| 66 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 67 | { |
| 68 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 69 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 70 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 71 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 72 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 73 | { |
| 74 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 75 | } |
| 76 | |
| 77 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 78 | conf->cid_len = len; |
| 79 | return( 0 ); |
| 80 | } |
| 81 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 82 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 83 | int enable, |
| 84 | unsigned char const *own_cid, |
| 85 | size_t own_cid_len ) |
| 86 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 87 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 88 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 89 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 90 | ssl->negotiate_cid = enable; |
| 91 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 92 | { |
| 93 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 94 | return( 0 ); |
| 95 | } |
| 96 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 97 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 98 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 99 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 100 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 101 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 102 | (unsigned) own_cid_len, |
| 103 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 104 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 105 | } |
| 106 | |
| 107 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 108 | /* Truncation is not an issue here because |
| 109 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 110 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 111 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 112 | return( 0 ); |
| 113 | } |
| 114 | |
| 115 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 116 | int *enabled, |
| 117 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 118 | size_t *peer_cid_len ) |
| 119 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 120 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 121 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 122 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 123 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 124 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 125 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 126 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 127 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 128 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 129 | * were used, but client and server requested the empty CID. |
| 130 | * This is indistinguishable from not using the CID extension |
| 131 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 132 | if( ssl->transform_in->in_cid_len == 0 && |
| 133 | ssl->transform_in->out_cid_len == 0 ) |
| 134 | { |
| 135 | return( 0 ); |
| 136 | } |
| 137 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 138 | if( peer_cid_len != NULL ) |
| 139 | { |
| 140 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 141 | if( peer_cid != NULL ) |
| 142 | { |
| 143 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 144 | ssl->transform_in->out_cid_len ); |
| 145 | } |
| 146 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 147 | |
| 148 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 149 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 150 | return( 0 ); |
| 151 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 152 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 157 | /* |
| 158 | * Convert max_fragment_length codes to length. |
| 159 | * RFC 6066 says: |
| 160 | * enum{ |
| 161 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 162 | * } MaxFragmentLength; |
| 163 | * and we add 0 -> extension unused |
| 164 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 165 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 166 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 167 | switch( mfl ) |
| 168 | { |
| 169 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 170 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 171 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 172 | return 512; |
| 173 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 174 | return 1024; |
| 175 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 176 | return 2048; |
| 177 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 178 | return 4096; |
| 179 | default: |
| 180 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 181 | } |
| 182 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 184 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 185 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 186 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 187 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | mbedtls_ssl_session_free( dst ); |
| 189 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 190 | |
吴敬辉 | 0f6c6bc | 2021-11-29 10:46:35 +0800 | [diff] [blame^] | 191 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 192 | dst->ticket = NULL; |
| 193 | #endif |
| 194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 196 | |
| 197 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 198 | if( src->peer_cert != NULL ) |
| 199 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 200 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 202 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 203 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 204 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p, |
Manuel Pégourié-Gonnard | 4d2a8eb | 2014-06-13 20:33:27 +0200 | [diff] [blame] | 209 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 212 | dst->peer_cert = NULL; |
| 213 | return( ret ); |
| 214 | } |
| 215 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 216 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 217 | if( src->peer_cert_digest != NULL ) |
| 218 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 219 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 220 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 221 | if( dst->peer_cert_digest == NULL ) |
| 222 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 223 | |
| 224 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 225 | src->peer_cert_digest_len ); |
| 226 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 227 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 228 | } |
| 229 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 232 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 233 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 234 | if( src->ticket != NULL ) |
| 235 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 236 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 237 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 238 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 239 | |
| 240 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 241 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 242 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 243 | |
| 244 | return( 0 ); |
| 245 | } |
| 246 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 247 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 248 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
| 249 | { |
| 250 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); |
| 251 | if( resized_buffer == NULL ) |
| 252 | return -1; |
| 253 | |
| 254 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 255 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 256 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 257 | * lost, are done outside of this function. */ |
| 258 | memcpy( resized_buffer, *buffer, |
| 259 | ( len_new < *len_old ) ? len_new : *len_old ); |
| 260 | mbedtls_platform_zeroize( *buffer, *len_old ); |
| 261 | mbedtls_free( *buffer ); |
| 262 | |
| 263 | *buffer = resized_buffer; |
| 264 | *len_old = len_new; |
| 265 | |
| 266 | return 0; |
| 267 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 268 | |
| 269 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, |
Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 270 | size_t in_buf_new_len, |
| 271 | size_t out_buf_new_len ) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 272 | { |
| 273 | int modified = 0; |
| 274 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 275 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
| 276 | if( ssl->in_buf != NULL ) |
| 277 | { |
| 278 | written_in = ssl->in_msg - ssl->in_buf; |
| 279 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 280 | len_offset_in = ssl->in_len - ssl->in_buf; |
| 281 | if( downsizing ? |
| 282 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : |
| 283 | ssl->in_buf_len < in_buf_new_len ) |
| 284 | { |
| 285 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) |
| 286 | { |
| 287 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 288 | } |
| 289 | else |
| 290 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 291 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 292 | in_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 293 | modified = 1; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if( ssl->out_buf != NULL ) |
| 299 | { |
| 300 | written_out = ssl->out_msg - ssl->out_buf; |
| 301 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 302 | len_offset_out = ssl->out_len - ssl->out_buf; |
| 303 | if( downsizing ? |
| 304 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : |
| 305 | ssl->out_buf_len < out_buf_new_len ) |
| 306 | { |
| 307 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) |
| 308 | { |
| 309 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 310 | } |
| 311 | else |
| 312 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 313 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 314 | out_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 315 | modified = 1; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | if( modified ) |
| 320 | { |
| 321 | /* Update pointers here to avoid doing it twice. */ |
| 322 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 323 | /* Fields below might not be properly updated with record |
| 324 | * splitting or with CID, so they are manually updated here. */ |
| 325 | ssl->out_msg = ssl->out_buf + written_out; |
| 326 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 327 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 328 | |
| 329 | ssl->in_msg = ssl->in_buf + written_in; |
| 330 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 331 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 332 | } |
| 333 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 334 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 335 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | /* |
| 337 | * Key material generation |
| 338 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 339 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 340 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 341 | const char *label, |
| 342 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 343 | unsigned char *dstbuf, size_t dlen ) |
| 344 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 345 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 346 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 347 | mbedtls_md5_context md5; |
| 348 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 349 | unsigned char padding[16]; |
| 350 | unsigned char sha1sum[20]; |
| 351 | ((void)label); |
| 352 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 353 | mbedtls_md5_init( &md5 ); |
| 354 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 355 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 356 | /* |
| 357 | * SSLv3: |
| 358 | * block = |
| 359 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 360 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 361 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 362 | * ... |
| 363 | */ |
| 364 | for( i = 0; i < dlen / 16; i++ ) |
| 365 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 366 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 367 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 368 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 369 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 370 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 371 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 372 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 373 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 374 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 375 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 376 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 377 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 378 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 379 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 380 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 381 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 382 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 383 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 384 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 385 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 386 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 389 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 390 | mbedtls_md5_free( &md5 ); |
| 391 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 392 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 393 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 394 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 395 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 396 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 397 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 398 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 399 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 400 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 401 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 402 | const char *label, |
| 403 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 404 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 405 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 406 | size_t nb, hs; |
| 407 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 408 | const unsigned char *S1, *S2; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 409 | unsigned char *tmp; |
| 410 | size_t tmp_len = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 411 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | const mbedtls_md_info_t *md_info; |
| 413 | mbedtls_md_context_t md_ctx; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 414 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 415 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 417 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 418 | tmp_len = 20 + strlen( label ) + rlen; |
| 419 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 420 | if( tmp == NULL ) |
| 421 | { |
| 422 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 423 | goto exit; |
| 424 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 425 | |
| 426 | hs = ( slen + 1 ) / 2; |
| 427 | S1 = secret; |
| 428 | S2 = secret + slen - hs; |
| 429 | |
| 430 | nb = strlen( label ); |
| 431 | memcpy( tmp + 20, label, nb ); |
| 432 | memcpy( tmp + 20 + nb, random, rlen ); |
| 433 | nb += rlen; |
| 434 | |
| 435 | /* |
| 436 | * First compute P_md5(secret,label+random)[0..dlen] |
| 437 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 438 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 439 | { |
| 440 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 441 | goto exit; |
| 442 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 443 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 445 | { |
| 446 | goto exit; |
| 447 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 449 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 450 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 451 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 452 | |
| 453 | for( i = 0; i < dlen; i += 16 ) |
| 454 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 455 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 456 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 457 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 458 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 460 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 461 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 462 | |
| 463 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 464 | |
| 465 | for( j = 0; j < k; j++ ) |
| 466 | dstbuf[i + j] = h_i[j]; |
| 467 | } |
| 468 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 469 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 470 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 471 | /* |
| 472 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 473 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 474 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 475 | { |
| 476 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 477 | goto exit; |
| 478 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 479 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 481 | { |
| 482 | goto exit; |
| 483 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 485 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 486 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 487 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 488 | |
| 489 | for( i = 0; i < dlen; i += 20 ) |
| 490 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 491 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 492 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 493 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 495 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 496 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 497 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 498 | |
| 499 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 500 | |
| 501 | for( j = 0; j < k; j++ ) |
| 502 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 503 | } |
| 504 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 505 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 506 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 507 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 508 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 509 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 510 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 511 | mbedtls_free( tmp ); |
| 512 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 513 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 514 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 515 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 516 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 517 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 518 | |
| 519 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 520 | psa_key_id_t key, |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 521 | psa_algorithm_t alg, |
| 522 | const unsigned char* seed, size_t seed_length, |
| 523 | const unsigned char* label, size_t label_length, |
| 524 | size_t capacity ) |
| 525 | { |
| 526 | psa_status_t status; |
| 527 | |
| 528 | status = psa_key_derivation_setup( derivation, alg ); |
| 529 | if( status != PSA_SUCCESS ) |
| 530 | return( status ); |
| 531 | |
| 532 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 533 | { |
| 534 | status = psa_key_derivation_input_bytes( derivation, |
| 535 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 536 | seed, seed_length ); |
| 537 | if( status != PSA_SUCCESS ) |
| 538 | return( status ); |
| 539 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 540 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 541 | { |
| 542 | status = psa_key_derivation_input_bytes( |
| 543 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 544 | NULL, 0 ); |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | status = psa_key_derivation_input_key( |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 549 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 550 | } |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 551 | if( status != PSA_SUCCESS ) |
| 552 | return( status ); |
| 553 | |
| 554 | status = psa_key_derivation_input_bytes( derivation, |
| 555 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 556 | label, label_length ); |
| 557 | if( status != PSA_SUCCESS ) |
| 558 | return( status ); |
| 559 | } |
| 560 | else |
| 561 | { |
| 562 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 563 | } |
| 564 | |
| 565 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 566 | if( status != PSA_SUCCESS ) |
| 567 | return( status ); |
| 568 | |
| 569 | return( PSA_SUCCESS ); |
| 570 | } |
| 571 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 572 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 573 | const unsigned char *secret, size_t slen, |
| 574 | const char *label, |
| 575 | const unsigned char *random, size_t rlen, |
| 576 | unsigned char *dstbuf, size_t dlen ) |
| 577 | { |
| 578 | psa_status_t status; |
| 579 | psa_algorithm_t alg; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 580 | psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 581 | psa_key_derivation_operation_t derivation = |
Janos Follath | 8dee877 | 2019-07-30 12:53:32 +0100 | [diff] [blame] | 582 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 583 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 584 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 585 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 586 | else |
| 587 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 588 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 589 | /* Normally a "secret" should be long enough to be impossible to |
| 590 | * find by brute force, and in particular should not be empty. But |
| 591 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 592 | * and for this use case it makes sense to have a 0-length "secret". |
| 593 | * Since the key API doesn't allow importing a key of length 0, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 594 | * keep master_key=0, which setup_psa_key_derivation() understands |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 595 | * to mean a 0-length "secret" input. */ |
| 596 | if( slen != 0 ) |
| 597 | { |
| 598 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 599 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 600 | psa_set_key_algorithm( &key_attributes, alg ); |
| 601 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 602 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 603 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 604 | if( status != PSA_SUCCESS ) |
| 605 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 606 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 607 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 608 | status = setup_psa_key_derivation( &derivation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 609 | master_key, alg, |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 610 | random, rlen, |
| 611 | (unsigned char const *) label, |
| 612 | (size_t) strlen( label ), |
| 613 | dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 614 | if( status != PSA_SUCCESS ) |
| 615 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 616 | psa_key_derivation_abort( &derivation ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 617 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 618 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 619 | } |
| 620 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 621 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 622 | if( status != PSA_SUCCESS ) |
| 623 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 624 | psa_key_derivation_abort( &derivation ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 625 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 626 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 627 | } |
| 628 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 629 | status = psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 630 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 631 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 632 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 633 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 634 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 635 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 636 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 637 | status = psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 638 | if( status != PSA_SUCCESS ) |
| 639 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 640 | |
Andrzej Kurek | 3317126 | 2019-01-15 03:25:18 -0500 | [diff] [blame] | 641 | return( 0 ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 645 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 646 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 647 | const unsigned char *secret, size_t slen, |
| 648 | const char *label, |
| 649 | const unsigned char *random, size_t rlen, |
| 650 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 651 | { |
| 652 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 653 | size_t i, j, k, md_len; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 654 | unsigned char *tmp; |
| 655 | size_t tmp_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 657 | const mbedtls_md_info_t *md_info; |
| 658 | mbedtls_md_context_t md_ctx; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 659 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 662 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 664 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 665 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 666 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 667 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 668 | tmp_len = md_len + strlen( label ) + rlen; |
| 669 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 670 | if( tmp == NULL ) |
| 671 | { |
| 672 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 673 | goto exit; |
| 674 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 675 | |
| 676 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 677 | memcpy( tmp + md_len, label, nb ); |
| 678 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 679 | nb += rlen; |
| 680 | |
| 681 | /* |
| 682 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 683 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 684 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 685 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 687 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 688 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 689 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 690 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 691 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 692 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 693 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 694 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 695 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 697 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 698 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 699 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 700 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 701 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 702 | |
| 703 | for( j = 0; j < k; j++ ) |
| 704 | dstbuf[i + j] = h_i[j]; |
| 705 | } |
| 706 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 707 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 709 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 710 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 711 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 712 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 713 | mbedtls_free( tmp ); |
| 714 | |
| 715 | return( ret ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 716 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 717 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 718 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 719 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 720 | const char *label, |
| 721 | const unsigned char *random, size_t rlen, |
| 722 | unsigned char *dstbuf, size_t dlen ) |
| 723 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 725 | label, random, rlen, dstbuf, dlen ) ); |
| 726 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 727 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 728 | |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 729 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 730 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 731 | const char *label, |
| 732 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 733 | unsigned char *dstbuf, size_t dlen ) |
| 734 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 735 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 736 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 737 | } |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 738 | #endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 739 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 740 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 742 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 743 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 744 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 745 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 746 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 748 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 749 | static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 750 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 751 | #endif |
| 752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 753 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 754 | static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 755 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 756 | #endif |
| 757 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 758 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 759 | #if defined(MBEDTLS_SHA256_C) |
| 760 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 761 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 762 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 763 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 764 | |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 765 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 766 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 767 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 768 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 769 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 770 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 771 | |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 772 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 773 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 774 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 775 | { |
| 776 | if( ssl->conf->f_psk != NULL ) |
| 777 | { |
| 778 | /* If we've used a callback to select the PSK, |
| 779 | * the static configuration is irrelevant. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 780 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 781 | return( 1 ); |
| 782 | |
| 783 | return( 0 ); |
| 784 | } |
| 785 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 786 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 787 | return( 1 ); |
| 788 | |
| 789 | return( 0 ); |
| 790 | } |
| 791 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 792 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 793 | |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 794 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 795 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 796 | { |
| 797 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 798 | if( tls_prf == ssl3_prf ) |
| 799 | { |
Ron Eldor | 0810f0b | 2019-05-15 12:32:32 +0300 | [diff] [blame] | 800 | return( MBEDTLS_SSL_TLS_PRF_SSL3 ); |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 801 | } |
| 802 | else |
| 803 | #endif |
| 804 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 805 | if( tls_prf == tls1_prf ) |
| 806 | { |
| 807 | return( MBEDTLS_SSL_TLS_PRF_TLS1 ); |
| 808 | } |
| 809 | else |
| 810 | #endif |
| 811 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 812 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 813 | if( tls_prf == tls_prf_sha384 ) |
| 814 | { |
| 815 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 816 | } |
| 817 | else |
| 818 | #endif |
| 819 | #if defined(MBEDTLS_SHA256_C) |
| 820 | if( tls_prf == tls_prf_sha256 ) |
| 821 | { |
| 822 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 823 | } |
| 824 | else |
| 825 | #endif |
| 826 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 827 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 828 | } |
| 829 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 830 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 831 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 832 | const unsigned char *secret, size_t slen, |
| 833 | const char *label, |
| 834 | const unsigned char *random, size_t rlen, |
| 835 | unsigned char *dstbuf, size_t dlen ) |
| 836 | { |
| 837 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 838 | |
| 839 | switch( prf ) |
| 840 | { |
| 841 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 842 | case MBEDTLS_SSL_TLS_PRF_SSL3: |
| 843 | tls_prf = ssl3_prf; |
| 844 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 845 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 846 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 847 | case MBEDTLS_SSL_TLS_PRF_TLS1: |
| 848 | tls_prf = tls1_prf; |
| 849 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 850 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 851 | |
| 852 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 853 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 854 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 855 | tls_prf = tls_prf_sha384; |
| 856 | break; |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 857 | #endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 858 | #if defined(MBEDTLS_SHA256_C) |
| 859 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 860 | tls_prf = tls_prf_sha256; |
| 861 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 862 | #endif /* MBEDTLS_SHA256_C */ |
| 863 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 864 | default: |
| 865 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 866 | } |
| 867 | |
| 868 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 869 | } |
| 870 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 871 | /* Type for the TLS PRF */ |
| 872 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 873 | const unsigned char *, size_t, |
| 874 | unsigned char *, size_t); |
| 875 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 876 | /* |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 877 | * Populate a transform structure with session keys and all the other |
| 878 | * necessary information. |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 879 | * |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 880 | * Parameters: |
| 881 | * - [in/out]: transform: structure to populate |
| 882 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 883 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 884 | * - [in] ciphersuite |
| 885 | * - [in] master |
| 886 | * - [in] encrypt_then_mac |
| 887 | * - [in] trunc_hmac |
| 888 | * - [in] compression |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 889 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 890 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 891 | * - [in] minor_ver: SSL/TLS minor version |
| 892 | * - [in] endpoint: client or server |
| 893 | * - [in] ssl: optionally used for: |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 894 | * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 895 | * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys |
| 896 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 897 | */ |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 898 | static int ssl_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 899 | int ciphersuite, |
| 900 | const unsigned char master[48], |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 901 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 902 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 903 | int encrypt_then_mac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 904 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 905 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 906 | int trunc_hmac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 907 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 908 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 909 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 910 | int compression, |
| 911 | #endif |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 912 | ssl_tls_prf_t tls_prf, |
| 913 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 914 | int minor_ver, |
| 915 | unsigned endpoint, |
Manuel Pégourié-Gonnard | 7ae6ed4 | 2020-03-13 11:28:19 +0100 | [diff] [blame] | 916 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 917 | const |
| 918 | #endif |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 919 | mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 920 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 921 | int ret = 0; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 922 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 923 | int psa_fallthrough; |
| 924 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 925 | unsigned char keyblk[256]; |
| 926 | unsigned char *key1; |
| 927 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 928 | unsigned char *mac_enc; |
| 929 | unsigned char *mac_dec; |
sander-visser | 3888b03 | 2020-05-06 21:49:46 +0200 | [diff] [blame] | 930 | size_t mac_key_len = 0; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 931 | size_t iv_copy_len; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 932 | unsigned keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 933 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 934 | const mbedtls_cipher_info_t *cipher_info; |
| 935 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 936 | |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 937 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \ |
| 938 | !defined(MBEDTLS_SSL_EXPORT_KEYS) && \ |
| 939 | !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 940 | ssl = NULL; /* make sure we don't use it except for those cases */ |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 941 | (void) ssl; |
| 942 | #endif |
| 943 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 944 | /* |
| 945 | * Some data just needs copying into the structure |
| 946 | */ |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 947 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 948 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 949 | transform->encrypt_then_mac = encrypt_then_mac; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 950 | #endif |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 951 | transform->minor_ver = minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 952 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 953 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 954 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 955 | #endif |
| 956 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 957 | /* |
| 958 | * Get various info structures |
| 959 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 960 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 961 | if( ciphersuite_info == NULL ) |
| 962 | { |
| 963 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 964 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 965 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 966 | } |
| 967 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 968 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 969 | if( cipher_info == NULL ) |
| 970 | { |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 971 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 972 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 973 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 974 | } |
| 975 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 976 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 977 | if( md_info == NULL ) |
| 978 | { |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 979 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 980 | (unsigned) ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 981 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 982 | } |
| 983 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 984 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 985 | /* Copy own and peer's CID if the use of the CID |
| 986 | * extension has been negotiated. */ |
| 987 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 988 | { |
| 989 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 990 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 991 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 992 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 993 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 994 | transform->in_cid_len ); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 995 | |
| 996 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 997 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 998 | ssl->handshake->peer_cid_len ); |
| 999 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 1000 | transform->out_cid_len ); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1001 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1002 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1003 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1004 | /* |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1005 | * Compute key block using the PRF |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1006 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1007 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1008 | if( ret != 0 ) |
| 1009 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1010 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1011 | return( ret ); |
| 1012 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1013 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1014 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1015 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1016 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1017 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1018 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1019 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1020 | /* |
| 1021 | * Determine the appropriate key, IV and MAC length. |
| 1022 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1023 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1024 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1025 | |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1026 | #if defined(MBEDTLS_GCM_C) || \ |
| 1027 | defined(MBEDTLS_CCM_C) || \ |
| 1028 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1029 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1030 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 1031 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1032 | { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1033 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1034 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1035 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1036 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1037 | transform->taglen = |
| 1038 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1039 | |
Hanno Becker | 447558d | 2020-05-28 07:36:33 +0100 | [diff] [blame] | 1040 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 1041 | * with mode and version: |
| 1042 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 1043 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 1044 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 1045 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 1046 | * sequence number). |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1047 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1048 | transform->ivlen = 12; |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 1049 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 1050 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 1051 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1052 | transform->fixed_ivlen = 12; |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 1053 | } |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1054 | else |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 1055 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 1056 | { |
| 1057 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1058 | transform->fixed_ivlen = 12; |
| 1059 | else |
| 1060 | transform->fixed_ivlen = 4; |
| 1061 | } |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1062 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1063 | /* Minimum length of encrypted record */ |
| 1064 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1065 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1066 | } |
| 1067 | else |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1068 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 1069 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1070 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1071 | cipher_info->mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1072 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1073 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1074 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 1075 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1076 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1077 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1078 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1079 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1080 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1081 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1082 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 1083 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1084 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1085 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1086 | /* |
| 1087 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 1088 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 1089 | * so we only need to adjust the length here. |
| 1090 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1091 | if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1092 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1093 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1094 | |
| 1095 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 1096 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 1097 | * HMAC implementation which also truncates the key |
| 1098 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1099 | mac_key_len = transform->maclen; |
| 1100 | #endif |
| 1101 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1102 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1103 | |
| 1104 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1105 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1106 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1107 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1108 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1109 | transform->minlen = transform->maclen; |
| 1110 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1111 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1112 | /* |
| 1113 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1114 | * 1. if EtM is in use: one block plus MAC |
| 1115 | * otherwise: * first multiple of blocklen greater than maclen |
| 1116 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1117 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1118 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1119 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1120 | { |
| 1121 | transform->minlen = transform->maclen |
| 1122 | + cipher_info->block_size; |
| 1123 | } |
| 1124 | else |
| 1125 | #endif |
| 1126 | { |
| 1127 | transform->minlen = transform->maclen |
| 1128 | + cipher_info->block_size |
| 1129 | - transform->maclen % cipher_info->block_size; |
| 1130 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1131 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1132 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1133 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 1134 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1135 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1136 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1137 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1138 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1139 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 1140 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1141 | { |
| 1142 | transform->minlen += transform->ivlen; |
| 1143 | } |
| 1144 | else |
| 1145 | #endif |
| 1146 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1147 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1148 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1149 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1150 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1151 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1152 | } |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1153 | else |
| 1154 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1155 | { |
| 1156 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1157 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1158 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1159 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1160 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 1161 | (unsigned) keylen, |
| 1162 | (unsigned) transform->minlen, |
| 1163 | (unsigned) transform->ivlen, |
| 1164 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1165 | |
| 1166 | /* |
| 1167 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 1168 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1169 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1170 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1171 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1172 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1173 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1174 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1175 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1176 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1177 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1178 | /* |
| 1179 | * This is not used in TLS v1.1. |
| 1180 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1181 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1182 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1183 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 1184 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1185 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1186 | } |
| 1187 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1188 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1189 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1190 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1191 | { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1192 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1193 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1194 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1195 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1196 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1197 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1198 | /* |
| 1199 | * This is not used in TLS v1.1. |
| 1200 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1201 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1202 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1203 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 1204 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1205 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1206 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1207 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1208 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1209 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1210 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1211 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1212 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1213 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1214 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1215 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1216 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1217 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1218 | { |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1219 | if( mac_key_len > sizeof( transform->mac_enc ) ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1220 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1221 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1222 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1223 | goto end; |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1224 | } |
| 1225 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1226 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1227 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1228 | } |
| 1229 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1230 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1231 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1232 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1233 | if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1234 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1235 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1236 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1237 | if( mac_key_len != 0 ) |
| 1238 | { |
| 1239 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1240 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1241 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1242 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1243 | else |
| 1244 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1247 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1248 | goto end; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1249 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1250 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1252 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1253 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1254 | { |
sander-visser | 1abe8ee | 2020-05-06 21:27:14 +0200 | [diff] [blame] | 1255 | ret = 0; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1257 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1258 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1259 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1260 | transform->iv_enc, transform->iv_dec, |
| 1261 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1262 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1263 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1264 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1265 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1266 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1267 | goto end; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1268 | } |
| 1269 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1270 | #else |
| 1271 | ((void) mac_dec); |
| 1272 | ((void) mac_enc); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1273 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1274 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1275 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1276 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1277 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1278 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1279 | master, keyblk, |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1280 | mac_key_len, keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1281 | iv_copy_len ); |
| 1282 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1283 | |
| 1284 | if( ssl->conf->f_export_keys_ext != NULL ) |
| 1285 | { |
| 1286 | ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1287 | master, keyblk, |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1288 | mac_key_len, keylen, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1289 | iv_copy_len, |
Jaeden Amero | fa63645 | 2019-09-12 10:47:37 +0100 | [diff] [blame] | 1290 | randbytes + 32, |
| 1291 | randbytes, |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1292 | tls_prf_get_type( tls_prf ) ); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1293 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1294 | #endif |
| 1295 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1296 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1297 | |
| 1298 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1299 | * That's relevant at least for TLS-1.0, where |
| 1300 | * we assume that mbedtls_cipher_crypt() updates |
| 1301 | * the structure field for the IV, which the PSA-based |
| 1302 | * implementation currently doesn't. */ |
| 1303 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1304 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1305 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1306 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1307 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1308 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1309 | { |
| 1310 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1311 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | if( ret == 0 ) |
| 1315 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1316 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1317 | psa_fallthrough = 0; |
| 1318 | } |
| 1319 | else |
| 1320 | { |
| 1321 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) ); |
| 1322 | psa_fallthrough = 1; |
| 1323 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1324 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1325 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1326 | psa_fallthrough = 1; |
| 1327 | #else |
| 1328 | psa_fallthrough = 1; |
| 1329 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1330 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1331 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1332 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1333 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1334 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1335 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1336 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1337 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1338 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1339 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1340 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1341 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1342 | * That's relevant at least for TLS-1.0, where |
| 1343 | * we assume that mbedtls_cipher_crypt() updates |
| 1344 | * the structure field for the IV, which the PSA-based |
| 1345 | * implementation currently doesn't. */ |
| 1346 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1347 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1348 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1349 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1350 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1351 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1352 | { |
| 1353 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1354 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | if( ret == 0 ) |
| 1358 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1359 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1360 | psa_fallthrough = 0; |
| 1361 | } |
| 1362 | else |
| 1363 | { |
| 1364 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) ); |
| 1365 | psa_fallthrough = 1; |
| 1366 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1367 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1368 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1369 | psa_fallthrough = 1; |
| 1370 | #else |
| 1371 | psa_fallthrough = 1; |
| 1372 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1373 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1374 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1375 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1376 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1377 | cipher_info ) ) != 0 ) |
| 1378 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1379 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1380 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1381 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1382 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1383 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1384 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1385 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1386 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1387 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1388 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1389 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1390 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1391 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1392 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1393 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1394 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1395 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1396 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1397 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1398 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1399 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1400 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1401 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1402 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1403 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1404 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1405 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1406 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1407 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1409 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1410 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1411 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1412 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1413 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1414 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1415 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1416 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1417 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1418 | |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1419 | /* Initialize Zlib contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1420 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1421 | if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1422 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1423 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1424 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1425 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1426 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1427 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1428 | if( deflateInit( &transform->ctx_deflate, |
| 1429 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1430 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1431 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1432 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1433 | ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED; |
| 1434 | goto end; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1435 | } |
| 1436 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1437 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1438 | |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1439 | end: |
Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 1440 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1441 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1444 | /* |
Manuel Pégourié-Gonnard | 47e33e1 | 2019-05-20 10:10:17 +0200 | [diff] [blame] | 1445 | * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1446 | * |
| 1447 | * Inputs: |
| 1448 | * - SSL/TLS minor version |
| 1449 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1450 | * |
Manuel Pégourié-Gonnard | 31d3ef1 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1451 | * Outputs: |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1452 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1453 | */ |
| 1454 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1455 | int minor_ver, |
| 1456 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1457 | { |
Gilles Peskine | fc9c07f | 2021-05-12 22:51:53 +0200 | [diff] [blame] | 1458 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || \ |
| 1459 | !( defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) ) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1460 | (void) hash; |
| 1461 | #endif |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1462 | |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1463 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1464 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1465 | { |
| 1466 | handshake->tls_prf = ssl3_prf; |
| 1467 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 1468 | handshake->calc_finished = ssl_calc_finished_ssl; |
| 1469 | } |
| 1470 | else |
| 1471 | #endif |
| 1472 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1473 | if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1474 | { |
| 1475 | handshake->tls_prf = tls1_prf; |
| 1476 | handshake->calc_verify = ssl_calc_verify_tls; |
| 1477 | handshake->calc_finished = ssl_calc_finished_tls; |
| 1478 | } |
| 1479 | else |
| 1480 | #endif |
| 1481 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 1482 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1483 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1484 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1485 | { |
| 1486 | handshake->tls_prf = tls_prf_sha384; |
| 1487 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1488 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1489 | } |
| 1490 | else |
| 1491 | #endif |
| 1492 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1493 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1494 | { |
| 1495 | handshake->tls_prf = tls_prf_sha256; |
| 1496 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1497 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1498 | } |
| 1499 | else |
| 1500 | #endif |
| 1501 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1502 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1503 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1504 | } |
| 1505 | |
| 1506 | return( 0 ); |
| 1507 | } |
| 1508 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1509 | /* |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1510 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1511 | * |
| 1512 | * Parameters: |
| 1513 | * [in/out] handshake |
| 1514 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1515 | * (PSA-PSK) ciphersuite_info, psk_opaque |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1516 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1517 | * [out] master |
| 1518 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 1519 | * debug: conf->f_dbg, conf->p_dbg |
| 1520 | * EMS: passed to calc_verify (debug + (SSL3) session_negotiate) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1521 | * PSA-PSA: minor_ver, conf |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1522 | */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1523 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1524 | unsigned char *master, |
Manuel Pégourié-Gonnard | 0d56aaa | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1525 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1526 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1527 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1528 | |
| 1529 | /* cf. RFC 5246, Section 8.1: |
| 1530 | * "The master secret is always exactly 48 bytes in length." */ |
| 1531 | size_t const master_secret_len = 48; |
| 1532 | |
| 1533 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1534 | unsigned char session_hash[48]; |
| 1535 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 1536 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1537 | /* The label for the KDF used for key expansion. |
| 1538 | * This is either "master secret" or "extended master secret" |
| 1539 | * depending on whether the Extended Master Secret extension |
| 1540 | * is used. */ |
| 1541 | char const *lbl = "master secret"; |
| 1542 | |
| 1543 | /* The salt for the KDF used for key expansion. |
| 1544 | * - If the Extended Master Secret extension is not used, |
| 1545 | * this is ClientHello.Random + ServerHello.Random |
| 1546 | * (see Sect. 8.1 in RFC 5246). |
| 1547 | * - If the Extended Master Secret extension is used, |
| 1548 | * this is the transcript of the handshake so far. |
| 1549 | * (see Sect. 4 in RFC 7627). */ |
| 1550 | unsigned char const *salt = handshake->randbytes; |
| 1551 | size_t salt_len = 64; |
| 1552 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1553 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 1554 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 1555 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1556 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1557 | ssl = NULL; /* make sure we don't use it except for those cases */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1558 | (void) ssl; |
| 1559 | #endif |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1560 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1561 | if( handshake->resume != 0 ) |
| 1562 | { |
| 1563 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1564 | return( 0 ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1565 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1566 | |
| 1567 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1568 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1569 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1570 | lbl = "extended master secret"; |
| 1571 | salt = session_hash; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1572 | handshake->calc_verify( ssl, session_hash, &salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1573 | |
Manuel Pégourié-Gonnard | 8faa70e | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1574 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1575 | session_hash, salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1576 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1577 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 1578 | |
| 1579 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1580 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1581 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1582 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1583 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 1584 | { |
| 1585 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1586 | psa_status_t status; |
| 1587 | psa_algorithm_t alg; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1588 | psa_key_id_t psk; |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1589 | psa_key_derivation_operation_t derivation = |
| 1590 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1591 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1592 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1593 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1594 | |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1595 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1596 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1597 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1598 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1599 | else |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1600 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1601 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 1602 | status = setup_psa_key_derivation( &derivation, psk, alg, |
| 1603 | salt, salt_len, |
| 1604 | (unsigned char const *) lbl, |
| 1605 | (size_t) strlen( lbl ), |
| 1606 | master_secret_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1607 | if( status != PSA_SUCCESS ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1608 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1609 | psa_key_derivation_abort( &derivation ); |
| 1610 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1611 | } |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1612 | |
| 1613 | status = psa_key_derivation_output_bytes( &derivation, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1614 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1615 | master_secret_len ); |
| 1616 | if( status != PSA_SUCCESS ) |
| 1617 | { |
| 1618 | psa_key_derivation_abort( &derivation ); |
| 1619 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1620 | } |
| 1621 | |
| 1622 | status = psa_key_derivation_abort( &derivation ); |
| 1623 | if( status != PSA_SUCCESS ) |
| 1624 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1625 | } |
| 1626 | else |
| 1627 | #endif |
| 1628 | { |
| 1629 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1630 | lbl, salt, salt_len, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1631 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1632 | master_secret_len ); |
| 1633 | if( ret != 0 ) |
| 1634 | { |
| 1635 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1636 | return( ret ); |
| 1637 | } |
| 1638 | |
| 1639 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 1640 | handshake->premaster, |
| 1641 | handshake->pmslen ); |
| 1642 | |
| 1643 | mbedtls_platform_zeroize( handshake->premaster, |
| 1644 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | return( 0 ); |
| 1648 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1649 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1650 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1651 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1652 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1653 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1654 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1655 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1656 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1657 | |
| 1658 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1659 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1660 | ssl->minor_ver, |
| 1661 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1662 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1663 | { |
| 1664 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1665 | return( ret ); |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1666 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1667 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1668 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1669 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1670 | ssl->session_negotiate->master, |
| 1671 | ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1672 | if( ret != 0 ) |
| 1673 | { |
| 1674 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1675 | return( ret ); |
| 1676 | } |
| 1677 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1678 | /* Swap the client and server random values: |
| 1679 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1680 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1681 | { |
| 1682 | unsigned char tmp[64]; |
| 1683 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1684 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1685 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1686 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1687 | } |
| 1688 | |
| 1689 | /* Populate transform structure */ |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1690 | ret = ssl_populate_transform( ssl->transform_negotiate, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1691 | ssl->session_negotiate->ciphersuite, |
| 1692 | ssl->session_negotiate->master, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1693 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1694 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1695 | ssl->session_negotiate->encrypt_then_mac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1696 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1697 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1698 | ssl->session_negotiate->trunc_hmac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1699 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 1700 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1701 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1702 | ssl->session_negotiate->compression, |
| 1703 | #endif |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1704 | ssl->handshake->tls_prf, |
| 1705 | ssl->handshake->randbytes, |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1706 | ssl->minor_ver, |
| 1707 | ssl->conf->endpoint, |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1708 | ssl ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1709 | if( ret != 0 ) |
| 1710 | { |
| 1711 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); |
| 1712 | return( ret ); |
| 1713 | } |
| 1714 | |
| 1715 | /* We no longer need Server/ClientHello.random values */ |
| 1716 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1717 | sizeof( ssl->handshake->randbytes ) ); |
| 1718 | |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1719 | /* Allocate compression buffer */ |
| 1720 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
jiblime | 9f25b8d | 2019-12-18 21:40:01 -0800 | [diff] [blame] | 1721 | if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE && |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1722 | ssl->compress_buf == NULL ) |
| 1723 | { |
| 1724 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
| 1725 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
| 1726 | if( ssl->compress_buf == NULL ) |
| 1727 | { |
| 1728 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1729 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1730 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1731 | } |
| 1732 | } |
| 1733 | #endif |
| 1734 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1735 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1736 | |
| 1737 | return( 0 ); |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1740 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1741 | void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1742 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1743 | size_t *hlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1744 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1745 | mbedtls_md5_context md5; |
| 1746 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1747 | unsigned char pad_1[48]; |
| 1748 | unsigned char pad_2[48]; |
| 1749 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1750 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1751 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1752 | mbedtls_md5_init( &md5 ); |
| 1753 | mbedtls_sha1_init( &sha1 ); |
| 1754 | |
| 1755 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1756 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1757 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1758 | memset( pad_1, 0x36, 48 ); |
| 1759 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1760 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1761 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1762 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1763 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1764 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1765 | mbedtls_md5_starts_ret( &md5 ); |
| 1766 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1767 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1768 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1769 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1770 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1771 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1772 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1773 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1774 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1775 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1776 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1777 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1778 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1779 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1780 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1781 | *hlen = 36; |
| 1782 | |
| 1783 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1784 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1785 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1786 | mbedtls_md5_free( &md5 ); |
| 1787 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1788 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1789 | return; |
| 1790 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1791 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1792 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1793 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1794 | void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1795 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1796 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1797 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1798 | mbedtls_md5_context md5; |
| 1799 | mbedtls_sha1_context sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1800 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1801 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1802 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1803 | mbedtls_md5_init( &md5 ); |
| 1804 | mbedtls_sha1_init( &sha1 ); |
| 1805 | |
| 1806 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1807 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1808 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1809 | mbedtls_md5_finish_ret( &md5, hash ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1810 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1811 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1812 | *hlen = 36; |
| 1813 | |
| 1814 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1815 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1816 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1817 | mbedtls_md5_free( &md5 ); |
| 1818 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1819 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1820 | return; |
| 1821 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1822 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1823 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1824 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1825 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1826 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1827 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1828 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1829 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1830 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1831 | size_t hash_size; |
| 1832 | psa_status_t status; |
| 1833 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1834 | |
| 1835 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 1836 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 1837 | if( status != PSA_SUCCESS ) |
| 1838 | { |
| 1839 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1840 | return; |
| 1841 | } |
| 1842 | |
| 1843 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 1844 | if( status != PSA_SUCCESS ) |
| 1845 | { |
| 1846 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1847 | return; |
| 1848 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1849 | |
| 1850 | *hlen = 32; |
| 1851 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1852 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1853 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1854 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1855 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1856 | mbedtls_sha256_init( &sha256 ); |
| 1857 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1858 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1859 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1860 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1861 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1862 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1863 | *hlen = 32; |
| 1864 | |
| 1865 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1866 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1867 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1868 | mbedtls_sha256_free( &sha256 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1869 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1870 | return; |
| 1871 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1872 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1873 | |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 1874 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1875 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1876 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1877 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1878 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1879 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1880 | size_t hash_size; |
| 1881 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1882 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1883 | |
| 1884 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1885 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1886 | if( status != PSA_SUCCESS ) |
| 1887 | { |
| 1888 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1889 | return; |
| 1890 | } |
| 1891 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1892 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1893 | if( status != PSA_SUCCESS ) |
| 1894 | { |
| 1895 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1896 | return; |
| 1897 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1898 | |
| 1899 | *hlen = 48; |
| 1900 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1901 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1902 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1903 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1904 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1905 | mbedtls_sha512_init( &sha512 ); |
| 1906 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1907 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1908 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1909 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1910 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1911 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1912 | *hlen = 48; |
| 1913 | |
| 1914 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1915 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1916 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1917 | mbedtls_sha512_free( &sha512 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1918 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1919 | return; |
| 1920 | } |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 1921 | #endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1922 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1923 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1924 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1925 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1926 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1927 | unsigned char *p = ssl->handshake->premaster; |
| 1928 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Guilhem Bryant | b5f04e4 | 2020-04-01 11:23:58 +0100 | [diff] [blame] | 1929 | const unsigned char *psk = NULL; |
Guilhem Bryant | 61b0fe6 | 2020-03-27 11:12:12 +0000 | [diff] [blame] | 1930 | size_t psk_len = 0; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1931 | |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1932 | if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) |
| 1933 | == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1934 | { |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1935 | /* |
| 1936 | * This should never happen because the existence of a PSK is always |
| 1937 | * checked before calling this function |
| 1938 | */ |
Guilhem Bryant | 82194c8 | 2020-03-26 17:04:31 +0000 | [diff] [blame] | 1939 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Guilhem Bryant | 0c9b195 | 2020-04-08 11:02:38 +0100 | [diff] [blame] | 1940 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1941 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1942 | |
| 1943 | /* |
| 1944 | * PMS = struct { |
| 1945 | * opaque other_secret<0..2^16-1>; |
| 1946 | * opaque psk<0..2^16-1>; |
| 1947 | * }; |
| 1948 | * with "other_secret" depending on the particular key exchange |
| 1949 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1950 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1951 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1952 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1953 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1954 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1955 | |
Joe Subbiani | a651e6f | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1956 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 1957 | p += 2; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1958 | |
| 1959 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1960 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1961 | |
| 1962 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1963 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1964 | } |
| 1965 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1966 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1967 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1968 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1969 | { |
| 1970 | /* |
| 1971 | * other_secret already set by the ClientKeyExchange message, |
| 1972 | * and is 48 bytes long |
| 1973 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1974 | if( end - p < 2 ) |
| 1975 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1976 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1977 | *p++ = 0; |
| 1978 | *p++ = 48; |
| 1979 | p += 48; |
| 1980 | } |
| 1981 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1982 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1983 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1984 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1985 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1986 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1987 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1988 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1989 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1990 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1991 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1992 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1993 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1994 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1995 | return( ret ); |
| 1996 | } |
Joe Subbiani | a651e6f | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1997 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 1998 | p += 2 + len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1999 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2000 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2001 | } |
| 2002 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2003 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 2004 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 2005 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2006 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2007 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2008 | size_t zlen; |
| 2009 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2010 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2011 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2012 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2013 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2014 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2015 | return( ret ); |
| 2016 | } |
| 2017 | |
Joe Subbiani | a651e6f | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 2018 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 2019 | p += 2 + zlen; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2020 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2021 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 2022 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2023 | } |
| 2024 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2025 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2026 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2027 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2028 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2032 | if( end - p < 2 ) |
| 2033 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 2034 | |
Joe Subbiani | a651e6f | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 2035 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 2036 | p += 2; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2037 | |
| 2038 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 2039 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2040 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2041 | memcpy( p, psk, psk_len ); |
| 2042 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2043 | |
| 2044 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 2045 | |
| 2046 | return( 0 ); |
| 2047 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2048 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2049 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2050 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2051 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2053 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 2054 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2055 | { |
| 2056 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 2057 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2058 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2059 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2060 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2061 | unsigned char doublings = 1; |
| 2062 | |
| 2063 | while( ratio != 0 ) |
| 2064 | { |
| 2065 | ++doublings; |
| 2066 | ratio >>= 1; |
| 2067 | } |
| 2068 | |
| 2069 | if( ++ssl->renego_records_seen > doublings ) |
| 2070 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2071 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2072 | return( 0 ); |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | return( ssl_write_hello_request( ssl ) ); |
| 2077 | } |
| 2078 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2079 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2080 | |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 2081 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2082 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 2083 | { |
| 2084 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2085 | if( session->peer_cert != NULL ) |
| 2086 | { |
| 2087 | mbedtls_x509_crt_free( session->peer_cert ); |
| 2088 | mbedtls_free( session->peer_cert ); |
| 2089 | session->peer_cert = NULL; |
| 2090 | } |
| 2091 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2092 | if( session->peer_cert_digest != NULL ) |
| 2093 | { |
| 2094 | /* Zeroization is not necessary. */ |
| 2095 | mbedtls_free( session->peer_cert_digest ); |
| 2096 | session->peer_cert_digest = NULL; |
| 2097 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 2098 | session->peer_cert_digest_len = 0; |
| 2099 | } |
| 2100 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2101 | } |
| 2102 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 2103 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2104 | /* |
| 2105 | * Handshake functions |
| 2106 | */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2107 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 2108 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2109 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2110 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2111 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 2112 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2114 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2115 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 2116 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2117 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2118 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2119 | ssl->state++; |
| 2120 | return( 0 ); |
| 2121 | } |
| 2122 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2123 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2124 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2125 | } |
| 2126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2127 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2128 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2129 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 2130 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2131 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2132 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2133 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 2134 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2135 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2136 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2137 | ssl->state++; |
| 2138 | return( 0 ); |
| 2139 | } |
| 2140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2141 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2142 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2143 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 2144 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2145 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 2146 | /* Some certificate support -> implement write and parse */ |
| 2147 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2148 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2149 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2150 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2151 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2152 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2153 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 2154 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2156 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2157 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 2158 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2159 | { |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 2160 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 2161 | ssl->state++; |
| 2162 | return( 0 ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2163 | } |
| 2164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2165 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2166 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2167 | { |
| 2168 | if( ssl->client_auth == 0 ) |
| 2169 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2170 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2171 | ssl->state++; |
| 2172 | return( 0 ); |
| 2173 | } |
| 2174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2175 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2176 | /* |
| 2177 | * If using SSLv3 and got no cert, send an Alert message |
| 2178 | * (otherwise an empty Certificate message will be sent). |
| 2179 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2180 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
| 2181 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2182 | { |
| 2183 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2184 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 2185 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 2186 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2187 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2188 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2189 | goto write_msg; |
| 2190 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2191 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2192 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2193 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2194 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2195 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2196 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2197 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2198 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 2200 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2201 | } |
| 2202 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2203 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2205 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2206 | |
| 2207 | /* |
| 2208 | * 0 . 0 handshake type |
| 2209 | * 1 . 3 handshake length |
| 2210 | * 4 . 6 length of all certs |
| 2211 | * 7 . 9 length of cert. 1 |
| 2212 | * 10 . n-1 peer certificate |
| 2213 | * n . n+2 length of cert. 2 |
| 2214 | * n+3 . ... upper level cert, etc. |
| 2215 | */ |
| 2216 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2217 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2218 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 2219 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2220 | { |
| 2221 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2222 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2223 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2224 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 2225 | " > %" MBEDTLS_PRINTF_SIZET, |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2226 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2227 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Joe Subbiani | ad1115a | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 2230 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 2231 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 2232 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2233 | |
| 2234 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 2235 | i += n; crt = crt->next; |
| 2236 | } |
| 2237 | |
Joe Subbiani | ad1115a | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 2238 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 2239 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 2240 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2241 | |
| 2242 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2243 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2244 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2245 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2246 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2247 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2248 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2249 | |
| 2250 | ssl->state++; |
| 2251 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2252 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2253 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2254 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2255 | return( ret ); |
| 2256 | } |
| 2257 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2258 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2259 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 2260 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 2263 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 2264 | |
| 2265 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2266 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 2267 | unsigned char *crt_buf, |
| 2268 | size_t crt_buf_len ) |
| 2269 | { |
| 2270 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 2271 | |
| 2272 | if( peer_crt == NULL ) |
| 2273 | return( -1 ); |
| 2274 | |
| 2275 | if( peer_crt->raw.len != crt_buf_len ) |
| 2276 | return( -1 ); |
| 2277 | |
k-stachowiak | 95b68ef | 2019-09-16 12:21:00 +0200 | [diff] [blame] | 2278 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2279 | } |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 2280 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2281 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 2282 | unsigned char *crt_buf, |
| 2283 | size_t crt_buf_len ) |
| 2284 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2285 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 2286 | unsigned char const * const peer_cert_digest = |
| 2287 | ssl->session->peer_cert_digest; |
| 2288 | mbedtls_md_type_t const peer_cert_digest_type = |
| 2289 | ssl->session->peer_cert_digest_type; |
| 2290 | mbedtls_md_info_t const * const digest_info = |
| 2291 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 2292 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 2293 | size_t digest_len; |
| 2294 | |
| 2295 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 2296 | return( -1 ); |
| 2297 | |
| 2298 | digest_len = mbedtls_md_get_size( digest_info ); |
| 2299 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 2300 | return( -1 ); |
| 2301 | |
| 2302 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 2303 | if( ret != 0 ) |
| 2304 | return( -1 ); |
| 2305 | |
| 2306 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 2307 | } |
| 2308 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 2309 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2310 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2311 | /* |
| 2312 | * Once the certificate message is read, parse it into a cert chain and |
| 2313 | * perform basic checks, but leave actual verification to the caller |
| 2314 | */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2315 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 2316 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2317 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2318 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2319 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 2320 | int crt_cnt=0; |
| 2321 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2322 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 2323 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2324 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2325 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2326 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2327 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2328 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2329 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2330 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2333 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 2334 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2335 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2336 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2337 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2338 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2339 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2342 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 2343 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2344 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2345 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2346 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 2347 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2348 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 2349 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2350 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2351 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2352 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2353 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2354 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2355 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2358 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 2359 | i += 3; |
| 2360 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2361 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2362 | while( i < ssl->in_hslen ) |
| 2363 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2364 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 2365 | if ( i + 3 > ssl->in_hslen ) { |
| 2366 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 2367 | mbedtls_ssl_send_alert_message( ssl, |
| 2368 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2369 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 2370 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 2371 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2372 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 2373 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2374 | if( ssl->in_msg[i] != 0 ) |
| 2375 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2376 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 2377 | mbedtls_ssl_send_alert_message( ssl, |
| 2378 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2379 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2380 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2383 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2384 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 2385 | | (unsigned int) ssl->in_msg[i + 2]; |
| 2386 | i += 3; |
| 2387 | |
| 2388 | if( n < 128 || i + n > ssl->in_hslen ) |
| 2389 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2390 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 2391 | mbedtls_ssl_send_alert_message( ssl, |
| 2392 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2393 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2394 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2397 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2398 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 2399 | if( crt_cnt++ == 0 && |
| 2400 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2401 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2402 | { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 2403 | /* During client-side renegotiation, check that the server's |
| 2404 | * end-CRTs hasn't changed compared to the initial handshake, |
| 2405 | * mitigating the triple handshake attack. On success, reuse |
| 2406 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2407 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 2408 | if( ssl_check_peer_crt_unchanged( ssl, |
| 2409 | &ssl->in_msg[i], |
| 2410 | n ) != 0 ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2411 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2412 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 2413 | mbedtls_ssl_send_alert_message( ssl, |
| 2414 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2415 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 2416 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2417 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2418 | |
| 2419 | /* Now we can safely free the original chain. */ |
| 2420 | ssl_clear_peer_cert( ssl->session ); |
| 2421 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2422 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 2423 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2424 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2425 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2426 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2427 | #else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 2428 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2429 | * it in-place from the input buffer instead of making a copy. */ |
| 2430 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 2431 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2432 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2433 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2434 | case 0: /*ok*/ |
| 2435 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 2436 | /* Ignore certificate with an unknown algorithm: maybe a |
| 2437 | prior certificate was already trusted. */ |
| 2438 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2439 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2440 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 2441 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 2442 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2443 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2444 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 2445 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2446 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2447 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2448 | default: |
| 2449 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 2450 | crt_parse_der_failed: |
| 2451 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 2452 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 2453 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | i += n; |
| 2457 | } |
| 2458 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2459 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2460 | return( 0 ); |
| 2461 | } |
| 2462 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2463 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2464 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 2465 | { |
| 2466 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2467 | return( -1 ); |
| 2468 | |
| 2469 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2470 | /* |
| 2471 | * Check if the client sent an empty certificate |
| 2472 | */ |
| 2473 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 2474 | { |
| 2475 | if( ssl->in_msglen == 2 && |
| 2476 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 2477 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 2478 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 2479 | { |
| 2480 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
| 2481 | return( 0 ); |
| 2482 | } |
| 2483 | |
| 2484 | return( -1 ); |
| 2485 | } |
| 2486 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2487 | |
| 2488 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2489 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2490 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 2491 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2492 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 2493 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 2494 | { |
| 2495 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 2496 | return( 0 ); |
| 2497 | } |
| 2498 | |
| 2499 | return( -1 ); |
| 2500 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2501 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2502 | } |
| 2503 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2504 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2505 | /* Check if a certificate message is expected. |
| 2506 | * Return either |
| 2507 | * - SSL_CERTIFICATE_EXPECTED, or |
| 2508 | * - SSL_CERTIFICATE_SKIP |
| 2509 | * indicating whether a Certificate message is expected or not. |
| 2510 | */ |
| 2511 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 2512 | #define SSL_CERTIFICATE_SKIP 1 |
| 2513 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 2514 | int authmode ) |
| 2515 | { |
| 2516 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2517 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2518 | |
| 2519 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 2520 | return( SSL_CERTIFICATE_SKIP ); |
| 2521 | |
| 2522 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2523 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2524 | { |
| 2525 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 2526 | return( SSL_CERTIFICATE_SKIP ); |
| 2527 | |
| 2528 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 2529 | { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2530 | ssl->session_negotiate->verify_result = |
| 2531 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 2532 | return( SSL_CERTIFICATE_SKIP ); |
| 2533 | } |
| 2534 | } |
Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 2535 | #else |
| 2536 | ((void) authmode); |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2537 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2538 | |
| 2539 | return( SSL_CERTIFICATE_EXPECTED ); |
| 2540 | } |
| 2541 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2542 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 2543 | int authmode, |
| 2544 | mbedtls_x509_crt *chain, |
| 2545 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2546 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2547 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2548 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2549 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2550 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2551 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2552 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 2553 | void *p_vrfy; |
| 2554 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2555 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 2556 | return( 0 ); |
| 2557 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2558 | if( ssl->f_vrfy != NULL ) |
| 2559 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2560 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2561 | f_vrfy = ssl->f_vrfy; |
| 2562 | p_vrfy = ssl->p_vrfy; |
| 2563 | } |
| 2564 | else |
| 2565 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2566 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2567 | f_vrfy = ssl->conf->f_vrfy; |
| 2568 | p_vrfy = ssl->conf->p_vrfy; |
| 2569 | } |
| 2570 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2571 | /* |
| 2572 | * Main check: verify certificate |
| 2573 | */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2574 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 2575 | if( ssl->conf->f_ca_cb != NULL ) |
| 2576 | { |
| 2577 | ((void) rs_ctx); |
| 2578 | have_ca_chain = 1; |
| 2579 | |
| 2580 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
Jarno Lamsa | 9822c0d | 2019-04-01 16:59:48 +0300 | [diff] [blame] | 2581 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2582 | chain, |
| 2583 | ssl->conf->f_ca_cb, |
| 2584 | ssl->conf->p_ca_cb, |
| 2585 | ssl->conf->cert_profile, |
| 2586 | ssl->hostname, |
| 2587 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2588 | f_vrfy, p_vrfy ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2589 | } |
| 2590 | else |
| 2591 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 2592 | { |
| 2593 | mbedtls_x509_crt *ca_chain; |
| 2594 | mbedtls_x509_crl *ca_crl; |
| 2595 | |
| 2596 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2597 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 2598 | { |
| 2599 | ca_chain = ssl->handshake->sni_ca_chain; |
| 2600 | ca_crl = ssl->handshake->sni_ca_crl; |
| 2601 | } |
| 2602 | else |
| 2603 | #endif |
| 2604 | { |
| 2605 | ca_chain = ssl->conf->ca_chain; |
| 2606 | ca_crl = ssl->conf->ca_crl; |
| 2607 | } |
| 2608 | |
| 2609 | if( ca_chain != NULL ) |
| 2610 | have_ca_chain = 1; |
| 2611 | |
| 2612 | ret = mbedtls_x509_crt_verify_restartable( |
| 2613 | chain, |
| 2614 | ca_chain, ca_crl, |
| 2615 | ssl->conf->cert_profile, |
| 2616 | ssl->hostname, |
| 2617 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2618 | f_vrfy, p_vrfy, rs_ctx ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2619 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2620 | |
| 2621 | if( ret != 0 ) |
| 2622 | { |
| 2623 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 2624 | } |
| 2625 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2626 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2627 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2628 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 2629 | #endif |
| 2630 | |
| 2631 | /* |
| 2632 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 2633 | */ |
| 2634 | |
| 2635 | #if defined(MBEDTLS_ECP_C) |
| 2636 | { |
| 2637 | const mbedtls_pk_context *pk = &chain->pk; |
| 2638 | |
| 2639 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 2640 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 2641 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 2642 | { |
| 2643 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 2644 | |
| 2645 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 2646 | if( ret == 0 ) |
| 2647 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 2648 | } |
| 2649 | } |
| 2650 | #endif /* MBEDTLS_ECP_C */ |
| 2651 | |
| 2652 | if( mbedtls_ssl_check_cert_usage( chain, |
| 2653 | ciphersuite_info, |
| 2654 | ! ssl->conf->endpoint, |
| 2655 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 2656 | { |
| 2657 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 2658 | if( ret == 0 ) |
| 2659 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 2660 | } |
| 2661 | |
| 2662 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 2663 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 2664 | * with details encoded in the verification flags. All other kinds |
| 2665 | * of error codes, including those from the user provided f_vrfy |
| 2666 | * functions, are treated as fatal and lead to a failure of |
| 2667 | * ssl_parse_certificate even if verification was optional. */ |
| 2668 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 2669 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 2670 | ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
| 2671 | { |
| 2672 | ret = 0; |
| 2673 | } |
| 2674 | |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2675 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2676 | { |
| 2677 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 2678 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 2679 | } |
| 2680 | |
| 2681 | if( ret != 0 ) |
| 2682 | { |
| 2683 | uint8_t alert; |
| 2684 | |
| 2685 | /* The certificate may have been rejected for several reasons. |
| 2686 | Pick one and send the corresponding alert. Which alert to send |
| 2687 | may be a subject of debate in some cases. */ |
| 2688 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 2689 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 2690 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 2691 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 2692 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 2693 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2694 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 2695 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2696 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 2697 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2698 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 2699 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2700 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 2701 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2702 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 2703 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 2704 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 2705 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 2706 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 2707 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 2708 | else |
| 2709 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 2710 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2711 | alert ); |
| 2712 | } |
| 2713 | |
| 2714 | #if defined(MBEDTLS_DEBUG_C) |
| 2715 | if( ssl->session_negotiate->verify_result != 0 ) |
| 2716 | { |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2717 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2718 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2719 | } |
| 2720 | else |
| 2721 | { |
| 2722 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 2723 | } |
| 2724 | #endif /* MBEDTLS_DEBUG_C */ |
| 2725 | |
| 2726 | return( ret ); |
| 2727 | } |
| 2728 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2729 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2730 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 2731 | unsigned char *start, size_t len ) |
| 2732 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2733 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2734 | /* Remember digest of the peer's end-CRT. */ |
| 2735 | ssl->session_negotiate->peer_cert_digest = |
| 2736 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 2737 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 2738 | { |
| 2739 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
irwir | 40883e9 | 2019-09-21 17:55:33 +0300 | [diff] [blame] | 2740 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2741 | mbedtls_ssl_send_alert_message( ssl, |
| 2742 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2743 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 2744 | |
| 2745 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2746 | } |
| 2747 | |
| 2748 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 2749 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 2750 | start, len, |
| 2751 | ssl->session_negotiate->peer_cert_digest ); |
| 2752 | |
| 2753 | ssl->session_negotiate->peer_cert_digest_type = |
| 2754 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 2755 | ssl->session_negotiate->peer_cert_digest_len = |
| 2756 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 2757 | |
| 2758 | return( ret ); |
| 2759 | } |
| 2760 | |
| 2761 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 2762 | unsigned char *start, size_t len ) |
| 2763 | { |
| 2764 | unsigned char *end = start + len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2765 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2766 | |
| 2767 | /* Make a copy of the peer's raw public key. */ |
| 2768 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 2769 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 2770 | &ssl->handshake->peer_pubkey ); |
| 2771 | if( ret != 0 ) |
| 2772 | { |
| 2773 | /* We should have parsed the public key before. */ |
| 2774 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2775 | } |
| 2776 | |
| 2777 | return( 0 ); |
| 2778 | } |
| 2779 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2780 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2781 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 2782 | { |
| 2783 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2784 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2785 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2786 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 2787 | ? ssl->handshake->sni_authmode |
| 2788 | : ssl->conf->authmode; |
| 2789 | #else |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 2790 | const int authmode = ssl->conf->authmode; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2791 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2792 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2793 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2794 | |
| 2795 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 2796 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2797 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 2798 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2799 | { |
| 2800 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2801 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2802 | } |
| 2803 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2804 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2805 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2806 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2807 | { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2808 | chain = ssl->handshake->ecrs_peer_cert; |
| 2809 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2810 | goto crt_verify; |
| 2811 | } |
| 2812 | #endif |
| 2813 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 2814 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2815 | { |
| 2816 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 2817 | let it decide whether to alert. */ |
| 2818 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2819 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2820 | } |
| 2821 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2822 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2823 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 2824 | { |
| 2825 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2826 | |
irwir | d3085ab | 2020-04-21 22:26:05 +0300 | [diff] [blame] | 2827 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2828 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2829 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2830 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2831 | } |
| 2832 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2833 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2834 | /* Clear existing peer CRT structure in case we tried to |
| 2835 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2836 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2837 | |
| 2838 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 2839 | if( chain == NULL ) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2840 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2841 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2842 | sizeof( mbedtls_x509_crt ) ) ); |
| 2843 | mbedtls_ssl_send_alert_message( ssl, |
| 2844 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2845 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2846 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2847 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2848 | goto exit; |
| 2849 | } |
| 2850 | mbedtls_x509_crt_init( chain ); |
| 2851 | |
| 2852 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2853 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2854 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2855 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2856 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2857 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2858 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2859 | |
| 2860 | crt_verify: |
| 2861 | if( ssl->handshake->ecrs_enabled) |
| 2862 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 2863 | #endif |
| 2864 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2865 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2866 | chain, rs_ctx ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2867 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2868 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2869 | |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2870 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2871 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2872 | unsigned char *crt_start, *pk_start; |
| 2873 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2874 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2875 | /* We parse the CRT chain without copying, so |
| 2876 | * these pointers point into the input buffer, |
| 2877 | * and are hence still valid after freeing the |
| 2878 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2879 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2880 | crt_start = chain->raw.p; |
| 2881 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2882 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2883 | pk_start = chain->pk_raw.p; |
| 2884 | pk_len = chain->pk_raw.len; |
| 2885 | |
| 2886 | /* Free the CRT structures before computing |
| 2887 | * digest and copying the peer's public key. */ |
| 2888 | mbedtls_x509_crt_free( chain ); |
| 2889 | mbedtls_free( chain ); |
| 2890 | chain = NULL; |
| 2891 | |
| 2892 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2893 | if( ret != 0 ) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2894 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2895 | |
| 2896 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 2897 | if( ret != 0 ) |
| 2898 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2899 | } |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2900 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2901 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2902 | ssl->session_negotiate->peer_cert = chain; |
| 2903 | chain = NULL; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2904 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2905 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2906 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2907 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2908 | exit: |
| 2909 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2910 | if( ret == 0 ) |
| 2911 | ssl->state++; |
| 2912 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2913 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2914 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 2915 | { |
| 2916 | ssl->handshake->ecrs_peer_cert = chain; |
| 2917 | chain = NULL; |
| 2918 | } |
| 2919 | #endif |
| 2920 | |
| 2921 | if( chain != NULL ) |
| 2922 | { |
| 2923 | mbedtls_x509_crt_free( chain ); |
| 2924 | mbedtls_free( chain ); |
| 2925 | } |
| 2926 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2927 | return( ret ); |
| 2928 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2929 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2930 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2931 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 2932 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2933 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 2934 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2935 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2936 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2937 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 2938 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2939 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2940 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2941 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2942 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 2943 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2944 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2945 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 2946 | else |
| 2947 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2948 | #if defined(MBEDTLS_SHA256_C) |
| 2949 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2950 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2951 | else |
| 2952 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2953 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2954 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2955 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2956 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2957 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2958 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2959 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2960 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2961 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2962 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2963 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2964 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 2965 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2966 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2967 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2968 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2969 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 2970 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2971 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 2972 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2973 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2974 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2975 | #endif |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 2976 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2977 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 2978 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2979 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2980 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2981 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2982 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2983 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2984 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2985 | } |
| 2986 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2987 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2988 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2989 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2990 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2991 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2992 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 2993 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2994 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2995 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2996 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2997 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2998 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 2999 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3000 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3001 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3002 | #endif |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 3003 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3004 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3005 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3006 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3007 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 3008 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3009 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3010 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3013 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3014 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3015 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3016 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3017 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3018 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 3019 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3020 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3021 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3022 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3023 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3024 | #if defined(MBEDTLS_SHA256_C) |
| 3025 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3026 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3027 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3028 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3029 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 3030 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3031 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3032 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3033 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3034 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3035 | |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 3036 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3037 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3038 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3039 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3040 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3041 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3042 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3043 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3044 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3045 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 3046 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3047 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3048 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3049 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3050 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3051 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3052 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3053 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3054 | mbedtls_md5_context md5; |
| 3055 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3056 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3057 | unsigned char padbuf[48]; |
| 3058 | unsigned char md5sum[16]; |
| 3059 | unsigned char sha1sum[20]; |
| 3060 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3061 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3062 | if( !session ) |
| 3063 | session = ssl->session; |
| 3064 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3065 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3066 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3067 | mbedtls_md5_init( &md5 ); |
| 3068 | mbedtls_sha1_init( &sha1 ); |
| 3069 | |
| 3070 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 3071 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3072 | |
| 3073 | /* |
| 3074 | * SSLv3: |
| 3075 | * hash = |
| 3076 | * MD5( master + pad2 + |
| 3077 | * MD5( handshake + sender + master + pad1 ) ) |
| 3078 | * + SHA1( master + pad2 + |
| 3079 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3080 | */ |
| 3081 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3082 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3083 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 3084 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3085 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3086 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3087 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3088 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 3089 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3090 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3091 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3092 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3093 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3094 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3095 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3096 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3097 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 3098 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 3099 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 3100 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3101 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3102 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 3103 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 3104 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 3105 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3106 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3107 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3108 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3109 | mbedtls_md5_starts_ret( &md5 ); |
| 3110 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 3111 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 3112 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 3113 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3114 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3115 | mbedtls_sha1_starts_ret( &sha1 ); |
| 3116 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 3117 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 3118 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 3119 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3120 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3121 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3122 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3123 | mbedtls_md5_free( &md5 ); |
| 3124 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3125 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3126 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 3127 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 3128 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3130 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3131 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3132 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3133 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3134 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3135 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3136 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3137 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3138 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3139 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3140 | mbedtls_md5_context md5; |
| 3141 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3142 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3144 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3145 | if( !session ) |
| 3146 | session = ssl->session; |
| 3147 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3148 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3149 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3150 | mbedtls_md5_init( &md5 ); |
| 3151 | mbedtls_sha1_init( &sha1 ); |
| 3152 | |
| 3153 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 3154 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3155 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3156 | /* |
| 3157 | * TLSv1: |
| 3158 | * hash = PRF( master, finished_label, |
| 3159 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 3160 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3162 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3163 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 3164 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3165 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3166 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3167 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3168 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 3169 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3170 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3172 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3173 | ? "client finished" |
| 3174 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3175 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3176 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 3177 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3178 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3179 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3180 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3182 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3183 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3184 | mbedtls_md5_free( &md5 ); |
| 3185 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3186 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3187 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3189 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3190 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3191 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3192 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3193 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3194 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3195 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3196 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3197 | { |
| 3198 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3199 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3200 | unsigned char padbuf[32]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3201 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3202 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 3203 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3204 | psa_status_t status; |
| 3205 | #else |
| 3206 | mbedtls_sha256_context sha256; |
| 3207 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3208 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3209 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3210 | if( !session ) |
| 3211 | session = ssl->session; |
| 3212 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3213 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 3214 | ? "client finished" |
| 3215 | : "server finished"; |
| 3216 | |
| 3217 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3218 | sha256_psa = psa_hash_operation_init(); |
| 3219 | |
| 3220 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 3221 | |
| 3222 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 3223 | if( status != PSA_SUCCESS ) |
| 3224 | { |
| 3225 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 3226 | return; |
| 3227 | } |
| 3228 | |
| 3229 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 3230 | if( status != PSA_SUCCESS ) |
| 3231 | { |
| 3232 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 3233 | return; |
| 3234 | } |
| 3235 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 3236 | #else |
| 3237 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3238 | mbedtls_sha256_init( &sha256 ); |
| 3239 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3240 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3241 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3242 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3243 | |
| 3244 | /* |
| 3245 | * TLSv1.2: |
| 3246 | * hash = PRF( master, finished_label, |
| 3247 | * Hash( handshake ) )[0.11] |
| 3248 | */ |
| 3249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3250 | #if !defined(MBEDTLS_SHA256_ALT) |
| 3251 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3252 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3253 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3254 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3255 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3256 | mbedtls_sha256_free( &sha256 ); |
| 3257 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3258 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3259 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3260 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3261 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3262 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3263 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3264 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3265 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3266 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3267 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3268 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3269 | |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 3270 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 3271 | |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3272 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3273 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3274 | { |
| 3275 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3276 | const char *sender; |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 3277 | unsigned char padbuf[48]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3278 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3279 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 3280 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3281 | psa_status_t status; |
| 3282 | #else |
| 3283 | mbedtls_sha512_context sha512; |
| 3284 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3285 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3286 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3287 | if( !session ) |
| 3288 | session = ssl->session; |
| 3289 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3290 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 3291 | ? "client finished" |
| 3292 | : "server finished"; |
| 3293 | |
| 3294 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3295 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3296 | |
| 3297 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 3298 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3299 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3300 | if( status != PSA_SUCCESS ) |
| 3301 | { |
| 3302 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 3303 | return; |
| 3304 | } |
| 3305 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3306 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3307 | if( status != PSA_SUCCESS ) |
| 3308 | { |
| 3309 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 3310 | return; |
| 3311 | } |
| 3312 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 3313 | #else |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3314 | mbedtls_sha512_init( &sha512 ); |
| 3315 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3316 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3317 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3318 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3319 | |
| 3320 | /* |
| 3321 | * TLSv1.2: |
| 3322 | * hash = PRF( master, finished_label, |
| 3323 | * Hash( handshake ) )[0.11] |
| 3324 | */ |
| 3325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3326 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3327 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 3328 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3329 | #endif |
Gilles Peskine | bb66dac | 2021-05-13 00:00:45 +0200 | [diff] [blame] | 3330 | /* mbedtls_sha512_finish_ret's output parameter is declared as a |
| 3331 | * 64-byte buffer, but sice we're using SHA-384, we know that the |
| 3332 | * output fits in 48 bytes. This is correct C, but GCC 11.1 warns |
| 3333 | * about it. |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 3334 | */ |
Gilles Peskine | bb66dac | 2021-05-13 00:00:45 +0200 | [diff] [blame] | 3335 | #if defined(__GNUC__) && __GNUC__ >= 11 |
| 3336 | #pragma GCC diagnostic push |
| 3337 | #pragma GCC diagnostic ignored "-Wstringop-overflow" |
| 3338 | #endif |
| 3339 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
| 3340 | #if defined(__GNUC__) && __GNUC__ >= 11 |
| 3341 | #pragma GCC diagnostic pop |
| 3342 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3343 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3344 | mbedtls_sha512_free( &sha512 ); |
| 3345 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3346 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3347 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3348 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3349 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3350 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3351 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3352 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3354 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3355 | } |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 3356 | #endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3357 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3358 | |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 3359 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3360 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3361 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3362 | |
| 3363 | /* |
| 3364 | * Free our handshake params |
| 3365 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3366 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3367 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3368 | ssl->handshake = NULL; |
| 3369 | |
| 3370 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3371 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3372 | */ |
| 3373 | if( ssl->transform ) |
| 3374 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3375 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3376 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3377 | } |
| 3378 | ssl->transform = ssl->transform_negotiate; |
| 3379 | ssl->transform_negotiate = NULL; |
| 3380 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3381 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3382 | } |
| 3383 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3384 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3385 | { |
| 3386 | int resume = ssl->handshake->resume; |
| 3387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3388 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3390 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3391 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3392 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3393 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3394 | ssl->renego_records_seen = 0; |
| 3395 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3396 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3397 | |
| 3398 | /* |
| 3399 | * Free the previous session and switch in the current one |
| 3400 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3401 | if( ssl->session ) |
| 3402 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3403 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 3404 | /* RFC 7366 3.1: keep the EtM state */ |
| 3405 | ssl->session_negotiate->encrypt_then_mac = |
| 3406 | ssl->session->encrypt_then_mac; |
| 3407 | #endif |
| 3408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3409 | mbedtls_ssl_session_free( ssl->session ); |
| 3410 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3411 | } |
| 3412 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3413 | ssl->session_negotiate = NULL; |
| 3414 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3415 | /* |
| 3416 | * Add cache entry |
| 3417 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3418 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 3419 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 3420 | resume == 0 ) |
| 3421 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 3422 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3423 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 3424 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3425 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3426 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3427 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3428 | ssl->handshake->flight != NULL ) |
| 3429 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3430 | /* Cancel handshake timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3431 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3432 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3433 | /* Keep last flight around in case we need to resend it: |
| 3434 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3435 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3436 | } |
| 3437 | else |
| 3438 | #endif |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 3439 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3440 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3441 | ssl->state++; |
| 3442 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3443 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3446 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3447 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3448 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3450 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3451 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3452 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 3453 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3454 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3455 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 3456 | /* |
| 3457 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 3458 | * may define some other value. Currently (early 2016), no defined |
| 3459 | * ciphersuite does this (and this is unlikely to change as activity has |
| 3460 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 3461 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3462 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3464 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3465 | ssl->verify_data_len = hash_len; |
| 3466 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3467 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3468 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3469 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3470 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3471 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3472 | |
| 3473 | /* |
| 3474 | * In case of session resuming, invert the client and server |
| 3475 | * ChangeCipherSpec messages order. |
| 3476 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3477 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3478 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3479 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3480 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3481 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3482 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3483 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3484 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3485 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3486 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3487 | } |
| 3488 | else |
| 3489 | ssl->state++; |
| 3490 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3491 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 3492 | * Switch to our negotiated transform and session parameters for outbound |
| 3493 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3494 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3495 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 3496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3497 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3498 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3499 | { |
| 3500 | unsigned char i; |
| 3501 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3502 | /* Remember current epoch settings for resending */ |
| 3503 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3504 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3505 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3506 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3507 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3508 | |
| 3509 | /* Increment epoch */ |
| 3510 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3511 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3512 | break; |
| 3513 | |
| 3514 | /* The loop goes to its end iff the counter is wrapping */ |
| 3515 | if( i == 0 ) |
| 3516 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3517 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 3518 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3519 | } |
| 3520 | } |
| 3521 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3522 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3523 | memset( ssl->cur_out_ctr, 0, 8 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3524 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3525 | ssl->transform_out = ssl->transform_negotiate; |
| 3526 | ssl->session_out = ssl->session_negotiate; |
| 3527 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3528 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3529 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 3530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3531 | if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 3532 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3533 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3534 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 3535 | } |
| 3536 | } |
| 3537 | #endif |
| 3538 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3539 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3540 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3541 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3542 | #endif |
| 3543 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3544 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3545 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3546 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3547 | return( ret ); |
| 3548 | } |
| 3549 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3550 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3551 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3552 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3553 | { |
| 3554 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 3555 | return( ret ); |
| 3556 | } |
| 3557 | #endif |
| 3558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3559 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3560 | |
| 3561 | return( 0 ); |
| 3562 | } |
| 3563 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3564 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3565 | #define SSL_MAX_HASH_LEN 36 |
| 3566 | #else |
| 3567 | #define SSL_MAX_HASH_LEN 12 |
| 3568 | #endif |
| 3569 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3570 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3571 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3572 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3573 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3574 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3575 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3576 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3577 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3578 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3579 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3580 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3581 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3582 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3583 | return( ret ); |
| 3584 | } |
| 3585 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3586 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3587 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3588 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3589 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3590 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3591 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3594 | /* There is currently no ciphersuite using another length with TLS 1.2 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3595 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 3596 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3597 | hash_len = 36; |
| 3598 | else |
| 3599 | #endif |
| 3600 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3602 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 3603 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3604 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3605 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3606 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3607 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3608 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
Gabor Mezei | 18a4494 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 3611 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
Manuel Pégourié-Gonnard | 4abc327 | 2014-09-10 12:02:46 +0000 | [diff] [blame] | 3612 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3613 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3614 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3615 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | dd5f624 | 2021-06-28 21:58:56 +0100 | [diff] [blame] | 3616 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3617 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3620 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3621 | ssl->verify_data_len = hash_len; |
| 3622 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3623 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3624 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3625 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3626 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3627 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3628 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3629 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3630 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3631 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3632 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3633 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3634 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3635 | } |
| 3636 | else |
| 3637 | ssl->state++; |
| 3638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3639 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3640 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3641 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3642 | #endif |
| 3643 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3644 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3645 | |
| 3646 | return( 0 ); |
| 3647 | } |
| 3648 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3649 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3650 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3651 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3653 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3654 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3655 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 3656 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3657 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 3658 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3659 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3660 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3661 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3662 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3663 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 3664 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 3665 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3666 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3667 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3668 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3669 | #endif |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 3670 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3671 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3672 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 3673 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3674 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3675 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3676 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3677 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3678 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3679 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3680 | |
| 3681 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3682 | |
| 3683 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3684 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3685 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 3686 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3688 | #if defined(MBEDTLS_DHM_C) |
| 3689 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3690 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3691 | #if defined(MBEDTLS_ECDH_C) |
| 3692 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3693 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3694 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3695 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3696 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3697 | handshake->ecjpake_cache = NULL; |
| 3698 | handshake->ecjpake_cache_len = 0; |
| 3699 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3700 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3701 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3702 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3703 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3704 | #endif |
| 3705 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3706 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3707 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 3708 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3709 | |
| 3710 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3711 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3712 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 3713 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3714 | } |
| 3715 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3716 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3717 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3718 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 3719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3720 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 3721 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 3722 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 3723 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3724 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 3725 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 3726 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3727 | } |
| 3728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3729 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3730 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3731 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3732 | } |
| 3733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3734 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3735 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3736 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3737 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3738 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3739 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3740 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3741 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3742 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3743 | |
| 3744 | /* |
| 3745 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 3746 | * Now allocate missing structures. |
| 3747 | */ |
| 3748 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3749 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3750 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3751 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3752 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3753 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3754 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3755 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3756 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3757 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 3758 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3759 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3760 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3761 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3762 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3763 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 3764 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3765 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 3766 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3767 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3768 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3769 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3770 | if( ssl->handshake == NULL || |
| 3771 | ssl->transform_negotiate == NULL || |
| 3772 | ssl->session_negotiate == NULL ) |
| 3773 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3774 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3775 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3776 | mbedtls_free( ssl->handshake ); |
| 3777 | mbedtls_free( ssl->transform_negotiate ); |
| 3778 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3779 | |
| 3780 | ssl->handshake = NULL; |
| 3781 | ssl->transform_negotiate = NULL; |
| 3782 | ssl->session_negotiate = NULL; |
| 3783 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3784 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3787 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3788 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3789 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 3790 | ssl_handshake_params_init( ssl->handshake ); |
| 3791 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3792 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3793 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3794 | { |
| 3795 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3796 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3797 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 3798 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 3799 | else |
| 3800 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3801 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3802 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3803 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3804 | #endif |
| 3805 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3806 | return( 0 ); |
| 3807 | } |
| 3808 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3809 | #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] | 3810 | /* Dummy cookie callbacks for defaults */ |
| 3811 | static int ssl_cookie_write_dummy( void *ctx, |
| 3812 | unsigned char **p, unsigned char *end, |
| 3813 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3814 | { |
| 3815 | ((void) ctx); |
| 3816 | ((void) p); |
| 3817 | ((void) end); |
| 3818 | ((void) cli_id); |
| 3819 | ((void) cli_id_len); |
| 3820 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3821 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | static int ssl_cookie_check_dummy( void *ctx, |
| 3825 | const unsigned char *cookie, size_t cookie_len, |
| 3826 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3827 | { |
| 3828 | ((void) ctx); |
| 3829 | ((void) cookie); |
| 3830 | ((void) cookie_len); |
| 3831 | ((void) cli_id); |
| 3832 | ((void) cli_id_len); |
| 3833 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3834 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3835 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3836 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3837 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3838 | /* |
| 3839 | * Initialize an SSL context |
| 3840 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3841 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 3842 | { |
| 3843 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 3844 | } |
| 3845 | |
| 3846 | /* |
| 3847 | * Setup an SSL context |
| 3848 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 3849 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3850 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 3851 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3852 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3853 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3854 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3855 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3856 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3857 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3858 | |
| 3859 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3860 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3861 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 3862 | |
| 3863 | /* Set to NULL in case of an error condition */ |
| 3864 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3865 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3866 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3867 | ssl->in_buf_len = in_buf_len; |
| 3868 | #endif |
| 3869 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3870 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3871 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3872 | 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] | 3873 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3874 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3875 | } |
| 3876 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3877 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3878 | ssl->out_buf_len = out_buf_len; |
| 3879 | #endif |
| 3880 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3881 | if( ssl->out_buf == NULL ) |
| 3882 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3883 | 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] | 3884 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3885 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3886 | } |
| 3887 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3888 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3889 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3890 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 3891 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3892 | #endif |
| 3893 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3894 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3895 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3896 | |
| 3897 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3898 | |
| 3899 | error: |
| 3900 | mbedtls_free( ssl->in_buf ); |
| 3901 | mbedtls_free( ssl->out_buf ); |
| 3902 | |
| 3903 | ssl->conf = NULL; |
| 3904 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3905 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3906 | ssl->in_buf_len = 0; |
| 3907 | ssl->out_buf_len = 0; |
| 3908 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3909 | ssl->in_buf = NULL; |
| 3910 | ssl->out_buf = NULL; |
| 3911 | |
| 3912 | ssl->in_hdr = NULL; |
| 3913 | ssl->in_ctr = NULL; |
| 3914 | ssl->in_len = NULL; |
| 3915 | ssl->in_iv = NULL; |
| 3916 | ssl->in_msg = NULL; |
| 3917 | |
| 3918 | ssl->out_hdr = NULL; |
| 3919 | ssl->out_ctr = NULL; |
| 3920 | ssl->out_len = NULL; |
| 3921 | ssl->out_iv = NULL; |
| 3922 | ssl->out_msg = NULL; |
| 3923 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3924 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3928 | * Reset an initialized and used SSL context for re-use while retaining |
| 3929 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3930 | * |
| 3931 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 3932 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3933 | */ |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3934 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3935 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3936 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3937 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3938 | size_t in_buf_len = ssl->in_buf_len; |
| 3939 | size_t out_buf_len = ssl->out_buf_len; |
| 3940 | #else |
| 3941 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3942 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 3943 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3944 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 3945 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 3946 | !defined(MBEDTLS_SSL_SRV_C) |
| 3947 | ((void) partial); |
| 3948 | #endif |
| 3949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3950 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3951 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3952 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3953 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3954 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3955 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3956 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3957 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3958 | |
| 3959 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3960 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 3961 | 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] | 3962 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3963 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3964 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3965 | ssl->in_offt = NULL; |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3966 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3967 | |
| 3968 | ssl->in_msgtype = 0; |
| 3969 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3970 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3971 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 3972 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3973 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3974 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 7e8e6a6 | 2020-02-05 10:45:48 +0000 | [diff] [blame] | 3975 | mbedtls_ssl_dtls_replay_reset( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3976 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3977 | |
| 3978 | ssl->in_hslen = 0; |
| 3979 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3980 | |
| 3981 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3982 | |
| 3983 | ssl->out_msgtype = 0; |
| 3984 | ssl->out_msglen = 0; |
| 3985 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3986 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 3987 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 3988 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 3989 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3990 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3991 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 3992 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3993 | ssl->transform_in = NULL; |
| 3994 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3995 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 3996 | ssl->session_in = NULL; |
| 3997 | ssl->session_out = NULL; |
| 3998 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3999 | memset( ssl->out_buf, 0, out_buf_len ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 4000 | |
| 4001 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4002 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 4003 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 4004 | { |
| 4005 | ssl->in_left = 0; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4006 | memset( ssl->in_buf, 0, in_buf_len ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 4007 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4008 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4009 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4010 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4011 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4012 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 4013 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4014 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4015 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 4016 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4017 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4018 | } |
| 4019 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4020 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4021 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4022 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4023 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4024 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4025 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4026 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4027 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4028 | if( ssl->session ) |
| 4029 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4030 | mbedtls_ssl_session_free( ssl->session ); |
| 4031 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4032 | ssl->session = NULL; |
| 4033 | } |
| 4034 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4035 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4036 | ssl->alpn_chosen = NULL; |
| 4037 | #endif |
| 4038 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4039 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 4040 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4041 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 4042 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4043 | { |
| 4044 | mbedtls_free( ssl->cli_id ); |
| 4045 | ssl->cli_id = NULL; |
| 4046 | ssl->cli_id_len = 0; |
| 4047 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4048 | #endif |
| 4049 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4050 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 4051 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4052 | |
| 4053 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 4056 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4057 | * Reset an initialized and used SSL context for re-use while retaining |
| 4058 | * all application-set variables, function pointers and data. |
| 4059 | */ |
| 4060 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 4061 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4062 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4066 | * SSL set accessors |
| 4067 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4068 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4069 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4070 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4073 | 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] | 4074 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4075 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 4076 | } |
| 4077 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4078 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4079 | 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] | 4080 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4081 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4082 | } |
| 4083 | #endif |
| 4084 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4085 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4086 | 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] | 4087 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4088 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 4089 | } |
| 4090 | #endif |
| 4091 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4092 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4093 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 4094 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 4095 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4096 | { |
| 4097 | ssl->disable_datagram_packing = !allow_packing; |
| 4098 | } |
| 4099 | |
| 4100 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 4101 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 4102 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4103 | conf->hs_timeout_min = min; |
| 4104 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 4105 | } |
| 4106 | #endif |
| 4107 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4108 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4109 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4110 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4113 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4114 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4115 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4116 | void *p_vrfy ) |
| 4117 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4118 | conf->f_vrfy = f_vrfy; |
| 4119 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4120 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4121 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4122 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4123 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 4124 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4125 | void *p_rng ) |
| 4126 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 4127 | conf->f_rng = f_rng; |
| 4128 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4131 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 4132 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4133 | void *p_dbg ) |
| 4134 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4135 | conf->f_dbg = f_dbg; |
| 4136 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4139 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 4140 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 4141 | mbedtls_ssl_send_t *f_send, |
| 4142 | mbedtls_ssl_recv_t *f_recv, |
| 4143 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 4144 | { |
| 4145 | ssl->p_bio = p_bio; |
| 4146 | ssl->f_send = f_send; |
| 4147 | ssl->f_recv = f_recv; |
| 4148 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 4149 | } |
| 4150 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 4151 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4152 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 4153 | { |
| 4154 | ssl->mtu = mtu; |
| 4155 | } |
| 4156 | #endif |
| 4157 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4158 | 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] | 4159 | { |
| 4160 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 4161 | } |
| 4162 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 4163 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 4164 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 4165 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 4166 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 4167 | { |
| 4168 | ssl->p_timer = p_timer; |
| 4169 | ssl->f_set_timer = f_set_timer; |
| 4170 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 4171 | |
| 4172 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 4173 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 4174 | } |
| 4175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4176 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4177 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 4178 | void *p_cache, |
| 4179 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 4180 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4181 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 4182 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4183 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4184 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4185 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4186 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4187 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4188 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4189 | 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] | 4190 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4191 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4192 | |
| 4193 | if( ssl == NULL || |
| 4194 | session == NULL || |
| 4195 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4196 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4197 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4198 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4199 | } |
| 4200 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 4201 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 4202 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4203 | return( ret ); |
| 4204 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4205 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4206 | |
| 4207 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4208 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4209 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4210 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4211 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4212 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4213 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4214 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 4215 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 4216 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 4217 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 4218 | } |
| 4219 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4220 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 4221 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 4222 | int major, int minor ) |
| 4223 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4224 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 4225 | return; |
| 4226 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4227 | if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 4228 | return; |
| 4229 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4230 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4233 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 4234 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 4235 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 4236 | { |
| 4237 | conf->cert_profile = profile; |
| 4238 | } |
| 4239 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4240 | /* Append a new keycert entry to a (possibly empty) list */ |
| 4241 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 4242 | mbedtls_x509_crt *cert, |
| 4243 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4244 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4245 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4246 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4247 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 4248 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4249 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4250 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4251 | new_cert->cert = cert; |
| 4252 | new_cert->key = key; |
| 4253 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4254 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4255 | /* Update head is the list was null, else add to the end */ |
| 4256 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 4257 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4258 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 4259 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4260 | else |
| 4261 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4262 | mbedtls_ssl_key_cert *cur = *head; |
| 4263 | while( cur->next != NULL ) |
| 4264 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4265 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4266 | } |
| 4267 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4268 | return( 0 ); |
| 4269 | } |
| 4270 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4271 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4272 | mbedtls_x509_crt *own_cert, |
| 4273 | mbedtls_pk_context *pk_key ) |
| 4274 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 4275 | 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] | 4276 | } |
| 4277 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4278 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4279 | mbedtls_x509_crt *ca_chain, |
| 4280 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4281 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4282 | conf->ca_chain = ca_chain; |
| 4283 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 4284 | |
| 4285 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 4286 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 4287 | * cannot be used together. */ |
| 4288 | conf->f_ca_cb = NULL; |
| 4289 | conf->p_ca_cb = NULL; |
| 4290 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4291 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 4292 | |
| 4293 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 4294 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 4295 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 4296 | void *p_ca_cb ) |
| 4297 | { |
| 4298 | conf->f_ca_cb = f_ca_cb; |
| 4299 | conf->p_ca_cb = p_ca_cb; |
| 4300 | |
| 4301 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 4302 | * cannot be used together. */ |
| 4303 | conf->ca_chain = NULL; |
| 4304 | conf->ca_crl = NULL; |
| 4305 | } |
| 4306 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4307 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 4308 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 4309 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4310 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 4311 | mbedtls_x509_crt *own_cert, |
| 4312 | mbedtls_pk_context *pk_key ) |
| 4313 | { |
| 4314 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 4315 | own_cert, pk_key ) ); |
| 4316 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 4317 | |
| 4318 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 4319 | mbedtls_x509_crt *ca_chain, |
| 4320 | mbedtls_x509_crl *ca_crl ) |
| 4321 | { |
| 4322 | ssl->handshake->sni_ca_chain = ca_chain; |
| 4323 | ssl->handshake->sni_ca_crl = ca_crl; |
| 4324 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 4325 | |
| 4326 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 4327 | int authmode ) |
| 4328 | { |
| 4329 | ssl->handshake->sni_authmode = authmode; |
| 4330 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 4331 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 4332 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 4333 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4334 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 4335 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 4336 | void *p_vrfy ) |
| 4337 | { |
| 4338 | ssl->f_vrfy = f_vrfy; |
| 4339 | ssl->p_vrfy = p_vrfy; |
| 4340 | } |
| 4341 | #endif |
| 4342 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4343 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 4344 | /* |
| 4345 | * Set EC J-PAKE password for current handshake |
| 4346 | */ |
| 4347 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 4348 | const unsigned char *pw, |
| 4349 | size_t pw_len ) |
| 4350 | { |
| 4351 | mbedtls_ecjpake_role role; |
| 4352 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 4353 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 4354 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4355 | |
| 4356 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4357 | role = MBEDTLS_ECJPAKE_SERVER; |
| 4358 | else |
| 4359 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 4360 | |
| 4361 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 4362 | role, |
| 4363 | MBEDTLS_MD_SHA256, |
| 4364 | MBEDTLS_ECP_DP_SECP256R1, |
| 4365 | pw, pw_len ) ); |
| 4366 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4367 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 4368 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4369 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4370 | |
| 4371 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 4372 | { |
| 4373 | /* Remove reference to existing PSK, if any. */ |
| 4374 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4375 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4376 | { |
| 4377 | /* The maintenance of the PSK key slot is the |
| 4378 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4379 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4380 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 4381 | /* This and the following branch should never |
| 4382 | * be taken simultaenously as we maintain the |
| 4383 | * invariant that raw and opaque PSKs are never |
| 4384 | * configured simultaneously. As a safeguard, |
| 4385 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4386 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4387 | if( conf->psk != NULL ) |
| 4388 | { |
| 4389 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 4390 | |
| 4391 | mbedtls_free( conf->psk ); |
| 4392 | conf->psk = NULL; |
| 4393 | conf->psk_len = 0; |
| 4394 | } |
| 4395 | |
| 4396 | /* Remove reference to PSK identity, if any. */ |
| 4397 | if( conf->psk_identity != NULL ) |
| 4398 | { |
| 4399 | mbedtls_free( conf->psk_identity ); |
| 4400 | conf->psk_identity = NULL; |
| 4401 | conf->psk_identity_len = 0; |
| 4402 | } |
| 4403 | } |
| 4404 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4405 | /* This function assumes that PSK identity in the SSL config is unset. |
| 4406 | * It checks that the provided identity is well-formed and attempts |
| 4407 | * to make a copy of it in the SSL config. |
| 4408 | * On failure, the PSK identity in the config remains unset. */ |
| 4409 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 4410 | unsigned char const *psk_identity, |
| 4411 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 4412 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 4413 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4414 | if( psk_identity == NULL || |
| 4415 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4416 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 4417 | { |
| 4418 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4419 | } |
| 4420 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4421 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 4422 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4423 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4424 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 4425 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 4426 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 4427 | |
| 4428 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4429 | } |
| 4430 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4431 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 4432 | const unsigned char *psk, size_t psk_len, |
| 4433 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 4434 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4435 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4436 | /* Remove opaque/raw PSK + PSK Identity */ |
| 4437 | ssl_conf_remove_psk( conf ); |
| 4438 | |
| 4439 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 4440 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4441 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 4442 | if( psk_len == 0 ) |
| 4443 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4444 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 4445 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4446 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4447 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 4448 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 4449 | conf->psk_len = psk_len; |
| 4450 | memcpy( conf->psk, psk, conf->psk_len ); |
| 4451 | |
| 4452 | /* Check and set PSK Identity */ |
| 4453 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 4454 | if( ret != 0 ) |
| 4455 | ssl_conf_remove_psk( conf ); |
| 4456 | |
| 4457 | return( ret ); |
| 4458 | } |
| 4459 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4460 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 4461 | { |
| 4462 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4463 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4464 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4465 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4466 | } |
| 4467 | else |
| 4468 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4469 | if( ssl->handshake->psk != NULL ) |
| 4470 | { |
| 4471 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 4472 | ssl->handshake->psk_len ); |
| 4473 | mbedtls_free( ssl->handshake->psk ); |
| 4474 | ssl->handshake->psk_len = 0; |
| 4475 | } |
| 4476 | } |
| 4477 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4478 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 4479 | const unsigned char *psk, size_t psk_len ) |
| 4480 | { |
| 4481 | if( psk == NULL || ssl->handshake == NULL ) |
| 4482 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4483 | |
| 4484 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 4485 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4486 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4487 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4488 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4489 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4490 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4491 | |
| 4492 | ssl->handshake->psk_len = psk_len; |
| 4493 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 4494 | |
| 4495 | return( 0 ); |
| 4496 | } |
| 4497 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4498 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4499 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4500 | psa_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4501 | const unsigned char *psk_identity, |
| 4502 | size_t psk_identity_len ) |
| 4503 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4504 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4505 | /* Clear opaque/raw PSK + PSK Identity, if present. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4506 | ssl_conf_remove_psk( conf ); |
| 4507 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4508 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4509 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4510 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4511 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4512 | |
| 4513 | /* Check and set PSK Identity */ |
| 4514 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 4515 | psk_identity_len ); |
| 4516 | if( ret != 0 ) |
| 4517 | ssl_conf_remove_psk( conf ); |
| 4518 | |
| 4519 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4523 | psa_key_id_t psk ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4524 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4525 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
Ronald Cron | c26f8d4 | 2020-09-01 10:51:51 +0200 | [diff] [blame] | 4526 | ( ssl->handshake == NULL ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4527 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4528 | |
| 4529 | ssl_remove_psk( ssl ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4530 | ssl->handshake->psk_opaque = psk; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4531 | return( 0 ); |
| 4532 | } |
| 4533 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4534 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4535 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4536 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4537 | size_t), |
| 4538 | void *p_psk ) |
| 4539 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4540 | conf->f_psk = f_psk; |
| 4541 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 4542 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4543 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 4544 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4545 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 4546 | |
| 4547 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4548 | int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4549 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4550 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4551 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4552 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 4553 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 4554 | { |
| 4555 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4556 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4557 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4558 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4559 | |
| 4560 | return( 0 ); |
| 4561 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 4562 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4563 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4564 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 4565 | const unsigned char *dhm_P, size_t P_len, |
| 4566 | const unsigned char *dhm_G, size_t G_len ) |
| 4567 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4568 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4569 | |
| 4570 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 4571 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 4572 | { |
| 4573 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4574 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4575 | return( ret ); |
| 4576 | } |
| 4577 | |
| 4578 | return( 0 ); |
| 4579 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4580 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4581 | 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] | 4582 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4583 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4584 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4585 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 4586 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 4587 | { |
| 4588 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4589 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4590 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4591 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4592 | |
| 4593 | return( 0 ); |
| 4594 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4595 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4596 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4597 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4598 | /* |
| 4599 | * Set the minimum length for Diffie-Hellman parameters |
| 4600 | */ |
| 4601 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 4602 | unsigned int bitlen ) |
| 4603 | { |
| 4604 | conf->dhm_min_bitlen = bitlen; |
| 4605 | } |
| 4606 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 4607 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4608 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 4609 | /* |
| 4610 | * Set allowed/preferred hashes for handshake signatures |
| 4611 | */ |
| 4612 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 4613 | const int *hashes ) |
| 4614 | { |
| 4615 | conf->sig_hashes = hashes; |
| 4616 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4617 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 4618 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4619 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4620 | /* |
| 4621 | * Set the allowed elliptic curves |
| 4622 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4623 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4624 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4625 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4626 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4627 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4628 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4629 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4630 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4631 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4632 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4633 | /* Initialize to suppress unnecessary compiler warning */ |
| 4634 | size_t hostname_len = 0; |
| 4635 | |
| 4636 | /* Check if new hostname is valid before |
| 4637 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4638 | if( hostname != NULL ) |
| 4639 | { |
| 4640 | hostname_len = strlen( hostname ); |
| 4641 | |
| 4642 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 4643 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4644 | } |
| 4645 | |
| 4646 | /* Now it's clear that we will overwrite the old hostname, |
| 4647 | * so we can free it safely */ |
| 4648 | |
| 4649 | if( ssl->hostname != NULL ) |
| 4650 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4651 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4652 | mbedtls_free( ssl->hostname ); |
| 4653 | } |
| 4654 | |
| 4655 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 4656 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4657 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4658 | { |
| 4659 | ssl->hostname = NULL; |
| 4660 | } |
| 4661 | else |
| 4662 | { |
| 4663 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4664 | if( ssl->hostname == NULL ) |
| 4665 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4666 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4667 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4668 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4669 | ssl->hostname[hostname_len] = '\0'; |
| 4670 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4671 | |
| 4672 | return( 0 ); |
| 4673 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 4674 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4675 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4676 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4677 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4678 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4679 | const unsigned char *, size_t), |
| 4680 | void *p_sni ) |
| 4681 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4682 | conf->f_sni = f_sni; |
| 4683 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4684 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4685 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4687 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4688 | 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] | 4689 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4690 | size_t cur_len, tot_len; |
| 4691 | const char **p; |
| 4692 | |
| 4693 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4694 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 4695 | * MUST NOT be truncated." |
| 4696 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4697 | */ |
| 4698 | tot_len = 0; |
| 4699 | for( p = protos; *p != NULL; p++ ) |
| 4700 | { |
| 4701 | cur_len = strlen( *p ); |
| 4702 | tot_len += cur_len; |
| 4703 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 4704 | if( ( cur_len == 0 ) || |
| 4705 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 4706 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4707 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4708 | } |
| 4709 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4710 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4711 | |
| 4712 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4713 | } |
| 4714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4715 | 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] | 4716 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4717 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4718 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4719 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4720 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4721 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4722 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 4723 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4724 | { |
| 4725 | conf->dtls_srtp_mki_support = support_mki_value; |
| 4726 | } |
| 4727 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4728 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 4729 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 4730 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4731 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4732 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4733 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4734 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4735 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4736 | |
| 4737 | 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] | 4738 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4739 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4740 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4741 | |
| 4742 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 4743 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4744 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4745 | } |
| 4746 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4747 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4748 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4749 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4750 | const mbedtls_ssl_srtp_profile *p; |
| 4751 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4752 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4753 | /* check the profiles list: all entry must be valid, |
| 4754 | * 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] | 4755 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 4756 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 4757 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4758 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4759 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4760 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 4761 | list_size++; |
| 4762 | } |
| 4763 | else |
| 4764 | { |
| 4765 | /* unsupported value, stop parsing and set the size to an error value */ |
| 4766 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4767 | } |
| 4768 | } |
| 4769 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4770 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 4771 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4772 | conf->dtls_srtp_profile_list = NULL; |
| 4773 | conf->dtls_srtp_profile_list_len = 0; |
| 4774 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4775 | } |
| 4776 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 4777 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4778 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4779 | |
| 4780 | return( 0 ); |
| 4781 | } |
| 4782 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4783 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 4784 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4785 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4786 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 4787 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4788 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4789 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4790 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4791 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4792 | else |
| 4793 | { |
| 4794 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4795 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 4796 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4797 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4798 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4799 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 4800 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4801 | 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] | 4802 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4803 | conf->max_major_ver = major; |
| 4804 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4807 | 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] | 4808 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4809 | conf->min_major_ver = major; |
| 4810 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4813 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4814 | void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback ) |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 4815 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 4816 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 4817 | } |
| 4818 | #endif |
| 4819 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4820 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4821 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 4822 | char cert_req_ca_list ) |
| 4823 | { |
| 4824 | conf->cert_req_ca_list = cert_req_ca_list; |
| 4825 | } |
| 4826 | #endif |
| 4827 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4828 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4829 | 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] | 4830 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4831 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 4832 | } |
| 4833 | #endif |
| 4834 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4835 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4836 | 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] | 4837 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4838 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 4839 | } |
| 4840 | #endif |
| 4841 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 4842 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4843 | void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 ) |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 4844 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4845 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 4846 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 4847 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 4848 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4849 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4850 | 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] | 4851 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4852 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4853 | 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] | 4854 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4855 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4856 | } |
| 4857 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 4858 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4859 | |
| 4860 | return( 0 ); |
| 4861 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4862 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4863 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4864 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4865 | void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate ) |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 4866 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4867 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 4868 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4869 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 4870 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4871 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4872 | void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 4873 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 4874 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 4875 | } |
| 4876 | #endif |
| 4877 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4878 | 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] | 4879 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4880 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4881 | } |
| 4882 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4883 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4884 | 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] | 4885 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4886 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 4887 | } |
| 4888 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4889 | 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] | 4890 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4891 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 4892 | } |
| 4893 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4894 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4895 | const unsigned char period[8] ) |
| 4896 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4897 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4898 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4899 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4900 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4901 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4902 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4903 | 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] | 4904 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 4905 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4906 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4907 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4908 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4909 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4910 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 4911 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 4912 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 4913 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4914 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4915 | conf->f_ticket_write = f_ticket_write; |
| 4916 | conf->f_ticket_parse = f_ticket_parse; |
| 4917 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4918 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4919 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4920 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4921 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4922 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 4923 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 4924 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 4925 | void *p_export_keys ) |
| 4926 | { |
| 4927 | conf->f_export_keys = f_export_keys; |
| 4928 | conf->p_export_keys = p_export_keys; |
| 4929 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 4930 | |
| 4931 | void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf, |
| 4932 | mbedtls_ssl_export_keys_ext_t *f_export_keys_ext, |
| 4933 | void *p_export_keys ) |
| 4934 | { |
| 4935 | conf->f_export_keys_ext = f_export_keys_ext; |
| 4936 | conf->p_export_keys = p_export_keys; |
| 4937 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4938 | #endif |
| 4939 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4940 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4941 | void mbedtls_ssl_conf_async_private_cb( |
| 4942 | mbedtls_ssl_config *conf, |
| 4943 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 4944 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 4945 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 4946 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4947 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4948 | { |
| 4949 | conf->f_async_sign_start = f_async_sign; |
| 4950 | conf->f_async_decrypt_start = f_async_decrypt; |
| 4951 | conf->f_async_resume = f_async_resume; |
| 4952 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4953 | conf->p_async_config_data = async_config_data; |
| 4954 | } |
| 4955 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 4956 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 4957 | { |
| 4958 | return( conf->p_async_config_data ); |
| 4959 | } |
| 4960 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4961 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4962 | { |
| 4963 | if( ssl->handshake == NULL ) |
| 4964 | return( NULL ); |
| 4965 | else |
| 4966 | return( ssl->handshake->user_async_ctx ); |
| 4967 | } |
| 4968 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4969 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4970 | void *ctx ) |
| 4971 | { |
| 4972 | if( ssl->handshake != NULL ) |
| 4973 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4974 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4975 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4976 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4977 | /* |
| 4978 | * SSL get accessors |
| 4979 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4980 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4981 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 4982 | if( ssl->session != NULL ) |
| 4983 | return( ssl->session->verify_result ); |
| 4984 | |
| 4985 | if( ssl->session_negotiate != NULL ) |
| 4986 | return( ssl->session_negotiate->verify_result ); |
| 4987 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 4988 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4991 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4992 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4993 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4994 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4995 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4996 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4999 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5000 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5001 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5002 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 5003 | { |
| 5004 | switch( ssl->minor_ver ) |
| 5005 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5006 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 5007 | return( "DTLSv1.0" ); |
| 5008 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5009 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 5010 | return( "DTLSv1.2" ); |
| 5011 | |
| 5012 | default: |
| 5013 | return( "unknown (DTLS)" ); |
| 5014 | } |
| 5015 | } |
| 5016 | #endif |
| 5017 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5018 | switch( ssl->minor_ver ) |
| 5019 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5020 | case MBEDTLS_SSL_MINOR_VERSION_0: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5021 | return( "SSLv3.0" ); |
| 5022 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5023 | case MBEDTLS_SSL_MINOR_VERSION_1: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5024 | return( "TLSv1.0" ); |
| 5025 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5026 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5027 | return( "TLSv1.1" ); |
| 5028 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5029 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 5030 | return( "TLSv1.2" ); |
| 5031 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5032 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 5033 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5034 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 5037 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 5038 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 5039 | { |
| 5040 | size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN; |
| 5041 | size_t read_mfl; |
| 5042 | |
| 5043 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 5044 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 5045 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 5046 | { |
| 5047 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 5048 | } |
| 5049 | |
| 5050 | /* Check if a smaller max length was negotiated */ |
| 5051 | if( ssl->session_out != NULL ) |
| 5052 | { |
| 5053 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 5054 | if( read_mfl < max_len ) |
| 5055 | { |
| 5056 | max_len = read_mfl; |
| 5057 | } |
| 5058 | } |
| 5059 | |
| 5060 | // During a handshake, use the value being negotiated |
| 5061 | if( ssl->session_negotiate != NULL ) |
| 5062 | { |
| 5063 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 5064 | if( read_mfl < max_len ) |
| 5065 | { |
| 5066 | max_len = read_mfl; |
| 5067 | } |
| 5068 | } |
| 5069 | |
| 5070 | return( max_len ); |
| 5071 | } |
| 5072 | |
| 5073 | 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] | 5074 | { |
| 5075 | size_t max_len; |
| 5076 | |
| 5077 | /* |
| 5078 | * Assume mfl_code is correct since it was checked when set |
| 5079 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5080 | 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] | 5081 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 5082 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 5083 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5084 | 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] | 5085 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5086 | 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] | 5087 | } |
| 5088 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 5089 | /* During a handshake, use the value being negotiated */ |
| 5090 | if( ssl->session_negotiate != NULL && |
| 5091 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 5092 | { |
| 5093 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 5094 | } |
| 5095 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5096 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 5097 | } |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 5098 | |
| 5099 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5100 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 5101 | { |
| 5102 | return mbedtls_ssl_get_output_max_frag_len( ssl ); |
| 5103 | } |
| 5104 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 5105 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 5106 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 5107 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 5108 | 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] | 5109 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 5110 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 5111 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 5112 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 5113 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 5114 | return ( 0 ); |
| 5115 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 5116 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 5117 | return( ssl->mtu ); |
| 5118 | |
| 5119 | if( ssl->mtu == 0 ) |
| 5120 | return( ssl->handshake->mtu ); |
| 5121 | |
| 5122 | return( ssl->mtu < ssl->handshake->mtu ? |
| 5123 | ssl->mtu : ssl->handshake->mtu ); |
| 5124 | } |
| 5125 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5126 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5127 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 5128 | { |
| 5129 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 5130 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 5131 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 5132 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5133 | (void) ssl; |
| 5134 | #endif |
| 5135 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5136 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 5137 | 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] | 5138 | |
| 5139 | if( max_len > mfl ) |
| 5140 | max_len = mfl; |
| 5141 | #endif |
| 5142 | |
| 5143 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 5144 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5145 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 5146 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5147 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 5148 | const size_t overhead = (size_t) ret; |
| 5149 | |
| 5150 | if( ret < 0 ) |
| 5151 | return( ret ); |
| 5152 | |
| 5153 | if( mtu <= overhead ) |
| 5154 | { |
| 5155 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 5156 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5157 | } |
| 5158 | |
| 5159 | if( max_len > mtu - overhead ) |
| 5160 | max_len = mtu - overhead; |
| 5161 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 5162 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5163 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 5164 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 5165 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5166 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5167 | #endif |
| 5168 | |
| 5169 | return( (int) max_len ); |
| 5170 | } |
| 5171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5172 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5173 | 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] | 5174 | { |
| 5175 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 5176 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5177 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 5178 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 5179 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 5180 | #else |
| 5181 | return( NULL ); |
| 5182 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5183 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5184 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5185 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5186 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 5187 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 5188 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5189 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5190 | if( ssl == NULL || |
| 5191 | dst == NULL || |
| 5192 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5193 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5194 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5195 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5196 | } |
| 5197 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 5198 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5199 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5200 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5201 | |
Manuel Pégourié-Gonnard | b5e4e0a | 2019-05-20 11:12:28 +0200 | [diff] [blame] | 5202 | const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ) |
| 5203 | { |
| 5204 | if( ssl == NULL ) |
| 5205 | return( NULL ); |
| 5206 | |
| 5207 | return( ssl->session ); |
| 5208 | } |
| 5209 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5210 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5211 | * Define ticket header determining Mbed TLS version |
| 5212 | * and structure of the ticket. |
| 5213 | */ |
| 5214 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5215 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5216 | * Define bitflag determining compile-time settings influencing |
| 5217 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5218 | */ |
| 5219 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5220 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5221 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5222 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5223 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5224 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5225 | |
| 5226 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5227 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5228 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5229 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5230 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5231 | |
| 5232 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5233 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5234 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5235 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5236 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 5237 | |
| 5238 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5239 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5240 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5241 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5242 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 5243 | |
| 5244 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5245 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5246 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5247 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5248 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 5249 | |
| 5250 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5251 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5252 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5253 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5254 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 5255 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5256 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5257 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 5258 | #else |
| 5259 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 5260 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 5261 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5262 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 5263 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 5264 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 5265 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
| 5266 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4 |
| 5267 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5 |
| 5268 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5269 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5270 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5271 | ( (uint16_t) ( \ |
| 5272 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 5273 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 5274 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 5275 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
| 5276 | ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \ |
| 5277 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 5278 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5279 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 5280 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5281 | MBEDTLS_VERSION_MAJOR, |
| 5282 | MBEDTLS_VERSION_MINOR, |
| 5283 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5284 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 5285 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 5286 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5287 | |
| 5288 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5289 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5290 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5291 | * |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5292 | * opaque mbedtls_version[3]; // major, minor, patch |
| 5293 | * opaque session_format[2]; // version-specific 16-bit field determining |
| 5294 | * // the format of the remaining |
| 5295 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 5296 | * |
| 5297 | * Note: When updating the format, remember to keep |
| 5298 | * these version+format bytes. |
| 5299 | * |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 5300 | * // In this version, `session_format` determines |
| 5301 | * // the setting of those compile-time |
| 5302 | * // configuration options which influence |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5303 | * // the structure of mbedtls_ssl_session. |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5304 | * uint64 start_time; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5305 | * uint8 ciphersuite[2]; // defined by the standard |
| 5306 | * uint8 compression; // 0 or 1 |
| 5307 | * uint8 session_id_len; // at most 32 |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5308 | * opaque session_id[32]; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5309 | * opaque master[48]; // fixed length in the standard |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5310 | * uint32 verify_result; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5311 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 5312 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5313 | * uint32 ticket_lifetime; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5314 | * uint8 mfl_code; // up to 255 according to standard |
| 5315 | * uint8 trunc_hmac; // 0 or 1 |
| 5316 | * uint8 encrypt_then_mac; // 0 or 1 |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5317 | * |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5318 | * The order is the same as in the definition of the structure, except |
| 5319 | * verify_result is put before peer_cert so that all mandatory fields come |
| 5320 | * together in one block. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5321 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5322 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 5323 | unsigned char omit_header, |
| 5324 | unsigned char *buf, |
| 5325 | size_t buf_len, |
| 5326 | size_t *olen ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5327 | { |
| 5328 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5329 | size_t used = 0; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5330 | #if defined(MBEDTLS_HAVE_TIME) |
| 5331 | uint64_t start; |
| 5332 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5333 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5334 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5335 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5336 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5337 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5338 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5339 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5340 | if( !omit_header ) |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5341 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5342 | /* |
| 5343 | * Add version identifier |
| 5344 | */ |
| 5345 | |
| 5346 | used += sizeof( ssl_serialized_session_header ); |
| 5347 | |
| 5348 | if( used <= buf_len ) |
| 5349 | { |
| 5350 | memcpy( p, ssl_serialized_session_header, |
| 5351 | sizeof( ssl_serialized_session_header ) ); |
| 5352 | p += sizeof( ssl_serialized_session_header ); |
| 5353 | } |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5354 | } |
| 5355 | |
| 5356 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5357 | * Time |
| 5358 | */ |
| 5359 | #if defined(MBEDTLS_HAVE_TIME) |
| 5360 | used += 8; |
| 5361 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5362 | if( used <= buf_len ) |
| 5363 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5364 | start = (uint64_t) session->start; |
| 5365 | |
Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5366 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 5367 | p += 8; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5368 | } |
| 5369 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5370 | |
| 5371 | /* |
| 5372 | * Basic mandatory fields |
| 5373 | */ |
| 5374 | used += 2 /* ciphersuite */ |
| 5375 | + 1 /* compression */ |
| 5376 | + 1 /* id_len */ |
| 5377 | + sizeof( session->id ) |
| 5378 | + sizeof( session->master ) |
| 5379 | + 4; /* verify_result */ |
| 5380 | |
| 5381 | if( used <= buf_len ) |
| 5382 | { |
Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5383 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 5384 | p += 2; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5385 | |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5386 | *p++ = MBEDTLS_BYTE_0( session->compression ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5387 | |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5388 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5389 | memcpy( p, session->id, 32 ); |
| 5390 | p += 32; |
| 5391 | |
| 5392 | memcpy( p, session->master, 48 ); |
| 5393 | p += 48; |
| 5394 | |
Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5395 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 5396 | p += 4; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5397 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5398 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5399 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5400 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5401 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5402 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5403 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5404 | if( session->peer_cert == NULL ) |
| 5405 | cert_len = 0; |
| 5406 | else |
| 5407 | cert_len = session->peer_cert->raw.len; |
| 5408 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5409 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5410 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5411 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5412 | { |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5413 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 5414 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 5415 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5416 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5417 | if( session->peer_cert != NULL ) |
| 5418 | { |
| 5419 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 5420 | p += cert_len; |
| 5421 | } |
| 5422 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5423 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5424 | if( session->peer_cert_digest != NULL ) |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5425 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5426 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 5427 | if( used <= buf_len ) |
| 5428 | { |
| 5429 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 5430 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 5431 | memcpy( p, session->peer_cert_digest, |
| 5432 | session->peer_cert_digest_len ); |
| 5433 | p += session->peer_cert_digest_len; |
| 5434 | } |
| 5435 | } |
| 5436 | else |
| 5437 | { |
| 5438 | used += 2; |
| 5439 | if( used <= buf_len ) |
| 5440 | { |
| 5441 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 5442 | *p++ = 0; |
| 5443 | } |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5444 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5445 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5446 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5447 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5448 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5449 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5450 | */ |
| 5451 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5452 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5453 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5454 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5455 | { |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5456 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 5457 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 5458 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5459 | |
| 5460 | if( session->ticket != NULL ) |
| 5461 | { |
| 5462 | memcpy( p, session->ticket, session->ticket_len ); |
| 5463 | p += session->ticket_len; |
| 5464 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5465 | |
Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5466 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 5467 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5468 | } |
| 5469 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5470 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5471 | /* |
| 5472 | * Misc extension-related info |
| 5473 | */ |
| 5474 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 5475 | used += 1; |
| 5476 | |
| 5477 | if( used <= buf_len ) |
| 5478 | *p++ = session->mfl_code; |
| 5479 | #endif |
| 5480 | |
| 5481 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 5482 | used += 1; |
| 5483 | |
| 5484 | if( used <= buf_len ) |
| 5485 | *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF ); |
| 5486 | #endif |
| 5487 | |
| 5488 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5489 | used += 1; |
| 5490 | |
| 5491 | if( used <= buf_len ) |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5492 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5493 | #endif |
| 5494 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5495 | /* Done */ |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5496 | *olen = used; |
| 5497 | |
| 5498 | if( used > buf_len ) |
| 5499 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5500 | |
| 5501 | return( 0 ); |
| 5502 | } |
| 5503 | |
| 5504 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5505 | * Public wrapper for ssl_session_save() |
| 5506 | */ |
| 5507 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 5508 | unsigned char *buf, |
| 5509 | size_t buf_len, |
| 5510 | size_t *olen ) |
| 5511 | { |
| 5512 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 5513 | } |
| 5514 | |
| 5515 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5516 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5517 | * |
| 5518 | * This internal version is wrapped by a public function that cleans up in |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5519 | * case of error, and has an extra option omit_header. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5520 | */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5521 | static int ssl_session_load( mbedtls_ssl_session *session, |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5522 | unsigned char omit_header, |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5523 | const unsigned char *buf, |
| 5524 | size_t len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5525 | { |
| 5526 | const unsigned char *p = buf; |
| 5527 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5528 | #if defined(MBEDTLS_HAVE_TIME) |
| 5529 | uint64_t start; |
| 5530 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5531 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5532 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5533 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5534 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5535 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5536 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5537 | if( !omit_header ) |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5538 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5539 | /* |
| 5540 | * Check version identifier |
| 5541 | */ |
| 5542 | |
| 5543 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 5544 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5545 | |
| 5546 | if( memcmp( p, ssl_serialized_session_header, |
| 5547 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 5548 | { |
| 5549 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 5550 | } |
| 5551 | p += sizeof( ssl_serialized_session_header ); |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5552 | } |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5553 | |
| 5554 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5555 | * Time |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5556 | */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5557 | #if defined(MBEDTLS_HAVE_TIME) |
| 5558 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5559 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5560 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5561 | start = ( (uint64_t) p[0] << 56 ) | |
| 5562 | ( (uint64_t) p[1] << 48 ) | |
| 5563 | ( (uint64_t) p[2] << 40 ) | |
| 5564 | ( (uint64_t) p[3] << 32 ) | |
| 5565 | ( (uint64_t) p[4] << 24 ) | |
| 5566 | ( (uint64_t) p[5] << 16 ) | |
| 5567 | ( (uint64_t) p[6] << 8 ) | |
| 5568 | ( (uint64_t) p[7] ); |
| 5569 | p += 8; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5570 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5571 | session->start = (time_t) start; |
| 5572 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5573 | |
| 5574 | /* |
| 5575 | * Basic mandatory fields |
| 5576 | */ |
| 5577 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 5578 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5579 | |
| 5580 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 5581 | p += 2; |
| 5582 | |
| 5583 | session->compression = *p++; |
| 5584 | |
| 5585 | session->id_len = *p++; |
| 5586 | memcpy( session->id, p, 32 ); |
| 5587 | p += 32; |
| 5588 | |
| 5589 | memcpy( session->master, p, 48 ); |
| 5590 | p += 48; |
| 5591 | |
| 5592 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 5593 | ( (uint32_t) p[1] << 16 ) | |
| 5594 | ( (uint32_t) p[2] << 8 ) | |
| 5595 | ( (uint32_t) p[3] ); |
| 5596 | p += 4; |
| 5597 | |
| 5598 | /* Immediately clear invalid pointer values that have been read, in case |
| 5599 | * we exit early before we replaced them with valid ones. */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5600 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5601 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5602 | session->peer_cert = NULL; |
| 5603 | #else |
| 5604 | session->peer_cert_digest = NULL; |
| 5605 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5606 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5607 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5608 | session->ticket = NULL; |
| 5609 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5610 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5611 | /* |
| 5612 | * Peer certificate |
| 5613 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5614 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5615 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5616 | /* Deserialize CRT from the end of the ticket. */ |
| 5617 | if( 3 > (size_t)( end - p ) ) |
| 5618 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5619 | |
| 5620 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5621 | p += 3; |
| 5622 | |
| 5623 | if( cert_len != 0 ) |
| 5624 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5625 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5626 | |
| 5627 | if( cert_len > (size_t)( end - p ) ) |
| 5628 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5629 | |
| 5630 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 5631 | |
| 5632 | if( session->peer_cert == NULL ) |
| 5633 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5634 | |
| 5635 | mbedtls_x509_crt_init( session->peer_cert ); |
| 5636 | |
| 5637 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 5638 | p, cert_len ) ) != 0 ) |
| 5639 | { |
| 5640 | mbedtls_x509_crt_free( session->peer_cert ); |
| 5641 | mbedtls_free( session->peer_cert ); |
| 5642 | session->peer_cert = NULL; |
| 5643 | return( ret ); |
| 5644 | } |
| 5645 | |
| 5646 | p += cert_len; |
| 5647 | } |
| 5648 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5649 | /* Deserialize CRT digest from the end of the ticket. */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5650 | if( 2 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5651 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5652 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5653 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 5654 | session->peer_cert_digest_len = (size_t) *p++; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5655 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5656 | if( session->peer_cert_digest_len != 0 ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5657 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5658 | const mbedtls_md_info_t *md_info = |
| 5659 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 5660 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5661 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5662 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 5663 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5664 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5665 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 5666 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5667 | |
| 5668 | session->peer_cert_digest = |
| 5669 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5670 | if( session->peer_cert_digest == NULL ) |
| 5671 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5672 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5673 | memcpy( session->peer_cert_digest, p, |
| 5674 | session->peer_cert_digest_len ); |
| 5675 | p += session->peer_cert_digest_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5676 | } |
| 5677 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5678 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5679 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5680 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5681 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5682 | */ |
| 5683 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5684 | if( 3 > (size_t)( end - p ) ) |
| 5685 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5686 | |
| 5687 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5688 | p += 3; |
| 5689 | |
| 5690 | if( session->ticket_len != 0 ) |
| 5691 | { |
| 5692 | if( session->ticket_len > (size_t)( end - p ) ) |
| 5693 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5694 | |
| 5695 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 5696 | if( session->ticket == NULL ) |
| 5697 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5698 | |
| 5699 | memcpy( session->ticket, p, session->ticket_len ); |
| 5700 | p += session->ticket_len; |
| 5701 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5702 | |
| 5703 | if( 4 > (size_t)( end - p ) ) |
| 5704 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5705 | |
| 5706 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 5707 | ( (uint32_t) p[1] << 16 ) | |
| 5708 | ( (uint32_t) p[2] << 8 ) | |
| 5709 | ( (uint32_t) p[3] ); |
| 5710 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5711 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5712 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5713 | /* |
| 5714 | * Misc extension-related info |
| 5715 | */ |
| 5716 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 5717 | if( 1 > (size_t)( end - p ) ) |
| 5718 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5719 | |
| 5720 | session->mfl_code = *p++; |
| 5721 | #endif |
| 5722 | |
| 5723 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 5724 | if( 1 > (size_t)( end - p ) ) |
| 5725 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5726 | |
| 5727 | session->trunc_hmac = *p++; |
| 5728 | #endif |
| 5729 | |
| 5730 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5731 | if( 1 > (size_t)( end - p ) ) |
| 5732 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5733 | |
| 5734 | session->encrypt_then_mac = *p++; |
| 5735 | #endif |
| 5736 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5737 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5738 | if( p != end ) |
| 5739 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5740 | |
| 5741 | return( 0 ); |
| 5742 | } |
| 5743 | |
| 5744 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5745 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5746 | */ |
| 5747 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 5748 | const unsigned char *buf, |
| 5749 | size_t len ) |
| 5750 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5751 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5752 | |
| 5753 | if( ret != 0 ) |
| 5754 | mbedtls_ssl_session_free( session ); |
| 5755 | |
| 5756 | return( ret ); |
| 5757 | } |
| 5758 | |
| 5759 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5760 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5761 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5762 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5763 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5764 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5765 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5766 | if( ssl == NULL || ssl->conf == NULL ) |
| 5767 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5768 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5769 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5770 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5771 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5772 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5773 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5774 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5775 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5776 | #endif |
| 5777 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5778 | return( ret ); |
| 5779 | } |
| 5780 | |
| 5781 | /* |
| 5782 | * Perform the SSL handshake |
| 5783 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5784 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5785 | { |
| 5786 | int ret = 0; |
| 5787 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5788 | /* Sanity checks */ |
| 5789 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5790 | if( ssl == NULL || ssl->conf == NULL ) |
| 5791 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5792 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5793 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5794 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5795 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 5796 | { |
| 5797 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 5798 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 5799 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5800 | } |
| 5801 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5802 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5803 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5804 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5805 | /* Main handshake loop */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5806 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5807 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5808 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5809 | |
| 5810 | if( ret != 0 ) |
| 5811 | break; |
| 5812 | } |
| 5813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5814 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5815 | |
| 5816 | return( ret ); |
| 5817 | } |
| 5818 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5819 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5820 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5821 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5822 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5823 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5824 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5825 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5826 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5827 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5828 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5829 | |
| 5830 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5831 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5832 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5833 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5834 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5835 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5836 | 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] | 5837 | return( ret ); |
| 5838 | } |
| 5839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5840 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5841 | |
| 5842 | return( 0 ); |
| 5843 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5844 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5845 | |
| 5846 | /* |
| 5847 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5848 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 5849 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 5850 | * - 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] | 5851 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5852 | * 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] | 5853 | * 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] | 5854 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5855 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5856 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5857 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5858 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5859 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5860 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5861 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 5862 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5863 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5864 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 5865 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5866 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5867 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5868 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5869 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5870 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 5871 | ssl->handshake->out_msg_seq = 1; |
| 5872 | else |
| 5873 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5874 | } |
| 5875 | #endif |
| 5876 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5877 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 5878 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5879 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5880 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5881 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5882 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5883 | return( ret ); |
| 5884 | } |
| 5885 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5886 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5887 | |
| 5888 | return( 0 ); |
| 5889 | } |
| 5890 | |
| 5891 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5892 | * Renegotiate current connection on client, |
| 5893 | * or request renegotiation on server |
| 5894 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5895 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5896 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5897 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5898 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5899 | if( ssl == NULL || ssl->conf == NULL ) |
| 5900 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5901 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5902 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5903 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5904 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5905 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5906 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5907 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5908 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5909 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5910 | |
| 5911 | /* Did we already try/start sending HelloRequest? */ |
| 5912 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5913 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5914 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5915 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5916 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5917 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5918 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5919 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5920 | /* |
| 5921 | * On client, either start the renegotiation process or, |
| 5922 | * if already in progress, continue the handshake |
| 5923 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5924 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5925 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5926 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5927 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5928 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5929 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5930 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5931 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5932 | return( ret ); |
| 5933 | } |
| 5934 | } |
| 5935 | else |
| 5936 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5937 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5938 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5939 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5940 | return( ret ); |
| 5941 | } |
| 5942 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5943 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5944 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 5945 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5946 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5947 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5948 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5949 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5950 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5951 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5952 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5953 | |
| 5954 | while( cur != NULL ) |
| 5955 | { |
| 5956 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5957 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5958 | cur = next; |
| 5959 | } |
| 5960 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5961 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5962 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5963 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5964 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5965 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 5966 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5967 | if( handshake == NULL ) |
| 5968 | return; |
| 5969 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5970 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 5971 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 5972 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 5973 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5974 | handshake->async_in_progress = 0; |
| 5975 | } |
| 5976 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 5977 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5978 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 5979 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 5980 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 5981 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 5982 | #endif |
| 5983 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5984 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5985 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5986 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 5987 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5988 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 5989 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5990 | #endif |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 5991 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5992 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 5993 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5994 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5995 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 5996 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5997 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5998 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5999 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6000 | #if defined(MBEDTLS_DHM_C) |
| 6001 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6002 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6003 | #if defined(MBEDTLS_ECDH_C) |
| 6004 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 6005 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 6006 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 6007 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 6008 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6009 | mbedtls_free( handshake->ecjpake_cache ); |
| 6010 | handshake->ecjpake_cache = NULL; |
| 6011 | handshake->ecjpake_cache_len = 0; |
| 6012 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 6013 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 6014 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 6015 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 6016 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 6017 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6018 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 6019 | #endif |
| 6020 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6021 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 6022 | if( handshake->psk != NULL ) |
| 6023 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6024 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 6025 | mbedtls_free( handshake->psk ); |
| 6026 | } |
| 6027 | #endif |
| 6028 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6029 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 6030 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 6031 | /* |
| 6032 | * Free only the linked list wrapper, not the keys themselves |
| 6033 | * since the belong to the SNI callback |
| 6034 | */ |
| 6035 | if( handshake->sni_key_cert != NULL ) |
| 6036 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6037 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 6038 | |
| 6039 | while( cur != NULL ) |
| 6040 | { |
| 6041 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6042 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 6043 | cur = next; |
| 6044 | } |
| 6045 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6046 | #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] | 6047 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6048 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 6049 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6050 | if( handshake->ecrs_peer_cert != NULL ) |
| 6051 | { |
| 6052 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 6053 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 6054 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 6055 | #endif |
| 6056 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 6057 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 6058 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6059 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 6060 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6061 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6062 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6063 | mbedtls_free( handshake->verify_cookie ); |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 6064 | mbedtls_ssl_flight_free( handshake->flight ); |
| 6065 | mbedtls_ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 6066 | #endif |
| 6067 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 6068 | #if defined(MBEDTLS_ECDH_C) && \ |
| 6069 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6070 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 6071 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 6072 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6073 | mbedtls_platform_zeroize( handshake, |
| 6074 | sizeof( mbedtls_ssl_handshake_params ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6075 | |
| 6076 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6077 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 6078 | * processes datagrams and the fact that a datagram is allowed to have |
| 6079 | * several records in it, it is possible that the I/O buffers are not |
| 6080 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 6081 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 6082 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6083 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6084 | } |
| 6085 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6086 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6087 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6088 | if( session == NULL ) |
| 6089 | return; |
| 6090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6091 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 6092 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6093 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6094 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 6095 | #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] | 6096 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 6097 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 6098 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6099 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6100 | } |
| 6101 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 6102 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 6103 | |
| 6104 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6105 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 6106 | #else |
| 6107 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 6108 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6109 | |
| 6110 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 6111 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
| 6112 | #else |
| 6113 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u |
| 6114 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 6115 | |
| 6116 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6117 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 6118 | #else |
| 6119 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 6120 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6121 | |
| 6122 | #if defined(MBEDTLS_SSL_ALPN) |
| 6123 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 6124 | #else |
| 6125 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 6126 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6127 | |
| 6128 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 6129 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 6130 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 6131 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 6132 | |
| 6133 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 6134 | ( (uint32_t) ( \ |
| 6135 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 6136 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 6137 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 6138 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 6139 | 0u ) ) |
| 6140 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6141 | static unsigned char ssl_serialized_context_header[] = { |
| 6142 | MBEDTLS_VERSION_MAJOR, |
| 6143 | MBEDTLS_VERSION_MINOR, |
| 6144 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 6145 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 6146 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 6147 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 6148 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 6149 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6150 | }; |
| 6151 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6152 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6153 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 6154 | * |
| 6155 | * The format of the serialized data is: |
| 6156 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 6157 | * |
| 6158 | * // header |
| 6159 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6160 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 6161 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6162 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 6163 | * Note: When updating the format, remember to keep these |
| 6164 | * 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] | 6165 | * |
| 6166 | * // session sub-structure |
| 6167 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 6168 | * // transform sub-structure |
| 6169 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 6170 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 6171 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 6172 | * // fields from ssl_context |
| 6173 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 6174 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 6175 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 6176 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 6177 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 6178 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 6179 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 6180 | * |
| 6181 | * Note that many fields of the ssl_context or sub-structures are not |
| 6182 | * serialized, as they fall in one of the following categories: |
| 6183 | * |
| 6184 | * 1. forced value (eg in_left must be 0) |
| 6185 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 6186 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 6187 | * 4. value was temporary (eg content of input buffer) |
| 6188 | * 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] | 6189 | */ |
| 6190 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 6191 | unsigned char *buf, |
| 6192 | size_t buf_len, |
| 6193 | size_t *olen ) |
| 6194 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6195 | unsigned char *p = buf; |
| 6196 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6197 | size_t session_len; |
| 6198 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6199 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6200 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6201 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 6202 | * this function's documentation. |
| 6203 | * |
| 6204 | * These are due to assumptions/limitations in the implementation. Some of |
| 6205 | * them are likely to stay (no handshake in progress) some might go away |
| 6206 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6207 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6208 | /* The initial handshake must be over */ |
| 6209 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6210 | { |
| 6211 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6212 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6213 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6214 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6215 | { |
| 6216 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6217 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6218 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6219 | /* Double-check that sub-structures are indeed ready */ |
| 6220 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6221 | { |
| 6222 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6223 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6224 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6225 | /* There must be no pending incoming or outgoing data */ |
| 6226 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6227 | { |
| 6228 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6229 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6230 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6231 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6232 | { |
| 6233 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6234 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6235 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6236 | /* Protocol must be DLTS, not TLS */ |
| 6237 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6238 | { |
| 6239 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6240 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6241 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6242 | /* Version must be 1.2 */ |
| 6243 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6244 | { |
| 6245 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6246 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6247 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6248 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6249 | { |
| 6250 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6251 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6252 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6253 | /* We must be using an AEAD ciphersuite */ |
| 6254 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6255 | { |
| 6256 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6257 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6258 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6259 | /* Renegotiation must not be enabled */ |
| 6260 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6261 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6262 | { |
| 6263 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6264 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6265 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6266 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6267 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6268 | /* |
| 6269 | * Version and format identifier |
| 6270 | */ |
| 6271 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6272 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6273 | if( used <= buf_len ) |
| 6274 | { |
| 6275 | memcpy( p, ssl_serialized_context_header, |
| 6276 | sizeof( ssl_serialized_context_header ) ); |
| 6277 | p += sizeof( ssl_serialized_context_header ); |
| 6278 | } |
| 6279 | |
| 6280 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6281 | * Session (length + data) |
| 6282 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6283 | 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] | 6284 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 6285 | return( ret ); |
| 6286 | |
| 6287 | used += 4 + session_len; |
| 6288 | if( used <= buf_len ) |
| 6289 | { |
Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6290 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 6291 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6292 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6293 | ret = ssl_session_save( ssl->session, 1, |
| 6294 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6295 | if( ret != 0 ) |
| 6296 | return( ret ); |
| 6297 | |
| 6298 | p += session_len; |
| 6299 | } |
| 6300 | |
| 6301 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6302 | * Transform |
| 6303 | */ |
| 6304 | used += sizeof( ssl->transform->randbytes ); |
| 6305 | if( used <= buf_len ) |
| 6306 | { |
| 6307 | memcpy( p, ssl->transform->randbytes, |
| 6308 | sizeof( ssl->transform->randbytes ) ); |
| 6309 | p += sizeof( ssl->transform->randbytes ); |
| 6310 | } |
| 6311 | |
| 6312 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6313 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 6314 | if( used <= buf_len ) |
| 6315 | { |
| 6316 | *p++ = ssl->transform->in_cid_len; |
| 6317 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 6318 | p += ssl->transform->in_cid_len; |
| 6319 | |
| 6320 | *p++ = ssl->transform->out_cid_len; |
| 6321 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 6322 | p += ssl->transform->out_cid_len; |
| 6323 | } |
| 6324 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6325 | |
| 6326 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6327 | * Saved fields from top-level ssl_context structure |
| 6328 | */ |
| 6329 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 6330 | used += 4; |
| 6331 | if( used <= buf_len ) |
| 6332 | { |
Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6333 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 6334 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6335 | } |
| 6336 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 6337 | |
| 6338 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6339 | used += 16; |
| 6340 | if( used <= buf_len ) |
| 6341 | { |
Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6342 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 6343 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6344 | |
Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6345 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 6346 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6347 | } |
| 6348 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6349 | |
| 6350 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6351 | used += 1; |
| 6352 | if( used <= buf_len ) |
| 6353 | { |
| 6354 | *p++ = ssl->disable_datagram_packing; |
| 6355 | } |
| 6356 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6357 | |
| 6358 | used += 8; |
| 6359 | if( used <= buf_len ) |
| 6360 | { |
| 6361 | memcpy( p, ssl->cur_out_ctr, 8 ); |
| 6362 | p += 8; |
| 6363 | } |
| 6364 | |
| 6365 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6366 | used += 2; |
| 6367 | if( used <= buf_len ) |
| 6368 | { |
Joe Subbiani | 2f98d79 | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6369 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 6370 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6371 | } |
| 6372 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6373 | |
| 6374 | #if defined(MBEDTLS_SSL_ALPN) |
| 6375 | { |
| 6376 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 6377 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6378 | : 0; |
| 6379 | |
| 6380 | used += 1 + alpn_len; |
| 6381 | if( used <= buf_len ) |
| 6382 | { |
| 6383 | *p++ = alpn_len; |
| 6384 | |
| 6385 | if( ssl->alpn_chosen != NULL ) |
| 6386 | { |
| 6387 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 6388 | p += alpn_len; |
| 6389 | } |
| 6390 | } |
| 6391 | } |
| 6392 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6393 | |
| 6394 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6395 | * Done |
| 6396 | */ |
| 6397 | *olen = used; |
| 6398 | |
| 6399 | if( used > buf_len ) |
| 6400 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6401 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6402 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 6403 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6404 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6405 | } |
| 6406 | |
| 6407 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6408 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 6409 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 6410 | */ |
| 6411 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 6412 | const char *label, |
| 6413 | const unsigned char *random, size_t rlen, |
| 6414 | unsigned char *dstbuf, size_t dlen ); |
| 6415 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 6416 | { |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 6417 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6418 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6419 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 6420 | |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6421 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 6422 | return( tls_prf_sha384 ); |
Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 6423 | #else |
| 6424 | (void) ciphersuite_id; |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6425 | #endif |
| 6426 | return( tls_prf_sha256 ); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6427 | } |
| 6428 | |
| 6429 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6430 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6431 | * |
| 6432 | * This internal version is wrapped by a public function that cleans up in |
| 6433 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6434 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6435 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 6436 | const unsigned char *buf, |
| 6437 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6438 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6439 | const unsigned char *p = buf; |
| 6440 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6441 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6442 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6443 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6444 | /* |
| 6445 | * The context should have been freshly setup or reset. |
| 6446 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 6447 | * (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] | 6448 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 6449 | */ |
| 6450 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 6451 | ssl->session != NULL ) |
| 6452 | { |
| 6453 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6454 | } |
| 6455 | |
| 6456 | /* |
| 6457 | * We can't check that the config matches the initial one, but we can at |
| 6458 | * least check it matches the requirements for serializing. |
| 6459 | */ |
| 6460 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 6461 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 6462 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 6463 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || |
| 6464 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || |
| 6465 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6466 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6467 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6468 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6469 | { |
| 6470 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6471 | } |
| 6472 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6473 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 6474 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6475 | /* |
| 6476 | * Check version identifier |
| 6477 | */ |
| 6478 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 6479 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6480 | |
| 6481 | if( memcmp( p, ssl_serialized_context_header, |
| 6482 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 6483 | { |
| 6484 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 6485 | } |
| 6486 | p += sizeof( ssl_serialized_context_header ); |
| 6487 | |
| 6488 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6489 | * Session |
| 6490 | */ |
| 6491 | if( (size_t)( end - p ) < 4 ) |
| 6492 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6493 | |
| 6494 | session_len = ( (size_t) p[0] << 24 ) | |
| 6495 | ( (size_t) p[1] << 16 ) | |
| 6496 | ( (size_t) p[2] << 8 ) | |
| 6497 | ( (size_t) p[3] ); |
| 6498 | p += 4; |
| 6499 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6500 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6501 | * 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] | 6502 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6503 | ssl->session_in = ssl->session; |
| 6504 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6505 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6506 | |
| 6507 | if( (size_t)( end - p ) < session_len ) |
| 6508 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6509 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6510 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6511 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6512 | { |
| 6513 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6514 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6515 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6516 | |
| 6517 | p += session_len; |
| 6518 | |
| 6519 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6520 | * Transform |
| 6521 | */ |
| 6522 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6523 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6524 | * 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] | 6525 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6526 | ssl->transform_in = ssl->transform; |
| 6527 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6528 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6529 | |
| 6530 | /* Read random bytes and populate structure */ |
| 6531 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 6532 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6533 | |
| 6534 | ret = ssl_populate_transform( ssl->transform, |
| 6535 | ssl->session->ciphersuite, |
| 6536 | ssl->session->master, |
| 6537 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 6538 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6539 | ssl->session->encrypt_then_mac, |
| 6540 | #endif |
| 6541 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 6542 | ssl->session->trunc_hmac, |
| 6543 | #endif |
| 6544 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 6545 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 6546 | ssl->session->compression, |
| 6547 | #endif |
| 6548 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 6549 | p, /* currently pointing to randbytes */ |
| 6550 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 6551 | ssl->conf->endpoint, |
| 6552 | ssl ); |
| 6553 | if( ret != 0 ) |
| 6554 | return( ret ); |
| 6555 | |
| 6556 | p += sizeof( ssl->transform->randbytes ); |
| 6557 | |
| 6558 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6559 | /* Read connection IDs and store them */ |
| 6560 | if( (size_t)( end - p ) < 1 ) |
| 6561 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6562 | |
| 6563 | ssl->transform->in_cid_len = *p++; |
| 6564 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 6565 | 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] | 6566 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6567 | |
| 6568 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 6569 | p += ssl->transform->in_cid_len; |
| 6570 | |
| 6571 | ssl->transform->out_cid_len = *p++; |
| 6572 | |
| 6573 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 6574 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6575 | |
| 6576 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 6577 | p += ssl->transform->out_cid_len; |
| 6578 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6579 | |
| 6580 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6581 | * Saved fields from top-level ssl_context structure |
| 6582 | */ |
| 6583 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 6584 | if( (size_t)( end - p ) < 4 ) |
| 6585 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6586 | |
| 6587 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 6588 | ( (uint32_t) p[1] << 16 ) | |
| 6589 | ( (uint32_t) p[2] << 8 ) | |
| 6590 | ( (uint32_t) p[3] ); |
| 6591 | p += 4; |
| 6592 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 6593 | |
| 6594 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6595 | if( (size_t)( end - p ) < 16 ) |
| 6596 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6597 | |
| 6598 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 6599 | ( (uint64_t) p[1] << 48 ) | |
| 6600 | ( (uint64_t) p[2] << 40 ) | |
| 6601 | ( (uint64_t) p[3] << 32 ) | |
| 6602 | ( (uint64_t) p[4] << 24 ) | |
| 6603 | ( (uint64_t) p[5] << 16 ) | |
| 6604 | ( (uint64_t) p[6] << 8 ) | |
| 6605 | ( (uint64_t) p[7] ); |
| 6606 | p += 8; |
| 6607 | |
| 6608 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 6609 | ( (uint64_t) p[1] << 48 ) | |
| 6610 | ( (uint64_t) p[2] << 40 ) | |
| 6611 | ( (uint64_t) p[3] << 32 ) | |
| 6612 | ( (uint64_t) p[4] << 24 ) | |
| 6613 | ( (uint64_t) p[5] << 16 ) | |
| 6614 | ( (uint64_t) p[6] << 8 ) | |
| 6615 | ( (uint64_t) p[7] ); |
| 6616 | p += 8; |
| 6617 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6618 | |
| 6619 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6620 | if( (size_t)( end - p ) < 1 ) |
| 6621 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6622 | |
| 6623 | ssl->disable_datagram_packing = *p++; |
| 6624 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6625 | |
| 6626 | if( (size_t)( end - p ) < 8 ) |
| 6627 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6628 | |
| 6629 | memcpy( ssl->cur_out_ctr, p, 8 ); |
| 6630 | p += 8; |
| 6631 | |
| 6632 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6633 | if( (size_t)( end - p ) < 2 ) |
| 6634 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6635 | |
| 6636 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 6637 | p += 2; |
| 6638 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6639 | |
| 6640 | #if defined(MBEDTLS_SSL_ALPN) |
| 6641 | { |
| 6642 | uint8_t alpn_len; |
| 6643 | const char **cur; |
| 6644 | |
| 6645 | if( (size_t)( end - p ) < 1 ) |
| 6646 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6647 | |
| 6648 | alpn_len = *p++; |
| 6649 | |
| 6650 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 6651 | { |
| 6652 | /* alpn_chosen should point to an item in the configured list */ |
| 6653 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 6654 | { |
| 6655 | if( strlen( *cur ) == alpn_len && |
| 6656 | memcmp( p, cur, alpn_len ) == 0 ) |
| 6657 | { |
| 6658 | ssl->alpn_chosen = *cur; |
| 6659 | break; |
| 6660 | } |
| 6661 | } |
| 6662 | } |
| 6663 | |
| 6664 | /* can only happen on conf mismatch */ |
| 6665 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 6666 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6667 | |
| 6668 | p += alpn_len; |
| 6669 | } |
| 6670 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6671 | |
| 6672 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6673 | * Forced fields from top-level ssl_context structure |
| 6674 | * |
| 6675 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 6676 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 6677 | */ |
| 6678 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 6679 | |
| 6680 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6681 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 6682 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6683 | /* Adjust pointers for header fields of outgoing records to |
| 6684 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 6685 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6686 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6687 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6688 | ssl->in_epoch = 1; |
| 6689 | #endif |
| 6690 | |
| 6691 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 6692 | * 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] | 6693 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 6694 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6695 | if( ssl->handshake != NULL ) |
| 6696 | { |
| 6697 | mbedtls_ssl_handshake_free( ssl ); |
| 6698 | mbedtls_free( ssl->handshake ); |
| 6699 | ssl->handshake = NULL; |
| 6700 | } |
| 6701 | |
| 6702 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6703 | * Done - should have consumed entire buffer |
| 6704 | */ |
| 6705 | if( p != end ) |
| 6706 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6707 | |
| 6708 | return( 0 ); |
| 6709 | } |
| 6710 | |
| 6711 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6712 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6713 | */ |
| 6714 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 6715 | const unsigned char *buf, |
| 6716 | size_t len ) |
| 6717 | { |
| 6718 | int ret = ssl_context_load( context, buf, len ); |
| 6719 | |
| 6720 | if( ret != 0 ) |
| 6721 | mbedtls_ssl_free( context ); |
| 6722 | |
| 6723 | return( ret ); |
| 6724 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 6725 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6726 | |
| 6727 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6728 | * Free an SSL context |
| 6729 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6730 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6731 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6732 | if( ssl == NULL ) |
| 6733 | return; |
| 6734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6735 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6736 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6737 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6738 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6739 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6740 | size_t out_buf_len = ssl->out_buf_len; |
| 6741 | #else |
| 6742 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 6743 | #endif |
| 6744 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6745 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6746 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6747 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6748 | } |
| 6749 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6750 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6751 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6752 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6753 | size_t in_buf_len = ssl->in_buf_len; |
| 6754 | #else |
| 6755 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 6756 | #endif |
| 6757 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6758 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6759 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6760 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6761 | } |
| 6762 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6763 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 6764 | if( ssl->compress_buf != NULL ) |
| 6765 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6766 | mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6767 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 6768 | } |
| 6769 | #endif |
| 6770 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6771 | if( ssl->transform ) |
| 6772 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6773 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6774 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6775 | } |
| 6776 | |
| 6777 | if( ssl->handshake ) |
| 6778 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6779 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6780 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 6781 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6783 | mbedtls_free( ssl->handshake ); |
| 6784 | mbedtls_free( ssl->transform_negotiate ); |
| 6785 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6786 | } |
| 6787 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6788 | if( ssl->session ) |
| 6789 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6790 | mbedtls_ssl_session_free( ssl->session ); |
| 6791 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6792 | } |
| 6793 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 6794 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 6795 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6796 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6797 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6798 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6799 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 6800 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6801 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6802 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 6803 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 6804 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6805 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 6806 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 6807 | } |
| 6808 | #endif |
| 6809 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6810 | #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] | 6811 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 6812 | #endif |
| 6813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6814 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 6815 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 6816 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6817 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6820 | /* |
| 6821 | * Initialze mbedtls_ssl_config |
| 6822 | */ |
| 6823 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 6824 | { |
| 6825 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 6826 | } |
| 6827 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6828 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6829 | static int ssl_preset_default_hashes[] = { |
Gilles Peskine | c54010c | 2021-05-12 22:52:09 +0200 | [diff] [blame] | 6830 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6831 | MBEDTLS_MD_SHA512, |
Gilles Peskine | c54010c | 2021-05-12 22:52:09 +0200 | [diff] [blame] | 6832 | #endif |
| 6833 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6834 | MBEDTLS_MD_SHA384, |
| 6835 | #endif |
| 6836 | #if defined(MBEDTLS_SHA256_C) |
| 6837 | MBEDTLS_MD_SHA256, |
| 6838 | MBEDTLS_MD_SHA224, |
| 6839 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 6840 | #if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6841 | MBEDTLS_MD_SHA1, |
| 6842 | #endif |
| 6843 | MBEDTLS_MD_NONE |
| 6844 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 6845 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6846 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6847 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 6848 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 6849 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 6850 | 0 |
| 6851 | }; |
| 6852 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6853 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6854 | static int ssl_preset_suiteb_hashes[] = { |
| 6855 | MBEDTLS_MD_SHA256, |
| 6856 | MBEDTLS_MD_SHA384, |
| 6857 | MBEDTLS_MD_NONE |
| 6858 | }; |
| 6859 | #endif |
| 6860 | |
| 6861 | #if defined(MBEDTLS_ECP_C) |
| 6862 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6863 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6864 | MBEDTLS_ECP_DP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6865 | #endif |
| 6866 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6867 | MBEDTLS_ECP_DP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6868 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6869 | MBEDTLS_ECP_DP_NONE |
| 6870 | }; |
| 6871 | #endif |
| 6872 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6873 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 6874 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6875 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6876 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6877 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6878 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6879 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6880 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6881 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6882 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 6883 | /* Use the functions here so that they are covered in tests, |
| 6884 | * but otherwise access member directly for efficiency */ |
| 6885 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 6886 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6887 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6888 | /* |
| 6889 | * Things that are common to all presets |
| 6890 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6891 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6892 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6893 | { |
| 6894 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 6895 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 6896 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 6897 | #endif |
| 6898 | } |
| 6899 | #endif |
| 6900 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 6901 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6902 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 6903 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6904 | |
| 6905 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6906 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 6907 | #endif |
| 6908 | |
| 6909 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6910 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 6911 | #endif |
| 6912 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 6913 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 6914 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 6915 | #endif |
| 6916 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6917 | #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] | 6918 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 6919 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 6920 | #endif |
| 6921 | |
| 6922 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6923 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 6924 | #endif |
| 6925 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6926 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6927 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
| 6928 | #endif |
| 6929 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6930 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6931 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 6932 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 6933 | #endif |
| 6934 | |
| 6935 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6936 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 6937 | memset( conf->renego_period, 0x00, 2 ); |
| 6938 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6939 | #endif |
| 6940 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6941 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 6942 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6943 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 6944 | const unsigned char dhm_p[] = |
| 6945 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 6946 | const unsigned char dhm_g[] = |
| 6947 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 6948 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 6949 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 6950 | dhm_p, sizeof( dhm_p ), |
| 6951 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6952 | { |
| 6953 | return( ret ); |
| 6954 | } |
| 6955 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 6956 | #endif |
| 6957 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6958 | /* |
| 6959 | * Preset-specific defaults |
| 6960 | */ |
| 6961 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6962 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6963 | /* |
| 6964 | * NSA Suite B |
| 6965 | */ |
| 6966 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 6967 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6968 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 6969 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6970 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6971 | |
| 6972 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 6973 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 6974 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 6975 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 6976 | ssl_preset_suiteb_ciphersuites; |
| 6977 | |
| 6978 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6979 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6980 | #endif |
| 6981 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6982 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6983 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 6984 | #endif |
| 6985 | |
| 6986 | #if defined(MBEDTLS_ECP_C) |
| 6987 | conf->curve_list = ssl_preset_suiteb_curves; |
| 6988 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 6989 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6990 | |
| 6991 | /* |
| 6992 | * Default |
| 6993 | */ |
| 6994 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 6995 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 6996 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 6997 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 6998 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 6999 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 7000 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 7001 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 7002 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 7003 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 7004 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 7005 | |
| 7006 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7007 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7008 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 7009 | #endif |
| 7010 | |
| 7011 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 7012 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 7013 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 7014 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 7015 | mbedtls_ssl_list_ciphersuites(); |
| 7016 | |
| 7017 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7018 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 7019 | #endif |
| 7020 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7021 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 7022 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 7023 | #endif |
| 7024 | |
| 7025 | #if defined(MBEDTLS_ECP_C) |
| 7026 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 7027 | #endif |
| 7028 | |
| 7029 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 7030 | conf->dhm_min_bitlen = 1024; |
| 7031 | #endif |
| 7032 | } |
| 7033 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7034 | return( 0 ); |
| 7035 | } |
| 7036 | |
| 7037 | /* |
| 7038 | * Free mbedtls_ssl_config |
| 7039 | */ |
| 7040 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 7041 | { |
| 7042 | #if defined(MBEDTLS_DHM_C) |
| 7043 | mbedtls_mpi_free( &conf->dhm_P ); |
| 7044 | mbedtls_mpi_free( &conf->dhm_G ); |
| 7045 | #endif |
| 7046 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7047 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7048 | if( conf->psk != NULL ) |
| 7049 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7050 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7051 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 7052 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7053 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 7054 | } |
| 7055 | |
| 7056 | if( conf->psk_identity != NULL ) |
| 7057 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7058 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 7059 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 7060 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7061 | conf->psk_identity_len = 0; |
| 7062 | } |
| 7063 | #endif |
| 7064 | |
| 7065 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7066 | ssl_key_cert_free( conf->key_cert ); |
| 7067 | #endif |
| 7068 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7069 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7070 | } |
| 7071 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 7072 | #if defined(MBEDTLS_PK_C) && \ |
| 7073 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7074 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7075 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7076 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7077 | 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] | 7078 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7079 | #if defined(MBEDTLS_RSA_C) |
| 7080 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 7081 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7082 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7083 | #if defined(MBEDTLS_ECDSA_C) |
| 7084 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 7085 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7086 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7087 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7088 | } |
| 7089 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7090 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 7091 | { |
| 7092 | switch( type ) { |
| 7093 | case MBEDTLS_PK_RSA: |
| 7094 | return( MBEDTLS_SSL_SIG_RSA ); |
| 7095 | case MBEDTLS_PK_ECDSA: |
| 7096 | case MBEDTLS_PK_ECKEY: |
| 7097 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 7098 | default: |
| 7099 | return( MBEDTLS_SSL_SIG_ANON ); |
| 7100 | } |
| 7101 | } |
| 7102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7103 | 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] | 7104 | { |
| 7105 | switch( sig ) |
| 7106 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7107 | #if defined(MBEDTLS_RSA_C) |
| 7108 | case MBEDTLS_SSL_SIG_RSA: |
| 7109 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7110 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7111 | #if defined(MBEDTLS_ECDSA_C) |
| 7112 | case MBEDTLS_SSL_SIG_ECDSA: |
| 7113 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7114 | #endif |
| 7115 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7116 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7117 | } |
| 7118 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 7119 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7120 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7121 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7122 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7123 | |
| 7124 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 7125 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 7126 | mbedtls_pk_type_t sig_alg ) |
| 7127 | { |
| 7128 | switch( sig_alg ) |
| 7129 | { |
| 7130 | case MBEDTLS_PK_RSA: |
| 7131 | return( set->rsa ); |
| 7132 | case MBEDTLS_PK_ECDSA: |
| 7133 | return( set->ecdsa ); |
| 7134 | default: |
| 7135 | return( MBEDTLS_MD_NONE ); |
| 7136 | } |
| 7137 | } |
| 7138 | |
| 7139 | /* Add a signature-hash-pair to a signature-hash set */ |
| 7140 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 7141 | mbedtls_pk_type_t sig_alg, |
| 7142 | mbedtls_md_type_t md_alg ) |
| 7143 | { |
| 7144 | switch( sig_alg ) |
| 7145 | { |
| 7146 | case MBEDTLS_PK_RSA: |
| 7147 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 7148 | set->rsa = md_alg; |
| 7149 | break; |
| 7150 | |
| 7151 | case MBEDTLS_PK_ECDSA: |
| 7152 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 7153 | set->ecdsa = md_alg; |
| 7154 | break; |
| 7155 | |
| 7156 | default: |
| 7157 | break; |
| 7158 | } |
| 7159 | } |
| 7160 | |
| 7161 | /* Allow exactly one hash algorithm for each signature. */ |
| 7162 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 7163 | mbedtls_md_type_t md_alg ) |
| 7164 | { |
| 7165 | set->rsa = md_alg; |
| 7166 | set->ecdsa = md_alg; |
| 7167 | } |
| 7168 | |
| 7169 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7170 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7171 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 7172 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7173 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 7174 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7175 | 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] | 7176 | { |
| 7177 | switch( hash ) |
| 7178 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7179 | #if defined(MBEDTLS_MD5_C) |
| 7180 | case MBEDTLS_SSL_HASH_MD5: |
| 7181 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7182 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7183 | #if defined(MBEDTLS_SHA1_C) |
| 7184 | case MBEDTLS_SSL_HASH_SHA1: |
| 7185 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7186 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7187 | #if defined(MBEDTLS_SHA256_C) |
| 7188 | case MBEDTLS_SSL_HASH_SHA224: |
| 7189 | return( MBEDTLS_MD_SHA224 ); |
| 7190 | case MBEDTLS_SSL_HASH_SHA256: |
| 7191 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7192 | #endif |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 7193 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7194 | case MBEDTLS_SSL_HASH_SHA384: |
| 7195 | return( MBEDTLS_MD_SHA384 ); |
Gilles Peskine | c54010c | 2021-05-12 22:52:09 +0200 | [diff] [blame] | 7196 | #endif |
| 7197 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7198 | case MBEDTLS_SSL_HASH_SHA512: |
| 7199 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7200 | #endif |
| 7201 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7202 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7203 | } |
| 7204 | } |
| 7205 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7206 | /* |
| 7207 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 7208 | */ |
| 7209 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 7210 | { |
| 7211 | switch( md ) |
| 7212 | { |
| 7213 | #if defined(MBEDTLS_MD5_C) |
| 7214 | case MBEDTLS_MD_MD5: |
| 7215 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 7216 | #endif |
| 7217 | #if defined(MBEDTLS_SHA1_C) |
| 7218 | case MBEDTLS_MD_SHA1: |
| 7219 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 7220 | #endif |
| 7221 | #if defined(MBEDTLS_SHA256_C) |
| 7222 | case MBEDTLS_MD_SHA224: |
| 7223 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 7224 | case MBEDTLS_MD_SHA256: |
| 7225 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 7226 | #endif |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 7227 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7228 | case MBEDTLS_MD_SHA384: |
| 7229 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Gilles Peskine | c54010c | 2021-05-12 22:52:09 +0200 | [diff] [blame] | 7230 | #endif |
| 7231 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7232 | case MBEDTLS_MD_SHA512: |
| 7233 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 7234 | #endif |
| 7235 | default: |
| 7236 | return( MBEDTLS_SSL_HASH_NONE ); |
| 7237 | } |
| 7238 | } |
| 7239 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7240 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7241 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7242 | * 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] | 7243 | * 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] | 7244 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7245 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7246 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7247 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7248 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7249 | if( ssl->conf->curve_list == NULL ) |
| 7250 | return( -1 ); |
| 7251 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7252 | for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7253 | if( *gid == grp_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7254 | return( 0 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7255 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7256 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7257 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7258 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7259 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7260 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7261 | /* |
| 7262 | * Check if a hash proposed by the peer is in our list. |
| 7263 | * Return 0 if we're willing to use it, -1 otherwise. |
| 7264 | */ |
| 7265 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 7266 | mbedtls_md_type_t md ) |
| 7267 | { |
| 7268 | const int *cur; |
| 7269 | |
| 7270 | if( ssl->conf->sig_hashes == NULL ) |
| 7271 | return( -1 ); |
| 7272 | |
| 7273 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 7274 | if( *cur == (int) md ) |
| 7275 | return( 0 ); |
| 7276 | |
| 7277 | return( -1 ); |
| 7278 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7279 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7281 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7282 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 7283 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7284 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7285 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7286 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7287 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7288 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7289 | int usage = 0; |
| 7290 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7291 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7292 | const char *ext_oid; |
| 7293 | size_t ext_len; |
| 7294 | #endif |
| 7295 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7296 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 7297 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7298 | ((void) cert); |
| 7299 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7300 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7301 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7303 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 7304 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7305 | { |
| 7306 | /* Server part of the key exchange */ |
| 7307 | switch( ciphersuite->key_exchange ) |
| 7308 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7309 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 7310 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7311 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7312 | break; |
| 7313 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7314 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 7315 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 7316 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 7317 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7318 | break; |
| 7319 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7320 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 7321 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7322 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7323 | break; |
| 7324 | |
| 7325 | /* 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] | 7326 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 7327 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 7328 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 7329 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 7330 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7331 | usage = 0; |
| 7332 | } |
| 7333 | } |
| 7334 | else |
| 7335 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7336 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 7337 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7338 | } |
| 7339 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7340 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7341 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7342 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7343 | ret = -1; |
| 7344 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7345 | #else |
| 7346 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7347 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7349 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 7350 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7351 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7352 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 7353 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7354 | } |
| 7355 | else |
| 7356 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7357 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 7358 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7359 | } |
| 7360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7361 | 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] | 7362 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7363 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7364 | ret = -1; |
| 7365 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7366 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7367 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7368 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7369 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7370 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 7371 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7372 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 7373 | { |
| 7374 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7375 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 7376 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 7377 | |
| 7378 | switch( md ) |
| 7379 | { |
| 7380 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7381 | #if defined(MBEDTLS_MD5_C) |
| 7382 | case MBEDTLS_SSL_HASH_MD5: |
Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 7383 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7384 | #endif |
| 7385 | #if defined(MBEDTLS_SHA1_C) |
| 7386 | case MBEDTLS_SSL_HASH_SHA1: |
| 7387 | ssl->handshake->calc_verify = ssl_calc_verify_tls; |
| 7388 | break; |
| 7389 | #endif |
| 7390 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Gilles Peskine | d2d5937 | 2021-05-12 22:43:27 +0200 | [diff] [blame] | 7391 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7392 | case MBEDTLS_SSL_HASH_SHA384: |
| 7393 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 7394 | break; |
| 7395 | #endif |
| 7396 | #if defined(MBEDTLS_SHA256_C) |
| 7397 | case MBEDTLS_SSL_HASH_SHA256: |
| 7398 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7399 | break; |
| 7400 | #endif |
| 7401 | default: |
| 7402 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 7403 | } |
| 7404 | |
| 7405 | return 0; |
| 7406 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7407 | (void) ssl; |
| 7408 | (void) md; |
| 7409 | |
| 7410 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 7411 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7412 | } |
| 7413 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7414 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7415 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7416 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 7417 | unsigned char *output, |
| 7418 | unsigned char *data, size_t data_len ) |
| 7419 | { |
| 7420 | int ret = 0; |
| 7421 | mbedtls_md5_context mbedtls_md5; |
| 7422 | mbedtls_sha1_context mbedtls_sha1; |
| 7423 | |
| 7424 | mbedtls_md5_init( &mbedtls_md5 ); |
| 7425 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 7426 | |
| 7427 | /* |
| 7428 | * digitally-signed struct { |
| 7429 | * opaque md5_hash[16]; |
| 7430 | * opaque sha_hash[20]; |
| 7431 | * }; |
| 7432 | * |
| 7433 | * md5_hash |
| 7434 | * MD5(ClientHello.random + ServerHello.random |
| 7435 | * + ServerParams); |
| 7436 | * sha_hash |
| 7437 | * SHA(ClientHello.random + ServerHello.random |
| 7438 | * + ServerParams); |
| 7439 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7440 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7441 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7442 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7443 | goto exit; |
| 7444 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7445 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7446 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7447 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7448 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7449 | goto exit; |
| 7450 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7451 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7452 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7453 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7454 | goto exit; |
| 7455 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7456 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7457 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7458 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7459 | goto exit; |
| 7460 | } |
| 7461 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7462 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7463 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7464 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7465 | goto exit; |
| 7466 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7467 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7468 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7469 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7470 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7471 | goto exit; |
| 7472 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7473 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7474 | data_len ) ) != 0 ) |
| 7475 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7476 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7477 | goto exit; |
| 7478 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7479 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7480 | output + 16 ) ) != 0 ) |
| 7481 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7482 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7483 | goto exit; |
| 7484 | } |
| 7485 | |
| 7486 | exit: |
| 7487 | mbedtls_md5_free( &mbedtls_md5 ); |
| 7488 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 7489 | |
| 7490 | if( ret != 0 ) |
| 7491 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7492 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7493 | |
| 7494 | return( ret ); |
| 7495 | |
| 7496 | } |
| 7497 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 7498 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 7499 | |
| 7500 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 7501 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7502 | |
| 7503 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7504 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7505 | unsigned char *hash, size_t *hashlen, |
| 7506 | unsigned char *data, size_t data_len, |
| 7507 | mbedtls_md_type_t md_alg ) |
| 7508 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7509 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7510 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7511 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 7512 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7513 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7514 | |
| 7515 | if( ( status = psa_hash_setup( &hash_operation, |
| 7516 | hash_alg ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7517 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7518 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7519 | goto exit; |
| 7520 | } |
| 7521 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7522 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 7523 | 64 ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7524 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7525 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7526 | goto exit; |
| 7527 | } |
| 7528 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7529 | if( ( status = psa_hash_update( &hash_operation, |
| 7530 | data, data_len ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7531 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7532 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7533 | goto exit; |
| 7534 | } |
| 7535 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7536 | if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, |
| 7537 | hashlen ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7538 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7539 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7540 | goto exit; |
| 7541 | } |
| 7542 | |
| 7543 | exit: |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7544 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7545 | { |
| 7546 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7547 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7548 | switch( status ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7549 | { |
| 7550 | case PSA_ERROR_NOT_SUPPORTED: |
| 7551 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7552 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7553 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 7554 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 7555 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 7556 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 7557 | default: |
| 7558 | return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED ); |
| 7559 | } |
| 7560 | } |
| 7561 | return( 0 ); |
| 7562 | } |
| 7563 | |
| 7564 | #else |
| 7565 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7566 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7567 | unsigned char *hash, size_t *hashlen, |
| 7568 | unsigned char *data, size_t data_len, |
| 7569 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7570 | { |
| 7571 | int ret = 0; |
| 7572 | mbedtls_md_context_t ctx; |
| 7573 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7574 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7575 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7576 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7577 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7578 | mbedtls_md_init( &ctx ); |
| 7579 | |
| 7580 | /* |
| 7581 | * digitally-signed struct { |
| 7582 | * opaque client_random[32]; |
| 7583 | * opaque server_random[32]; |
| 7584 | * ServerDHParams params; |
| 7585 | * }; |
| 7586 | */ |
| 7587 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7588 | { |
| 7589 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7590 | goto exit; |
| 7591 | } |
| 7592 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7593 | { |
| 7594 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7595 | goto exit; |
| 7596 | } |
| 7597 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7598 | { |
| 7599 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7600 | goto exit; |
| 7601 | } |
| 7602 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7603 | { |
| 7604 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7605 | goto exit; |
| 7606 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7607 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7608 | { |
| 7609 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7610 | goto exit; |
| 7611 | } |
| 7612 | |
| 7613 | exit: |
| 7614 | mbedtls_md_free( &ctx ); |
| 7615 | |
| 7616 | if( ret != 0 ) |
| 7617 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7618 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7619 | |
| 7620 | return( ret ); |
| 7621 | } |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7622 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7623 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7624 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 7625 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7627 | #endif /* MBEDTLS_SSL_TLS_C */ |