| 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" | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 46 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 47 | #include <string.h> | 
|  | 48 |  | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 49 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 50 | #include "mbedtls/psa_util.h" | 
|  | 51 | #include "psa/crypto.h" | 
|  | 52 | #endif | 
|  | 53 |  | 
| Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 54 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/oid.h" | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 56 | #endif | 
|  | 57 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 58 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 59 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 60 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 61 | /* Top-level Connection ID API */ | 
|  | 62 |  | 
| Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 63 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, | 
|  | 64 | size_t len, | 
|  | 65 | int ignore_other_cid ) | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 66 | { | 
|  | 67 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) | 
|  | 68 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 69 |  | 
| Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 70 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && | 
|  | 71 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) | 
|  | 72 | { | 
|  | 73 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | conf->ignore_unexpected_cid = ignore_other_cid; | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 77 | conf->cid_len = len; | 
|  | 78 | return( 0 ); | 
|  | 79 | } | 
|  | 80 |  | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 81 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, | 
|  | 82 | int enable, | 
|  | 83 | unsigned char const *own_cid, | 
|  | 84 | size_t own_cid_len ) | 
|  | 85 | { | 
| Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 86 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 87 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 88 |  | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 89 | ssl->negotiate_cid = enable; | 
|  | 90 | if( enable == MBEDTLS_SSL_CID_DISABLED ) | 
|  | 91 | { | 
|  | 92 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); | 
|  | 93 | return( 0 ); | 
|  | 94 | } | 
|  | 95 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 96 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 97 |  | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 98 | if( own_cid_len != ssl->conf->cid_len ) | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 99 | { | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 100 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", | 
|  | 101 | (unsigned) own_cid_len, | 
|  | 102 | (unsigned) ssl->conf->cid_len ) ); | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 103 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | memcpy( ssl->own_cid, own_cid, own_cid_len ); | 
| Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 107 | /* Truncation is not an issue here because | 
|  | 108 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ | 
|  | 109 | ssl->own_cid_len = (uint8_t) own_cid_len; | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 110 |  | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 111 | return( 0 ); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, | 
|  | 115 | int *enabled, | 
|  | 116 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], | 
|  | 117 | size_t *peer_cid_len ) | 
|  | 118 | { | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 119 | *enabled = MBEDTLS_SSL_CID_DISABLED; | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 120 |  | 
| Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 121 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | 
|  | 122 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 123 | { | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 124 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 125 | } | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 126 |  | 
| Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 127 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions | 
|  | 128 | * were used, but client and server requested the empty CID. | 
|  | 129 | * This is indistinguishable from not using the CID extension | 
|  | 130 | * in the first place. */ | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 131 | if( ssl->transform_in->in_cid_len  == 0 && | 
|  | 132 | ssl->transform_in->out_cid_len == 0 ) | 
|  | 133 | { | 
|  | 134 | return( 0 ); | 
|  | 135 | } | 
|  | 136 |  | 
| Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 137 | if( peer_cid_len != NULL ) | 
|  | 138 | { | 
|  | 139 | *peer_cid_len = ssl->transform_in->out_cid_len; | 
|  | 140 | if( peer_cid != NULL ) | 
|  | 141 | { | 
|  | 142 | memcpy( peer_cid, ssl->transform_in->out_cid, | 
|  | 143 | ssl->transform_in->out_cid_len ); | 
|  | 144 | } | 
|  | 145 | } | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 146 |  | 
|  | 147 | *enabled = MBEDTLS_SSL_CID_ENABLED; | 
|  | 148 |  | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 149 | return( 0 ); | 
|  | 150 | } | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 151 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 152 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 154 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 156 | /* | 
|  | 157 | * Convert max_fragment_length codes to length. | 
|  | 158 | * RFC 6066 says: | 
|  | 159 | *    enum{ | 
|  | 160 | *        2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) | 
|  | 161 | *    } MaxFragmentLength; | 
|  | 162 | * and we add 0 -> extension unused | 
|  | 163 | */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 164 | static unsigned int ssl_mfl_code_to_length( int mfl ) | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 165 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 166 | switch( mfl ) | 
|  | 167 | { | 
|  | 168 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: | 
|  | 169 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); | 
|  | 170 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: | 
|  | 171 | return 512; | 
|  | 172 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: | 
|  | 173 | return 1024; | 
|  | 174 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: | 
|  | 175 | return 2048; | 
|  | 176 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: | 
|  | 177 | return 4096; | 
|  | 178 | default: | 
|  | 179 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); | 
|  | 180 | } | 
|  | 181 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 183 |  | 
| Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 184 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, | 
|  | 185 | const mbedtls_ssl_session *src ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 186 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | mbedtls_ssl_session_free( dst ); | 
|  | 188 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 189 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 191 |  | 
|  | 192 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 193 | if( src->peer_cert != NULL ) | 
|  | 194 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 195 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 196 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 197 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 198 | if( dst->peer_cert == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 199 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 200 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | mbedtls_x509_crt_init( dst->peer_cert ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 202 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | 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] | 204 | src->peer_cert->raw.len ) ) != 0 ) | 
| 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_free( dst->peer_cert ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 207 | dst->peer_cert = NULL; | 
|  | 208 | return( ret ); | 
|  | 209 | } | 
|  | 210 | } | 
| Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 211 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 212 | if( src->peer_cert_digest != NULL ) | 
|  | 213 | { | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 214 | dst->peer_cert_digest = | 
| Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 215 | mbedtls_calloc( 1, src->peer_cert_digest_len ); | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 216 | if( dst->peer_cert_digest == NULL ) | 
|  | 217 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 218 |  | 
|  | 219 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, | 
|  | 220 | src->peer_cert_digest_len ); | 
|  | 221 | dst->peer_cert_digest_type = src->peer_cert_digest_type; | 
| Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 222 | dst->peer_cert_digest_len = src->peer_cert_digest_len; | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 223 | } | 
|  | 224 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 225 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 227 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 228 | #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] | 229 | if( src->ticket != NULL ) | 
|  | 230 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 231 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 232 | if( dst->ticket == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 233 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 234 |  | 
|  | 235 | memcpy( dst->ticket, src->ticket, src->ticket_len ); | 
|  | 236 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 237 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 238 |  | 
|  | 239 | return( 0 ); | 
|  | 240 | } | 
|  | 241 |  | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 242 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 243 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) | 
|  | 244 | { | 
|  | 245 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); | 
|  | 246 | if( resized_buffer == NULL ) | 
|  | 247 | return -1; | 
|  | 248 |  | 
|  | 249 | /* We want to copy len_new bytes when downsizing the buffer, and | 
|  | 250 | * len_old bytes when upsizing, so we choose the smaller of two sizes, | 
|  | 251 | * to fit one buffer into another. Size checks, ensuring that no data is | 
|  | 252 | * lost, are done outside of this function. */ | 
|  | 253 | memcpy( resized_buffer, *buffer, | 
|  | 254 | ( len_new < *len_old ) ? len_new : *len_old ); | 
|  | 255 | mbedtls_platform_zeroize( *buffer, *len_old ); | 
|  | 256 | mbedtls_free( *buffer ); | 
|  | 257 |  | 
|  | 258 | *buffer = resized_buffer; | 
|  | 259 | *len_old = len_new; | 
|  | 260 |  | 
|  | 261 | return 0; | 
|  | 262 | } | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 263 |  | 
|  | 264 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, | 
| Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 265 | size_t in_buf_new_len, | 
|  | 266 | size_t out_buf_new_len ) | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 267 | { | 
|  | 268 | int modified = 0; | 
|  | 269 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; | 
|  | 270 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; | 
|  | 271 | if( ssl->in_buf != NULL ) | 
|  | 272 | { | 
|  | 273 | written_in = ssl->in_msg - ssl->in_buf; | 
|  | 274 | iv_offset_in = ssl->in_iv - ssl->in_buf; | 
|  | 275 | len_offset_in = ssl->in_len - ssl->in_buf; | 
|  | 276 | if( downsizing ? | 
|  | 277 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : | 
|  | 278 | ssl->in_buf_len < in_buf_new_len ) | 
|  | 279 | { | 
|  | 280 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) | 
|  | 281 | { | 
|  | 282 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); | 
|  | 283 | } | 
|  | 284 | else | 
|  | 285 | { | 
|  | 286 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %d", in_buf_new_len ) ); | 
|  | 287 | modified = 1; | 
|  | 288 | } | 
|  | 289 | } | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | if( ssl->out_buf != NULL ) | 
|  | 293 | { | 
|  | 294 | written_out = ssl->out_msg - ssl->out_buf; | 
|  | 295 | iv_offset_out = ssl->out_iv - ssl->out_buf; | 
|  | 296 | len_offset_out = ssl->out_len - ssl->out_buf; | 
|  | 297 | if( downsizing ? | 
|  | 298 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : | 
|  | 299 | ssl->out_buf_len < out_buf_new_len ) | 
|  | 300 | { | 
|  | 301 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) | 
|  | 302 | { | 
|  | 303 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); | 
|  | 304 | } | 
|  | 305 | else | 
|  | 306 | { | 
|  | 307 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %d", out_buf_new_len ) ); | 
|  | 308 | modified = 1; | 
|  | 309 | } | 
|  | 310 | } | 
|  | 311 | } | 
|  | 312 | if( modified ) | 
|  | 313 | { | 
|  | 314 | /* Update pointers here to avoid doing it twice. */ | 
|  | 315 | mbedtls_ssl_reset_in_out_pointers( ssl ); | 
|  | 316 | /* Fields below might not be properly updated with record | 
|  | 317 | * splitting or with CID, so they are manually updated here. */ | 
|  | 318 | ssl->out_msg = ssl->out_buf + written_out; | 
|  | 319 | ssl->out_len = ssl->out_buf + len_offset_out; | 
|  | 320 | ssl->out_iv = ssl->out_buf + iv_offset_out; | 
|  | 321 |  | 
|  | 322 | ssl->in_msg = ssl->in_buf + written_in; | 
|  | 323 | ssl->in_len = ssl->in_buf + len_offset_in; | 
|  | 324 | ssl->in_iv = ssl->in_buf + iv_offset_in; | 
|  | 325 | } | 
|  | 326 | } | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 327 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ | 
|  | 328 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 329 | /* | 
|  | 330 | * Key material generation | 
|  | 331 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 332 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 333 | static int ssl3_prf( const unsigned char *secret, size_t slen, | 
|  | 334 | const char *label, | 
|  | 335 | const unsigned char *random, size_t rlen, | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 336 | unsigned char *dstbuf, size_t dlen ) | 
|  | 337 | { | 
| Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 338 | int ret = 0; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 339 | size_t i; | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 340 | mbedtls_md5_context md5; | 
|  | 341 | mbedtls_sha1_context sha1; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 342 | unsigned char padding[16]; | 
|  | 343 | unsigned char sha1sum[20]; | 
|  | 344 | ((void)label); | 
|  | 345 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 346 | mbedtls_md5_init(  &md5  ); | 
|  | 347 | mbedtls_sha1_init( &sha1 ); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 348 |  | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 349 | /* | 
|  | 350 | *  SSLv3: | 
|  | 351 | *    block = | 
|  | 352 | *      MD5( secret + SHA1( 'A'    + secret + random ) ) + | 
|  | 353 | *      MD5( secret + SHA1( 'BB'   + secret + random ) ) + | 
|  | 354 | *      MD5( secret + SHA1( 'CCC'  + secret + random ) ) + | 
|  | 355 | *      ... | 
|  | 356 | */ | 
|  | 357 | for( i = 0; i < dlen / 16; i++ ) | 
|  | 358 | { | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 359 | memset( padding, (unsigned char) ('A' + i), 1 + i ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 360 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 361 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 362 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 363 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 364 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 365 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 366 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 367 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 368 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 369 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 370 | goto exit; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 371 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 372 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 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_md5_update_ret( &md5, secret, slen ) ) != 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_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 377 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 378 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 379 | goto exit; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 380 | } | 
|  | 381 |  | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 382 | exit: | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 383 | mbedtls_md5_free(  &md5  ); | 
|  | 384 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 385 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 386 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); | 
|  | 387 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 388 |  | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 389 | return( ret ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 390 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 392 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 394 | static int tls1_prf( const unsigned char *secret, size_t slen, | 
|  | 395 | const char *label, | 
|  | 396 | const unsigned char *random, size_t rlen, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 397 | unsigned char *dstbuf, size_t dlen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 398 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 399 | size_t nb, hs; | 
|  | 400 | size_t i, j, k; | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 401 | const unsigned char *S1, *S2; | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 402 | unsigned char *tmp; | 
|  | 403 | size_t tmp_len = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 404 | unsigned char h_i[20]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 405 | const mbedtls_md_info_t *md_info; | 
|  | 406 | mbedtls_md_context_t md_ctx; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 407 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 408 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | mbedtls_md_init( &md_ctx ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 410 |  | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 411 | tmp_len = 20 + strlen( label ) + rlen; | 
|  | 412 | tmp = mbedtls_calloc( 1, tmp_len ); | 
|  | 413 | if( tmp == NULL ) | 
|  | 414 | { | 
|  | 415 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 416 | goto exit; | 
|  | 417 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 418 |  | 
|  | 419 | hs = ( slen + 1 ) / 2; | 
|  | 420 | S1 = secret; | 
|  | 421 | S2 = secret + slen - hs; | 
|  | 422 |  | 
|  | 423 | nb = strlen( label ); | 
|  | 424 | memcpy( tmp + 20, label, nb ); | 
|  | 425 | memcpy( tmp + 20 + nb, random, rlen ); | 
|  | 426 | nb += rlen; | 
|  | 427 |  | 
|  | 428 | /* | 
|  | 429 | * First compute P_md5(secret,label+random)[0..dlen] | 
|  | 430 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 431 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 432 | { | 
|  | 433 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 434 | goto exit; | 
|  | 435 | } | 
| Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 436 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 437 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 438 | { | 
|  | 439 | goto exit; | 
|  | 440 | } | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 441 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 442 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); | 
|  | 443 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); | 
|  | 444 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 445 |  | 
|  | 446 | for( i = 0; i < dlen; i += 16 ) | 
|  | 447 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 448 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 449 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); | 
|  | 450 | mbedtls_md_hmac_finish( &md_ctx, h_i ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 451 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 452 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 453 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); | 
|  | 454 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 455 |  | 
|  | 456 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; | 
|  | 457 |  | 
|  | 458 | for( j = 0; j < k; j++ ) | 
|  | 459 | dstbuf[i + j]  = h_i[j]; | 
|  | 460 | } | 
|  | 461 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 462 | mbedtls_md_free( &md_ctx ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 463 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 464 | /* | 
|  | 465 | * XOR out with P_sha1(secret,label+random)[0..dlen] | 
|  | 466 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 467 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 468 | { | 
|  | 469 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 470 | goto exit; | 
|  | 471 | } | 
| Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 472 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 473 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 474 | { | 
|  | 475 | goto exit; | 
|  | 476 | } | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 477 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 478 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); | 
|  | 479 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); | 
|  | 480 | mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 481 |  | 
|  | 482 | for( i = 0; i < dlen; i += 20 ) | 
|  | 483 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 484 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 485 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); | 
|  | 486 | mbedtls_md_hmac_finish( &md_ctx, h_i ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 487 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 488 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 489 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); | 
|  | 490 | mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 491 |  | 
|  | 492 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; | 
|  | 493 |  | 
|  | 494 | for( j = 0; j < k; j++ ) | 
|  | 495 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); | 
|  | 496 | } | 
|  | 497 |  | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 498 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | mbedtls_md_free( &md_ctx ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 500 |  | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 501 | mbedtls_platform_zeroize( tmp, tmp_len ); | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 502 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 503 |  | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 504 | mbedtls_free( tmp ); | 
|  | 505 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 506 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 508 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 510 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 511 |  | 
|  | 512 | 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] | 513 | psa_key_id_t key, | 
| k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 514 | psa_algorithm_t alg, | 
|  | 515 | const unsigned char* seed, size_t seed_length, | 
|  | 516 | const unsigned char* label, size_t label_length, | 
|  | 517 | size_t capacity ) | 
|  | 518 | { | 
|  | 519 | psa_status_t status; | 
|  | 520 |  | 
|  | 521 | status = psa_key_derivation_setup( derivation, alg ); | 
|  | 522 | if( status != PSA_SUCCESS ) | 
|  | 523 | return( status ); | 
|  | 524 |  | 
|  | 525 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) | 
|  | 526 | { | 
|  | 527 | status = psa_key_derivation_input_bytes( derivation, | 
|  | 528 | PSA_KEY_DERIVATION_INPUT_SEED, | 
|  | 529 | seed, seed_length ); | 
|  | 530 | if( status != PSA_SUCCESS ) | 
|  | 531 | return( status ); | 
|  | 532 |  | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 533 | if( mbedtls_svc_key_id_is_null( key ) ) | 
| Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 534 | { | 
|  | 535 | status = psa_key_derivation_input_bytes( | 
|  | 536 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, | 
|  | 537 | NULL, 0 ); | 
|  | 538 | } | 
|  | 539 | else | 
|  | 540 | { | 
|  | 541 | status = psa_key_derivation_input_key( | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 542 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); | 
| Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 543 | } | 
| k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 544 | if( status != PSA_SUCCESS ) | 
|  | 545 | return( status ); | 
|  | 546 |  | 
|  | 547 | status = psa_key_derivation_input_bytes( derivation, | 
|  | 548 | PSA_KEY_DERIVATION_INPUT_LABEL, | 
|  | 549 | label, label_length ); | 
|  | 550 | if( status != PSA_SUCCESS ) | 
|  | 551 | return( status ); | 
|  | 552 | } | 
|  | 553 | else | 
|  | 554 | { | 
|  | 555 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | status = psa_key_derivation_set_capacity( derivation, capacity ); | 
|  | 559 | if( status != PSA_SUCCESS ) | 
|  | 560 | return( status ); | 
|  | 561 |  | 
|  | 562 | return( PSA_SUCCESS ); | 
|  | 563 | } | 
|  | 564 |  | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 565 | static int tls_prf_generic( mbedtls_md_type_t md_type, | 
|  | 566 | const unsigned char *secret, size_t slen, | 
|  | 567 | const char *label, | 
|  | 568 | const unsigned char *random, size_t rlen, | 
|  | 569 | unsigned char *dstbuf, size_t dlen ) | 
|  | 570 | { | 
|  | 571 | psa_status_t status; | 
|  | 572 | psa_algorithm_t alg; | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 573 | psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 574 | psa_key_derivation_operation_t derivation = | 
| Janos Follath | 8dee877 | 2019-07-30 12:53:32 +0100 | [diff] [blame] | 575 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 576 |  | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 577 | if( md_type == MBEDTLS_MD_SHA384 ) | 
|  | 578 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); | 
|  | 579 | else | 
|  | 580 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); | 
|  | 581 |  | 
| Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 582 | /* Normally a "secret" should be long enough to be impossible to | 
|  | 583 | * find by brute force, and in particular should not be empty. But | 
|  | 584 | * this PRF is also used to derive an IV, in particular in EAP-TLS, | 
|  | 585 | * and for this use case it makes sense to have a 0-length "secret". | 
|  | 586 | * 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] | 587 | * keep master_key=0, which setup_psa_key_derivation() understands | 
| Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 588 | * to mean a 0-length "secret" input. */ | 
|  | 589 | if( slen != 0 ) | 
|  | 590 | { | 
|  | 591 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); | 
|  | 592 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); | 
|  | 593 | psa_set_key_algorithm( &key_attributes, alg ); | 
|  | 594 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 595 |  | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 596 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); | 
| Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 597 | if( status != PSA_SUCCESS ) | 
|  | 598 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 599 | } | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 600 |  | 
| k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 601 | status = setup_psa_key_derivation( &derivation, | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 602 | master_key, alg, | 
| k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 603 | random, rlen, | 
|  | 604 | (unsigned char const *) label, | 
|  | 605 | (size_t) strlen( label ), | 
|  | 606 | dlen ); | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 607 | if( status != PSA_SUCCESS ) | 
|  | 608 | { | 
| Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 609 | psa_key_derivation_abort( &derivation ); | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 610 | psa_destroy_key( master_key ); | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 611 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 612 | } | 
|  | 613 |  | 
| Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 614 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 615 | if( status != PSA_SUCCESS ) | 
|  | 616 | { | 
| Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 617 | psa_key_derivation_abort( &derivation ); | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 618 | psa_destroy_key( master_key ); | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 619 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 620 | } | 
|  | 621 |  | 
| Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 622 | status = psa_key_derivation_abort( &derivation ); | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 623 | if( status != PSA_SUCCESS ) | 
| Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 624 | { | 
| 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 ); | 
| Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 627 | } | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 628 |  | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 629 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) | 
|  | 630 | status = psa_destroy_key( master_key ); | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 631 | if( status != PSA_SUCCESS ) | 
|  | 632 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 633 |  | 
| Andrzej Kurek | 3317126 | 2019-01-15 03:25:18 -0500 | [diff] [blame] | 634 | return( 0 ); | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 635 | } | 
|  | 636 |  | 
|  | 637 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 638 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 639 | 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] | 640 | const unsigned char *secret, size_t slen, | 
|  | 641 | const char *label, | 
|  | 642 | const unsigned char *random, size_t rlen, | 
|  | 643 | unsigned char *dstbuf, size_t dlen ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 644 | { | 
|  | 645 | size_t nb; | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 646 | size_t i, j, k, md_len; | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 647 | unsigned char *tmp; | 
|  | 648 | size_t tmp_len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; | 
|  | 650 | const mbedtls_md_info_t *md_info; | 
|  | 651 | mbedtls_md_context_t md_ctx; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 652 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 653 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | mbedtls_md_init( &md_ctx ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 655 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) | 
|  | 657 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 658 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 659 | md_len = mbedtls_md_get_size( md_info ); | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 660 |  | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 661 | tmp_len = md_len + strlen( label ) + rlen; | 
|  | 662 | tmp = mbedtls_calloc( 1, tmp_len ); | 
|  | 663 | if( tmp == NULL ) | 
|  | 664 | { | 
|  | 665 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 666 | goto exit; | 
|  | 667 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 668 |  | 
|  | 669 | nb = strlen( label ); | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 670 | memcpy( tmp + md_len, label, nb ); | 
|  | 671 | memcpy( tmp + md_len + nb, random, rlen ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 672 | nb += rlen; | 
|  | 673 |  | 
|  | 674 | /* | 
|  | 675 | * Compute P_<hash>(secret, label + random)[0..dlen] | 
|  | 676 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 678 | goto exit; | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 679 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); | 
|  | 681 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); | 
|  | 682 | mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
| Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 683 |  | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 684 | for( i = 0; i < dlen; i += md_len ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 685 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 687 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); | 
|  | 688 | mbedtls_md_hmac_finish( &md_ctx, h_i ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 689 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 690 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 691 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); | 
|  | 692 | mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 693 |  | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 694 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 695 |  | 
|  | 696 | for( j = 0; j < k; j++ ) | 
|  | 697 | dstbuf[i + j]  = h_i[j]; | 
|  | 698 | } | 
|  | 699 |  | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 700 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | mbedtls_md_free( &md_ctx ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 702 |  | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 703 | mbedtls_platform_zeroize( tmp, tmp_len ); | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 704 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 705 |  | 
| Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 706 | mbedtls_free( tmp ); | 
|  | 707 |  | 
|  | 708 | return( ret ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 709 | } | 
| Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 710 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 711 | #if defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 712 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, | 
|  | 713 | const char *label, | 
|  | 714 | const unsigned char *random, size_t rlen, | 
|  | 715 | unsigned char *dstbuf, size_t dlen ) | 
|  | 716 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 718 | label, random, rlen, dstbuf, dlen ) ); | 
|  | 719 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | #endif /* MBEDTLS_SHA256_C */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 721 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 722 | #if defined(MBEDTLS_SHA512_C) | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 723 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, | 
|  | 724 | const char *label, | 
|  | 725 | const unsigned char *random, size_t rlen, | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 726 | unsigned char *dstbuf, size_t dlen ) | 
|  | 727 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 729 | label, random, rlen, dstbuf, dlen ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 730 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 731 | #endif /* MBEDTLS_SHA512_C */ | 
|  | 732 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 733 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 734 | 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] | 735 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 736 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 737 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 738 | 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] | 739 | #endif | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 740 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 742 | 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] | 743 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 744 | #endif | 
|  | 745 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 746 | #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] | 747 | 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] | 748 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 749 | #endif | 
|  | 750 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 751 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 752 | #if defined(MBEDTLS_SHA256_C) | 
|  | 753 | 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] | 754 | 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] | 755 | 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] | 756 | #endif | 
| Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 757 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 758 | #if defined(MBEDTLS_SHA512_C) | 
|  | 759 | 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] | 760 | 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] | 761 | 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] | 762 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 763 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 764 |  | 
| Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 765 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ | 
| Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 766 | defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 767 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) | 
|  | 768 | { | 
|  | 769 | if( ssl->conf->f_psk != NULL ) | 
|  | 770 | { | 
|  | 771 | /* If we've used a callback to select the PSK, | 
|  | 772 | * the static configuration is irrelevant. */ | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 773 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) | 
| Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 774 | return( 1 ); | 
|  | 775 |  | 
|  | 776 | return( 0 ); | 
|  | 777 | } | 
|  | 778 |  | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 779 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) | 
| Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 780 | return( 1 ); | 
|  | 781 |  | 
|  | 782 | return( 0 ); | 
|  | 783 | } | 
|  | 784 | #endif /* MBEDTLS_USE_PSA_CRYPTO && | 
| Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 785 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
| Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 786 |  | 
| Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 787 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) | 
|  | 788 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) | 
|  | 789 | { | 
|  | 790 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 791 | if( tls_prf == ssl3_prf ) | 
|  | 792 | { | 
| Ron Eldor | 0810f0b | 2019-05-15 12:32:32 +0300 | [diff] [blame] | 793 | return( MBEDTLS_SSL_TLS_PRF_SSL3 ); | 
| Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 794 | } | 
|  | 795 | else | 
|  | 796 | #endif | 
|  | 797 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 798 | if( tls_prf == tls1_prf ) | 
|  | 799 | { | 
|  | 800 | return( MBEDTLS_SSL_TLS_PRF_TLS1 ); | 
|  | 801 | } | 
|  | 802 | else | 
|  | 803 | #endif | 
|  | 804 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 805 | #if defined(MBEDTLS_SHA512_C) | 
|  | 806 | if( tls_prf == tls_prf_sha384 ) | 
|  | 807 | { | 
|  | 808 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); | 
|  | 809 | } | 
|  | 810 | else | 
|  | 811 | #endif | 
|  | 812 | #if defined(MBEDTLS_SHA256_C) | 
|  | 813 | if( tls_prf == tls_prf_sha256 ) | 
|  | 814 | { | 
|  | 815 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); | 
|  | 816 | } | 
|  | 817 | else | 
|  | 818 | #endif | 
|  | 819 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 820 | return( MBEDTLS_SSL_TLS_PRF_NONE ); | 
|  | 821 | } | 
|  | 822 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ | 
|  | 823 |  | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 824 | int  mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, | 
|  | 825 | const unsigned char *secret, size_t slen, | 
|  | 826 | const char *label, | 
|  | 827 | const unsigned char *random, size_t rlen, | 
|  | 828 | unsigned char *dstbuf, size_t dlen ) | 
|  | 829 | { | 
|  | 830 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; | 
|  | 831 |  | 
|  | 832 | switch( prf ) | 
|  | 833 | { | 
|  | 834 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 835 | case MBEDTLS_SSL_TLS_PRF_SSL3: | 
|  | 836 | tls_prf = ssl3_prf; | 
|  | 837 | break; | 
| Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 838 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 839 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 840 | case MBEDTLS_SSL_TLS_PRF_TLS1: | 
|  | 841 | tls_prf = tls1_prf; | 
|  | 842 | break; | 
| Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 843 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 844 |  | 
|  | 845 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 846 | #if defined(MBEDTLS_SHA512_C) | 
|  | 847 | case MBEDTLS_SSL_TLS_PRF_SHA384: | 
|  | 848 | tls_prf = tls_prf_sha384; | 
|  | 849 | break; | 
| Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 850 | #endif /* MBEDTLS_SHA512_C */ | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 851 | #if defined(MBEDTLS_SHA256_C) | 
|  | 852 | case MBEDTLS_SSL_TLS_PRF_SHA256: | 
|  | 853 | tls_prf = tls_prf_sha256; | 
|  | 854 | break; | 
| Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 855 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 856 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 857 | default: | 
|  | 858 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 859 | } | 
|  | 860 |  | 
|  | 861 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); | 
|  | 862 | } | 
|  | 863 |  | 
| Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 864 | /* Type for the TLS PRF */ | 
|  | 865 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, | 
|  | 866 | const unsigned char *, size_t, | 
|  | 867 | unsigned char *, size_t); | 
|  | 868 |  | 
| Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 869 | /* | 
| Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 870 | * Populate a transform structure with session keys and all the other | 
|  | 871 | * necessary information. | 
| Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 872 | * | 
| Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 873 | * Parameters: | 
|  | 874 | * - [in/out]: transform: structure to populate | 
|  | 875 | *      [in] must be just initialised with mbedtls_ssl_transform_init() | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 876 | *      [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] | 877 | * - [in] ciphersuite | 
|  | 878 | * - [in] master | 
|  | 879 | * - [in] encrypt_then_mac | 
|  | 880 | * - [in] trunc_hmac | 
|  | 881 | * - [in] compression | 
| Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 882 | * - [in] tls_prf: pointer to PRF to use for key derivation | 
|  | 883 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 884 | * - [in] minor_ver: SSL/TLS minor version | 
|  | 885 | * - [in] endpoint: client or server | 
|  | 886 | * - [in] ssl: optionally used for: | 
| Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 887 | *        - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const) | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 888 | *        - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys | 
|  | 889 | *        - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg | 
| Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 890 | */ | 
| Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 891 | static int ssl_populate_transform( mbedtls_ssl_transform *transform, | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 892 | int ciphersuite, | 
|  | 893 | const unsigned char master[48], | 
| Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 894 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 895 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 896 | int encrypt_then_mac, | 
| Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 897 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 898 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 899 | int trunc_hmac, | 
| Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 900 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
|  | 901 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 902 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 903 | int compression, | 
|  | 904 | #endif | 
| Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 905 | ssl_tls_prf_t tls_prf, | 
|  | 906 | const unsigned char randbytes[64], | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 907 | int minor_ver, | 
|  | 908 | unsigned endpoint, | 
| Manuel Pégourié-Gonnard | 7ae6ed4 | 2020-03-13 11:28:19 +0100 | [diff] [blame] | 909 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 910 | const | 
|  | 911 | #endif | 
| Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 912 | mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 913 | { | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 914 | int ret = 0; | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 915 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 916 | int psa_fallthrough; | 
|  | 917 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 918 | unsigned char keyblk[256]; | 
|  | 919 | unsigned char *key1; | 
|  | 920 | unsigned char *key2; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 921 | unsigned char *mac_enc; | 
|  | 922 | unsigned char *mac_dec; | 
| sander-visser | 3888b03 | 2020-05-06 21:49:46 +0200 | [diff] [blame] | 923 | size_t mac_key_len = 0; | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 924 | size_t iv_copy_len; | 
| Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 925 | unsigned keylen; | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 926 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 927 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 928 | const mbedtls_md_info_t *md_info; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 929 |  | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 930 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \ | 
|  | 931 | !defined(MBEDTLS_SSL_EXPORT_KEYS) && \ | 
|  | 932 | !defined(MBEDTLS_DEBUG_C) | 
| Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 933 | 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] | 934 | (void) ssl; | 
|  | 935 | #endif | 
|  | 936 |  | 
| Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 937 | /* | 
|  | 938 | * Some data just needs copying into the structure | 
|  | 939 | */ | 
| Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 940 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ | 
|  | 941 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 942 | transform->encrypt_then_mac = encrypt_then_mac; | 
| Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 943 | #endif | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 944 | transform->minor_ver = minor_ver; | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 945 |  | 
| Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 946 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
|  | 947 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); | 
|  | 948 | #endif | 
|  | 949 |  | 
| Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 950 | /* | 
|  | 951 | * Get various info structures | 
|  | 952 | */ | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 953 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); | 
| Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 954 | if( ciphersuite_info == NULL ) | 
|  | 955 | { | 
|  | 956 | 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] | 957 | ciphersuite ) ); | 
| Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 958 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 959 | } | 
|  | 960 |  | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 961 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 962 | if( cipher_info == NULL ) | 
|  | 963 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 964 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 965 | ciphersuite_info->cipher ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 966 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 967 | } | 
|  | 968 |  | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 969 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 970 | if( md_info == NULL ) | 
|  | 971 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 972 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 973 | ciphersuite_info->mac ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 974 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 975 | } | 
|  | 976 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 977 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 978 | /* Copy own and peer's CID if the use of the CID | 
|  | 979 | * extension has been negotiated. */ | 
|  | 980 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) | 
|  | 981 | { | 
|  | 982 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); | 
| Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 983 |  | 
| Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 984 | transform->in_cid_len = ssl->own_cid_len; | 
| Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 985 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); | 
| Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 986 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, | 
| Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 987 | transform->in_cid_len ); | 
| Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 988 |  | 
|  | 989 | transform->out_cid_len = ssl->handshake->peer_cid_len; | 
|  | 990 | memcpy( transform->out_cid, ssl->handshake->peer_cid, | 
|  | 991 | ssl->handshake->peer_cid_len ); | 
|  | 992 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, | 
|  | 993 | transform->out_cid_len ); | 
| Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 994 | } | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 995 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 996 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 997 | /* | 
| Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 998 | * Compute key block using the PRF | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 999 | */ | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1000 | 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] | 1001 | if( ret != 0 ) | 
|  | 1002 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1003 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
| Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1004 | return( ret ); | 
|  | 1005 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1006 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1007 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", | 
| Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1008 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1009 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); | 
| Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1010 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1012 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1013 | /* | 
|  | 1014 | * Determine the appropriate key, IV and MAC length. | 
|  | 1015 | */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1016 |  | 
| Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1017 | keylen = cipher_info->key_bitlen / 8; | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1018 |  | 
| Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1019 | #if defined(MBEDTLS_GCM_C) ||                           \ | 
|  | 1020 | defined(MBEDTLS_CCM_C) ||                           \ | 
|  | 1021 | defined(MBEDTLS_CHACHAPOLY_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1022 | if( cipher_info->mode == MBEDTLS_MODE_GCM || | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1023 | cipher_info->mode == MBEDTLS_MODE_CCM || | 
|  | 1024 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1025 | { | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1026 | size_t explicit_ivlen; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1027 |  | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1028 | transform->maclen = 0; | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1029 | mac_key_len = 0; | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1030 | transform->taglen = | 
|  | 1031 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1032 |  | 
| Hanno Becker | 447558d | 2020-05-28 07:36:33 +0100 | [diff] [blame] | 1033 | /* All modes haves 96-bit IVs, but the length of the static parts vary | 
|  | 1034 | * with mode and version: | 
|  | 1035 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes | 
|  | 1036 | *   (to be concatenated with a dynamically chosen IV of 8 Bytes) | 
| Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 1037 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's | 
|  | 1038 | *   a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record | 
|  | 1039 | *   sequence number). | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1040 | */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1041 | transform->ivlen = 12; | 
| Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 1042 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) | 
|  | 1043 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) | 
|  | 1044 | { | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1045 | transform->fixed_ivlen = 12; | 
| Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 1046 | } | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1047 | else | 
| Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 1048 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ | 
|  | 1049 | { | 
|  | 1050 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) | 
|  | 1051 | transform->fixed_ivlen = 12; | 
|  | 1052 | else | 
|  | 1053 | transform->fixed_ivlen = 4; | 
|  | 1054 | } | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1055 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1056 | /* Minimum length of encrypted record */ | 
|  | 1057 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1058 | transform->minlen = explicit_ivlen + transform->taglen; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1059 | } | 
|  | 1060 | else | 
| Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1061 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ | 
|  | 1062 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
|  | 1063 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || | 
|  | 1064 | cipher_info->mode == MBEDTLS_MODE_CBC ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1065 | { | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1066 | /* Initialize HMAC contexts */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1067 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || | 
|  | 1068 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1069 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1070 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1071 | goto end; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1072 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1073 |  | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1074 | /* Get MAC length */ | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1075 | mac_key_len = mbedtls_md_get_size( md_info ); | 
|  | 1076 | transform->maclen = mac_key_len; | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1077 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1078 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1079 | /* | 
|  | 1080 | * If HMAC is to be truncated, we shall keep the leftmost bytes, | 
|  | 1081 | * (rfc 6066 page 13 or rfc 2104 section 4), | 
|  | 1082 | * so we only need to adjust the length here. | 
|  | 1083 | */ | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1084 | if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) | 
| Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1085 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1086 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; | 
| Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1087 |  | 
|  | 1088 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) | 
|  | 1089 | /* Fall back to old, non-compliant version of the truncated | 
| Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 1090 | * HMAC implementation which also truncates the key | 
|  | 1091 | * (Mbed TLS versions from 1.3 to 2.6.0) */ | 
| Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1092 | mac_key_len = transform->maclen; | 
|  | 1093 | #endif | 
|  | 1094 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1095 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1096 |  | 
|  | 1097 | /* IV length */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1098 | transform->ivlen = cipher_info->iv_size; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1099 |  | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1100 | /* Minimum length */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1101 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1102 | transform->minlen = transform->maclen; | 
|  | 1103 | else | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1104 | { | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1105 | /* | 
|  | 1106 | * GenericBlockCipher: | 
| Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1107 | * 1. if EtM is in use: one block plus MAC | 
|  | 1108 | *    otherwise: * first multiple of blocklen greater than maclen | 
|  | 1109 | * 2. IV except for SSL3 and TLS 1.0 | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1110 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1111 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1112 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) | 
| Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1113 | { | 
|  | 1114 | transform->minlen = transform->maclen | 
|  | 1115 | + cipher_info->block_size; | 
|  | 1116 | } | 
|  | 1117 | else | 
|  | 1118 | #endif | 
|  | 1119 | { | 
|  | 1120 | transform->minlen = transform->maclen | 
|  | 1121 | + cipher_info->block_size | 
|  | 1122 | - transform->maclen % cipher_info->block_size; | 
|  | 1123 | } | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1124 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1125 | #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] | 1126 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || | 
|  | 1127 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1128 | ; /* No need to adjust minlen */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1129 | else | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1130 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1131 | #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] | 1132 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || | 
|  | 1133 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1134 | { | 
|  | 1135 | transform->minlen += transform->ivlen; | 
|  | 1136 | } | 
|  | 1137 | else | 
|  | 1138 | #endif | 
|  | 1139 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1140 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1141 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 1142 | goto end; | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1143 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1144 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1145 | } | 
| Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1146 | else | 
|  | 1147 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
|  | 1148 | { | 
|  | 1149 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1150 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 1151 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1152 |  | 
| Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1153 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", | 
|  | 1154 | (unsigned) keylen, | 
|  | 1155 | (unsigned) transform->minlen, | 
|  | 1156 | (unsigned) transform->ivlen, | 
|  | 1157 | (unsigned) transform->maclen ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1158 |  | 
|  | 1159 | /* | 
|  | 1160 | * Finally setup the cipher contexts, IVs and MAC secrets. | 
|  | 1161 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1162 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1163 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1164 | { | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1165 | key1 = keyblk + mac_key_len * 2; | 
| Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1166 | key2 = keyblk + mac_key_len * 2 + keylen; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1167 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1168 | mac_enc = keyblk; | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1169 | mac_dec = keyblk + mac_key_len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1170 |  | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1171 | /* | 
|  | 1172 | * This is not used in TLS v1.1. | 
|  | 1173 | */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1174 | iv_copy_len = ( transform->fixed_ivlen ) ? | 
|  | 1175 | transform->fixed_ivlen : transform->ivlen; | 
| Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1176 | memcpy( transform->iv_enc, key2 + keylen,  iv_copy_len ); | 
|  | 1177 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1178 | iv_copy_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1179 | } | 
|  | 1180 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1181 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 1182 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1183 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1184 | { | 
| Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1185 | key1 = keyblk + mac_key_len * 2 + keylen; | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1186 | key2 = keyblk + mac_key_len * 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1187 |  | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1188 | mac_enc = keyblk + mac_key_len; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1189 | mac_dec = keyblk; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1190 |  | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1191 | /* | 
|  | 1192 | * This is not used in TLS v1.1. | 
|  | 1193 | */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1194 | iv_copy_len = ( transform->fixed_ivlen ) ? | 
|  | 1195 | transform->fixed_ivlen : transform->ivlen; | 
| Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1196 | memcpy( transform->iv_dec, key1 + keylen,  iv_copy_len ); | 
|  | 1197 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1198 | iv_copy_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1199 | } | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1200 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1201 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1202 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1203 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1204 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 1205 | goto end; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1206 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1207 |  | 
| Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1208 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1209 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1210 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1211 | { | 
| Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1212 | if( mac_key_len > sizeof( transform->mac_enc ) ) | 
| Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1213 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1214 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1215 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 1216 | goto end; | 
| Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1217 | } | 
|  | 1218 |  | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1219 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); | 
|  | 1220 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1221 | } | 
|  | 1222 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1223 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 1224 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 1225 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1226 | if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1227 | { | 
| Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1228 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. | 
|  | 1229 | For AEAD-based ciphersuites, there is nothing to do here. */ | 
|  | 1230 | if( mac_key_len != 0 ) | 
|  | 1231 | { | 
|  | 1232 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); | 
|  | 1233 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); | 
|  | 1234 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1235 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1236 | else | 
|  | 1237 | #endif | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1238 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1239 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1240 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 1241 | goto end; | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1242 | } | 
| Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1243 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1244 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1245 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 1246 | if( mbedtls_ssl_hw_record_init != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1247 | { | 
| sander-visser | 1abe8ee | 2020-05-06 21:27:14 +0200 | [diff] [blame] | 1248 | ret = 0; | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1249 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1250 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1251 |  | 
| Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1252 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, | 
| Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1253 | transform->iv_enc, transform->iv_dec, | 
|  | 1254 | iv_copy_len, | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1255 | mac_enc, mac_dec, | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1256 | mac_key_len ) ) != 0 ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1257 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1258 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1259 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
|  | 1260 | goto end; | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1261 | } | 
|  | 1262 | } | 
| Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1263 | #else | 
|  | 1264 | ((void) mac_dec); | 
|  | 1265 | ((void) mac_enc); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1267 |  | 
| Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1268 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) | 
|  | 1269 | if( ssl->conf->f_export_keys != NULL ) | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1270 | { | 
| Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1271 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1272 | master, keyblk, | 
| Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1273 | mac_key_len, keylen, | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1274 | iv_copy_len ); | 
|  | 1275 | } | 
| Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1276 |  | 
|  | 1277 | if( ssl->conf->f_export_keys_ext != NULL ) | 
|  | 1278 | { | 
|  | 1279 | 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] | 1280 | master, keyblk, | 
| Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1281 | mac_key_len, keylen, | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1282 | iv_copy_len, | 
| Jaeden Amero | fa63645 | 2019-09-12 10:47:37 +0100 | [diff] [blame] | 1283 | randbytes + 32, | 
|  | 1284 | randbytes, | 
| Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1285 | tls_prf_get_type( tls_prf ) ); | 
| Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1286 | } | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1287 | #endif | 
|  | 1288 |  | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1289 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1290 |  | 
|  | 1291 | /* Only use PSA-based ciphers for TLS-1.2. | 
|  | 1292 | * That's relevant at least for TLS-1.0, where | 
|  | 1293 | * we assume that mbedtls_cipher_crypt() updates | 
|  | 1294 | * the structure field for the IV, which the PSA-based | 
|  | 1295 | * implementation currently doesn't. */ | 
|  | 1296 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1297 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1298 | { | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1299 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc, | 
| Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1300 | cipher_info, transform->taglen ); | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1301 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) | 
|  | 1302 | { | 
|  | 1303 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1304 | goto end; | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1305 | } | 
|  | 1306 |  | 
|  | 1307 | if( ret == 0 ) | 
|  | 1308 | { | 
| Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1309 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) ); | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1310 | psa_fallthrough = 0; | 
|  | 1311 | } | 
|  | 1312 | else | 
|  | 1313 | { | 
|  | 1314 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) ); | 
|  | 1315 | psa_fallthrough = 1; | 
|  | 1316 | } | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1317 | } | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1318 | else | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1319 | psa_fallthrough = 1; | 
|  | 1320 | #else | 
|  | 1321 | psa_fallthrough = 1; | 
|  | 1322 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1323 |  | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1324 | if( psa_fallthrough == 1 ) | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1325 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1326 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1327 | cipher_info ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1328 | { | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1329 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1330 | goto end; | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1331 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1332 |  | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1333 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1334 | /* Only use PSA-based ciphers for TLS-1.2. | 
|  | 1335 | * That's relevant at least for TLS-1.0, where | 
|  | 1336 | * we assume that mbedtls_cipher_crypt() updates | 
|  | 1337 | * the structure field for the IV, which the PSA-based | 
|  | 1338 | * implementation currently doesn't. */ | 
|  | 1339 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1340 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1341 | { | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1342 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec, | 
| Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1343 | cipher_info, transform->taglen ); | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1344 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) | 
|  | 1345 | { | 
|  | 1346 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1347 | goto end; | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1348 | } | 
|  | 1349 |  | 
|  | 1350 | if( ret == 0 ) | 
|  | 1351 | { | 
| Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1352 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) ); | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1353 | psa_fallthrough = 0; | 
|  | 1354 | } | 
|  | 1355 | else | 
|  | 1356 | { | 
|  | 1357 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) ); | 
|  | 1358 | psa_fallthrough = 1; | 
|  | 1359 | } | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1360 | } | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1361 | else | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1362 | psa_fallthrough = 1; | 
|  | 1363 | #else | 
|  | 1364 | psa_fallthrough = 1; | 
|  | 1365 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1366 |  | 
| Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1367 | if( psa_fallthrough == 1 ) | 
| Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1368 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1369 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1370 | cipher_info ) ) != 0 ) | 
|  | 1371 | { | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1372 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1373 | goto end; | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1374 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1375 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1376 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, | 
| Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1377 | cipher_info->key_bitlen, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1378 | MBEDTLS_ENCRYPT ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1379 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1380 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1381 | goto end; | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1382 | } | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1383 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1384 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, | 
| Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1385 | cipher_info->key_bitlen, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1386 | MBEDTLS_DECRYPT ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1387 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1388 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1389 | goto end; | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1390 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1391 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1392 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
|  | 1393 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) | 
| 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 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, | 
|  | 1396 | MBEDTLS_PADDING_NONE ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1397 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1398 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1399 | goto end; | 
| Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1400 | } | 
| 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_dec, | 
|  | 1403 | MBEDTLS_PADDING_NONE ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +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 | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1407 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1408 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1409 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1410 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1411 |  | 
| Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1412 | /* Initialize Zlib contexts */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1413 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1414 | if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1415 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1416 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1417 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1418 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); | 
|  | 1419 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1420 |  | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1421 | if( deflateInit( &transform->ctx_deflate, | 
|  | 1422 | Z_DEFAULT_COMPRESSION )   != Z_OK || | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1423 | inflateInit( &transform->ctx_inflate ) != Z_OK ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1424 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1425 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1426 | ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED; | 
|  | 1427 | goto end; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1428 | } | 
|  | 1429 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1430 | #endif /* MBEDTLS_ZLIB_SUPPORT */ | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1431 |  | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1432 | end: | 
| Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 1433 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); | 
| Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1434 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1435 | } | 
|  | 1436 |  | 
| Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1437 | /* | 
| Manuel Pégourié-Gonnard | 47e33e1 | 2019-05-20 10:10:17 +0200 | [diff] [blame] | 1438 | * 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] | 1439 | * | 
|  | 1440 | * Inputs: | 
|  | 1441 | * - SSL/TLS minor version | 
|  | 1442 | * - hash associated with the ciphersuite (only used by TLS 1.2) | 
|  | 1443 | * | 
| Manuel Pégourié-Gonnard | 31d3ef1 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1444 | * Outputs: | 
| Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1445 | * - the tls_prf, calc_verify and calc_finished members of handshake structure | 
|  | 1446 | */ | 
|  | 1447 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, | 
|  | 1448 | int minor_ver, | 
|  | 1449 | mbedtls_md_type_t hash ) | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1450 | { | 
| Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1451 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) | 
|  | 1452 | (void) hash; | 
|  | 1453 | #endif | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1454 |  | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1455 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1456 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1457 | { | 
|  | 1458 | handshake->tls_prf = ssl3_prf; | 
|  | 1459 | handshake->calc_verify = ssl_calc_verify_ssl; | 
|  | 1460 | handshake->calc_finished = ssl_calc_finished_ssl; | 
|  | 1461 | } | 
|  | 1462 | else | 
|  | 1463 | #endif | 
|  | 1464 | #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] | 1465 | if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1466 | { | 
|  | 1467 | handshake->tls_prf = tls1_prf; | 
|  | 1468 | handshake->calc_verify = ssl_calc_verify_tls; | 
|  | 1469 | handshake->calc_finished = ssl_calc_finished_tls; | 
|  | 1470 | } | 
|  | 1471 | else | 
|  | 1472 | #endif | 
|  | 1473 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1474 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1475 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && | 
|  | 1476 | hash == MBEDTLS_MD_SHA384 ) | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1477 | { | 
|  | 1478 | handshake->tls_prf = tls_prf_sha384; | 
|  | 1479 | handshake->calc_verify = ssl_calc_verify_tls_sha384; | 
|  | 1480 | handshake->calc_finished = ssl_calc_finished_tls_sha384; | 
|  | 1481 | } | 
|  | 1482 | else | 
|  | 1483 | #endif | 
|  | 1484 | #if defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1485 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1486 | { | 
|  | 1487 | handshake->tls_prf = tls_prf_sha256; | 
|  | 1488 | handshake->calc_verify = ssl_calc_verify_tls_sha256; | 
|  | 1489 | handshake->calc_finished = ssl_calc_finished_tls_sha256; | 
|  | 1490 | } | 
|  | 1491 | else | 
|  | 1492 | #endif | 
|  | 1493 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 1494 | { | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1495 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 1496 | } | 
|  | 1497 |  | 
|  | 1498 | return( 0 ); | 
|  | 1499 | } | 
|  | 1500 |  | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1501 | /* | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1502 | * Compute master secret if needed | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1503 | * | 
|  | 1504 | * Parameters: | 
|  | 1505 | * [in/out] handshake | 
|  | 1506 | *          [in] resume, premaster, extended_ms, calc_verify, tls_prf | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1507 | *               (PSA-PSK) ciphersuite_info, psk_opaque | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1508 | *          [out] premaster (cleared) | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1509 | * [out] master | 
|  | 1510 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK | 
|  | 1511 | *      debug: conf->f_dbg, conf->p_dbg | 
|  | 1512 | *      EMS: passed to calc_verify (debug + (SSL3) session_negotiate) | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1513 | *      PSA-PSA: minor_ver, conf | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1514 | */ | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1515 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1516 | unsigned char *master, | 
| Manuel Pégourié-Gonnard | 0d56aaa | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1517 | const mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1518 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1519 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1520 |  | 
|  | 1521 | /* cf. RFC 5246, Section 8.1: | 
|  | 1522 | * "The master secret is always exactly 48 bytes in length." */ | 
|  | 1523 | size_t const master_secret_len = 48; | 
|  | 1524 |  | 
|  | 1525 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 1526 | unsigned char session_hash[48]; | 
|  | 1527 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
|  | 1528 |  | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1529 | /* The label for the KDF used for key expansion. | 
|  | 1530 | * This is either "master secret" or "extended master secret" | 
|  | 1531 | * depending on whether the Extended Master Secret extension | 
|  | 1532 | * is used. */ | 
|  | 1533 | char const *lbl = "master secret"; | 
|  | 1534 |  | 
|  | 1535 | /* The salt for the KDF used for key expansion. | 
|  | 1536 | * - If the Extended Master Secret extension is not used, | 
|  | 1537 | *   this is ClientHello.Random + ServerHello.Random | 
|  | 1538 | *   (see Sect. 8.1 in RFC 5246). | 
|  | 1539 | * - If the Extended Master Secret extension is used, | 
|  | 1540 | *   this is the transcript of the handshake so far. | 
|  | 1541 | *   (see Sect. 4 in RFC 7627). */ | 
|  | 1542 | unsigned char const *salt = handshake->randbytes; | 
|  | 1543 | size_t salt_len = 64; | 
|  | 1544 |  | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1545 | #if !defined(MBEDTLS_DEBUG_C) &&                    \ | 
|  | 1546 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ | 
|  | 1547 | !(defined(MBEDTLS_USE_PSA_CRYPTO) &&            \ | 
|  | 1548 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) | 
| Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1549 | 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] | 1550 | (void) ssl; | 
|  | 1551 | #endif | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1552 |  | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1553 | if( handshake->resume != 0 ) | 
|  | 1554 | { | 
|  | 1555 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1556 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1557 | } | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1558 |  | 
|  | 1559 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1560 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1561 | { | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1562 | lbl  = "extended master secret"; | 
|  | 1563 | salt = session_hash; | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1564 | handshake->calc_verify( ssl, session_hash, &salt_len ); | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1565 |  | 
| Manuel Pégourié-Gonnard | 8faa70e | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1566 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", | 
|  | 1567 | session_hash, salt_len ); | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1568 | } | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1569 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ | 
|  | 1570 |  | 
|  | 1571 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&          \ | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1572 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 1573 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1574 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1575 | ssl_use_opaque_psk( ssl ) == 1 ) | 
|  | 1576 | { | 
|  | 1577 | /* Perform PSK-to-MS expansion in a single step. */ | 
|  | 1578 | psa_status_t status; | 
|  | 1579 | psa_algorithm_t alg; | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1580 | psa_key_id_t psk; | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1581 | psa_key_derivation_operation_t derivation = | 
|  | 1582 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1583 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1584 |  | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1585 | 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] | 1586 |  | 
| Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1587 | psk = mbedtls_ssl_get_opaque_psk( ssl ); | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1588 |  | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1589 | if( hash_alg == MBEDTLS_MD_SHA384 ) | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1590 | 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] | 1591 | else | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1592 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); | 
|  | 1593 |  | 
| k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 1594 | status = setup_psa_key_derivation( &derivation, psk, alg, | 
|  | 1595 | salt, salt_len, | 
|  | 1596 | (unsigned char const *) lbl, | 
|  | 1597 | (size_t) strlen( lbl ), | 
|  | 1598 | master_secret_len ); | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1599 | if( status != PSA_SUCCESS ) | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1600 | { | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1601 | psa_key_derivation_abort( &derivation ); | 
|  | 1602 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1603 | } | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1604 |  | 
|  | 1605 | status = psa_key_derivation_output_bytes( &derivation, | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1606 | master, | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1607 | master_secret_len ); | 
|  | 1608 | if( status != PSA_SUCCESS ) | 
|  | 1609 | { | 
|  | 1610 | psa_key_derivation_abort( &derivation ); | 
|  | 1611 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 1612 | } | 
|  | 1613 |  | 
|  | 1614 | status = psa_key_derivation_abort( &derivation ); | 
|  | 1615 | if( status != PSA_SUCCESS ) | 
|  | 1616 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
|  | 1617 | } | 
|  | 1618 | else | 
|  | 1619 | #endif | 
|  | 1620 | { | 
|  | 1621 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, | 
|  | 1622 | lbl, salt, salt_len, | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1623 | master, | 
| Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1624 | master_secret_len ); | 
|  | 1625 | if( ret != 0 ) | 
|  | 1626 | { | 
|  | 1627 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
|  | 1628 | return( ret ); | 
|  | 1629 | } | 
|  | 1630 |  | 
|  | 1631 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", | 
|  | 1632 | handshake->premaster, | 
|  | 1633 | handshake->pmslen ); | 
|  | 1634 |  | 
|  | 1635 | mbedtls_platform_zeroize( handshake->premaster, | 
|  | 1636 | sizeof(handshake->premaster) ); | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1637 | } | 
|  | 1638 |  | 
|  | 1639 | return( 0 ); | 
|  | 1640 | } | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1641 |  | 
| Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1642 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) | 
|  | 1643 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1644 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1645 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = | 
|  | 1646 | ssl->handshake->ciphersuite_info; | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1647 |  | 
| Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1648 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); | 
|  | 1649 |  | 
|  | 1650 | /* Set PRF, calc_verify and calc_finished function pointers */ | 
| Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1651 | ret = ssl_set_handshake_prfs( ssl->handshake, | 
|  | 1652 | ssl->minor_ver, | 
|  | 1653 | ciphersuite_info->mac ); | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1654 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1655 | { | 
|  | 1656 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1657 | return( ret ); | 
| Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1658 | } | 
| Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1659 |  | 
| Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1660 | /* Compute master secret if needed */ | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1661 | ret = ssl_compute_master( ssl->handshake, | 
| Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1662 | ssl->session_negotiate->master, | 
|  | 1663 | ssl ); | 
| Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1664 | if( ret != 0 ) | 
|  | 1665 | { | 
|  | 1666 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); | 
|  | 1667 | return( ret ); | 
|  | 1668 | } | 
|  | 1669 |  | 
| Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1670 | /* Swap the client and server random values: | 
|  | 1671 | * - MS derivation wanted client+server (RFC 5246 8.1) | 
|  | 1672 | * - key derivation wants server+client (RFC 5246 6.3) */ | 
|  | 1673 | { | 
|  | 1674 | unsigned char tmp[64]; | 
|  | 1675 | memcpy( tmp, ssl->handshake->randbytes, 64 ); | 
|  | 1676 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); | 
|  | 1677 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); | 
|  | 1678 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
|  | 1679 | } | 
|  | 1680 |  | 
|  | 1681 | /* Populate transform structure */ | 
| Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1682 | ret = ssl_populate_transform( ssl->transform_negotiate, | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1683 | ssl->session_negotiate->ciphersuite, | 
|  | 1684 | ssl->session_negotiate->master, | 
| Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1685 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1686 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 1687 | ssl->session_negotiate->encrypt_then_mac, | 
| Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1688 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1689 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 1690 | ssl->session_negotiate->trunc_hmac, | 
| Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1691 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
|  | 1692 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
| Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1693 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 1694 | ssl->session_negotiate->compression, | 
|  | 1695 | #endif | 
| Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1696 | ssl->handshake->tls_prf, | 
|  | 1697 | ssl->handshake->randbytes, | 
| Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1698 | ssl->minor_ver, | 
|  | 1699 | ssl->conf->endpoint, | 
| Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1700 | ssl ); | 
| Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1701 | if( ret != 0 ) | 
|  | 1702 | { | 
|  | 1703 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); | 
|  | 1704 | return( ret ); | 
|  | 1705 | } | 
|  | 1706 |  | 
|  | 1707 | /* We no longer need Server/ClientHello.random values */ | 
|  | 1708 | mbedtls_platform_zeroize( ssl->handshake->randbytes, | 
|  | 1709 | sizeof( ssl->handshake->randbytes ) ); | 
|  | 1710 |  | 
| Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1711 | /* Allocate compression buffer */ | 
|  | 1712 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| jiblime | 9f25b8d | 2019-12-18 21:40:01 -0800 | [diff] [blame] | 1713 | if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE && | 
| Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1714 | ssl->compress_buf == NULL ) | 
|  | 1715 | { | 
|  | 1716 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); | 
|  | 1717 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); | 
|  | 1718 | if( ssl->compress_buf == NULL ) | 
|  | 1719 | { | 
|  | 1720 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | 
| Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1721 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); | 
| Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1722 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 1723 | } | 
|  | 1724 | } | 
|  | 1725 | #endif | 
|  | 1726 |  | 
| Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1727 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); | 
|  | 1728 |  | 
|  | 1729 | return( 0 ); | 
| Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1730 | } | 
|  | 1731 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1732 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1733 | void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, | 
| Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1734 | unsigned char *hash, | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1735 | size_t *hlen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1736 | { | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1737 | mbedtls_md5_context md5; | 
|  | 1738 | mbedtls_sha1_context sha1; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1739 | unsigned char pad_1[48]; | 
|  | 1740 | unsigned char pad_2[48]; | 
|  | 1741 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1742 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1743 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1744 | mbedtls_md5_init( &md5 ); | 
|  | 1745 | mbedtls_sha1_init( &sha1 ); | 
|  | 1746 |  | 
|  | 1747 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 1748 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1749 |  | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1750 | memset( pad_1, 0x36, 48 ); | 
|  | 1751 | memset( pad_2, 0x5C, 48 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1752 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1753 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); | 
|  | 1754 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); | 
|  | 1755 | mbedtls_md5_finish_ret( &md5, hash ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1756 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1757 | mbedtls_md5_starts_ret( &md5 ); | 
|  | 1758 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); | 
|  | 1759 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); | 
|  | 1760 | mbedtls_md5_update_ret( &md5, hash,  16 ); | 
|  | 1761 | mbedtls_md5_finish_ret( &md5, hash ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1762 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1763 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); | 
|  | 1764 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); | 
|  | 1765 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1766 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1767 | mbedtls_sha1_starts_ret( &sha1 ); | 
|  | 1768 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); | 
|  | 1769 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); | 
|  | 1770 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); | 
|  | 1771 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1772 |  | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1773 | *hlen = 36; | 
|  | 1774 |  | 
|  | 1775 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1776 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1777 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1778 | mbedtls_md5_free(  &md5  ); | 
|  | 1779 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1780 |  | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1781 | return; | 
|  | 1782 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1783 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1784 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1785 | #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] | 1786 | void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, | 
| Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1787 | unsigned char *hash, | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1788 | size_t *hlen ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1789 | { | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1790 | mbedtls_md5_context md5; | 
|  | 1791 | mbedtls_sha1_context sha1; | 
| 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 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1794 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1795 | mbedtls_md5_init( &md5 ); | 
|  | 1796 | mbedtls_sha1_init( &sha1 ); | 
|  | 1797 |  | 
|  | 1798 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 1799 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1800 |  | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1801 | mbedtls_md5_finish_ret( &md5,  hash ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1802 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1803 |  | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1804 | *hlen = 36; | 
|  | 1805 |  | 
|  | 1806 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1807 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1808 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1809 | mbedtls_md5_free(  &md5  ); | 
|  | 1810 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1811 |  | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1812 | return; | 
|  | 1813 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1814 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1815 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1816 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1817 | #if defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1818 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, | 
| Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1819 | unsigned char *hash, | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1820 | size_t *hlen ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1821 | { | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1822 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 1823 | size_t hash_size; | 
|  | 1824 | psa_status_t status; | 
|  | 1825 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); | 
|  | 1826 |  | 
|  | 1827 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); | 
|  | 1828 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); | 
|  | 1829 | if( status != PSA_SUCCESS ) | 
|  | 1830 | { | 
|  | 1831 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 1832 | return; | 
|  | 1833 | } | 
|  | 1834 |  | 
|  | 1835 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); | 
|  | 1836 | if( status != PSA_SUCCESS ) | 
|  | 1837 | { | 
|  | 1838 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 1839 | return; | 
|  | 1840 | } | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1841 |  | 
|  | 1842 | *hlen = 32; | 
|  | 1843 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1844 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); | 
|  | 1845 | #else | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1846 | mbedtls_sha256_context sha256; | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1847 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1848 | mbedtls_sha256_init( &sha256 ); | 
|  | 1849 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1850 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1851 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1852 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1853 | mbedtls_sha256_finish_ret( &sha256, hash ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1854 |  | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1855 | *hlen = 32; | 
|  | 1856 |  | 
|  | 1857 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1858 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1859 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1860 | mbedtls_sha256_free( &sha256 ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1861 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1862 | return; | 
|  | 1863 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1864 | #endif /* MBEDTLS_SHA256_C */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1865 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1866 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1867 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, | 
| Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1868 | unsigned char *hash, | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1869 | size_t *hlen ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1870 | { | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1871 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 1872 | size_t hash_size; | 
|  | 1873 | psa_status_t status; | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1874 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1875 |  | 
|  | 1876 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1877 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1878 | if( status != PSA_SUCCESS ) | 
|  | 1879 | { | 
|  | 1880 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 1881 | return; | 
|  | 1882 | } | 
|  | 1883 |  | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1884 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1885 | if( status != PSA_SUCCESS ) | 
|  | 1886 | { | 
|  | 1887 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 1888 | return; | 
|  | 1889 | } | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1890 |  | 
|  | 1891 | *hlen = 48; | 
|  | 1892 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1893 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); | 
|  | 1894 | #else | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1895 | mbedtls_sha512_context sha512; | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1896 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1897 | mbedtls_sha512_init( &sha512 ); | 
|  | 1898 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1899 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1900 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1901 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1902 | mbedtls_sha512_finish_ret( &sha512, hash ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1903 |  | 
| Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1904 | *hlen = 48; | 
|  | 1905 |  | 
|  | 1906 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1907 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1908 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1909 | mbedtls_sha512_free( &sha512 ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1910 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1911 | return; | 
|  | 1912 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1913 | #endif /* MBEDTLS_SHA512_C */ | 
|  | 1914 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1915 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1916 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1917 | 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] | 1918 | { | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1919 | unsigned char *p = ssl->handshake->premaster; | 
|  | 1920 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); | 
| Guilhem Bryant | b5f04e4 | 2020-04-01 11:23:58 +0100 | [diff] [blame] | 1921 | const unsigned char *psk = NULL; | 
| Guilhem Bryant | 61b0fe6 | 2020-03-27 11:12:12 +0000 | [diff] [blame] | 1922 | size_t psk_len = 0; | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1923 |  | 
| Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1924 | if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) | 
|  | 1925 | == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1926 | { | 
| Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1927 | /* | 
|  | 1928 | * This should never happen because the existence of a PSK is always | 
|  | 1929 | * checked before calling this function | 
|  | 1930 | */ | 
| Guilhem Bryant | 82194c8 | 2020-03-26 17:04:31 +0000 | [diff] [blame] | 1931 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Guilhem Bryant | 0c9b195 | 2020-04-08 11:02:38 +0100 | [diff] [blame] | 1932 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1933 | } | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1934 |  | 
|  | 1935 | /* | 
|  | 1936 | * PMS = struct { | 
|  | 1937 | *     opaque other_secret<0..2^16-1>; | 
|  | 1938 | *     opaque psk<0..2^16-1>; | 
|  | 1939 | * }; | 
|  | 1940 | * with "other_secret" depending on the particular key exchange | 
|  | 1941 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1942 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 1943 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1944 | { | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1945 | if( end - p < 2 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1946 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1947 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1948 | *(p++) = (unsigned char)( psk_len >> 8 ); | 
|  | 1949 | *(p++) = (unsigned char)( psk_len      ); | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1950 |  | 
|  | 1951 | if( end < p || (size_t)( end - p ) < psk_len ) | 
|  | 1952 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1953 |  | 
|  | 1954 | memset( p, 0, psk_len ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1955 | p += psk_len; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1956 | } | 
|  | 1957 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1958 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
|  | 1959 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 1960 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1961 | { | 
|  | 1962 | /* | 
|  | 1963 | * other_secret already set by the ClientKeyExchange message, | 
|  | 1964 | * and is 48 bytes long | 
|  | 1965 | */ | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1966 | if( end - p < 2 ) | 
|  | 1967 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1968 |  | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1969 | *p++ = 0; | 
|  | 1970 | *p++ = 48; | 
|  | 1971 | p += 48; | 
|  | 1972 | } | 
|  | 1973 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1974 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
|  | 1975 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 1976 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1977 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1978 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1979 | size_t len; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1980 |  | 
| Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1981 | /* Write length only when we know the actual value */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1982 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | 
| Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1983 | p + 2, end - ( p + 2 ), &len, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1984 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1985 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1986 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1987 | return( ret ); | 
|  | 1988 | } | 
| Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1989 | *(p++) = (unsigned char)( len >> 8 ); | 
|  | 1990 | *(p++) = (unsigned char)( len ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1991 | p += len; | 
|  | 1992 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1993 | 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] | 1994 | } | 
|  | 1995 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1996 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
|  | 1997 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 1998 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1999 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2000 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2001 | size_t zlen; | 
|  | 2002 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2003 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2004 | p + 2, end - ( p + 2 ), | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2005 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2006 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2007 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2008 | return( ret ); | 
|  | 2009 | } | 
|  | 2010 |  | 
|  | 2011 | *(p++) = (unsigned char)( zlen >> 8 ); | 
|  | 2012 | *(p++) = (unsigned char)( zlen      ); | 
|  | 2013 | p += zlen; | 
|  | 2014 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2015 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 2016 | MBEDTLS_DEBUG_ECDH_Z ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2017 | } | 
|  | 2018 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2019 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2020 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2021 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2022 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2023 | } | 
|  | 2024 |  | 
|  | 2025 | /* opaque psk<0..2^16-1>; */ | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2026 | if( end - p < 2 ) | 
|  | 2027 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 2028 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2029 | *(p++) = (unsigned char)( psk_len >> 8 ); | 
|  | 2030 | *(p++) = (unsigned char)( psk_len      ); | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2031 |  | 
|  | 2032 | if( end < p || (size_t)( end - p ) < psk_len ) | 
|  | 2033 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2034 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2035 | memcpy( p, psk, psk_len ); | 
|  | 2036 | p += psk_len; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2037 |  | 
|  | 2038 | ssl->handshake->pmslen = p - ssl->handshake->premaster; | 
|  | 2039 |  | 
|  | 2040 | return( 0 ); | 
|  | 2041 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2042 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2043 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2044 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2045 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2046 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2047 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 2048 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2049 | { | 
|  | 2050 | /* If renegotiation is not enforced, retransmit until we would reach max | 
|  | 2051 | * timeout if we were using the usual handshake doubling scheme */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2052 | if( ssl->conf->renego_max_records < 0 ) | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2053 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2054 | 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] | 2055 | unsigned char doublings = 1; | 
|  | 2056 |  | 
|  | 2057 | while( ratio != 0 ) | 
|  | 2058 | { | 
|  | 2059 | ++doublings; | 
|  | 2060 | ratio >>= 1; | 
|  | 2061 | } | 
|  | 2062 |  | 
|  | 2063 | if( ++ssl->renego_records_seen > doublings ) | 
|  | 2064 | { | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2065 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2066 | return( 0 ); | 
|  | 2067 | } | 
|  | 2068 | } | 
|  | 2069 |  | 
|  | 2070 | return( ssl_write_hello_request( ssl ) ); | 
|  | 2071 | } | 
|  | 2072 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2073 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2074 |  | 
| Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 2075 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 2076 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) | 
|  | 2077 | { | 
|  | 2078 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 2079 | if( session->peer_cert != NULL ) | 
|  | 2080 | { | 
|  | 2081 | mbedtls_x509_crt_free( session->peer_cert ); | 
|  | 2082 | mbedtls_free( session->peer_cert ); | 
|  | 2083 | session->peer_cert = NULL; | 
|  | 2084 | } | 
|  | 2085 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 2086 | if( session->peer_cert_digest != NULL ) | 
|  | 2087 | { | 
|  | 2088 | /* Zeroization is not necessary. */ | 
|  | 2089 | mbedtls_free( session->peer_cert_digest ); | 
|  | 2090 | session->peer_cert_digest      = NULL; | 
|  | 2091 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; | 
|  | 2092 | session->peer_cert_digest_len  = 0; | 
|  | 2093 | } | 
|  | 2094 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 2095 | } | 
|  | 2096 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 2097 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2098 | /* | 
|  | 2099 | * Handshake functions | 
|  | 2100 | */ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2101 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 2102 | /* No certificate support -> dummy functions */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2103 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2104 | { | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2105 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 2106 | ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2107 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2108 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2109 |  | 
| Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 2110 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2111 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2112 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2113 | ssl->state++; | 
|  | 2114 | return( 0 ); | 
|  | 2115 | } | 
|  | 2116 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2117 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2118 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2119 | } | 
|  | 2120 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2121 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2122 | { | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2123 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 2124 | ssl->handshake->ciphersuite_info; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2125 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2126 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2127 |  | 
| Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 2128 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2129 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2130 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2131 | ssl->state++; | 
|  | 2132 | return( 0 ); | 
|  | 2133 | } | 
|  | 2134 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2135 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2136 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2137 | } | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 2138 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2139 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 2140 | /* Some certificate support -> implement write and parse */ | 
|  | 2141 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2142 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2143 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2144 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2145 | size_t i, n; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2146 | const mbedtls_x509_crt *crt; | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2147 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 2148 | ssl->handshake->ciphersuite_info; | 
| 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 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2151 |  | 
| Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 2152 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2153 | { | 
| Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 2154 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
|  | 2155 | ssl->state++; | 
|  | 2156 | return( 0 ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2157 | } | 
|  | 2158 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2159 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2160 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2161 | { | 
|  | 2162 | if( ssl->client_auth == 0 ) | 
|  | 2163 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2164 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2165 | ssl->state++; | 
|  | 2166 | return( 0 ); | 
|  | 2167 | } | 
|  | 2168 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2169 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2170 | /* | 
|  | 2171 | * If using SSLv3 and got no cert, send an Alert message | 
|  | 2172 | * (otherwise an empty Certificate message will be sent). | 
|  | 2173 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2174 | if( mbedtls_ssl_own_cert( ssl )  == NULL && | 
|  | 2175 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2176 | { | 
|  | 2177 | ssl->out_msglen  = 2; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2178 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; | 
|  | 2179 | ssl->out_msg[0]  = MBEDTLS_SSL_ALERT_LEVEL_WARNING; | 
|  | 2180 | ssl->out_msg[1]  = MBEDTLS_SSL_ALERT_MSG_NO_CERT; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2181 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2182 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2183 | goto write_msg; | 
|  | 2184 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2185 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2186 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2187 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 2188 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2189 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2190 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2191 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) | 
| 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 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); | 
|  | 2194 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2195 | } | 
|  | 2196 | } | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2197 | #endif | 
| 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_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2200 |  | 
|  | 2201 | /* | 
|  | 2202 | *     0  .  0    handshake type | 
|  | 2203 | *     1  .  3    handshake length | 
|  | 2204 | *     4  .  6    length of all certs | 
|  | 2205 | *     7  .  9    length of cert. 1 | 
|  | 2206 | *    10  . n-1   peer certificate | 
|  | 2207 | *     n  . n+2   length of cert. 2 | 
|  | 2208 | *    n+3 . ...   upper level cert, etc. | 
|  | 2209 | */ | 
|  | 2210 | i = 7; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2211 | crt = mbedtls_ssl_own_cert( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2212 |  | 
| Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 2213 | while( crt != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2214 | { | 
|  | 2215 | n = crt->raw.len; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2216 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2217 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2218 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2219 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2220 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2221 | } | 
|  | 2222 |  | 
|  | 2223 | ssl->out_msg[i    ] = (unsigned char)( n >> 16 ); | 
|  | 2224 | ssl->out_msg[i + 1] = (unsigned char)( n >>  8 ); | 
|  | 2225 | ssl->out_msg[i + 2] = (unsigned char)( n       ); | 
|  | 2226 |  | 
|  | 2227 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); | 
|  | 2228 | i += n; crt = crt->next; | 
|  | 2229 | } | 
|  | 2230 |  | 
|  | 2231 | ssl->out_msg[4]  = (unsigned char)( ( i - 7 ) >> 16 ); | 
|  | 2232 | ssl->out_msg[5]  = (unsigned char)( ( i - 7 ) >>  8 ); | 
|  | 2233 | ssl->out_msg[6]  = (unsigned char)( ( i - 7 )       ); | 
|  | 2234 |  | 
|  | 2235 | ssl->out_msglen  = i; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2236 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 2237 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2238 |  | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2239 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2240 | write_msg: | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2241 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2242 |  | 
|  | 2243 | ssl->state++; | 
|  | 2244 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2245 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2246 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2247 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2248 | return( ret ); | 
|  | 2249 | } | 
|  | 2250 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2251 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2252 |  | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 2253 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2254 | } | 
|  | 2255 |  | 
| Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 2256 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 2257 |  | 
|  | 2258 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2259 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, | 
|  | 2260 | unsigned char *crt_buf, | 
|  | 2261 | size_t crt_buf_len ) | 
|  | 2262 | { | 
|  | 2263 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; | 
|  | 2264 |  | 
|  | 2265 | if( peer_crt == NULL ) | 
|  | 2266 | return( -1 ); | 
|  | 2267 |  | 
|  | 2268 | if( peer_crt->raw.len != crt_buf_len ) | 
|  | 2269 | return( -1 ); | 
|  | 2270 |  | 
| k-stachowiak | 95b68ef | 2019-09-16 12:21:00 +0200 | [diff] [blame] | 2271 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2272 | } | 
| Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 2273 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 2274 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, | 
|  | 2275 | unsigned char *crt_buf, | 
|  | 2276 | size_t crt_buf_len ) | 
|  | 2277 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2278 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 2279 | unsigned char const * const peer_cert_digest = | 
|  | 2280 | ssl->session->peer_cert_digest; | 
|  | 2281 | mbedtls_md_type_t const peer_cert_digest_type = | 
|  | 2282 | ssl->session->peer_cert_digest_type; | 
|  | 2283 | mbedtls_md_info_t const * const digest_info = | 
|  | 2284 | mbedtls_md_info_from_type( peer_cert_digest_type ); | 
|  | 2285 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; | 
|  | 2286 | size_t digest_len; | 
|  | 2287 |  | 
|  | 2288 | if( peer_cert_digest == NULL || digest_info == NULL ) | 
|  | 2289 | return( -1 ); | 
|  | 2290 |  | 
|  | 2291 | digest_len = mbedtls_md_get_size( digest_info ); | 
|  | 2292 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) | 
|  | 2293 | return( -1 ); | 
|  | 2294 |  | 
|  | 2295 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); | 
|  | 2296 | if( ret != 0 ) | 
|  | 2297 | return( -1 ); | 
|  | 2298 |  | 
|  | 2299 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); | 
|  | 2300 | } | 
|  | 2301 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 2302 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2303 |  | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2304 | /* | 
|  | 2305 | * Once the certificate message is read, parse it into a cert chain and | 
|  | 2306 | * perform basic checks, but leave actual verification to the caller | 
|  | 2307 | */ | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2308 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, | 
|  | 2309 | mbedtls_x509_crt *chain ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2310 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2311 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2312 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 2313 | int crt_cnt=0; | 
|  | 2314 | #endif | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2315 | size_t i, n; | 
| Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 2316 | uint8_t alert; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2317 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2318 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2319 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2320 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2321 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2322 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2323 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2324 | } | 
|  | 2325 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2326 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || | 
|  | 2327 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2328 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2329 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2330 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2331 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2332 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2333 | } | 
|  | 2334 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2335 | i = mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 2336 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2337 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2338 | * Same message structure as in mbedtls_ssl_write_certificate() | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2339 | */ | 
| Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 2340 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2341 |  | 
| Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 2342 | if( ssl->in_msg[i] != 0 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2343 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) | 
| 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 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2346 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2347 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2348 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2349 | } | 
|  | 2350 |  | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2351 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ | 
|  | 2352 | i += 3; | 
|  | 2353 |  | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2354 | /* Iterate through and parse the CRTs in the provided chain. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2355 | while( i < ssl->in_hslen ) | 
|  | 2356 | { | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2357 | /* Check that there's room for the next CRT's length fields. */ | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 2358 | if ( i + 3 > ssl->in_hslen ) { | 
|  | 2359 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 2360 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 2361 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2362 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 2363 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
|  | 2364 | } | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2365 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support | 
|  | 2366 | * anything beyond 2**16 ~ 64K. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2367 | if( ssl->in_msg[i] != 0 ) | 
|  | 2368 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2369 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 2370 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 2371 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2372 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2373 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2374 | } | 
|  | 2375 |  | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2376 | /* Read length of the next CRT in the chain. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2377 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) | 
|  | 2378 | | (unsigned int) ssl->in_msg[i + 2]; | 
|  | 2379 | i += 3; | 
|  | 2380 |  | 
|  | 2381 | if( n < 128 || i + n > ssl->in_hslen ) | 
|  | 2382 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2383 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 2384 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 2385 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2386 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2387 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2388 | } | 
|  | 2389 |  | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2390 | /* Check if we're handling the first CRT in the chain. */ | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2391 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 2392 | if( crt_cnt++ == 0 && | 
|  | 2393 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 2394 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2395 | { | 
| Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 2396 | /* During client-side renegotiation, check that the server's | 
|  | 2397 | * end-CRTs hasn't changed compared to the initial handshake, | 
|  | 2398 | * mitigating the triple handshake attack. On success, reuse | 
|  | 2399 | * the original end-CRT instead of parsing it again. */ | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2400 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); | 
|  | 2401 | if( ssl_check_peer_crt_unchanged( ssl, | 
|  | 2402 | &ssl->in_msg[i], | 
|  | 2403 | n ) != 0 ) | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2404 | { | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2405 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); | 
|  | 2406 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 2407 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2408 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); | 
|  | 2409 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2410 | } | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2411 |  | 
|  | 2412 | /* Now we can safely free the original chain. */ | 
|  | 2413 | ssl_clear_peer_cert( ssl->session ); | 
|  | 2414 | } | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2415 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
|  | 2416 |  | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2417 | /* Parse the next certificate in the chain. */ | 
| Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2418 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2419 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); | 
| Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2420 | #else | 
| Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 2421 | /* If we don't need to store the CRT chain permanently, parse | 
| Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2422 | * it in-place from the input buffer instead of making a copy. */ | 
|  | 2423 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); | 
|  | 2424 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2425 | switch( ret ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2426 | { | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2427 | case 0: /*ok*/ | 
|  | 2428 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: | 
|  | 2429 | /* Ignore certificate with an unknown algorithm: maybe a | 
|  | 2430 | prior certificate was already trusted. */ | 
|  | 2431 | break; | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2432 |  | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2433 | case MBEDTLS_ERR_X509_ALLOC_FAILED: | 
|  | 2434 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; | 
|  | 2435 | goto crt_parse_der_failed; | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2436 |  | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2437 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: | 
|  | 2438 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 2439 | goto crt_parse_der_failed; | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2440 |  | 
| Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2441 | default: | 
|  | 2442 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
|  | 2443 | crt_parse_der_failed: | 
|  | 2444 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); | 
|  | 2445 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); | 
|  | 2446 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2447 | } | 
|  | 2448 |  | 
|  | 2449 | i += n; | 
|  | 2450 | } | 
|  | 2451 |  | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2452 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2453 | return( 0 ); | 
|  | 2454 | } | 
|  | 2455 |  | 
| Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2456 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 2457 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) | 
|  | 2458 | { | 
|  | 2459 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 2460 | return( -1 ); | 
|  | 2461 |  | 
|  | 2462 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 2463 | /* | 
|  | 2464 | * Check if the client sent an empty certificate | 
|  | 2465 | */ | 
|  | 2466 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
|  | 2467 | { | 
|  | 2468 | if( ssl->in_msglen  == 2                        && | 
|  | 2469 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT            && | 
|  | 2470 | ssl->in_msg[0]  == MBEDTLS_SSL_ALERT_LEVEL_WARNING  && | 
|  | 2471 | ssl->in_msg[1]  == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) | 
|  | 2472 | { | 
|  | 2473 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); | 
|  | 2474 | return( 0 ); | 
|  | 2475 | } | 
|  | 2476 |  | 
|  | 2477 | return( -1 ); | 
|  | 2478 | } | 
|  | 2479 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 2480 |  | 
|  | 2481 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 2482 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2483 | if( ssl->in_hslen   == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && | 
|  | 2484 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE    && | 
|  | 2485 | ssl->in_msg[0]  == MBEDTLS_SSL_HS_CERTIFICATE   && | 
|  | 2486 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) | 
|  | 2487 | { | 
|  | 2488 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); | 
|  | 2489 | return( 0 ); | 
|  | 2490 | } | 
|  | 2491 |  | 
|  | 2492 | return( -1 ); | 
|  | 2493 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 2494 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 2495 | } | 
|  | 2496 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 2497 |  | 
| Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2498 | /* Check if a certificate message is expected. | 
|  | 2499 | * Return either | 
|  | 2500 | * - SSL_CERTIFICATE_EXPECTED, or | 
|  | 2501 | * - SSL_CERTIFICATE_SKIP | 
|  | 2502 | * indicating whether a Certificate message is expected or not. | 
|  | 2503 | */ | 
|  | 2504 | #define SSL_CERTIFICATE_EXPECTED 0 | 
|  | 2505 | #define SSL_CERTIFICATE_SKIP     1 | 
|  | 2506 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, | 
|  | 2507 | int authmode ) | 
|  | 2508 | { | 
|  | 2509 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2510 | ssl->handshake->ciphersuite_info; | 
| Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2511 |  | 
|  | 2512 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
|  | 2513 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 2514 |  | 
|  | 2515 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 2516 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 2517 | { | 
|  | 2518 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
|  | 2519 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 2520 |  | 
|  | 2521 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
|  | 2522 | { | 
| Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2523 | ssl->session_negotiate->verify_result = | 
|  | 2524 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; | 
|  | 2525 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 2526 | } | 
|  | 2527 | } | 
| Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 2528 | #else | 
|  | 2529 | ((void) authmode); | 
| Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2530 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 2531 |  | 
|  | 2532 | return( SSL_CERTIFICATE_EXPECTED ); | 
|  | 2533 | } | 
|  | 2534 |  | 
| Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2535 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, | 
|  | 2536 | int authmode, | 
|  | 2537 | mbedtls_x509_crt *chain, | 
|  | 2538 | void *rs_ctx ) | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2539 | { | 
| Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2540 | int ret = 0; | 
| Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2541 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2542 | ssl->handshake->ciphersuite_info; | 
| Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2543 | int have_ca_chain = 0; | 
| Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2544 |  | 
| Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2545 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); | 
|  | 2546 | void *p_vrfy; | 
|  | 2547 |  | 
| Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2548 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
|  | 2549 | return( 0 ); | 
|  | 2550 |  | 
| Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2551 | if( ssl->f_vrfy != NULL ) | 
|  | 2552 | { | 
| Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2553 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); | 
| Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2554 | f_vrfy = ssl->f_vrfy; | 
|  | 2555 | p_vrfy = ssl->p_vrfy; | 
|  | 2556 | } | 
|  | 2557 | else | 
|  | 2558 | { | 
| Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2559 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); | 
| Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2560 | f_vrfy = ssl->conf->f_vrfy; | 
|  | 2561 | p_vrfy = ssl->conf->p_vrfy; | 
|  | 2562 | } | 
|  | 2563 |  | 
| Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2564 | /* | 
|  | 2565 | * Main check: verify certificate | 
|  | 2566 | */ | 
| Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2567 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
|  | 2568 | if( ssl->conf->f_ca_cb != NULL ) | 
|  | 2569 | { | 
|  | 2570 | ((void) rs_ctx); | 
|  | 2571 | have_ca_chain = 1; | 
|  | 2572 |  | 
|  | 2573 | 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] | 2574 | ret = mbedtls_x509_crt_verify_with_ca_cb( | 
| Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2575 | chain, | 
|  | 2576 | ssl->conf->f_ca_cb, | 
|  | 2577 | ssl->conf->p_ca_cb, | 
|  | 2578 | ssl->conf->cert_profile, | 
|  | 2579 | ssl->hostname, | 
|  | 2580 | &ssl->session_negotiate->verify_result, | 
| Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2581 | f_vrfy, p_vrfy ); | 
| Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2582 | } | 
|  | 2583 | else | 
|  | 2584 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
|  | 2585 | { | 
|  | 2586 | mbedtls_x509_crt *ca_chain; | 
|  | 2587 | mbedtls_x509_crl *ca_crl; | 
|  | 2588 |  | 
|  | 2589 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 2590 | if( ssl->handshake->sni_ca_chain != NULL ) | 
|  | 2591 | { | 
|  | 2592 | ca_chain = ssl->handshake->sni_ca_chain; | 
|  | 2593 | ca_crl   = ssl->handshake->sni_ca_crl; | 
|  | 2594 | } | 
|  | 2595 | else | 
|  | 2596 | #endif | 
|  | 2597 | { | 
|  | 2598 | ca_chain = ssl->conf->ca_chain; | 
|  | 2599 | ca_crl   = ssl->conf->ca_crl; | 
|  | 2600 | } | 
|  | 2601 |  | 
|  | 2602 | if( ca_chain != NULL ) | 
|  | 2603 | have_ca_chain = 1; | 
|  | 2604 |  | 
|  | 2605 | ret = mbedtls_x509_crt_verify_restartable( | 
|  | 2606 | chain, | 
|  | 2607 | ca_chain, ca_crl, | 
|  | 2608 | ssl->conf->cert_profile, | 
|  | 2609 | ssl->hostname, | 
|  | 2610 | &ssl->session_negotiate->verify_result, | 
| Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2611 | f_vrfy, p_vrfy, rs_ctx ); | 
| Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2612 | } | 
| Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2613 |  | 
|  | 2614 | if( ret != 0 ) | 
|  | 2615 | { | 
|  | 2616 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); | 
|  | 2617 | } | 
|  | 2618 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2619 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2620 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
|  | 2621 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); | 
|  | 2622 | #endif | 
|  | 2623 |  | 
|  | 2624 | /* | 
|  | 2625 | * Secondary checks: always done, but change 'ret' only if it was 0 | 
|  | 2626 | */ | 
|  | 2627 |  | 
|  | 2628 | #if defined(MBEDTLS_ECP_C) | 
|  | 2629 | { | 
|  | 2630 | const mbedtls_pk_context *pk = &chain->pk; | 
|  | 2631 |  | 
|  | 2632 | /* If certificate uses an EC key, make sure the curve is OK */ | 
|  | 2633 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && | 
|  | 2634 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) | 
|  | 2635 | { | 
|  | 2636 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; | 
|  | 2637 |  | 
|  | 2638 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); | 
|  | 2639 | if( ret == 0 ) | 
|  | 2640 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; | 
|  | 2641 | } | 
|  | 2642 | } | 
|  | 2643 | #endif /* MBEDTLS_ECP_C */ | 
|  | 2644 |  | 
|  | 2645 | if( mbedtls_ssl_check_cert_usage( chain, | 
|  | 2646 | ciphersuite_info, | 
|  | 2647 | ! ssl->conf->endpoint, | 
|  | 2648 | &ssl->session_negotiate->verify_result ) != 0 ) | 
|  | 2649 | { | 
|  | 2650 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); | 
|  | 2651 | if( ret == 0 ) | 
|  | 2652 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; | 
|  | 2653 | } | 
|  | 2654 |  | 
|  | 2655 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a | 
|  | 2656 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, | 
|  | 2657 | * with details encoded in the verification flags. All other kinds | 
|  | 2658 | * of error codes, including those from the user provided f_vrfy | 
|  | 2659 | * functions, are treated as fatal and lead to a failure of | 
|  | 2660 | * ssl_parse_certificate even if verification was optional. */ | 
|  | 2661 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && | 
|  | 2662 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || | 
|  | 2663 | ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) | 
|  | 2664 | { | 
|  | 2665 | ret = 0; | 
|  | 2666 | } | 
|  | 2667 |  | 
| Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2668 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) | 
| Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2669 | { | 
|  | 2670 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); | 
|  | 2671 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; | 
|  | 2672 | } | 
|  | 2673 |  | 
|  | 2674 | if( ret != 0 ) | 
|  | 2675 | { | 
|  | 2676 | uint8_t alert; | 
|  | 2677 |  | 
|  | 2678 | /* The certificate may have been rejected for several reasons. | 
|  | 2679 | Pick one and send the corresponding alert. Which alert to send | 
|  | 2680 | may be a subject of debate in some cases. */ | 
|  | 2681 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) | 
|  | 2682 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; | 
|  | 2683 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) | 
|  | 2684 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
|  | 2685 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) | 
|  | 2686 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 2687 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) | 
|  | 2688 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 2689 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) | 
|  | 2690 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 2691 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) | 
|  | 2692 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 2693 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) | 
|  | 2694 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 2695 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) | 
|  | 2696 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; | 
|  | 2697 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) | 
|  | 2698 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; | 
|  | 2699 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) | 
|  | 2700 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; | 
|  | 2701 | else | 
|  | 2702 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; | 
|  | 2703 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2704 | alert ); | 
|  | 2705 | } | 
|  | 2706 |  | 
|  | 2707 | #if defined(MBEDTLS_DEBUG_C) | 
|  | 2708 | if( ssl->session_negotiate->verify_result != 0 ) | 
|  | 2709 | { | 
|  | 2710 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", | 
|  | 2711 | ssl->session_negotiate->verify_result ) ); | 
|  | 2712 | } | 
|  | 2713 | else | 
|  | 2714 | { | 
|  | 2715 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); | 
|  | 2716 | } | 
|  | 2717 | #endif /* MBEDTLS_DEBUG_C */ | 
|  | 2718 |  | 
|  | 2719 | return( ret ); | 
|  | 2720 | } | 
|  | 2721 |  | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2722 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 2723 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, | 
|  | 2724 | unsigned char *start, size_t len ) | 
|  | 2725 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2726 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2727 | /* Remember digest of the peer's end-CRT. */ | 
|  | 2728 | ssl->session_negotiate->peer_cert_digest = | 
|  | 2729 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); | 
|  | 2730 | if( ssl->session_negotiate->peer_cert_digest == NULL ) | 
|  | 2731 | { | 
|  | 2732 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | 
| irwir | 40883e9 | 2019-09-21 17:55:33 +0300 | [diff] [blame] | 2733 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2734 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 2735 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2736 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 2737 |  | 
|  | 2738 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 2739 | } | 
|  | 2740 |  | 
|  | 2741 | ret = mbedtls_md( mbedtls_md_info_from_type( | 
|  | 2742 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), | 
|  | 2743 | start, len, | 
|  | 2744 | ssl->session_negotiate->peer_cert_digest ); | 
|  | 2745 |  | 
|  | 2746 | ssl->session_negotiate->peer_cert_digest_type = | 
|  | 2747 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; | 
|  | 2748 | ssl->session_negotiate->peer_cert_digest_len = | 
|  | 2749 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; | 
|  | 2750 |  | 
|  | 2751 | return( ret ); | 
|  | 2752 | } | 
|  | 2753 |  | 
|  | 2754 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, | 
|  | 2755 | unsigned char *start, size_t len ) | 
|  | 2756 | { | 
|  | 2757 | unsigned char *end = start + len; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2758 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2759 |  | 
|  | 2760 | /* Make a copy of the peer's raw public key. */ | 
|  | 2761 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); | 
|  | 2762 | ret = mbedtls_pk_parse_subpubkey( &start, end, | 
|  | 2763 | &ssl->handshake->peer_pubkey ); | 
|  | 2764 | if( ret != 0 ) | 
|  | 2765 | { | 
|  | 2766 | /* We should have parsed the public key before. */ | 
|  | 2767 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 2768 | } | 
|  | 2769 |  | 
|  | 2770 | return( 0 ); | 
|  | 2771 | } | 
|  | 2772 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 2773 |  | 
| Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2774 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | 
|  | 2775 | { | 
|  | 2776 | int ret = 0; | 
| Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2777 | int crt_expected; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2778 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 2779 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET | 
|  | 2780 | ? ssl->handshake->sni_authmode | 
|  | 2781 | : ssl->conf->authmode; | 
|  | 2782 | #else | 
| Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 2783 | const int authmode = ssl->conf->authmode; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2784 | #endif | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2785 | void *rs_ctx = NULL; | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2786 | mbedtls_x509_crt *chain = NULL; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2787 |  | 
|  | 2788 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | 
|  | 2789 |  | 
| Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2790 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); | 
|  | 2791 | if( crt_expected == SSL_CERTIFICATE_SKIP ) | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2792 | { | 
|  | 2793 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
| Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2794 | goto exit; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2795 | } | 
|  | 2796 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2797 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2798 | if( ssl->handshake->ecrs_enabled && | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2799 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2800 | { | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2801 | chain = ssl->handshake->ecrs_peer_cert; | 
|  | 2802 | ssl->handshake->ecrs_peer_cert = NULL; | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2803 | goto crt_verify; | 
|  | 2804 | } | 
|  | 2805 | #endif | 
|  | 2806 |  | 
| Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 2807 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2808 | { | 
|  | 2809 | /* mbedtls_ssl_read_record may have sent an alert already. We | 
|  | 2810 | let it decide whether to alert. */ | 
|  | 2811 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2812 | goto exit; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2813 | } | 
|  | 2814 |  | 
| Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2815 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 2816 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) | 
|  | 2817 | { | 
|  | 2818 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; | 
| Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2819 |  | 
| irwir | d3085ab | 2020-04-21 22:26:05 +0300 | [diff] [blame] | 2820 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) | 
| Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2821 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; | 
| Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2822 |  | 
| Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2823 | goto exit; | 
| Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2824 | } | 
|  | 2825 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 2826 |  | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2827 | /* Clear existing peer CRT structure in case we tried to | 
|  | 2828 | * reuse a session but it failed, and allocate a new one. */ | 
| Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2829 | ssl_clear_peer_cert( ssl->session_negotiate ); | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2830 |  | 
|  | 2831 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); | 
|  | 2832 | if( chain == NULL ) | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2833 | { | 
|  | 2834 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | 
|  | 2835 | sizeof( mbedtls_x509_crt ) ) ); | 
|  | 2836 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 2837 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 2838 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2839 |  | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2840 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 2841 | goto exit; | 
|  | 2842 | } | 
|  | 2843 | mbedtls_x509_crt_init( chain ); | 
|  | 2844 |  | 
|  | 2845 | ret = ssl_parse_certificate_chain( ssl, chain ); | 
| Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2846 | if( ret != 0 ) | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2847 | goto exit; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2848 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2849 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2850 | if( ssl->handshake->ecrs_enabled) | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2851 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2852 |  | 
|  | 2853 | crt_verify: | 
|  | 2854 | if( ssl->handshake->ecrs_enabled) | 
|  | 2855 | rs_ctx = &ssl->handshake->ecrs_ctx; | 
|  | 2856 | #endif | 
|  | 2857 |  | 
| Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2858 | ret = ssl_parse_certificate_verify( ssl, authmode, | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2859 | chain, rs_ctx ); | 
| Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2860 | if( ret != 0 ) | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2861 | goto exit; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2862 |  | 
| Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2863 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2864 | { | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2865 | unsigned char *crt_start, *pk_start; | 
|  | 2866 | size_t crt_len, pk_len; | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2867 |  | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2868 | /* We parse the CRT chain without copying, so | 
|  | 2869 | * these pointers point into the input buffer, | 
|  | 2870 | * and are hence still valid after freeing the | 
|  | 2871 | * CRT chain. */ | 
| Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2872 |  | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2873 | crt_start = chain->raw.p; | 
|  | 2874 | crt_len   = chain->raw.len; | 
| Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2875 |  | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2876 | pk_start = chain->pk_raw.p; | 
|  | 2877 | pk_len   = chain->pk_raw.len; | 
|  | 2878 |  | 
|  | 2879 | /* Free the CRT structures before computing | 
|  | 2880 | * digest and copying the peer's public key. */ | 
|  | 2881 | mbedtls_x509_crt_free( chain ); | 
|  | 2882 | mbedtls_free( chain ); | 
|  | 2883 | chain = NULL; | 
|  | 2884 |  | 
|  | 2885 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); | 
| Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2886 | if( ret != 0 ) | 
| Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2887 | goto exit; | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2888 |  | 
|  | 2889 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); | 
|  | 2890 | if( ret != 0 ) | 
|  | 2891 | goto exit; | 
| Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2892 | } | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2893 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 2894 | /* Pass ownership to session structure. */ | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2895 | ssl->session_negotiate->peer_cert = chain; | 
|  | 2896 | chain = NULL; | 
| Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2897 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2898 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2899 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2900 |  | 
| Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2901 | exit: | 
|  | 2902 |  | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2903 | if( ret == 0 ) | 
|  | 2904 | ssl->state++; | 
|  | 2905 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2906 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2907 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) | 
|  | 2908 | { | 
|  | 2909 | ssl->handshake->ecrs_peer_cert = chain; | 
|  | 2910 | chain = NULL; | 
|  | 2911 | } | 
|  | 2912 | #endif | 
|  | 2913 |  | 
|  | 2914 | if( chain != NULL ) | 
|  | 2915 | { | 
|  | 2916 | mbedtls_x509_crt_free( chain ); | 
|  | 2917 | mbedtls_free( chain ); | 
|  | 2918 | } | 
|  | 2919 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2920 | return( ret ); | 
|  | 2921 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2922 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2923 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2924 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, | 
|  | 2925 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2926 | { | 
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 2927 | ((void) ciphersuite_info); | 
| Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2928 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2929 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 2930 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 2931 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2932 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2933 | else | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2934 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2935 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2936 | #if defined(MBEDTLS_SHA512_C) | 
|  | 2937 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2938 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; | 
|  | 2939 | else | 
|  | 2940 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2941 | #if defined(MBEDTLS_SHA256_C) | 
|  | 2942 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2943 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2944 | else | 
|  | 2945 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2946 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2947 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2948 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2949 | return; | 
| Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2950 | } | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2951 | } | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2952 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2953 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2954 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2955 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 2956 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2957 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5  ); | 
|  | 2958 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2959 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2960 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2961 | #if defined(MBEDTLS_SHA256_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2962 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 2963 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2964 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); | 
|  | 2965 | #else | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2966 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2967 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2968 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2969 | #if defined(MBEDTLS_SHA512_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2970 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 2971 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2972 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2973 | #else | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2974 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2975 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2976 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2977 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2978 | } | 
|  | 2979 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2980 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2981 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2982 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2983 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 2984 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2985 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); | 
|  | 2986 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2987 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2988 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2989 | #if defined(MBEDTLS_SHA256_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2990 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 2991 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); | 
|  | 2992 | #else | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2993 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2994 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2995 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2996 | #if defined(MBEDTLS_SHA512_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2997 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2998 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2999 | #else | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3000 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); | 
| Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 3001 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3002 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3003 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3004 | } | 
|  | 3005 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3006 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 3007 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 3008 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3009 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3010 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3011 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); | 
|  | 3012 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3013 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3014 | #endif | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3015 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3016 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3017 | #if defined(MBEDTLS_SHA256_C) | 
|  | 3018 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3019 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3020 | { | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3021 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3022 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); | 
|  | 3023 | #else | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3024 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3025 | #endif | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3026 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3027 | #endif | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3028 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3029 | #if defined(MBEDTLS_SHA512_C) | 
|  | 3030 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3031 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3032 | { | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3033 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3034 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3035 | #else | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3036 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3037 | #endif | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3038 | } | 
| Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 3039 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3040 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 3041 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3042 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3043 | static void ssl_calc_finished_ssl( | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3044 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3045 | { | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3046 | const char *sender; | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3047 | mbedtls_md5_context  md5; | 
|  | 3048 | mbedtls_sha1_context sha1; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3049 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3050 | unsigned char padbuf[48]; | 
|  | 3051 | unsigned char md5sum[16]; | 
|  | 3052 | unsigned char sha1sum[20]; | 
|  | 3053 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3054 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3055 | if( !session ) | 
|  | 3056 | session = ssl->session; | 
|  | 3057 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3058 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished ssl" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3059 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3060 | mbedtls_md5_init( &md5 ); | 
|  | 3061 | mbedtls_sha1_init( &sha1 ); | 
|  | 3062 |  | 
|  | 3063 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 3064 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3065 |  | 
|  | 3066 | /* | 
|  | 3067 | * SSLv3: | 
|  | 3068 | *   hash = | 
|  | 3069 | *      MD5( master + pad2 + | 
|  | 3070 | *          MD5( handshake + sender + master + pad1 ) ) | 
|  | 3071 | *   + SHA1( master + pad2 + | 
|  | 3072 | *         SHA1( handshake + sender + master + pad1 ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3073 | */ | 
|  | 3074 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3075 | #if !defined(MBEDTLS_MD5_ALT) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3076 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished  md5 state", (unsigned char *) | 
|  | 3077 | md5.state, sizeof(  md5.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3078 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3079 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3080 | #if !defined(MBEDTLS_SHA1_ALT) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3081 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) | 
|  | 3082 | sha1.state, sizeof( sha1.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3083 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3084 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3085 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3086 | : "SRVR"; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3087 |  | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3088 | memset( padbuf, 0x36, 48 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3089 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3090 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); | 
|  | 3091 | mbedtls_md5_update_ret( &md5, session->master, 48 ); | 
|  | 3092 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); | 
|  | 3093 | mbedtls_md5_finish_ret( &md5, md5sum ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3094 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3095 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); | 
|  | 3096 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); | 
|  | 3097 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); | 
|  | 3098 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3099 |  | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3100 | memset( padbuf, 0x5C, 48 ); | 
| 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_md5_starts_ret( &md5 ); | 
|  | 3103 | mbedtls_md5_update_ret( &md5, session->master, 48 ); | 
|  | 3104 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); | 
|  | 3105 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); | 
|  | 3106 | mbedtls_md5_finish_ret( &md5, buf ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3107 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3108 | mbedtls_sha1_starts_ret( &sha1 ); | 
|  | 3109 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); | 
|  | 3110 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); | 
|  | 3111 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); | 
|  | 3112 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3113 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3114 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3115 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3116 | mbedtls_md5_free(  &md5  ); | 
|  | 3117 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3118 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3119 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
|  | 3120 | mbedtls_platform_zeroize(  md5sum, sizeof(  md5sum ) ); | 
|  | 3121 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3122 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3123 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3124 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3125 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3126 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3127 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3128 | static void ssl_calc_finished_tls( | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3129 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3130 | { | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3131 | int len = 12; | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3132 | const char *sender; | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3133 | mbedtls_md5_context  md5; | 
|  | 3134 | mbedtls_sha1_context sha1; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3135 | unsigned char padbuf[36]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3136 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3137 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3138 | if( !session ) | 
|  | 3139 | session = ssl->session; | 
|  | 3140 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3141 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3142 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3143 | mbedtls_md5_init( &md5 ); | 
|  | 3144 | mbedtls_sha1_init( &sha1 ); | 
|  | 3145 |  | 
|  | 3146 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 3147 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3148 |  | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3149 | /* | 
|  | 3150 | * TLSv1: | 
|  | 3151 | *   hash = PRF( master, finished_label, | 
|  | 3152 | *               MD5( handshake ) + SHA1( handshake ) )[0..11] | 
|  | 3153 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3154 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3155 | #if !defined(MBEDTLS_MD5_ALT) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3156 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished  md5 state", (unsigned char *) | 
|  | 3157 | md5.state, sizeof(  md5.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3158 | #endif | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3159 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3160 | #if !defined(MBEDTLS_SHA1_ALT) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3161 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) | 
|  | 3162 | sha1.state, sizeof( sha1.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3163 | #endif | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3164 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3165 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3166 | ? "client finished" | 
|  | 3167 | : "server finished"; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3168 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3169 | mbedtls_md5_finish_ret(  &md5, padbuf ); | 
|  | 3170 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3171 |  | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3172 | ssl->handshake->tls_prf( session->master, 48, sender, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3173 | padbuf, 36, buf, len ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3174 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3175 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3176 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3177 | mbedtls_md5_free(  &md5  ); | 
|  | 3178 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3179 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3180 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
| 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_MSG( 2, ( "<= calc  finished" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3183 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3184 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3185 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3186 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3187 | #if defined(MBEDTLS_SHA256_C) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3188 | static void ssl_calc_finished_tls_sha256( | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3189 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3190 | { | 
|  | 3191 | int len = 12; | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3192 | const char *sender; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3193 | unsigned char padbuf[32]; | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3194 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3195 | size_t hash_size; | 
| Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 3196 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3197 | psa_status_t status; | 
|  | 3198 | #else | 
|  | 3199 | mbedtls_sha256_context sha256; | 
|  | 3200 | #endif | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3201 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3202 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3203 | if( !session ) | 
|  | 3204 | session = ssl->session; | 
|  | 3205 |  | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3206 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 3207 | ? "client finished" | 
|  | 3208 | : "server finished"; | 
|  | 3209 |  | 
|  | 3210 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3211 | sha256_psa = psa_hash_operation_init(); | 
|  | 3212 |  | 
|  | 3213 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); | 
|  | 3214 |  | 
|  | 3215 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); | 
|  | 3216 | if( status != PSA_SUCCESS ) | 
|  | 3217 | { | 
|  | 3218 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 3219 | return; | 
|  | 3220 | } | 
|  | 3221 |  | 
|  | 3222 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); | 
|  | 3223 | if( status != PSA_SUCCESS ) | 
|  | 3224 | { | 
|  | 3225 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 3226 | return; | 
|  | 3227 | } | 
|  | 3228 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); | 
|  | 3229 | #else | 
|  | 3230 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3231 | mbedtls_sha256_init( &sha256 ); | 
|  | 3232 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3233 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha256" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3234 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3235 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3236 |  | 
|  | 3237 | /* | 
|  | 3238 | * TLSv1.2: | 
|  | 3239 | *   hash = PRF( master, finished_label, | 
|  | 3240 | *               Hash( handshake ) )[0.11] | 
|  | 3241 | */ | 
|  | 3242 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3243 | #if !defined(MBEDTLS_SHA256_ALT) | 
|  | 3244 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3245 | sha256.state, sizeof( sha256.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3246 | #endif | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3247 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3248 | mbedtls_sha256_finish_ret( &sha256, padbuf ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3249 | mbedtls_sha256_free( &sha256 ); | 
|  | 3250 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3251 |  | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3252 | ssl->handshake->tls_prf( session->master, 48, sender, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3253 | padbuf, 32, buf, len ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3254 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3255 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3256 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3257 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3258 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3259 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3260 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3261 | #endif /* MBEDTLS_SHA256_C */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3262 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3263 | #if defined(MBEDTLS_SHA512_C) | 
| Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 3264 |  | 
| Rodrigo Dias Correa | f06a614 | 2020-11-25 07:30:26 -0300 | [diff] [blame] | 3265 | typedef int (*finish_sha384_t)(mbedtls_sha512_context*, unsigned char*); | 
| Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 3266 |  | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3267 | static void ssl_calc_finished_tls_sha384( | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3268 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3269 | { | 
|  | 3270 | int len = 12; | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3271 | const char *sender; | 
| Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 3272 | unsigned char padbuf[48]; | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3273 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3274 | size_t hash_size; | 
| Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 3275 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3276 | psa_status_t status; | 
|  | 3277 | #else | 
|  | 3278 | mbedtls_sha512_context sha512; | 
|  | 3279 | #endif | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3280 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3281 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3282 | if( !session ) | 
|  | 3283 | session = ssl->session; | 
|  | 3284 |  | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3285 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 3286 | ? "client finished" | 
|  | 3287 | : "server finished"; | 
|  | 3288 |  | 
|  | 3289 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3290 | sha384_psa = psa_hash_operation_init(); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3291 |  | 
|  | 3292 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); | 
|  | 3293 |  | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3294 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3295 | if( status != PSA_SUCCESS ) | 
|  | 3296 | { | 
|  | 3297 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | 
|  | 3298 | return; | 
|  | 3299 | } | 
|  | 3300 |  | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3301 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3302 | if( status != PSA_SUCCESS ) | 
|  | 3303 | { | 
|  | 3304 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | 
|  | 3305 | return; | 
|  | 3306 | } | 
|  | 3307 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); | 
|  | 3308 | #else | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3309 | mbedtls_sha512_init( &sha512 ); | 
|  | 3310 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3311 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha384" ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3312 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3313 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3314 |  | 
|  | 3315 | /* | 
|  | 3316 | * TLSv1.2: | 
|  | 3317 | *   hash = PRF( master, finished_label, | 
|  | 3318 | *               Hash( handshake ) )[0.11] | 
|  | 3319 | */ | 
|  | 3320 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3321 | #if !defined(MBEDTLS_SHA512_ALT) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3322 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) | 
|  | 3323 | sha512.state, sizeof( sha512.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3324 | #endif | 
| Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 3325 | /* | 
|  | 3326 | * For SHA-384, we can save 16 bytes by keeping padbuf 48 bytes long. | 
|  | 3327 | * However, to avoid stringop-overflow warning in gcc, we have to cast | 
|  | 3328 | * mbedtls_sha512_finish_ret(). | 
|  | 3329 | */ | 
|  | 3330 | finish_sha384_t finish = (finish_sha384_t)mbedtls_sha512_finish_ret; | 
|  | 3331 | finish( &sha512, padbuf ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3332 |  | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3333 | mbedtls_sha512_free( &sha512 ); | 
|  | 3334 | #endif | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3335 |  | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3336 | ssl->handshake->tls_prf( session->master, 48, sender, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3337 | padbuf, 48, buf, len ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3338 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3339 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3340 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3341 | mbedtls_platform_zeroize(  padbuf, sizeof( padbuf ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3342 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3343 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3344 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3345 | #endif /* MBEDTLS_SHA512_C */ | 
|  | 3346 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3347 |  | 
| Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 3348 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3349 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3350 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3351 |  | 
|  | 3352 | /* | 
|  | 3353 | * Free our handshake params | 
|  | 3354 | */ | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3355 | mbedtls_ssl_handshake_free( ssl ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3356 | mbedtls_free( ssl->handshake ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3357 | ssl->handshake = NULL; | 
|  | 3358 |  | 
|  | 3359 | /* | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3360 | * Free the previous transform and swith in the current one | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3361 | */ | 
|  | 3362 | if( ssl->transform ) | 
|  | 3363 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3364 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 3365 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3366 | } | 
|  | 3367 | ssl->transform = ssl->transform_negotiate; | 
|  | 3368 | ssl->transform_negotiate = NULL; | 
|  | 3369 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3370 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3371 | } | 
|  | 3372 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3373 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3374 | { | 
|  | 3375 | int resume = ssl->handshake->resume; | 
|  | 3376 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3377 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3378 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3379 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 3380 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3381 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3382 | ssl->renego_status =  MBEDTLS_SSL_RENEGOTIATION_DONE; | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3383 | ssl->renego_records_seen = 0; | 
|  | 3384 | } | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3385 | #endif | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3386 |  | 
|  | 3387 | /* | 
|  | 3388 | * Free the previous session and switch in the current one | 
|  | 3389 | */ | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3390 | if( ssl->session ) | 
|  | 3391 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3392 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 3393 | /* RFC 7366 3.1: keep the EtM state */ | 
|  | 3394 | ssl->session_negotiate->encrypt_then_mac = | 
|  | 3395 | ssl->session->encrypt_then_mac; | 
|  | 3396 | #endif | 
|  | 3397 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3398 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 3399 | mbedtls_free( ssl->session ); | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3400 | } | 
|  | 3401 | ssl->session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3402 | ssl->session_negotiate = NULL; | 
|  | 3403 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3404 | /* | 
|  | 3405 | * Add cache entry | 
|  | 3406 | */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3407 | if( ssl->conf->f_set_cache != NULL && | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 3408 | ssl->session->id_len != 0 && | 
| Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 3409 | resume == 0 ) | 
|  | 3410 | { | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 3411 | 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] | 3412 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); | 
| Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 3413 | } | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3414 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3415 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3416 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3417 | ssl->handshake->flight != NULL ) | 
|  | 3418 | { | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3419 | /* Cancel handshake timer */ | 
| Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3420 | mbedtls_ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3421 |  | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3422 | /* Keep last flight around in case we need to resend it: | 
|  | 3423 | * we need the handshake and transform structures for that */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3424 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3425 | } | 
|  | 3426 | else | 
|  | 3427 | #endif | 
| Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 3428 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3429 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3430 | ssl->state++; | 
|  | 3431 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3432 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3433 | } | 
|  | 3434 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3435 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3436 | { | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3437 | int ret, hash_len; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3438 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3439 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3440 |  | 
| Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3441 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); | 
| Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 3442 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3443 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3444 |  | 
| Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 3445 | /* | 
|  | 3446 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites | 
|  | 3447 | * may define some other value. Currently (early 2016), no defined | 
|  | 3448 | * ciphersuite does this (and this is unlikely to change as activity has | 
|  | 3449 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. | 
|  | 3450 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3451 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3452 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3453 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3454 | ssl->verify_data_len = hash_len; | 
|  | 3455 | 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] | 3456 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3457 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3458 | ssl->out_msglen  = 4 + hash_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3459 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 3460 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_FINISHED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3461 |  | 
|  | 3462 | /* | 
|  | 3463 | * In case of session resuming, invert the client and server | 
|  | 3464 | * ChangeCipherSpec messages order. | 
|  | 3465 | */ | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3466 | if( ssl->handshake->resume != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3467 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3468 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3469 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3470 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3471 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3472 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3473 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3474 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3475 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3476 | } | 
|  | 3477 | else | 
|  | 3478 | ssl->state++; | 
|  | 3479 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3480 | /* | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 3481 | * Switch to our negotiated transform and session parameters for outbound | 
|  | 3482 | * data. | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3483 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3484 | 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] | 3485 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3486 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3487 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3488 | { | 
|  | 3489 | unsigned char i; | 
|  | 3490 |  | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3491 | /* Remember current epoch settings for resending */ | 
|  | 3492 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3493 | 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] | 3494 |  | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3495 | /* Set sequence_number to zero */ | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3496 | memset( ssl->cur_out_ctr + 2, 0, 6 ); | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3497 |  | 
|  | 3498 | /* Increment epoch */ | 
|  | 3499 | for( i = 2; i > 0; i-- ) | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3500 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3501 | break; | 
|  | 3502 |  | 
|  | 3503 | /* The loop goes to its end iff the counter is wrapping */ | 
|  | 3504 | if( i == 0 ) | 
|  | 3505 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3506 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); | 
|  | 3507 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3508 | } | 
|  | 3509 | } | 
|  | 3510 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3511 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3512 | memset( ssl->cur_out_ctr, 0, 8 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3513 |  | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3514 | ssl->transform_out = ssl->transform_negotiate; | 
|  | 3515 | ssl->session_out = ssl->session_negotiate; | 
|  | 3516 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3517 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 3518 | if( mbedtls_ssl_hw_record_activate != NULL ) | 
| Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 3519 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3520 | 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] | 3521 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3522 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); | 
|  | 3523 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 3524 | } | 
|  | 3525 | } | 
|  | 3526 | #endif | 
|  | 3527 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3528 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3529 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3530 | mbedtls_ssl_send_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3531 | #endif | 
|  | 3532 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3533 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3534 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3535 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3536 | return( ret ); | 
|  | 3537 | } | 
|  | 3538 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3539 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3540 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 3541 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 3542 | { | 
|  | 3543 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 3544 | return( ret ); | 
|  | 3545 | } | 
|  | 3546 | #endif | 
|  | 3547 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3548 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3549 |  | 
|  | 3550 | return( 0 ); | 
|  | 3551 | } | 
|  | 3552 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3553 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3554 | #define SSL_MAX_HASH_LEN 36 | 
|  | 3555 | #else | 
|  | 3556 | #define SSL_MAX_HASH_LEN 12 | 
|  | 3557 | #endif | 
|  | 3558 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3559 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3560 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3561 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3562 | unsigned int hash_len; | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3563 | unsigned char buf[SSL_MAX_HASH_LEN]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3564 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3565 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3566 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3567 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3568 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3569 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3570 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3571 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3572 | return( ret ); | 
|  | 3573 | } | 
|  | 3574 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3575 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3576 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3577 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3578 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3579 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3580 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3581 | } | 
|  | 3582 |  | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3583 | /* 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] | 3584 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 3585 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3586 | hash_len = 36; | 
|  | 3587 | else | 
|  | 3588 | #endif | 
|  | 3589 | hash_len = 12; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3590 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3591 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || | 
|  | 3592 | ssl->in_hslen  != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3593 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3594 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3595 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3596 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3597 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3598 | } | 
|  | 3599 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3600 | if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), | 
| Manuel Pégourié-Gonnard | 4abc327 | 2014-09-10 12:02:46 +0000 | [diff] [blame] | 3601 | buf, hash_len ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3602 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3603 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3604 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 3605 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3606 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3607 | } | 
|  | 3608 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3609 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3610 | ssl->verify_data_len = hash_len; | 
|  | 3611 | memcpy( ssl->peer_verify_data, buf, hash_len ); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3612 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3613 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3614 | if( ssl->handshake->resume != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3615 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3616 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3617 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3618 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3619 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3620 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3621 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3622 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3623 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3624 | } | 
|  | 3625 | else | 
|  | 3626 | ssl->state++; | 
|  | 3627 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3628 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3629 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3630 | mbedtls_ssl_recv_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3631 | #endif | 
|  | 3632 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3633 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3634 |  | 
|  | 3635 | return( 0 ); | 
|  | 3636 | } | 
|  | 3637 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3638 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3639 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3640 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3641 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3642 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 3643 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 3644 | mbedtls_md5_init(   &handshake->fin_md5  ); | 
|  | 3645 | mbedtls_sha1_init(   &handshake->fin_sha1 ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3646 | mbedtls_md5_starts_ret( &handshake->fin_md5  ); | 
|  | 3647 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3648 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3649 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3650 | #if defined(MBEDTLS_SHA256_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3651 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 3652 | handshake->fin_sha256_psa = psa_hash_operation_init(); | 
|  | 3653 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); | 
|  | 3654 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3655 | mbedtls_sha256_init(   &handshake->fin_sha256    ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3656 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3657 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3658 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3659 | #if defined(MBEDTLS_SHA512_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3660 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3661 | handshake->fin_sha384_psa = psa_hash_operation_init(); | 
|  | 3662 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3663 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3664 | mbedtls_sha512_init(   &handshake->fin_sha512    ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3665 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3666 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3667 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3668 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3669 |  | 
|  | 3670 | handshake->update_checksum = ssl_update_checksum_start; | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3671 |  | 
|  | 3672 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3673 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3674 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); | 
|  | 3675 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3676 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3677 | #if defined(MBEDTLS_DHM_C) | 
|  | 3678 | mbedtls_dhm_init( &handshake->dhm_ctx ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3679 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3680 | #if defined(MBEDTLS_ECDH_C) | 
|  | 3681 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3682 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3683 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3684 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3685 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 3686 | handshake->ecjpake_cache = NULL; | 
|  | 3687 | handshake->ecjpake_cache_len = 0; | 
|  | 3688 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3689 | #endif | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3690 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3691 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3692 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3693 | #endif | 
|  | 3694 |  | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3695 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 3696 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; | 
|  | 3697 | #endif | 
| Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3698 |  | 
|  | 3699 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 3700 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 3701 | mbedtls_pk_init( &handshake->peer_pubkey ); | 
|  | 3702 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3703 | } | 
|  | 3704 |  | 
| Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3705 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3706 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3707 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 3708 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3709 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); | 
|  | 3710 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 3711 |  | 
| Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 3712 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3713 | mbedtls_md_init( &transform->md_ctx_enc ); | 
|  | 3714 | mbedtls_md_init( &transform->md_ctx_dec ); | 
| Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 3715 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3716 | } | 
|  | 3717 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3718 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3719 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3720 | memset( session, 0, sizeof(mbedtls_ssl_session) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3721 | } | 
|  | 3722 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3723 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3724 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3725 | /* Clear old handshake information if present */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3726 | if( ssl->transform_negotiate ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3727 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3728 | if( ssl->session_negotiate ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3729 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3730 | if( ssl->handshake ) | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3731 | mbedtls_ssl_handshake_free( ssl ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3732 |  | 
|  | 3733 | /* | 
|  | 3734 | * Either the pointers are now NULL or cleared properly and can be freed. | 
|  | 3735 | * Now allocate missing structures. | 
|  | 3736 | */ | 
|  | 3737 | if( ssl->transform_negotiate == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3738 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3739 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3740 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3741 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3742 | if( ssl->session_negotiate == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3743 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3744 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3745 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3746 |  | 
| Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 3747 | if( ssl->handshake == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3748 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3749 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3750 | } | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3751 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3752 | /* If the buffers are too small - reallocate */ | 
| Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 3753 |  | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3754 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, | 
|  | 3755 | MBEDTLS_SSL_OUT_BUFFER_LEN ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3756 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3757 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3758 | /* All pointers should exist and can be directly freed without issue */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3759 | if( ssl->handshake == NULL || | 
|  | 3760 | ssl->transform_negotiate == NULL || | 
|  | 3761 | ssl->session_negotiate == NULL ) | 
|  | 3762 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3763 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3764 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3765 | mbedtls_free( ssl->handshake ); | 
|  | 3766 | mbedtls_free( ssl->transform_negotiate ); | 
|  | 3767 | mbedtls_free( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3768 |  | 
|  | 3769 | ssl->handshake = NULL; | 
|  | 3770 | ssl->transform_negotiate = NULL; | 
|  | 3771 | ssl->session_negotiate = NULL; | 
|  | 3772 |  | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3773 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3774 | } | 
|  | 3775 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3776 | /* Initialize structures */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3777 | mbedtls_ssl_session_init( ssl->session_negotiate ); | 
| Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3778 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); | 
| Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 3779 | ssl_handshake_params_init( ssl->handshake ); | 
|  | 3780 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3781 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3782 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 3783 | { | 
|  | 3784 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3785 |  | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3786 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 3787 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; | 
|  | 3788 | else | 
|  | 3789 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3790 |  | 
| Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3791 | mbedtls_ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3792 | } | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3793 | #endif | 
|  | 3794 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3795 | return( 0 ); | 
|  | 3796 | } | 
|  | 3797 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3798 | #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] | 3799 | /* Dummy cookie callbacks for defaults */ | 
|  | 3800 | static int ssl_cookie_write_dummy( void *ctx, | 
|  | 3801 | unsigned char **p, unsigned char *end, | 
|  | 3802 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 3803 | { | 
|  | 3804 | ((void) ctx); | 
|  | 3805 | ((void) p); | 
|  | 3806 | ((void) end); | 
|  | 3807 | ((void) cli_id); | 
|  | 3808 | ((void) cli_id_len); | 
|  | 3809 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3810 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3811 | } | 
|  | 3812 |  | 
|  | 3813 | static int ssl_cookie_check_dummy( void *ctx, | 
|  | 3814 | const unsigned char *cookie, size_t cookie_len, | 
|  | 3815 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 3816 | { | 
|  | 3817 | ((void) ctx); | 
|  | 3818 | ((void) cookie); | 
|  | 3819 | ((void) cookie_len); | 
|  | 3820 | ((void) cli_id); | 
|  | 3821 | ((void) cli_id_len); | 
|  | 3822 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3823 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3824 | } | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3825 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3826 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3827 | /* | 
|  | 3828 | * Initialize an SSL context | 
|  | 3829 | */ | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3830 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) | 
|  | 3831 | { | 
|  | 3832 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); | 
|  | 3833 | } | 
|  | 3834 |  | 
|  | 3835 | /* | 
|  | 3836 | * Setup an SSL context | 
|  | 3837 | */ | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 3838 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3839 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 3840 | const mbedtls_ssl_config *conf ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3841 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3842 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3843 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 3844 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3845 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3846 | ssl->conf = conf; | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3847 |  | 
|  | 3848 | /* | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3849 | * Prepare base structures | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3850 | */ | 
| k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 3851 |  | 
|  | 3852 | /* Set to NULL in case of an error condition */ | 
|  | 3853 | ssl->out_buf = NULL; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3854 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3855 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3856 | ssl->in_buf_len = in_buf_len; | 
|  | 3857 | #endif | 
|  | 3858 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3859 | if( ssl->in_buf == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3860 | { | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3861 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", in_buf_len ) ); | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3862 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3863 | goto error; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3864 | } | 
|  | 3865 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3866 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3867 | ssl->out_buf_len = out_buf_len; | 
|  | 3868 | #endif | 
|  | 3869 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3870 | if( ssl->out_buf == NULL ) | 
|  | 3871 | { | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3872 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", out_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; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3875 | } | 
|  | 3876 |  | 
| Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3877 | mbedtls_ssl_reset_in_out_pointers( ssl ); | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3878 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3879 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 3880 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3881 | #endif | 
|  | 3882 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3883 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3884 | goto error; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3885 |  | 
|  | 3886 | return( 0 ); | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3887 |  | 
|  | 3888 | error: | 
|  | 3889 | mbedtls_free( ssl->in_buf ); | 
|  | 3890 | mbedtls_free( ssl->out_buf ); | 
|  | 3891 |  | 
|  | 3892 | ssl->conf = NULL; | 
|  | 3893 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3894 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3895 | ssl->in_buf_len = 0; | 
|  | 3896 | ssl->out_buf_len = 0; | 
|  | 3897 | #endif | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3898 | ssl->in_buf = NULL; | 
|  | 3899 | ssl->out_buf = NULL; | 
|  | 3900 |  | 
|  | 3901 | ssl->in_hdr = NULL; | 
|  | 3902 | ssl->in_ctr = NULL; | 
|  | 3903 | ssl->in_len = NULL; | 
|  | 3904 | ssl->in_iv = NULL; | 
|  | 3905 | ssl->in_msg = NULL; | 
|  | 3906 |  | 
|  | 3907 | ssl->out_hdr = NULL; | 
|  | 3908 | ssl->out_ctr = NULL; | 
|  | 3909 | ssl->out_len = NULL; | 
|  | 3910 | ssl->out_iv = NULL; | 
|  | 3911 | ssl->out_msg = NULL; | 
|  | 3912 |  | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3913 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3914 | } | 
|  | 3915 |  | 
|  | 3916 | /* | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3917 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 3918 | * all application-set variables, function pointers and data. | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3919 | * | 
|  | 3920 | * If partial is non-zero, keep data in the input buffer and client ID. | 
|  | 3921 | * (Use when a DTLS client reconnects from the same port.) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3922 | */ | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3923 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3924 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3925 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3926 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 3927 | size_t in_buf_len = ssl->in_buf_len; | 
|  | 3928 | size_t out_buf_len = ssl->out_buf_len; | 
|  | 3929 | #else | 
|  | 3930 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 3931 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
|  | 3932 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3933 |  | 
| Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 3934 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) ||     \ | 
|  | 3935 | !defined(MBEDTLS_SSL_SRV_C) | 
|  | 3936 | ((void) partial); | 
|  | 3937 | #endif | 
|  | 3938 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3939 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3940 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3941 | /* Cancel any possibly running timer */ | 
| Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3942 | mbedtls_ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3943 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3944 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 3945 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3946 | ssl->renego_records_seen = 0; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3947 |  | 
|  | 3948 | ssl->verify_data_len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3949 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); | 
|  | 3950 | 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] | 3951 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3952 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3953 |  | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3954 | ssl->in_offt = NULL; | 
| Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3955 | mbedtls_ssl_reset_in_out_pointers( ssl ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3956 |  | 
|  | 3957 | ssl->in_msgtype = 0; | 
|  | 3958 | ssl->in_msglen = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3959 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3960 | ssl->next_record_offset = 0; | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 3961 | ssl->in_epoch = 0; | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3962 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3963 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Hanno Becker | 7e8e6a6 | 2020-02-05 10:45:48 +0000 | [diff] [blame] | 3964 | mbedtls_ssl_dtls_replay_reset( ssl ); | 
| Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3965 | #endif | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3966 |  | 
|  | 3967 | ssl->in_hslen = 0; | 
|  | 3968 | ssl->nb_zero = 0; | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3969 |  | 
|  | 3970 | ssl->keep_current_message = 0; | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3971 |  | 
|  | 3972 | ssl->out_msgtype = 0; | 
|  | 3973 | ssl->out_msglen = 0; | 
|  | 3974 | ssl->out_left = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3975 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
|  | 3976 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) | 
| Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 3977 | ssl->split_done = 0; | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 3978 | #endif | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3979 |  | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3980 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); | 
|  | 3981 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3982 | ssl->transform_in = NULL; | 
|  | 3983 | ssl->transform_out = NULL; | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3984 |  | 
| Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 3985 | ssl->session_in = NULL; | 
|  | 3986 | ssl->session_out = NULL; | 
|  | 3987 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3988 | memset( ssl->out_buf, 0, out_buf_len ); | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3989 |  | 
|  | 3990 | #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] | 3991 | if( partial == 0 ) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3992 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ | 
|  | 3993 | { | 
|  | 3994 | ssl->in_left = 0; | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3995 | memset( ssl->in_buf, 0, in_buf_len ); | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3996 | } | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3997 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3998 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 3999 | if( mbedtls_ssl_hw_record_reset != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4000 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4001 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); | 
|  | 4002 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4003 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4004 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); | 
|  | 4005 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4006 | } | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4007 | } | 
|  | 4008 | #endif | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4009 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4010 | if( ssl->transform ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4011 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4012 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 4013 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4014 | ssl->transform = NULL; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4015 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4016 |  | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4017 | if( ssl->session ) | 
|  | 4018 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4019 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 4020 | mbedtls_free( ssl->session ); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4021 | ssl->session = NULL; | 
|  | 4022 | } | 
|  | 4023 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4024 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4025 | ssl->alpn_chosen = NULL; | 
|  | 4026 | #endif | 
|  | 4027 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4028 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 4029 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4030 | if( partial == 0 ) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 4031 | #endif | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4032 | { | 
|  | 4033 | mbedtls_free( ssl->cli_id ); | 
|  | 4034 | ssl->cli_id = NULL; | 
|  | 4035 | ssl->cli_id_len = 0; | 
|  | 4036 | } | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4037 | #endif | 
|  | 4038 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4039 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
|  | 4040 | return( ret ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4041 |  | 
|  | 4042 | return( 0 ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 4043 | } | 
|  | 4044 |  | 
| Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 4045 | /* | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4046 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 4047 | * all application-set variables, function pointers and data. | 
|  | 4048 | */ | 
|  | 4049 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) | 
|  | 4050 | { | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4051 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4052 | } | 
|  | 4053 |  | 
|  | 4054 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4055 | * SSL set accessors | 
|  | 4056 | */ | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4057 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4058 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4059 | conf->endpoint   = endpoint; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4060 | } | 
|  | 4061 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4062 | 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] | 4063 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4064 | conf->transport = transport; | 
| Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 4065 | } | 
|  | 4066 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4067 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4068 | 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] | 4069 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4070 | conf->anti_replay = mode; | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4071 | } | 
|  | 4072 | #endif | 
|  | 4073 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4074 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4075 | 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] | 4076 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4077 | conf->badmac_limit = limit; | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 4078 | } | 
|  | 4079 | #endif | 
|  | 4080 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4081 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4082 |  | 
| Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 4083 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, | 
|  | 4084 | unsigned allow_packing ) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4085 | { | 
|  | 4086 | ssl->disable_datagram_packing = !allow_packing; | 
|  | 4087 | } | 
|  | 4088 |  | 
|  | 4089 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, | 
|  | 4090 | uint32_t min, uint32_t max ) | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 4091 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4092 | conf->hs_timeout_min = min; | 
|  | 4093 | conf->hs_timeout_max = max; | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 4094 | } | 
|  | 4095 | #endif | 
|  | 4096 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4097 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4098 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4099 | conf->authmode   = authmode; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4100 | } | 
|  | 4101 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4102 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4103 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4104 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4105 | void *p_vrfy ) | 
|  | 4106 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4107 | conf->f_vrfy      = f_vrfy; | 
|  | 4108 | conf->p_vrfy      = p_vrfy; | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4109 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4110 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4111 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4112 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 4113 | int (*f_rng)(void *, unsigned char *, size_t), | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4114 | void *p_rng ) | 
|  | 4115 | { | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 4116 | conf->f_rng      = f_rng; | 
|  | 4117 | conf->p_rng      = p_rng; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4118 | } | 
|  | 4119 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4120 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 4121 | void (*f_dbg)(void *, int, const char *, int, const char *), | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4122 | void  *p_dbg ) | 
|  | 4123 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4124 | conf->f_dbg      = f_dbg; | 
|  | 4125 | conf->p_dbg      = p_dbg; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4126 | } | 
|  | 4127 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4128 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 4129 | void *p_bio, | 
| Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 4130 | mbedtls_ssl_send_t *f_send, | 
|  | 4131 | mbedtls_ssl_recv_t *f_recv, | 
|  | 4132 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 4133 | { | 
|  | 4134 | ssl->p_bio          = p_bio; | 
|  | 4135 | ssl->f_send         = f_send; | 
|  | 4136 | ssl->f_recv         = f_recv; | 
|  | 4137 | ssl->f_recv_timeout = f_recv_timeout; | 
| Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 4138 | } | 
|  | 4139 |  | 
| Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 4140 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4141 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) | 
|  | 4142 | { | 
|  | 4143 | ssl->mtu = mtu; | 
|  | 4144 | } | 
|  | 4145 | #endif | 
|  | 4146 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4147 | 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] | 4148 | { | 
|  | 4149 | conf->read_timeout   = timeout; | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 4150 | } | 
|  | 4151 |  | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 4152 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, | 
|  | 4153 | void *p_timer, | 
| Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 4154 | mbedtls_ssl_set_timer_t *f_set_timer, | 
|  | 4155 | mbedtls_ssl_get_timer_t *f_get_timer ) | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 4156 | { | 
|  | 4157 | ssl->p_timer        = p_timer; | 
|  | 4158 | ssl->f_set_timer    = f_set_timer; | 
|  | 4159 | ssl->f_get_timer    = f_get_timer; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 4160 |  | 
|  | 4161 | /* Make sure we start with no timer running */ | 
| Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 4162 | mbedtls_ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 4163 | } | 
|  | 4164 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4165 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4166 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 4167 | void *p_cache, | 
|  | 4168 | int (*f_get_cache)(void *, mbedtls_ssl_session *), | 
|  | 4169 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4170 | { | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 4171 | conf->p_cache = p_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4172 | conf->f_get_cache = f_get_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4173 | conf->f_set_cache = f_set_cache; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4174 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4175 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4176 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4177 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 4178 | 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] | 4179 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4180 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4181 |  | 
|  | 4182 | if( ssl == NULL || | 
|  | 4183 | session == NULL || | 
|  | 4184 | ssl->session_negotiate == NULL || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4185 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4186 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4187 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4188 | } | 
|  | 4189 |  | 
| Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 4190 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, | 
|  | 4191 | session ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4192 | return( ret ); | 
|  | 4193 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4194 | ssl->handshake->resume = 1; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4195 |  | 
|  | 4196 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4197 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4198 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4199 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4200 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4201 | const int *ciphersuites ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4202 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4203 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; | 
|  | 4204 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; | 
|  | 4205 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; | 
|  | 4206 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 4207 | } | 
|  | 4208 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4209 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 4210 | const int *ciphersuites, | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 4211 | int major, int minor ) | 
|  | 4212 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4213 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 4214 | return; | 
|  | 4215 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4216 | 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] | 4217 | return; | 
|  | 4218 |  | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4219 | conf->ciphersuite_list[minor] = ciphersuites; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4220 | } | 
|  | 4221 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4222 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 4223 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, | 
| Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 4224 | const mbedtls_x509_crt_profile *profile ) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 4225 | { | 
|  | 4226 | conf->cert_profile = profile; | 
|  | 4227 | } | 
|  | 4228 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4229 | /* Append a new keycert entry to a (possibly empty) list */ | 
|  | 4230 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, | 
|  | 4231 | mbedtls_x509_crt *cert, | 
|  | 4232 | mbedtls_pk_context *key ) | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4233 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4234 | mbedtls_ssl_key_cert *new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4235 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4236 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); | 
|  | 4237 | if( new_cert == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4238 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4239 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4240 | new_cert->cert = cert; | 
|  | 4241 | new_cert->key  = key; | 
|  | 4242 | new_cert->next = NULL; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4243 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4244 | /* Update head is the list was null, else add to the end */ | 
|  | 4245 | if( *head == NULL ) | 
| Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 4246 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4247 | *head = new_cert; | 
| Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 4248 | } | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4249 | else | 
|  | 4250 | { | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4251 | mbedtls_ssl_key_cert *cur = *head; | 
|  | 4252 | while( cur->next != NULL ) | 
|  | 4253 | cur = cur->next; | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4254 | cur->next = new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4255 | } | 
|  | 4256 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4257 | return( 0 ); | 
|  | 4258 | } | 
|  | 4259 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4260 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4261 | mbedtls_x509_crt *own_cert, | 
|  | 4262 | mbedtls_pk_context *pk_key ) | 
|  | 4263 | { | 
| Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 4264 | 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] | 4265 | } | 
|  | 4266 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4267 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4268 | mbedtls_x509_crt *ca_chain, | 
|  | 4269 | mbedtls_x509_crl *ca_crl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4270 | { | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4271 | conf->ca_chain   = ca_chain; | 
|  | 4272 | conf->ca_crl     = ca_crl; | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 4273 |  | 
|  | 4274 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
|  | 4275 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() | 
|  | 4276 | * cannot be used together. */ | 
|  | 4277 | conf->f_ca_cb = NULL; | 
|  | 4278 | conf->p_ca_cb = NULL; | 
|  | 4279 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4280 | } | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 4281 |  | 
|  | 4282 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
|  | 4283 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, | 
| Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 4284 | mbedtls_x509_crt_ca_cb_t f_ca_cb, | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 4285 | void *p_ca_cb ) | 
|  | 4286 | { | 
|  | 4287 | conf->f_ca_cb = f_ca_cb; | 
|  | 4288 | conf->p_ca_cb = p_ca_cb; | 
|  | 4289 |  | 
|  | 4290 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() | 
|  | 4291 | * cannot be used together. */ | 
|  | 4292 | conf->ca_chain   = NULL; | 
|  | 4293 | conf->ca_crl     = NULL; | 
|  | 4294 | } | 
|  | 4295 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4296 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 4297 |  | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 4298 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 4299 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, | 
|  | 4300 | mbedtls_x509_crt *own_cert, | 
|  | 4301 | mbedtls_pk_context *pk_key ) | 
|  | 4302 | { | 
|  | 4303 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, | 
|  | 4304 | own_cert, pk_key ) ); | 
|  | 4305 | } | 
| Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 4306 |  | 
|  | 4307 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, | 
|  | 4308 | mbedtls_x509_crt *ca_chain, | 
|  | 4309 | mbedtls_x509_crl *ca_crl ) | 
|  | 4310 | { | 
|  | 4311 | ssl->handshake->sni_ca_chain   = ca_chain; | 
|  | 4312 | ssl->handshake->sni_ca_crl     = ca_crl; | 
|  | 4313 | } | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 4314 |  | 
|  | 4315 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, | 
|  | 4316 | int authmode ) | 
|  | 4317 | { | 
|  | 4318 | ssl->handshake->sni_authmode = authmode; | 
|  | 4319 | } | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 4320 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
|  | 4321 |  | 
| Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 4322 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 4323 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, | 
|  | 4324 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | 
|  | 4325 | void *p_vrfy ) | 
|  | 4326 | { | 
|  | 4327 | ssl->f_vrfy = f_vrfy; | 
|  | 4328 | ssl->p_vrfy = p_vrfy; | 
|  | 4329 | } | 
|  | 4330 | #endif | 
|  | 4331 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4332 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 4333 | /* | 
|  | 4334 | * Set EC J-PAKE password for current handshake | 
|  | 4335 | */ | 
|  | 4336 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, | 
|  | 4337 | const unsigned char *pw, | 
|  | 4338 | size_t pw_len ) | 
|  | 4339 | { | 
|  | 4340 | mbedtls_ecjpake_role role; | 
|  | 4341 |  | 
| Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 4342 | if( ssl->handshake == NULL || ssl->conf == NULL ) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 4343 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 4344 |  | 
|  | 4345 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 4346 | role = MBEDTLS_ECJPAKE_SERVER; | 
|  | 4347 | else | 
|  | 4348 | role = MBEDTLS_ECJPAKE_CLIENT; | 
|  | 4349 |  | 
|  | 4350 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, | 
|  | 4351 | role, | 
|  | 4352 | MBEDTLS_MD_SHA256, | 
|  | 4353 | MBEDTLS_ECP_DP_SECP256R1, | 
|  | 4354 | pw, pw_len ) ); | 
|  | 4355 | } | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4356 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 4357 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4358 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4359 |  | 
|  | 4360 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) | 
|  | 4361 | { | 
|  | 4362 | /* Remove reference to existing PSK, if any. */ | 
|  | 4363 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4364 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4365 | { | 
|  | 4366 | /* The maintenance of the PSK key slot is the | 
|  | 4367 | * user's responsibility. */ | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4368 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4369 | } | 
| Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 4370 | /* This and the following branch should never | 
|  | 4371 | * be taken simultaenously as we maintain the | 
|  | 4372 | * invariant that raw and opaque PSKs are never | 
|  | 4373 | * configured simultaneously. As a safeguard, | 
|  | 4374 | * though, `else` is omitted here. */ | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4375 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 4376 | if( conf->psk != NULL ) | 
|  | 4377 | { | 
|  | 4378 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | 
|  | 4379 |  | 
|  | 4380 | mbedtls_free( conf->psk ); | 
|  | 4381 | conf->psk = NULL; | 
|  | 4382 | conf->psk_len = 0; | 
|  | 4383 | } | 
|  | 4384 |  | 
|  | 4385 | /* Remove reference to PSK identity, if any. */ | 
|  | 4386 | if( conf->psk_identity != NULL ) | 
|  | 4387 | { | 
|  | 4388 | mbedtls_free( conf->psk_identity ); | 
|  | 4389 | conf->psk_identity = NULL; | 
|  | 4390 | conf->psk_identity_len = 0; | 
|  | 4391 | } | 
|  | 4392 | } | 
|  | 4393 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4394 | /* This function assumes that PSK identity in the SSL config is unset. | 
|  | 4395 | * It checks that the provided identity is well-formed and attempts | 
|  | 4396 | * to make a copy of it in the SSL config. | 
|  | 4397 | * On failure, the PSK identity in the config remains unset. */ | 
|  | 4398 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, | 
|  | 4399 | unsigned char const *psk_identity, | 
|  | 4400 | size_t psk_identity_len ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 4401 | { | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 4402 | /* Identity len will be encoded on two bytes */ | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4403 | if( psk_identity               == NULL || | 
|  | 4404 | ( psk_identity_len >> 16 ) != 0    || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4405 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 4406 | { | 
|  | 4407 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 4408 | } | 
|  | 4409 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4410 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); | 
|  | 4411 | if( conf->psk_identity == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4412 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4413 |  | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 4414 | conf->psk_identity_len = psk_identity_len; | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 4415 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); | 
| Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 4416 |  | 
|  | 4417 | return( 0 ); | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4418 | } | 
|  | 4419 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4420 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, | 
|  | 4421 | const unsigned char *psk, size_t psk_len, | 
|  | 4422 | const unsigned char *psk_identity, size_t psk_identity_len ) | 
|  | 4423 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4424 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4425 | /* Remove opaque/raw PSK + PSK Identity */ | 
|  | 4426 | ssl_conf_remove_psk( conf ); | 
|  | 4427 |  | 
|  | 4428 | /* Check and set raw PSK */ | 
| Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 4429 | if( psk == NULL ) | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4430 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 4431 | if( psk_len == 0 ) | 
|  | 4432 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 4433 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | 
|  | 4434 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 4435 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4436 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) | 
|  | 4437 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 4438 | conf->psk_len = psk_len; | 
|  | 4439 | memcpy( conf->psk, psk, conf->psk_len ); | 
|  | 4440 |  | 
|  | 4441 | /* Check and set PSK Identity */ | 
|  | 4442 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); | 
|  | 4443 | if( ret != 0 ) | 
|  | 4444 | ssl_conf_remove_psk( conf ); | 
|  | 4445 |  | 
|  | 4446 | return( ret ); | 
|  | 4447 | } | 
|  | 4448 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4449 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) | 
|  | 4450 | { | 
|  | 4451 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4452 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4453 | { | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4454 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4455 | } | 
|  | 4456 | else | 
|  | 4457 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 4458 | if( ssl->handshake->psk != NULL ) | 
|  | 4459 | { | 
|  | 4460 | mbedtls_platform_zeroize( ssl->handshake->psk, | 
|  | 4461 | ssl->handshake->psk_len ); | 
|  | 4462 | mbedtls_free( ssl->handshake->psk ); | 
|  | 4463 | ssl->handshake->psk_len = 0; | 
|  | 4464 | } | 
|  | 4465 | } | 
|  | 4466 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4467 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, | 
|  | 4468 | const unsigned char *psk, size_t psk_len ) | 
|  | 4469 | { | 
|  | 4470 | if( psk == NULL || ssl->handshake == NULL ) | 
|  | 4471 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 4472 |  | 
|  | 4473 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | 
|  | 4474 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 4475 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4476 | ssl_remove_psk( ssl ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4477 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4478 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4479 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4480 |  | 
|  | 4481 | ssl->handshake->psk_len = psk_len; | 
|  | 4482 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); | 
|  | 4483 |  | 
|  | 4484 | return( 0 ); | 
|  | 4485 | } | 
|  | 4486 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4487 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 4488 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4489 | psa_key_id_t psk, | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4490 | const unsigned char *psk_identity, | 
|  | 4491 | size_t psk_identity_len ) | 
|  | 4492 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4493 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4494 | /* Clear opaque/raw PSK + PSK Identity, if present. */ | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4495 | ssl_conf_remove_psk( conf ); | 
|  | 4496 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4497 | /* Check and set opaque PSK */ | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4498 | if( mbedtls_svc_key_id_is_null( psk ) ) | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4499 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4500 | conf->psk_opaque = psk; | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4501 |  | 
|  | 4502 | /* Check and set PSK Identity */ | 
|  | 4503 | ret = ssl_conf_set_psk_identity( conf, psk_identity, | 
|  | 4504 | psk_identity_len ); | 
|  | 4505 | if( ret != 0 ) | 
|  | 4506 | ssl_conf_remove_psk( conf ); | 
|  | 4507 |  | 
|  | 4508 | return( ret ); | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4509 | } | 
|  | 4510 |  | 
|  | 4511 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4512 | psa_key_id_t psk ) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4513 | { | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4514 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || | 
| Ronald Cron | c26f8d4 | 2020-09-01 10:51:51 +0200 | [diff] [blame] | 4515 | ( ssl->handshake == NULL ) ) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4516 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 4517 |  | 
|  | 4518 | ssl_remove_psk( ssl ); | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4519 | ssl->handshake->psk_opaque = psk; | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4520 | return( 0 ); | 
|  | 4521 | } | 
|  | 4522 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 4523 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4524 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4525 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4526 | size_t), | 
|  | 4527 | void *p_psk ) | 
|  | 4528 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4529 | conf->f_psk = f_psk; | 
|  | 4530 | conf->p_psk = p_psk; | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 4531 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4532 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 4533 |  | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4534 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 4535 |  | 
|  | 4536 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4537 | 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] | 4538 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4539 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4540 |  | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4541 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || | 
|  | 4542 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) | 
|  | 4543 | { | 
|  | 4544 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 4545 | mbedtls_mpi_free( &conf->dhm_G ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4546 | return( ret ); | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4547 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4548 |  | 
|  | 4549 | return( 0 ); | 
|  | 4550 | } | 
| Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 4551 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4552 |  | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4553 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, | 
|  | 4554 | const unsigned char *dhm_P, size_t P_len, | 
|  | 4555 | const unsigned char *dhm_G, size_t G_len ) | 
|  | 4556 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4557 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4558 |  | 
|  | 4559 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || | 
|  | 4560 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) | 
|  | 4561 | { | 
|  | 4562 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 4563 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 4564 | return( ret ); | 
|  | 4565 | } | 
|  | 4566 |  | 
|  | 4567 | return( 0 ); | 
|  | 4568 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4569 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4570 | 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] | 4571 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4572 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4573 |  | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4574 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || | 
|  | 4575 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) | 
|  | 4576 | { | 
|  | 4577 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 4578 | mbedtls_mpi_free( &conf->dhm_G ); | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4579 | return( ret ); | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4580 | } | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4581 |  | 
|  | 4582 | return( 0 ); | 
|  | 4583 | } | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4584 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4585 |  | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4586 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 4587 | /* | 
|  | 4588 | * Set the minimum length for Diffie-Hellman parameters | 
|  | 4589 | */ | 
|  | 4590 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, | 
|  | 4591 | unsigned int bitlen ) | 
|  | 4592 | { | 
|  | 4593 | conf->dhm_min_bitlen = bitlen; | 
|  | 4594 | } | 
|  | 4595 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ | 
|  | 4596 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4597 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 4598 | /* | 
|  | 4599 | * Set allowed/preferred hashes for handshake signatures | 
|  | 4600 | */ | 
|  | 4601 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, | 
|  | 4602 | const int *hashes ) | 
|  | 4603 | { | 
|  | 4604 | conf->sig_hashes = hashes; | 
|  | 4605 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4606 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 4607 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4608 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4609 | /* | 
|  | 4610 | * Set the allowed elliptic curves | 
|  | 4611 | */ | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4612 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4613 | const mbedtls_ecp_group_id *curve_list ) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4614 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4615 | conf->curve_list = curve_list; | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4616 | } | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4617 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4618 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4619 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4620 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4621 | { | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4622 | /* Initialize to suppress unnecessary compiler warning */ | 
|  | 4623 | size_t hostname_len = 0; | 
|  | 4624 |  | 
|  | 4625 | /* Check if new hostname is valid before | 
|  | 4626 | * making any change to current one */ | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4627 | if( hostname != NULL ) | 
|  | 4628 | { | 
|  | 4629 | hostname_len = strlen( hostname ); | 
|  | 4630 |  | 
|  | 4631 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) | 
|  | 4632 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 4633 | } | 
|  | 4634 |  | 
|  | 4635 | /* Now it's clear that we will overwrite the old hostname, | 
|  | 4636 | * so we can free it safely */ | 
|  | 4637 |  | 
|  | 4638 | if( ssl->hostname != NULL ) | 
|  | 4639 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4640 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4641 | mbedtls_free( ssl->hostname ); | 
|  | 4642 | } | 
|  | 4643 |  | 
|  | 4644 | /* Passing NULL as hostname shall clear the old one */ | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 4645 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4646 | if( hostname == NULL ) | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4647 | { | 
|  | 4648 | ssl->hostname = NULL; | 
|  | 4649 | } | 
|  | 4650 | else | 
|  | 4651 | { | 
|  | 4652 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4653 | if( ssl->hostname == NULL ) | 
|  | 4654 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4655 |  | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4656 | memcpy( ssl->hostname, hostname, hostname_len ); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4657 |  | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4658 | ssl->hostname[hostname_len] = '\0'; | 
|  | 4659 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4660 |  | 
|  | 4661 | return( 0 ); | 
|  | 4662 | } | 
| Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 4663 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4664 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4665 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4666 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4667 | int (*f_sni)(void *, mbedtls_ssl_context *, | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4668 | const unsigned char *, size_t), | 
|  | 4669 | void *p_sni ) | 
|  | 4670 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4671 | conf->f_sni = f_sni; | 
|  | 4672 | conf->p_sni = p_sni; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4673 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4674 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4675 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4676 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4677 | 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] | 4678 | { | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4679 | size_t cur_len, tot_len; | 
|  | 4680 | const char **p; | 
|  | 4681 |  | 
|  | 4682 | /* | 
| Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4683 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings | 
|  | 4684 | * MUST NOT be truncated." | 
|  | 4685 | * We check lengths now rather than later. | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4686 | */ | 
|  | 4687 | tot_len = 0; | 
|  | 4688 | for( p = protos; *p != NULL; p++ ) | 
|  | 4689 | { | 
|  | 4690 | cur_len = strlen( *p ); | 
|  | 4691 | tot_len += cur_len; | 
|  | 4692 |  | 
| Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 4693 | if( ( cur_len == 0 ) || | 
|  | 4694 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || | 
|  | 4695 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4696 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4697 | } | 
|  | 4698 |  | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4699 | conf->alpn_list = protos; | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4700 |  | 
|  | 4701 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4702 | } | 
|  | 4703 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4704 | 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] | 4705 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4706 | return( ssl->alpn_chosen ); | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4707 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4708 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4709 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4710 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4711 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, | 
|  | 4712 | int support_mki_value ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4713 | { | 
|  | 4714 | conf->dtls_srtp_mki_support = support_mki_value; | 
|  | 4715 | } | 
|  | 4716 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4717 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, | 
|  | 4718 | unsigned char *mki_value, | 
| Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 4719 | uint16_t mki_len ) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4720 | { | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4721 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4722 | { | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4723 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4724 | } | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4725 |  | 
|  | 4726 | 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] | 4727 | { | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4728 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4729 | } | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4730 |  | 
|  | 4731 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); | 
|  | 4732 | ssl->dtls_srtp_info.mki_len = mki_len; | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4733 | return( 0 ); | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4734 | } | 
|  | 4735 |  | 
| Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4736 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4737 | const mbedtls_ssl_srtp_profile *profiles ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4738 | { | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4739 | const mbedtls_ssl_srtp_profile *p; | 
|  | 4740 | size_t list_size = 0; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4741 |  | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4742 | /* check the profiles list: all entry must be valid, | 
|  | 4743 | * 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] | 4744 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && | 
|  | 4745 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; | 
|  | 4746 | p++ ) | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4747 | { | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4748 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) | 
| Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4749 | { | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 4750 | list_size++; | 
|  | 4751 | } | 
|  | 4752 | else | 
|  | 4753 | { | 
|  | 4754 | /* unsupported value, stop parsing and set the size to an error value */ | 
|  | 4755 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4756 | } | 
|  | 4757 | } | 
|  | 4758 |  | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4759 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) | 
| Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 4760 | { | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4761 | conf->dtls_srtp_profile_list = NULL; | 
|  | 4762 | conf->dtls_srtp_profile_list_len = 0; | 
|  | 4763 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 4764 | } | 
|  | 4765 |  | 
| Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 4766 | conf->dtls_srtp_profile_list = profiles; | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4767 | conf->dtls_srtp_profile_list_len = list_size; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4768 |  | 
|  | 4769 | return( 0 ); | 
|  | 4770 | } | 
|  | 4771 |  | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4772 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, | 
|  | 4773 | mbedtls_dtls_srtp_info *dtls_srtp_info ) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4774 | { | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4775 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; | 
|  | 4776 | /* do not copy the mki value if there is no chosen profile */ | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4777 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) | 
| Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4778 | { | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4779 | dtls_srtp_info->mki_len = 0; | 
| Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4780 | } | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4781 | else | 
|  | 4782 | { | 
|  | 4783 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; | 
| Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4784 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, | 
|  | 4785 | ssl->dtls_srtp_info.mki_len ); | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4786 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4787 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4788 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 4789 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4790 | 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] | 4791 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4792 | conf->max_major_ver = major; | 
|  | 4793 | conf->max_minor_ver = minor; | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 4794 | } | 
|  | 4795 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4796 | 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] | 4797 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4798 | conf->min_major_ver = major; | 
|  | 4799 | conf->min_minor_ver = minor; | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 4800 | } | 
|  | 4801 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4802 | #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] | 4803 | 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] | 4804 | { | 
| Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 4805 | conf->fallback = fallback; | 
| Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 4806 | } | 
|  | 4807 | #endif | 
|  | 4808 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4809 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 4810 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, | 
|  | 4811 | char cert_req_ca_list ) | 
|  | 4812 | { | 
|  | 4813 | conf->cert_req_ca_list = cert_req_ca_list; | 
|  | 4814 | } | 
|  | 4815 | #endif | 
|  | 4816 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4817 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4818 | 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] | 4819 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4820 | conf->encrypt_then_mac = etm; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 4821 | } | 
|  | 4822 | #endif | 
|  | 4823 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4824 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4825 | 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] | 4826 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4827 | conf->extended_ms = ems; | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 4828 | } | 
|  | 4829 | #endif | 
|  | 4830 |  | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 4831 | #if defined(MBEDTLS_ARC4_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4832 | 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] | 4833 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4834 | conf->arc4_disabled = arc4; | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 4835 | } | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 4836 | #endif | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 4837 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4838 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4839 | 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] | 4840 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4841 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4842 | 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] | 4843 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4844 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4845 | } | 
|  | 4846 |  | 
| Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 4847 | conf->mfl_code = mfl_code; | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4848 |  | 
|  | 4849 | return( 0 ); | 
|  | 4850 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4851 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4852 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4853 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4854 | 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] | 4855 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4856 | conf->trunc_hmac = truncate; | 
| Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 4857 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4858 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 4859 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4860 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4861 | 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] | 4862 | { | 
| Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 4863 | conf->cbc_record_splitting = split; | 
| Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 4864 | } | 
|  | 4865 | #endif | 
|  | 4866 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4867 | 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] | 4868 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4869 | conf->allow_legacy_renegotiation = allow_legacy; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4870 | } | 
|  | 4871 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4872 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4873 | 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] | 4874 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4875 | conf->disable_renegotiation = renegotiation; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 4876 | } | 
|  | 4877 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4878 | 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] | 4879 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4880 | conf->renego_max_records = max_records; | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 4881 | } | 
|  | 4882 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4883 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4884 | const unsigned char period[8] ) | 
|  | 4885 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4886 | memcpy( conf->renego_period, period, 8 ); | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4887 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4888 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4889 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4890 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4891 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 4892 | 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] | 4893 | { | 
| Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 4894 | conf->session_tickets = use_tickets; | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4895 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4896 | #endif | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4897 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4898 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4899 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, | 
|  | 4900 | mbedtls_ssl_ticket_write_t *f_ticket_write, | 
|  | 4901 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, | 
|  | 4902 | void *p_ticket ) | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4903 | { | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4904 | conf->f_ticket_write = f_ticket_write; | 
|  | 4905 | conf->f_ticket_parse = f_ticket_parse; | 
|  | 4906 | conf->p_ticket       = p_ticket; | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4907 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4908 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4909 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4910 |  | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4911 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) | 
|  | 4912 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, | 
|  | 4913 | mbedtls_ssl_export_keys_t *f_export_keys, | 
|  | 4914 | void *p_export_keys ) | 
|  | 4915 | { | 
|  | 4916 | conf->f_export_keys = f_export_keys; | 
|  | 4917 | conf->p_export_keys = p_export_keys; | 
|  | 4918 | } | 
| Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 4919 |  | 
|  | 4920 | void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf, | 
|  | 4921 | mbedtls_ssl_export_keys_ext_t *f_export_keys_ext, | 
|  | 4922 | void *p_export_keys ) | 
|  | 4923 | { | 
|  | 4924 | conf->f_export_keys_ext = f_export_keys_ext; | 
|  | 4925 | conf->p_export_keys = p_export_keys; | 
|  | 4926 | } | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4927 | #endif | 
|  | 4928 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4929 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4930 | void mbedtls_ssl_conf_async_private_cb( | 
|  | 4931 | mbedtls_ssl_config *conf, | 
|  | 4932 | mbedtls_ssl_async_sign_t *f_async_sign, | 
|  | 4933 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, | 
|  | 4934 | mbedtls_ssl_async_resume_t *f_async_resume, | 
|  | 4935 | mbedtls_ssl_async_cancel_t *f_async_cancel, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4936 | void *async_config_data ) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4937 | { | 
|  | 4938 | conf->f_async_sign_start = f_async_sign; | 
|  | 4939 | conf->f_async_decrypt_start = f_async_decrypt; | 
|  | 4940 | conf->f_async_resume = f_async_resume; | 
|  | 4941 | conf->f_async_cancel = f_async_cancel; | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4942 | conf->p_async_config_data = async_config_data; | 
|  | 4943 | } | 
|  | 4944 |  | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 4945 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) | 
|  | 4946 | { | 
|  | 4947 | return( conf->p_async_config_data ); | 
|  | 4948 | } | 
|  | 4949 |  | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4950 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4951 | { | 
|  | 4952 | if( ssl->handshake == NULL ) | 
|  | 4953 | return( NULL ); | 
|  | 4954 | else | 
|  | 4955 | return( ssl->handshake->user_async_ctx ); | 
|  | 4956 | } | 
|  | 4957 |  | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4958 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4959 | void *ctx ) | 
|  | 4960 | { | 
|  | 4961 | if( ssl->handshake != NULL ) | 
|  | 4962 | ssl->handshake->user_async_ctx = ctx; | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4963 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4964 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4965 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4966 | /* | 
|  | 4967 | * SSL get accessors | 
|  | 4968 | */ | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4969 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4970 | { | 
| Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 4971 | if( ssl->session != NULL ) | 
|  | 4972 | return( ssl->session->verify_result ); | 
|  | 4973 |  | 
|  | 4974 | if( ssl->session_negotiate != NULL ) | 
|  | 4975 | return( ssl->session_negotiate->verify_result ); | 
|  | 4976 |  | 
| Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 4977 | return( 0xFFFFFFFF ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4978 | } | 
|  | 4979 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4980 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4981 | { | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4982 | if( ssl == NULL || ssl->session == NULL ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4983 | return( NULL ); | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4984 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4985 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4986 | } | 
|  | 4987 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4988 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4989 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4990 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4991 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4992 | { | 
|  | 4993 | switch( ssl->minor_ver ) | 
|  | 4994 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4995 | case MBEDTLS_SSL_MINOR_VERSION_2: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4996 | return( "DTLSv1.0" ); | 
|  | 4997 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4998 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4999 | return( "DTLSv1.2" ); | 
|  | 5000 |  | 
|  | 5001 | default: | 
|  | 5002 | return( "unknown (DTLS)" ); | 
|  | 5003 | } | 
|  | 5004 | } | 
|  | 5005 | #endif | 
|  | 5006 |  | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5007 | switch( ssl->minor_ver ) | 
|  | 5008 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5009 | case MBEDTLS_SSL_MINOR_VERSION_0: | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5010 | return( "SSLv3.0" ); | 
|  | 5011 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5012 | case MBEDTLS_SSL_MINOR_VERSION_1: | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5013 | return( "TLSv1.0" ); | 
|  | 5014 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5015 | case MBEDTLS_SSL_MINOR_VERSION_2: | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5016 | return( "TLSv1.1" ); | 
|  | 5017 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5018 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 5019 | return( "TLSv1.2" ); | 
|  | 5020 |  | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5021 | default: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 5022 | return( "unknown" ); | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5023 | } | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5024 | } | 
|  | 5025 |  | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 5026 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 5027 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) | 
|  | 5028 | { | 
|  | 5029 | size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN; | 
|  | 5030 | size_t read_mfl; | 
|  | 5031 |  | 
|  | 5032 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ | 
|  | 5033 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 5034 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) | 
|  | 5035 | { | 
|  | 5036 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); | 
|  | 5037 | } | 
|  | 5038 |  | 
|  | 5039 | /* Check if a smaller max length was negotiated */ | 
|  | 5040 | if( ssl->session_out != NULL ) | 
|  | 5041 | { | 
|  | 5042 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); | 
|  | 5043 | if( read_mfl < max_len ) | 
|  | 5044 | { | 
|  | 5045 | max_len = read_mfl; | 
|  | 5046 | } | 
|  | 5047 | } | 
|  | 5048 |  | 
|  | 5049 | // During a handshake, use the value being negotiated | 
|  | 5050 | if( ssl->session_negotiate != NULL ) | 
|  | 5051 | { | 
|  | 5052 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); | 
|  | 5053 | if( read_mfl < max_len ) | 
|  | 5054 | { | 
|  | 5055 | max_len = read_mfl; | 
|  | 5056 | } | 
|  | 5057 | } | 
|  | 5058 |  | 
|  | 5059 | return( max_len ); | 
|  | 5060 | } | 
|  | 5061 |  | 
|  | 5062 | 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] | 5063 | { | 
|  | 5064 | size_t max_len; | 
|  | 5065 |  | 
|  | 5066 | /* | 
|  | 5067 | * Assume mfl_code is correct since it was checked when set | 
|  | 5068 | */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5069 | 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] | 5070 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 5071 | /* Check if a smaller max length was negotiated */ | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 5072 | if( ssl->session_out != NULL && | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5073 | 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] | 5074 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5075 | 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] | 5076 | } | 
|  | 5077 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 5078 | /* During a handshake, use the value being negotiated */ | 
|  | 5079 | if( ssl->session_negotiate != NULL && | 
|  | 5080 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) | 
|  | 5081 | { | 
|  | 5082 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); | 
|  | 5083 | } | 
|  | 5084 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5085 | return( max_len ); | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 5086 | } | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 5087 |  | 
|  | 5088 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 5089 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) | 
|  | 5090 | { | 
|  | 5091 | return mbedtls_ssl_get_output_max_frag_len( ssl ); | 
|  | 5092 | } | 
|  | 5093 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 5094 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 5095 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 5096 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 5097 | 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] | 5098 | { | 
| Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 5099 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ | 
|  | 5100 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 5101 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || | 
|  | 5102 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) | 
|  | 5103 | return ( 0 ); | 
|  | 5104 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 5105 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) | 
|  | 5106 | return( ssl->mtu ); | 
|  | 5107 |  | 
|  | 5108 | if( ssl->mtu == 0 ) | 
|  | 5109 | return( ssl->handshake->mtu ); | 
|  | 5110 |  | 
|  | 5111 | return( ssl->mtu < ssl->handshake->mtu ? | 
|  | 5112 | ssl->mtu : ssl->handshake->mtu ); | 
|  | 5113 | } | 
|  | 5114 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 5115 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5116 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) | 
|  | 5117 | { | 
|  | 5118 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; | 
|  | 5119 |  | 
| Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 5120 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ | 
|  | 5121 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5122 | (void) ssl; | 
|  | 5123 | #endif | 
|  | 5124 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5125 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 5126 | 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] | 5127 |  | 
|  | 5128 | if( max_len > mfl ) | 
|  | 5129 | max_len = mfl; | 
|  | 5130 | #endif | 
|  | 5131 |  | 
|  | 5132 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 5133 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5134 | { | 
| Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 5135 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5136 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); | 
|  | 5137 | const size_t overhead = (size_t) ret; | 
|  | 5138 |  | 
|  | 5139 | if( ret < 0 ) | 
|  | 5140 | return( ret ); | 
|  | 5141 |  | 
|  | 5142 | if( mtu <= overhead ) | 
|  | 5143 | { | 
|  | 5144 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); | 
|  | 5145 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 5146 | } | 
|  | 5147 |  | 
|  | 5148 | if( max_len > mtu - overhead ) | 
|  | 5149 | max_len = mtu - overhead; | 
|  | 5150 | } | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 5151 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5152 |  | 
| Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 5153 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) &&        \ | 
|  | 5154 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5155 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5156 | #endif | 
|  | 5157 |  | 
|  | 5158 | return( (int) max_len ); | 
|  | 5159 | } | 
|  | 5160 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5161 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 5162 | 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] | 5163 | { | 
|  | 5164 | if( ssl == NULL || ssl->session == NULL ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 5165 | return( NULL ); | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5166 |  | 
| Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 5167 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 5168 | return( ssl->session->peer_cert ); | 
| Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 5169 | #else | 
|  | 5170 | return( NULL ); | 
|  | 5171 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5172 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5173 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5174 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5175 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 5176 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, | 
|  | 5177 | mbedtls_ssl_session *dst ) | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5178 | { | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5179 | if( ssl == NULL || | 
|  | 5180 | dst == NULL || | 
|  | 5181 | ssl->session == NULL || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5182 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5183 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5184 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5185 | } | 
|  | 5186 |  | 
| Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 5187 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5188 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5189 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5190 |  | 
| Manuel Pégourié-Gonnard | b5e4e0a | 2019-05-20 11:12:28 +0200 | [diff] [blame] | 5191 | const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ) | 
|  | 5192 | { | 
|  | 5193 | if( ssl == NULL ) | 
|  | 5194 | return( NULL ); | 
|  | 5195 |  | 
|  | 5196 | return( ssl->session ); | 
|  | 5197 | } | 
|  | 5198 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5199 | /* | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5200 | * Define ticket header determining Mbed TLS version | 
|  | 5201 | * and structure of the ticket. | 
|  | 5202 | */ | 
|  | 5203 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5204 | /* | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5205 | * Define bitflag determining compile-time settings influencing | 
|  | 5206 | * structure of serialized SSL sessions. | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5207 | */ | 
|  | 5208 |  | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5209 | #if defined(MBEDTLS_HAVE_TIME) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5210 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5211 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5212 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5213 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 5214 |  | 
|  | 5215 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5216 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5217 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5218 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5219 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 5220 |  | 
|  | 5221 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5222 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5223 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5224 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5225 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 5226 |  | 
|  | 5227 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5228 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5229 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5230 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5231 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 5232 |  | 
|  | 5233 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5234 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5235 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5236 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5237 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
|  | 5238 |  | 
|  | 5239 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5240 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5241 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5242 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5243 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
|  | 5244 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5245 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 5246 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 | 
|  | 5247 | #else | 
|  | 5248 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 | 
|  | 5249 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 5250 |  | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5251 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT          0 | 
|  | 5252 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT           1 | 
|  | 5253 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 | 
|  | 5254 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT           3 | 
|  | 5255 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT    4 | 
|  | 5256 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT           5 | 
|  | 5257 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT        6 | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5258 |  | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5259 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG                           \ | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5260 | ( (uint16_t) (                                                      \ | 
|  | 5261 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME          << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT          ) | \ | 
|  | 5262 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT           << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT           ) | \ | 
|  | 5263 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ | 
|  | 5264 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL           << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT           ) | \ | 
|  | 5265 | ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC    << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT    ) | \ | 
|  | 5266 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM           << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT           ) | \ | 
| Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 5267 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET        << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT        ) ) ) | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5268 |  | 
| Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 5269 | static unsigned char ssl_serialized_session_header[] = { | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5270 | MBEDTLS_VERSION_MAJOR, | 
|  | 5271 | MBEDTLS_VERSION_MINOR, | 
|  | 5272 | MBEDTLS_VERSION_PATCH, | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5273 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, | 
|  | 5274 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, | 
| Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 5275 | }; | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5276 |  | 
|  | 5277 | /* | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5278 | * Serialize a session in the following format: | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5279 | * (in the presentation language of TLS, RFC 8446 section 3) | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5280 | * | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5281 | *  opaque mbedtls_version[3];   // major, minor, patch | 
|  | 5282 | *  opaque session_format[2];    // version-specific 16-bit field determining | 
|  | 5283 | *                               // the format of the remaining | 
|  | 5284 | *                               // serialized data. | 
| Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 5285 | * | 
|  | 5286 | *  Note: When updating the format, remember to keep | 
|  | 5287 | *        these version+format bytes. | 
|  | 5288 | * | 
| Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 5289 | *                               // In this version, `session_format` determines | 
|  | 5290 | *                               // the setting of those compile-time | 
|  | 5291 | *                               // configuration options which influence | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5292 | *                               // the structure of mbedtls_ssl_session. | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5293 | *  uint64 start_time; | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5294 | *  uint8 ciphersuite[2];        // defined by the standard | 
|  | 5295 | *  uint8 compression;           // 0 or 1 | 
|  | 5296 | *  uint8 session_id_len;        // at most 32 | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5297 | *  opaque session_id[32]; | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5298 | *  opaque master[48];           // fixed length in the standard | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5299 | *  uint32 verify_result; | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5300 | *  opaque peer_cert<0..2^24-1>; // length 0 means no peer cert | 
|  | 5301 | *  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] | 5302 | *  uint32 ticket_lifetime; | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5303 | *  uint8 mfl_code;              // up to 255 according to standard | 
|  | 5304 | *  uint8 trunc_hmac;            // 0 or 1 | 
|  | 5305 | *  uint8 encrypt_then_mac;      // 0 or 1 | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5306 | * | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5307 | * The order is the same as in the definition of the structure, except | 
|  | 5308 | * verify_result is put before peer_cert so that all mandatory fields come | 
|  | 5309 | * together in one block. | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5310 | */ | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5311 | static int ssl_session_save( const mbedtls_ssl_session *session, | 
|  | 5312 | unsigned char omit_header, | 
|  | 5313 | unsigned char *buf, | 
|  | 5314 | size_t buf_len, | 
|  | 5315 | size_t *olen ) | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5316 | { | 
|  | 5317 | unsigned char *p = buf; | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5318 | size_t used = 0; | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5319 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 5320 | uint64_t start; | 
|  | 5321 | #endif | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5322 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 5323 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 5324 | size_t cert_len; | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5325 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 5326 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 5327 |  | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5328 |  | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5329 | if( !omit_header ) | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5330 | { | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5331 | /* | 
|  | 5332 | * Add version identifier | 
|  | 5333 | */ | 
|  | 5334 |  | 
|  | 5335 | used += sizeof( ssl_serialized_session_header ); | 
|  | 5336 |  | 
|  | 5337 | if( used <= buf_len ) | 
|  | 5338 | { | 
|  | 5339 | memcpy( p, ssl_serialized_session_header, | 
|  | 5340 | sizeof( ssl_serialized_session_header ) ); | 
|  | 5341 | p += sizeof( ssl_serialized_session_header ); | 
|  | 5342 | } | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5343 | } | 
|  | 5344 |  | 
|  | 5345 | /* | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5346 | * Time | 
|  | 5347 | */ | 
|  | 5348 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 5349 | used += 8; | 
|  | 5350 |  | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5351 | if( used <= buf_len ) | 
|  | 5352 | { | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5353 | start = (uint64_t) session->start; | 
|  | 5354 |  | 
|  | 5355 | *p++ = (unsigned char)( ( start >> 56 ) & 0xFF ); | 
|  | 5356 | *p++ = (unsigned char)( ( start >> 48 ) & 0xFF ); | 
|  | 5357 | *p++ = (unsigned char)( ( start >> 40 ) & 0xFF ); | 
|  | 5358 | *p++ = (unsigned char)( ( start >> 32 ) & 0xFF ); | 
|  | 5359 | *p++ = (unsigned char)( ( start >> 24 ) & 0xFF ); | 
|  | 5360 | *p++ = (unsigned char)( ( start >> 16 ) & 0xFF ); | 
|  | 5361 | *p++ = (unsigned char)( ( start >>  8 ) & 0xFF ); | 
|  | 5362 | *p++ = (unsigned char)( ( start       ) & 0xFF ); | 
|  | 5363 | } | 
|  | 5364 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 5365 |  | 
|  | 5366 | /* | 
|  | 5367 | * Basic mandatory fields | 
|  | 5368 | */ | 
|  | 5369 | used += 2   /* ciphersuite */ | 
|  | 5370 | + 1   /* compression */ | 
|  | 5371 | + 1   /* id_len */ | 
|  | 5372 | + sizeof( session->id ) | 
|  | 5373 | + sizeof( session->master ) | 
|  | 5374 | + 4;  /* verify_result */ | 
|  | 5375 |  | 
|  | 5376 | if( used <= buf_len ) | 
|  | 5377 | { | 
|  | 5378 | *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF ); | 
|  | 5379 | *p++ = (unsigned char)( ( session->ciphersuite      ) & 0xFF ); | 
|  | 5380 |  | 
|  | 5381 | *p++ = (unsigned char)( session->compression & 0xFF ); | 
|  | 5382 |  | 
|  | 5383 | *p++ = (unsigned char)( session->id_len & 0xFF ); | 
|  | 5384 | memcpy( p, session->id, 32 ); | 
|  | 5385 | p += 32; | 
|  | 5386 |  | 
|  | 5387 | memcpy( p, session->master, 48 ); | 
|  | 5388 | p += 48; | 
|  | 5389 |  | 
|  | 5390 | *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF ); | 
|  | 5391 | *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF ); | 
|  | 5392 | *p++ = (unsigned char)( ( session->verify_result >>  8 ) & 0xFF ); | 
|  | 5393 | *p++ = (unsigned char)( ( session->verify_result       ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5394 | } | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5395 |  | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5396 | /* | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5397 | * Peer's end-entity certificate | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5398 | */ | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5399 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 5400 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 5401 | if( session->peer_cert == NULL ) | 
|  | 5402 | cert_len = 0; | 
|  | 5403 | else | 
|  | 5404 | cert_len = session->peer_cert->raw.len; | 
|  | 5405 |  | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5406 | used += 3 + cert_len; | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5407 |  | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5408 | if( used <= buf_len ) | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5409 | { | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5410 | *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); | 
|  | 5411 | *p++ = (unsigned char)( ( cert_len >>  8 ) & 0xFF ); | 
|  | 5412 | *p++ = (unsigned char)( ( cert_len       ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5413 |  | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5414 | if( session->peer_cert != NULL ) | 
|  | 5415 | { | 
|  | 5416 | memcpy( p, session->peer_cert->raw.p, cert_len ); | 
|  | 5417 | p += cert_len; | 
|  | 5418 | } | 
|  | 5419 | } | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5420 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5421 | if( session->peer_cert_digest != NULL ) | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5422 | { | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5423 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; | 
|  | 5424 | if( used <= buf_len ) | 
|  | 5425 | { | 
|  | 5426 | *p++ = (unsigned char) session->peer_cert_digest_type; | 
|  | 5427 | *p++ = (unsigned char) session->peer_cert_digest_len; | 
|  | 5428 | memcpy( p, session->peer_cert_digest, | 
|  | 5429 | session->peer_cert_digest_len ); | 
|  | 5430 | p += session->peer_cert_digest_len; | 
|  | 5431 | } | 
|  | 5432 | } | 
|  | 5433 | else | 
|  | 5434 | { | 
|  | 5435 | used += 2; | 
|  | 5436 | if( used <= buf_len ) | 
|  | 5437 | { | 
|  | 5438 | *p++ = (unsigned char) MBEDTLS_MD_NONE; | 
|  | 5439 | *p++ = 0; | 
|  | 5440 | } | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5441 | } | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5442 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 5443 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 5444 |  | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5445 | /* | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5446 | * Session ticket if any, plus associated data | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5447 | */ | 
|  | 5448 | #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] | 5449 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5450 |  | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5451 | if( used <= buf_len ) | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5452 | { | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5453 | *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF ); | 
|  | 5454 | *p++ = (unsigned char)( ( session->ticket_len >>  8 ) & 0xFF ); | 
|  | 5455 | *p++ = (unsigned char)( ( session->ticket_len       ) & 0xFF ); | 
|  | 5456 |  | 
|  | 5457 | if( session->ticket != NULL ) | 
|  | 5458 | { | 
|  | 5459 | memcpy( p, session->ticket, session->ticket_len ); | 
|  | 5460 | p += session->ticket_len; | 
|  | 5461 | } | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5462 |  | 
|  | 5463 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF ); | 
|  | 5464 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF ); | 
|  | 5465 | *p++ = (unsigned char)( ( session->ticket_lifetime >>  8 ) & 0xFF ); | 
|  | 5466 | *p++ = (unsigned char)( ( session->ticket_lifetime       ) & 0xFF ); | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5467 | } | 
|  | 5468 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 5469 |  | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5470 | /* | 
|  | 5471 | * Misc extension-related info | 
|  | 5472 | */ | 
|  | 5473 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 5474 | used += 1; | 
|  | 5475 |  | 
|  | 5476 | if( used <= buf_len ) | 
|  | 5477 | *p++ = session->mfl_code; | 
|  | 5478 | #endif | 
|  | 5479 |  | 
|  | 5480 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 5481 | used += 1; | 
|  | 5482 |  | 
|  | 5483 | if( used <= buf_len ) | 
|  | 5484 | *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF ); | 
|  | 5485 | #endif | 
|  | 5486 |  | 
|  | 5487 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 5488 | used += 1; | 
|  | 5489 |  | 
|  | 5490 | if( used <= buf_len ) | 
|  | 5491 | *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF ); | 
|  | 5492 | #endif | 
|  | 5493 |  | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5494 | /* Done */ | 
| Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5495 | *olen = used; | 
|  | 5496 |  | 
|  | 5497 | if( used > buf_len ) | 
|  | 5498 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5499 |  | 
|  | 5500 | return( 0 ); | 
|  | 5501 | } | 
|  | 5502 |  | 
|  | 5503 | /* | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5504 | * Public wrapper for ssl_session_save() | 
|  | 5505 | */ | 
|  | 5506 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, | 
|  | 5507 | unsigned char *buf, | 
|  | 5508 | size_t buf_len, | 
|  | 5509 | size_t *olen ) | 
|  | 5510 | { | 
|  | 5511 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); | 
|  | 5512 | } | 
|  | 5513 |  | 
|  | 5514 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5515 | * Deserialize session, see mbedtls_ssl_session_save() for format. | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5516 | * | 
|  | 5517 | * 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] | 5518 | * case of error, and has an extra option omit_header. | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5519 | */ | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5520 | static int ssl_session_load( mbedtls_ssl_session *session, | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5521 | unsigned char omit_header, | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5522 | const unsigned char *buf, | 
|  | 5523 | size_t len ) | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5524 | { | 
|  | 5525 | const unsigned char *p = buf; | 
|  | 5526 | const unsigned char * const end = buf + len; | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5527 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 5528 | uint64_t start; | 
|  | 5529 | #endif | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5530 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 5531 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 5532 | size_t cert_len; | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5533 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 5534 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 5535 |  | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5536 | if( !omit_header ) | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5537 | { | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5538 | /* | 
|  | 5539 | * Check version identifier | 
|  | 5540 | */ | 
|  | 5541 |  | 
|  | 5542 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) | 
|  | 5543 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5544 |  | 
|  | 5545 | if( memcmp( p, ssl_serialized_session_header, | 
|  | 5546 | sizeof( ssl_serialized_session_header ) ) != 0 ) | 
|  | 5547 | { | 
|  | 5548 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | 
|  | 5549 | } | 
|  | 5550 | p += sizeof( ssl_serialized_session_header ); | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5551 | } | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5552 |  | 
|  | 5553 | /* | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5554 | * Time | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5555 | */ | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5556 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 5557 | if( 8 > (size_t)( end - p ) ) | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5558 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5559 |  | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5560 | start = ( (uint64_t) p[0] << 56 ) | | 
|  | 5561 | ( (uint64_t) p[1] << 48 ) | | 
|  | 5562 | ( (uint64_t) p[2] << 40 ) | | 
|  | 5563 | ( (uint64_t) p[3] << 32 ) | | 
|  | 5564 | ( (uint64_t) p[4] << 24 ) | | 
|  | 5565 | ( (uint64_t) p[5] << 16 ) | | 
|  | 5566 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 5567 | ( (uint64_t) p[7]       ); | 
|  | 5568 | p += 8; | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5569 |  | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5570 | session->start = (time_t) start; | 
|  | 5571 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 5572 |  | 
|  | 5573 | /* | 
|  | 5574 | * Basic mandatory fields | 
|  | 5575 | */ | 
|  | 5576 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) | 
|  | 5577 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5578 |  | 
|  | 5579 | session->ciphersuite = ( p[0] << 8 ) | p[1]; | 
|  | 5580 | p += 2; | 
|  | 5581 |  | 
|  | 5582 | session->compression = *p++; | 
|  | 5583 |  | 
|  | 5584 | session->id_len = *p++; | 
|  | 5585 | memcpy( session->id, p, 32 ); | 
|  | 5586 | p += 32; | 
|  | 5587 |  | 
|  | 5588 | memcpy( session->master, p, 48 ); | 
|  | 5589 | p += 48; | 
|  | 5590 |  | 
|  | 5591 | session->verify_result = ( (uint32_t) p[0] << 24 ) | | 
|  | 5592 | ( (uint32_t) p[1] << 16 ) | | 
|  | 5593 | ( (uint32_t) p[2] <<  8 ) | | 
|  | 5594 | ( (uint32_t) p[3]       ); | 
|  | 5595 | p += 4; | 
|  | 5596 |  | 
|  | 5597 | /* Immediately clear invalid pointer values that have been read, in case | 
|  | 5598 | * we exit early before we replaced them with valid ones. */ | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5599 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 5600 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 5601 | session->peer_cert = NULL; | 
|  | 5602 | #else | 
|  | 5603 | session->peer_cert_digest = NULL; | 
|  | 5604 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 5605 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 5606 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 5607 | session->ticket = NULL; | 
|  | 5608 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 5609 |  | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5610 | /* | 
|  | 5611 | * Peer certificate | 
|  | 5612 | */ | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5613 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 5614 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 5615 | /* Deserialize CRT from the end of the ticket. */ | 
|  | 5616 | if( 3 > (size_t)( end - p ) ) | 
|  | 5617 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5618 |  | 
|  | 5619 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; | 
|  | 5620 | p += 3; | 
|  | 5621 |  | 
|  | 5622 | if( cert_len != 0 ) | 
|  | 5623 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5624 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5625 |  | 
|  | 5626 | if( cert_len > (size_t)( end - p ) ) | 
|  | 5627 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5628 |  | 
|  | 5629 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); | 
|  | 5630 |  | 
|  | 5631 | if( session->peer_cert == NULL ) | 
|  | 5632 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 5633 |  | 
|  | 5634 | mbedtls_x509_crt_init( session->peer_cert ); | 
|  | 5635 |  | 
|  | 5636 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, | 
|  | 5637 | p, cert_len ) ) != 0 ) | 
|  | 5638 | { | 
|  | 5639 | mbedtls_x509_crt_free( session->peer_cert ); | 
|  | 5640 | mbedtls_free( session->peer_cert ); | 
|  | 5641 | session->peer_cert = NULL; | 
|  | 5642 | return( ret ); | 
|  | 5643 | } | 
|  | 5644 |  | 
|  | 5645 | p += cert_len; | 
|  | 5646 | } | 
|  | 5647 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 5648 | /* Deserialize CRT digest from the end of the ticket. */ | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5649 | if( 2 > (size_t)( end - p ) ) | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5650 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5651 |  | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5652 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; | 
|  | 5653 | session->peer_cert_digest_len  = (size_t) *p++; | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5654 |  | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5655 | if( session->peer_cert_digest_len != 0 ) | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5656 | { | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5657 | const mbedtls_md_info_t *md_info = | 
|  | 5658 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); | 
|  | 5659 | if( md_info == NULL ) | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5660 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5661 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) | 
|  | 5662 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5663 |  | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5664 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) | 
|  | 5665 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5666 |  | 
|  | 5667 | session->peer_cert_digest = | 
|  | 5668 | mbedtls_calloc( 1, session->peer_cert_digest_len ); | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5669 | if( session->peer_cert_digest == NULL ) | 
|  | 5670 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 5671 |  | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5672 | memcpy( session->peer_cert_digest, p, | 
|  | 5673 | session->peer_cert_digest_len ); | 
|  | 5674 | p += session->peer_cert_digest_len; | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5675 | } | 
|  | 5676 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 5677 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 5678 |  | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5679 | /* | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5680 | * Session ticket and associated data | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5681 | */ | 
|  | 5682 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 5683 | if( 3 > (size_t)( end - p ) ) | 
|  | 5684 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5685 |  | 
|  | 5686 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; | 
|  | 5687 | p += 3; | 
|  | 5688 |  | 
|  | 5689 | if( session->ticket_len != 0 ) | 
|  | 5690 | { | 
|  | 5691 | if( session->ticket_len > (size_t)( end - p ) ) | 
|  | 5692 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5693 |  | 
|  | 5694 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); | 
|  | 5695 | if( session->ticket == NULL ) | 
|  | 5696 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 5697 |  | 
|  | 5698 | memcpy( session->ticket, p, session->ticket_len ); | 
|  | 5699 | p += session->ticket_len; | 
|  | 5700 | } | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5701 |  | 
|  | 5702 | if( 4 > (size_t)( end - p ) ) | 
|  | 5703 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5704 |  | 
|  | 5705 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | | 
|  | 5706 | ( (uint32_t) p[1] << 16 ) | | 
|  | 5707 | ( (uint32_t) p[2] <<  8 ) | | 
|  | 5708 | ( (uint32_t) p[3]       ); | 
|  | 5709 | p += 4; | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5710 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 5711 |  | 
| Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5712 | /* | 
|  | 5713 | * Misc extension-related info | 
|  | 5714 | */ | 
|  | 5715 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 5716 | if( 1 > (size_t)( end - p ) ) | 
|  | 5717 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5718 |  | 
|  | 5719 | session->mfl_code = *p++; | 
|  | 5720 | #endif | 
|  | 5721 |  | 
|  | 5722 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 5723 | if( 1 > (size_t)( end - p ) ) | 
|  | 5724 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5725 |  | 
|  | 5726 | session->trunc_hmac = *p++; | 
|  | 5727 | #endif | 
|  | 5728 |  | 
|  | 5729 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 5730 | if( 1 > (size_t)( end - p ) ) | 
|  | 5731 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5732 |  | 
|  | 5733 | session->encrypt_then_mac = *p++; | 
|  | 5734 | #endif | 
|  | 5735 |  | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5736 | /* Done, should have consumed entire buffer */ | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5737 | if( p != end ) | 
|  | 5738 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5739 |  | 
|  | 5740 | return( 0 ); | 
|  | 5741 | } | 
|  | 5742 |  | 
|  | 5743 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5744 | * Deserialize session: public wrapper for error cleaning | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5745 | */ | 
|  | 5746 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, | 
|  | 5747 | const unsigned char *buf, | 
|  | 5748 | size_t len ) | 
|  | 5749 | { | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5750 | int ret = ssl_session_load( session, 0, buf, len ); | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5751 |  | 
|  | 5752 | if( ret != 0 ) | 
|  | 5753 | mbedtls_ssl_session_free( session ); | 
|  | 5754 |  | 
|  | 5755 | return( ret ); | 
|  | 5756 | } | 
|  | 5757 |  | 
|  | 5758 | /* | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5759 | * Perform a single step of the SSL handshake | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5760 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5761 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5762 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5763 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5764 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5765 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 5766 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5767 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5768 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5769 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5770 | ret = mbedtls_ssl_handshake_client_step( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5771 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5772 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5773 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5774 | ret = mbedtls_ssl_handshake_server_step( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5775 | #endif | 
|  | 5776 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5777 | return( ret ); | 
|  | 5778 | } | 
|  | 5779 |  | 
|  | 5780 | /* | 
|  | 5781 | * Perform the SSL handshake | 
|  | 5782 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5783 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5784 | { | 
|  | 5785 | int ret = 0; | 
|  | 5786 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5787 | /* Sanity checks */ | 
|  | 5788 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5789 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 5790 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5791 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5792 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5793 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 5794 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) | 
|  | 5795 | { | 
|  | 5796 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " | 
|  | 5797 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); | 
|  | 5798 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5799 | } | 
|  | 5800 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 5801 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5802 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5803 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5804 | /* Main handshake loop */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5805 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5806 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5807 | ret = mbedtls_ssl_handshake_step( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5808 |  | 
|  | 5809 | if( ret != 0 ) | 
|  | 5810 | break; | 
|  | 5811 | } | 
|  | 5812 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5813 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5814 |  | 
|  | 5815 | return( ret ); | 
|  | 5816 | } | 
|  | 5817 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5818 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 5819 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5820 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5821 | * Write HelloRequest to request renegotiation on server | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5822 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5823 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5824 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5825 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5826 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5827 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5828 |  | 
|  | 5829 | ssl->out_msglen  = 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5830 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 5831 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_HELLO_REQUEST; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5832 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5833 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5834 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5835 | 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] | 5836 | return( ret ); | 
|  | 5837 | } | 
|  | 5838 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5839 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5840 |  | 
|  | 5841 | return( 0 ); | 
|  | 5842 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5843 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5844 |  | 
|  | 5845 | /* | 
|  | 5846 | * Actually renegotiate current connection, triggered by either: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5847 | * - any side: calling mbedtls_ssl_renegotiate(), | 
|  | 5848 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), | 
|  | 5849 | * - 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] | 5850 | *   the initial handshake is completed. | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5851 | * 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] | 5852 | * 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] | 5853 | */ | 
| Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5854 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5855 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5856 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5857 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5858 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5859 |  | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5860 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
|  | 5861 | return( ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5862 |  | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5863 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and | 
|  | 5864 | * the ServerHello will have message_seq = 1" */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5865 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5866 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5867 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5868 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5869 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 5870 | ssl->handshake->out_msg_seq = 1; | 
|  | 5871 | else | 
|  | 5872 | ssl->handshake->in_msg_seq = 1; | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5873 | } | 
|  | 5874 | #endif | 
|  | 5875 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5876 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
|  | 5877 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5878 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5879 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5880 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5881 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5882 | return( ret ); | 
|  | 5883 | } | 
|  | 5884 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5885 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5886 |  | 
|  | 5887 | return( 0 ); | 
|  | 5888 | } | 
|  | 5889 |  | 
|  | 5890 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5891 | * Renegotiate current connection on client, | 
|  | 5892 | * or request renegotiation on server | 
|  | 5893 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5894 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5895 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5896 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5897 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5898 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 5899 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5900 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5901 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5902 | /* On server, just send the request */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5903 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5904 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5905 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 5906 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5907 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5908 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5909 |  | 
|  | 5910 | /* Did we already try/start sending HelloRequest? */ | 
|  | 5911 | if( ssl->out_left != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5912 | return( mbedtls_ssl_flush_output( ssl ) ); | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5913 |  | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5914 | return( ssl_write_hello_request( ssl ) ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5915 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5916 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5917 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5918 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5919 | /* | 
|  | 5920 | * On client, either start the renegotiation process or, | 
|  | 5921 | * if already in progress, continue the handshake | 
|  | 5922 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5923 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5924 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5925 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 5926 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5927 |  | 
| Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5928 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5929 | { | 
| Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5930 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5931 | return( ret ); | 
|  | 5932 | } | 
|  | 5933 | } | 
|  | 5934 | else | 
|  | 5935 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5936 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5937 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5938 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5939 | return( ret ); | 
|  | 5940 | } | 
|  | 5941 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5942 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5943 |  | 
| Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 5944 | return( ret ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5945 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5946 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5947 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5948 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 5949 | 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] | 5950 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5951 | mbedtls_ssl_key_cert *cur = key_cert, *next; | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5952 |  | 
|  | 5953 | while( cur != NULL ) | 
|  | 5954 | { | 
|  | 5955 | next = cur->next; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5956 | mbedtls_free( cur ); | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5957 | cur = next; | 
|  | 5958 | } | 
|  | 5959 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5960 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5961 |  | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5962 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5963 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5964 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
|  | 5965 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5966 | if( handshake == NULL ) | 
|  | 5967 | return; | 
|  | 5968 |  | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5969 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
|  | 5970 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) | 
|  | 5971 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 5972 | ssl->conf->f_async_cancel( ssl ); | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5973 | handshake->async_in_progress = 0; | 
|  | 5974 | } | 
|  | 5975 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
|  | 5976 |  | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5977 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 5978 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 5979 | mbedtls_md5_free(    &handshake->fin_md5  ); | 
|  | 5980 | mbedtls_sha1_free(   &handshake->fin_sha1 ); | 
|  | 5981 | #endif | 
|  | 5982 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 5983 | #if defined(MBEDTLS_SHA256_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5984 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 5985 | psa_hash_abort( &handshake->fin_sha256_psa ); | 
|  | 5986 | #else | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5987 | mbedtls_sha256_free(   &handshake->fin_sha256    ); | 
|  | 5988 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5989 | #endif | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5990 | #if defined(MBEDTLS_SHA512_C) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5991 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 5992 | psa_hash_abort( &handshake->fin_sha384_psa ); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5993 | #else | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5994 | mbedtls_sha512_free(   &handshake->fin_sha512    ); | 
|  | 5995 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5996 | #endif | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5997 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 5998 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5999 | #if defined(MBEDTLS_DHM_C) | 
|  | 6000 | mbedtls_dhm_free( &handshake->dhm_ctx ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6001 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6002 | #if defined(MBEDTLS_ECDH_C) | 
|  | 6003 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 6004 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 6005 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 6006 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 6007 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 6008 | mbedtls_free( handshake->ecjpake_cache ); | 
|  | 6009 | handshake->ecjpake_cache = NULL; | 
|  | 6010 | handshake->ecjpake_cache_len = 0; | 
|  | 6011 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 6012 | #endif | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 6013 |  | 
| Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 6014 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
|  | 6015 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 6016 | /* explicit void pointer cast for buggy MS compiler */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6017 | mbedtls_free( (void *) handshake->curves ); | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 6018 | #endif | 
|  | 6019 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6020 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 6021 | if( handshake->psk != NULL ) | 
|  | 6022 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6023 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 6024 | mbedtls_free( handshake->psk ); | 
|  | 6025 | } | 
|  | 6026 | #endif | 
|  | 6027 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6028 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 6029 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 6030 | /* | 
|  | 6031 | * Free only the linked list wrapper, not the keys themselves | 
|  | 6032 | * since the belong to the SNI callback | 
|  | 6033 | */ | 
|  | 6034 | if( handshake->sni_key_cert != NULL ) | 
|  | 6035 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6036 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 6037 |  | 
|  | 6038 | while( cur != NULL ) | 
|  | 6039 | { | 
|  | 6040 | next = cur->next; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6041 | mbedtls_free( cur ); | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 6042 | cur = next; | 
|  | 6043 | } | 
|  | 6044 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6045 | #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] | 6046 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6047 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 6048 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6049 | if( handshake->ecrs_peer_cert != NULL ) | 
|  | 6050 | { | 
|  | 6051 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); | 
|  | 6052 | mbedtls_free( handshake->ecrs_peer_cert ); | 
|  | 6053 | } | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 6054 | #endif | 
|  | 6055 |  | 
| Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 6056 | #if defined(MBEDTLS_X509_CRT_PARSE_C) &&        \ | 
|  | 6057 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 6058 | mbedtls_pk_free( &handshake->peer_pubkey ); | 
|  | 6059 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 6060 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6061 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6062 | mbedtls_free( handshake->verify_cookie ); | 
| Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 6063 | mbedtls_ssl_flight_free( handshake->flight ); | 
|  | 6064 | mbedtls_ssl_buffering_free( ssl ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 6065 | #endif | 
|  | 6066 |  | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 6067 | #if defined(MBEDTLS_ECDH_C) &&                  \ | 
|  | 6068 | defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6069 | psa_destroy_key( handshake->ecdh_psa_privkey ); | 
|  | 6070 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 6071 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6072 | mbedtls_platform_zeroize( handshake, | 
|  | 6073 | sizeof( mbedtls_ssl_handshake_params ) ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6074 |  | 
|  | 6075 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 6076 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS | 
|  | 6077 | * processes datagrams and the fact that a datagram is allowed to have | 
|  | 6078 | * several records in it, it is possible that the I/O buffers are not | 
|  | 6079 | * empty at this stage */ | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 6080 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), | 
|  | 6081 | mbedtls_ssl_get_output_buflen( ssl ) ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6082 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6083 | } | 
|  | 6084 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6085 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6086 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6087 | if( session == NULL ) | 
|  | 6088 | return; | 
|  | 6089 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6090 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 6091 | ssl_clear_peer_cert( session ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6092 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6093 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 6094 | #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] | 6095 | mbedtls_free( session->ticket ); | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 6096 | #endif | 
| Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 6097 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6098 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6099 | } | 
|  | 6100 |  | 
| Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 6101 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 6102 |  | 
|  | 6103 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 6104 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u | 
|  | 6105 | #else | 
|  | 6106 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u | 
|  | 6107 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 6108 |  | 
|  | 6109 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) | 
|  | 6110 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u | 
|  | 6111 | #else | 
|  | 6112 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u | 
|  | 6113 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ | 
|  | 6114 |  | 
|  | 6115 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 6116 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u | 
|  | 6117 | #else | 
|  | 6118 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u | 
|  | 6119 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 6120 |  | 
|  | 6121 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 6122 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u | 
|  | 6123 | #else | 
|  | 6124 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u | 
|  | 6125 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 6126 |  | 
|  | 6127 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT    0 | 
|  | 6128 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT     1 | 
|  | 6129 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT      2 | 
|  | 6130 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT                  3 | 
|  | 6131 |  | 
|  | 6132 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG   \ | 
|  | 6133 | ( (uint32_t) (                              \ | 
|  | 6134 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID     << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT     ) | \ | 
|  | 6135 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT      << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT      ) | \ | 
|  | 6136 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY       << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT       ) | \ | 
|  | 6137 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN                   << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT                   ) | \ | 
|  | 6138 | 0u ) ) | 
|  | 6139 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6140 | static unsigned char ssl_serialized_context_header[] = { | 
|  | 6141 | MBEDTLS_VERSION_MAJOR, | 
|  | 6142 | MBEDTLS_VERSION_MINOR, | 
|  | 6143 | MBEDTLS_VERSION_PATCH, | 
|  | 6144 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, | 
|  | 6145 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 6146 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF, | 
|  | 6147 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >>  8 ) & 0xFF, | 
|  | 6148 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >>  0 ) & 0xFF, | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6149 | }; | 
|  | 6150 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6151 | /* | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6152 | * Serialize a full SSL context | 
| Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 6153 | * | 
|  | 6154 | * The format of the serialized data is: | 
|  | 6155 | * (in the presentation language of TLS, RFC 8446 section 3) | 
|  | 6156 | * | 
|  | 6157 | *  // header | 
|  | 6158 | *  opaque mbedtls_version[3];   // major, minor, patch | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6159 | *  opaque context_format[5];    // version-specific field determining | 
| Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 6160 | *                               // the format of the remaining | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6161 | *                               // serialized data. | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 6162 | *  Note: When updating the format, remember to keep these | 
|  | 6163 | *        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] | 6164 | * | 
|  | 6165 | *  // session sub-structure | 
|  | 6166 | *  opaque session<1..2^32-1>;  // see mbedtls_ssl_session_save() | 
|  | 6167 | *  // transform sub-structure | 
|  | 6168 | *  uint8 random[64];           // ServerHello.random+ClientHello.random | 
|  | 6169 | *  uint8 in_cid<0..2^8-1>      // Connection ID: expected incoming value | 
|  | 6170 | *  uint8 out_cid<0..2^8-1>     // Connection ID: outgoing value to use | 
|  | 6171 | *  // fields from ssl_context | 
|  | 6172 | *  uint32 badmac_seen;         // DTLS: number of records with failing MAC | 
|  | 6173 | *  uint64 in_window_top;       // DTLS: last validated record seq_num | 
|  | 6174 | *  uint64 in_window;           // DTLS: bitmask for replay protection | 
|  | 6175 | *  uint8 disable_datagram_packing; // DTLS: only one record per datagram | 
|  | 6176 | *  uint64 cur_out_ctr;         // Record layer: outgoing sequence number | 
|  | 6177 | *  uint16 mtu;                 // DTLS: path mtu (max outgoing fragment size) | 
|  | 6178 | *  uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol | 
|  | 6179 | * | 
|  | 6180 | * Note that many fields of the ssl_context or sub-structures are not | 
|  | 6181 | * serialized, as they fall in one of the following categories: | 
|  | 6182 | * | 
|  | 6183 | *  1. forced value (eg in_left must be 0) | 
|  | 6184 | *  2. pointer to dynamically-allocated memory (eg session, transform) | 
|  | 6185 | *  3. value can be re-derived from other data (eg session keys from MS) | 
|  | 6186 | *  4. value was temporary (eg content of input buffer) | 
|  | 6187 | *  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] | 6188 | */ | 
|  | 6189 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, | 
|  | 6190 | unsigned char *buf, | 
|  | 6191 | size_t buf_len, | 
|  | 6192 | size_t *olen ) | 
|  | 6193 | { | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6194 | unsigned char *p = buf; | 
|  | 6195 | size_t used = 0; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6196 | size_t session_len; | 
|  | 6197 | int ret = 0; | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6198 |  | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6199 | /* | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6200 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in | 
|  | 6201 | * this function's documentation. | 
|  | 6202 | * | 
|  | 6203 | * These are due to assumptions/limitations in the implementation. Some of | 
|  | 6204 | * them are likely to stay (no handshake in progress) some might go away | 
|  | 6205 | * (only DTLS) but are currently used to simplify the implementation. | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6206 | */ | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6207 | /* The initial handshake must be over */ | 
|  | 6208 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6209 | { | 
|  | 6210 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6211 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6212 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6213 | if( ssl->handshake != NULL ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6214 | { | 
|  | 6215 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6216 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6217 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6218 | /* Double-check that sub-structures are indeed ready */ | 
|  | 6219 | if( ssl->transform == NULL || ssl->session == NULL ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6220 | { | 
|  | 6221 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6222 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6223 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6224 | /* There must be no pending incoming or outgoing data */ | 
|  | 6225 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6226 | { | 
|  | 6227 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6228 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6229 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6230 | if( ssl->out_left != 0 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6231 | { | 
|  | 6232 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6233 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6234 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6235 | /* Protocol must be DLTS, not TLS */ | 
|  | 6236 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6237 | { | 
|  | 6238 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6239 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6240 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6241 | /* Version must be 1.2 */ | 
|  | 6242 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6243 | { | 
|  | 6244 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6245 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6246 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6247 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6248 | { | 
|  | 6249 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6250 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6251 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6252 | /* We must be using an AEAD ciphersuite */ | 
|  | 6253 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6254 | { | 
|  | 6255 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6256 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6257 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6258 | /* Renegotiation must not be enabled */ | 
|  | 6259 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6260 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6261 | { | 
|  | 6262 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6263 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6264 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6265 | #endif | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6266 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6267 | /* | 
|  | 6268 | * Version and format identifier | 
|  | 6269 | */ | 
|  | 6270 | used += sizeof( ssl_serialized_context_header ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6271 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6272 | if( used <= buf_len ) | 
|  | 6273 | { | 
|  | 6274 | memcpy( p, ssl_serialized_context_header, | 
|  | 6275 | sizeof( ssl_serialized_context_header ) ); | 
|  | 6276 | p += sizeof( ssl_serialized_context_header ); | 
|  | 6277 | } | 
|  | 6278 |  | 
|  | 6279 | /* | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6280 | * Session (length + data) | 
|  | 6281 | */ | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6282 | 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] | 6283 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) | 
|  | 6284 | return( ret ); | 
|  | 6285 |  | 
|  | 6286 | used += 4 + session_len; | 
|  | 6287 | if( used <= buf_len ) | 
|  | 6288 | { | 
|  | 6289 | *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF ); | 
|  | 6290 | *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF ); | 
|  | 6291 | *p++ = (unsigned char)( ( session_len >>  8 ) & 0xFF ); | 
|  | 6292 | *p++ = (unsigned char)( ( session_len       ) & 0xFF ); | 
|  | 6293 |  | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6294 | ret = ssl_session_save( ssl->session, 1, | 
|  | 6295 | p, session_len, &session_len ); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6296 | if( ret != 0 ) | 
|  | 6297 | return( ret ); | 
|  | 6298 |  | 
|  | 6299 | p += session_len; | 
|  | 6300 | } | 
|  | 6301 |  | 
|  | 6302 | /* | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6303 | * Transform | 
|  | 6304 | */ | 
|  | 6305 | used += sizeof( ssl->transform->randbytes ); | 
|  | 6306 | if( used <= buf_len ) | 
|  | 6307 | { | 
|  | 6308 | memcpy( p, ssl->transform->randbytes, | 
|  | 6309 | sizeof( ssl->transform->randbytes ) ); | 
|  | 6310 | p += sizeof( ssl->transform->randbytes ); | 
|  | 6311 | } | 
|  | 6312 |  | 
|  | 6313 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 6314 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; | 
|  | 6315 | if( used <= buf_len ) | 
|  | 6316 | { | 
|  | 6317 | *p++ = ssl->transform->in_cid_len; | 
|  | 6318 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); | 
|  | 6319 | p += ssl->transform->in_cid_len; | 
|  | 6320 |  | 
|  | 6321 | *p++ = ssl->transform->out_cid_len; | 
|  | 6322 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); | 
|  | 6323 | p += ssl->transform->out_cid_len; | 
|  | 6324 | } | 
|  | 6325 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 6326 |  | 
|  | 6327 | /* | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6328 | * Saved fields from top-level ssl_context structure | 
|  | 6329 | */ | 
|  | 6330 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) | 
|  | 6331 | used += 4; | 
|  | 6332 | if( used <= buf_len ) | 
|  | 6333 | { | 
|  | 6334 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF ); | 
|  | 6335 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF ); | 
|  | 6336 | *p++ = (unsigned char)( ( ssl->badmac_seen >>  8 ) & 0xFF ); | 
|  | 6337 | *p++ = (unsigned char)( ( ssl->badmac_seen       ) & 0xFF ); | 
|  | 6338 | } | 
|  | 6339 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ | 
|  | 6340 |  | 
|  | 6341 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 6342 | used += 16; | 
|  | 6343 | if( used <= buf_len ) | 
|  | 6344 | { | 
|  | 6345 | *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF ); | 
|  | 6346 | *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF ); | 
|  | 6347 | *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF ); | 
|  | 6348 | *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF ); | 
|  | 6349 | *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF ); | 
|  | 6350 | *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF ); | 
|  | 6351 | *p++ = (unsigned char)( ( ssl->in_window_top >>  8 ) & 0xFF ); | 
|  | 6352 | *p++ = (unsigned char)( ( ssl->in_window_top       ) & 0xFF ); | 
|  | 6353 |  | 
|  | 6354 | *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF ); | 
|  | 6355 | *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF ); | 
|  | 6356 | *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF ); | 
|  | 6357 | *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF ); | 
|  | 6358 | *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF ); | 
|  | 6359 | *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF ); | 
|  | 6360 | *p++ = (unsigned char)( ( ssl->in_window >>  8 ) & 0xFF ); | 
|  | 6361 | *p++ = (unsigned char)( ( ssl->in_window       ) & 0xFF ); | 
|  | 6362 | } | 
|  | 6363 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 6364 |  | 
|  | 6365 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6366 | used += 1; | 
|  | 6367 | if( used <= buf_len ) | 
|  | 6368 | { | 
|  | 6369 | *p++ = ssl->disable_datagram_packing; | 
|  | 6370 | } | 
|  | 6371 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 6372 |  | 
|  | 6373 | used += 8; | 
|  | 6374 | if( used <= buf_len ) | 
|  | 6375 | { | 
|  | 6376 | memcpy( p, ssl->cur_out_ctr, 8 ); | 
|  | 6377 | p += 8; | 
|  | 6378 | } | 
|  | 6379 |  | 
|  | 6380 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6381 | used += 2; | 
|  | 6382 | if( used <= buf_len ) | 
|  | 6383 | { | 
|  | 6384 | *p++ = (unsigned char)( ( ssl->mtu >>  8 ) & 0xFF ); | 
|  | 6385 | *p++ = (unsigned char)( ( ssl->mtu       ) & 0xFF ); | 
|  | 6386 | } | 
|  | 6387 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 6388 |  | 
|  | 6389 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 6390 | { | 
|  | 6391 | const uint8_t alpn_len = ssl->alpn_chosen | 
| Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 6392 | ? (uint8_t) strlen( ssl->alpn_chosen ) | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6393 | : 0; | 
|  | 6394 |  | 
|  | 6395 | used += 1 + alpn_len; | 
|  | 6396 | if( used <= buf_len ) | 
|  | 6397 | { | 
|  | 6398 | *p++ = alpn_len; | 
|  | 6399 |  | 
|  | 6400 | if( ssl->alpn_chosen != NULL ) | 
|  | 6401 | { | 
|  | 6402 | memcpy( p, ssl->alpn_chosen, alpn_len ); | 
|  | 6403 | p += alpn_len; | 
|  | 6404 | } | 
|  | 6405 | } | 
|  | 6406 | } | 
|  | 6407 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 6408 |  | 
|  | 6409 | /* | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6410 | * Done | 
|  | 6411 | */ | 
|  | 6412 | *olen = used; | 
|  | 6413 |  | 
|  | 6414 | if( used > buf_len ) | 
|  | 6415 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6416 |  | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6417 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); | 
|  | 6418 |  | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6419 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6420 | } | 
|  | 6421 |  | 
|  | 6422 | /* | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6423 | * Helper to get TLS 1.2 PRF from ciphersuite | 
|  | 6424 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) | 
|  | 6425 | */ | 
|  | 6426 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, | 
|  | 6427 | const char *label, | 
|  | 6428 | const unsigned char *random, size_t rlen, | 
|  | 6429 | unsigned char *dstbuf, size_t dlen ); | 
|  | 6430 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) | 
|  | 6431 | { | 
| Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 6432 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6433 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = | 
|  | 6434 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); | 
|  | 6435 |  | 
| Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6436 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) | 
|  | 6437 | return( tls_prf_sha384 ); | 
| Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 6438 | #else | 
|  | 6439 | (void) ciphersuite_id; | 
| Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6440 | #endif | 
|  | 6441 | return( tls_prf_sha256 ); | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6442 | } | 
|  | 6443 |  | 
|  | 6444 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6445 | * Deserialize context, see mbedtls_ssl_context_save() for format. | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6446 | * | 
|  | 6447 | * This internal version is wrapped by a public function that cleans up in | 
|  | 6448 | * case of error. | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6449 | */ | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6450 | static int ssl_context_load( mbedtls_ssl_context *ssl, | 
|  | 6451 | const unsigned char *buf, | 
|  | 6452 | size_t len ) | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6453 | { | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6454 | const unsigned char *p = buf; | 
|  | 6455 | const unsigned char * const end = buf + len; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6456 | size_t session_len; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6457 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6458 |  | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6459 | /* | 
|  | 6460 | * The context should have been freshly setup or reset. | 
|  | 6461 | * Give the user an error in case of obvious misuse. | 
| Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 6462 | * (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] | 6463 | * renegotiating, or if the user mistakenly loaded a session first.) | 
|  | 6464 | */ | 
|  | 6465 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || | 
|  | 6466 | ssl->session != NULL ) | 
|  | 6467 | { | 
|  | 6468 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6469 | } | 
|  | 6470 |  | 
|  | 6471 | /* | 
|  | 6472 | * We can't check that the config matches the initial one, but we can at | 
|  | 6473 | * least check it matches the requirements for serializing. | 
|  | 6474 | */ | 
|  | 6475 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | 
|  | 6476 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || | 
|  | 6477 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || | 
|  | 6478 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || | 
|  | 6479 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || | 
|  | 6480 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6481 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6482 | #endif | 
| Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6483 | 0 ) | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6484 | { | 
|  | 6485 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6486 | } | 
|  | 6487 |  | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6488 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); | 
|  | 6489 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6490 | /* | 
|  | 6491 | * Check version identifier | 
|  | 6492 | */ | 
|  | 6493 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) | 
|  | 6494 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6495 |  | 
|  | 6496 | if( memcmp( p, ssl_serialized_context_header, | 
|  | 6497 | sizeof( ssl_serialized_context_header ) ) != 0 ) | 
|  | 6498 | { | 
|  | 6499 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | 
|  | 6500 | } | 
|  | 6501 | p += sizeof( ssl_serialized_context_header ); | 
|  | 6502 |  | 
|  | 6503 | /* | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6504 | * Session | 
|  | 6505 | */ | 
|  | 6506 | if( (size_t)( end - p ) < 4 ) | 
|  | 6507 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6508 |  | 
|  | 6509 | session_len = ( (size_t) p[0] << 24 ) | | 
|  | 6510 | ( (size_t) p[1] << 16 ) | | 
|  | 6511 | ( (size_t) p[2] <<  8 ) | | 
|  | 6512 | ( (size_t) p[3]       ); | 
|  | 6513 | p += 4; | 
|  | 6514 |  | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6515 | /* This has been allocated by ssl_handshake_init(), called by | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6516 | * 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] | 6517 | ssl->session = ssl->session_negotiate; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6518 | ssl->session_in = ssl->session; | 
|  | 6519 | ssl->session_out = ssl->session; | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6520 | ssl->session_negotiate = NULL; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6521 |  | 
|  | 6522 | if( (size_t)( end - p ) < session_len ) | 
|  | 6523 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6524 |  | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6525 | ret = ssl_session_load( ssl->session, 1, p, session_len ); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6526 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6527 | { | 
|  | 6528 | mbedtls_ssl_session_free( ssl->session ); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6529 | return( ret ); | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6530 | } | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6531 |  | 
|  | 6532 | p += session_len; | 
|  | 6533 |  | 
|  | 6534 | /* | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6535 | * Transform | 
|  | 6536 | */ | 
|  | 6537 |  | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6538 | /* This has been allocated by ssl_handshake_init(), called by | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6539 | * 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] | 6540 | ssl->transform = ssl->transform_negotiate; | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6541 | ssl->transform_in = ssl->transform; | 
|  | 6542 | ssl->transform_out = ssl->transform; | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6543 | ssl->transform_negotiate = NULL; | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6544 |  | 
|  | 6545 | /* Read random bytes and populate structure */ | 
|  | 6546 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) | 
|  | 6547 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6548 |  | 
|  | 6549 | ret = ssl_populate_transform( ssl->transform, | 
|  | 6550 | ssl->session->ciphersuite, | 
|  | 6551 | ssl->session->master, | 
|  | 6552 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
|  | 6553 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 6554 | ssl->session->encrypt_then_mac, | 
|  | 6555 | #endif | 
|  | 6556 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 6557 | ssl->session->trunc_hmac, | 
|  | 6558 | #endif | 
|  | 6559 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
|  | 6560 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 6561 | ssl->session->compression, | 
|  | 6562 | #endif | 
|  | 6563 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), | 
|  | 6564 | p, /* currently pointing to randbytes */ | 
|  | 6565 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ | 
|  | 6566 | ssl->conf->endpoint, | 
|  | 6567 | ssl ); | 
|  | 6568 | if( ret != 0 ) | 
|  | 6569 | return( ret ); | 
|  | 6570 |  | 
|  | 6571 | p += sizeof( ssl->transform->randbytes ); | 
|  | 6572 |  | 
|  | 6573 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 6574 | /* Read connection IDs and store them */ | 
|  | 6575 | if( (size_t)( end - p ) < 1 ) | 
|  | 6576 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6577 |  | 
|  | 6578 | ssl->transform->in_cid_len = *p++; | 
|  | 6579 |  | 
| Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 6580 | 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] | 6581 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6582 |  | 
|  | 6583 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); | 
|  | 6584 | p += ssl->transform->in_cid_len; | 
|  | 6585 |  | 
|  | 6586 | ssl->transform->out_cid_len = *p++; | 
|  | 6587 |  | 
|  | 6588 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) | 
|  | 6589 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6590 |  | 
|  | 6591 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); | 
|  | 6592 | p += ssl->transform->out_cid_len; | 
|  | 6593 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 6594 |  | 
|  | 6595 | /* | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6596 | * Saved fields from top-level ssl_context structure | 
|  | 6597 | */ | 
|  | 6598 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) | 
|  | 6599 | if( (size_t)( end - p ) < 4 ) | 
|  | 6600 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6601 |  | 
|  | 6602 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | | 
|  | 6603 | ( (uint32_t) p[1] << 16 ) | | 
|  | 6604 | ( (uint32_t) p[2] <<  8 ) | | 
|  | 6605 | ( (uint32_t) p[3]       ); | 
|  | 6606 | p += 4; | 
|  | 6607 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ | 
|  | 6608 |  | 
|  | 6609 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 6610 | if( (size_t)( end - p ) < 16 ) | 
|  | 6611 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6612 |  | 
|  | 6613 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | | 
|  | 6614 | ( (uint64_t) p[1] << 48 ) | | 
|  | 6615 | ( (uint64_t) p[2] << 40 ) | | 
|  | 6616 | ( (uint64_t) p[3] << 32 ) | | 
|  | 6617 | ( (uint64_t) p[4] << 24 ) | | 
|  | 6618 | ( (uint64_t) p[5] << 16 ) | | 
|  | 6619 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 6620 | ( (uint64_t) p[7]       ); | 
|  | 6621 | p += 8; | 
|  | 6622 |  | 
|  | 6623 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | | 
|  | 6624 | ( (uint64_t) p[1] << 48 ) | | 
|  | 6625 | ( (uint64_t) p[2] << 40 ) | | 
|  | 6626 | ( (uint64_t) p[3] << 32 ) | | 
|  | 6627 | ( (uint64_t) p[4] << 24 ) | | 
|  | 6628 | ( (uint64_t) p[5] << 16 ) | | 
|  | 6629 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 6630 | ( (uint64_t) p[7]       ); | 
|  | 6631 | p += 8; | 
|  | 6632 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 6633 |  | 
|  | 6634 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6635 | if( (size_t)( end - p ) < 1 ) | 
|  | 6636 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6637 |  | 
|  | 6638 | ssl->disable_datagram_packing = *p++; | 
|  | 6639 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 6640 |  | 
|  | 6641 | if( (size_t)( end - p ) < 8 ) | 
|  | 6642 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6643 |  | 
|  | 6644 | memcpy( ssl->cur_out_ctr, p, 8 ); | 
|  | 6645 | p += 8; | 
|  | 6646 |  | 
|  | 6647 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6648 | if( (size_t)( end - p ) < 2 ) | 
|  | 6649 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6650 |  | 
|  | 6651 | ssl->mtu = ( p[0] << 8 ) | p[1]; | 
|  | 6652 | p += 2; | 
|  | 6653 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 6654 |  | 
|  | 6655 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 6656 | { | 
|  | 6657 | uint8_t alpn_len; | 
|  | 6658 | const char **cur; | 
|  | 6659 |  | 
|  | 6660 | if( (size_t)( end - p ) < 1 ) | 
|  | 6661 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6662 |  | 
|  | 6663 | alpn_len = *p++; | 
|  | 6664 |  | 
|  | 6665 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) | 
|  | 6666 | { | 
|  | 6667 | /* alpn_chosen should point to an item in the configured list */ | 
|  | 6668 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) | 
|  | 6669 | { | 
|  | 6670 | if( strlen( *cur ) == alpn_len && | 
|  | 6671 | memcmp( p, cur, alpn_len ) == 0 ) | 
|  | 6672 | { | 
|  | 6673 | ssl->alpn_chosen = *cur; | 
|  | 6674 | break; | 
|  | 6675 | } | 
|  | 6676 | } | 
|  | 6677 | } | 
|  | 6678 |  | 
|  | 6679 | /* can only happen on conf mismatch */ | 
|  | 6680 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) | 
|  | 6681 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6682 |  | 
|  | 6683 | p += alpn_len; | 
|  | 6684 | } | 
|  | 6685 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 6686 |  | 
|  | 6687 | /* | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6688 | * Forced fields from top-level ssl_context structure | 
|  | 6689 | * | 
|  | 6690 | * Most of them already set to the correct value by mbedtls_ssl_init() and | 
|  | 6691 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. | 
|  | 6692 | */ | 
|  | 6693 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; | 
|  | 6694 |  | 
|  | 6695 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
|  | 6696 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; | 
|  | 6697 |  | 
| Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6698 | /* Adjust pointers for header fields of outgoing records to | 
|  | 6699 | * the given transform, accounting for explicit IV and CID. */ | 
| Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 6700 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); | 
| Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6701 |  | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6702 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6703 | ssl->in_epoch = 1; | 
|  | 6704 | #endif | 
|  | 6705 |  | 
|  | 6706 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, | 
|  | 6707 | * 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] | 6708 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() | 
|  | 6709 | * inappropriately. */ | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6710 | if( ssl->handshake != NULL ) | 
|  | 6711 | { | 
|  | 6712 | mbedtls_ssl_handshake_free( ssl ); | 
|  | 6713 | mbedtls_free( ssl->handshake ); | 
|  | 6714 | ssl->handshake = NULL; | 
|  | 6715 | } | 
|  | 6716 |  | 
|  | 6717 | /* | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6718 | * Done - should have consumed entire buffer | 
|  | 6719 | */ | 
|  | 6720 | if( p != end ) | 
|  | 6721 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6722 |  | 
|  | 6723 | return( 0 ); | 
|  | 6724 | } | 
|  | 6725 |  | 
|  | 6726 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6727 | * Deserialize context: public wrapper for error cleaning | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6728 | */ | 
|  | 6729 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, | 
|  | 6730 | const unsigned char *buf, | 
|  | 6731 | size_t len ) | 
|  | 6732 | { | 
|  | 6733 | int ret = ssl_context_load( context, buf, len ); | 
|  | 6734 |  | 
|  | 6735 | if( ret != 0 ) | 
|  | 6736 | mbedtls_ssl_free( context ); | 
|  | 6737 |  | 
|  | 6738 | return( ret ); | 
|  | 6739 | } | 
| Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 6740 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6741 |  | 
|  | 6742 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6743 | * Free an SSL context | 
|  | 6744 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6745 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6746 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6747 | if( ssl == NULL ) | 
|  | 6748 | return; | 
|  | 6749 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6750 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6751 |  | 
| Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6752 | if( ssl->out_buf != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6753 | { | 
| sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6754 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 6755 | size_t out_buf_len = ssl->out_buf_len; | 
|  | 6756 | #else | 
|  | 6757 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
|  | 6758 | #endif | 
|  | 6759 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6760 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6761 | mbedtls_free( ssl->out_buf ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6762 | ssl->out_buf = NULL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6763 | } | 
|  | 6764 |  | 
| Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6765 | if( ssl->in_buf != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6766 | { | 
| sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6767 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 6768 | size_t in_buf_len = ssl->in_buf_len; | 
|  | 6769 | #else | 
|  | 6770 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 6771 | #endif | 
|  | 6772 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6773 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6774 | mbedtls_free( ssl->in_buf ); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6775 | ssl->in_buf = NULL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6776 | } | 
|  | 6777 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6778 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 6779 | if( ssl->compress_buf != NULL ) | 
|  | 6780 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6781 | 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] | 6782 | mbedtls_free( ssl->compress_buf ); | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 6783 | } | 
|  | 6784 | #endif | 
|  | 6785 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6786 | if( ssl->transform ) | 
|  | 6787 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6788 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 6789 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6790 | } | 
|  | 6791 |  | 
|  | 6792 | if( ssl->handshake ) | 
|  | 6793 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6794 | mbedtls_ssl_handshake_free( ssl ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6795 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); | 
|  | 6796 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6797 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6798 | mbedtls_free( ssl->handshake ); | 
|  | 6799 | mbedtls_free( ssl->transform_negotiate ); | 
|  | 6800 | mbedtls_free( ssl->session_negotiate ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6801 | } | 
|  | 6802 |  | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6803 | if( ssl->session ) | 
|  | 6804 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6805 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 6806 | mbedtls_free( ssl->session ); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6807 | } | 
|  | 6808 |  | 
| Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 6809 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 6810 | if( ssl->hostname != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6811 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6812 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6813 | mbedtls_free( ssl->hostname ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6814 | } | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 6815 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6816 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6817 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 6818 | if( mbedtls_ssl_hw_record_finish != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 6819 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6820 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); | 
|  | 6821 | mbedtls_ssl_hw_record_finish( ssl ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 6822 | } | 
|  | 6823 | #endif | 
|  | 6824 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6825 | #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] | 6826 | mbedtls_free( ssl->cli_id ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 6827 | #endif | 
|  | 6828 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6829 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); | 
| Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 6830 |  | 
| Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 6831 | /* Actually clear after last debug message */ | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6832 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6833 | } | 
|  | 6834 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6835 | /* | 
|  | 6836 | * Initialze mbedtls_ssl_config | 
|  | 6837 | */ | 
|  | 6838 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) | 
|  | 6839 | { | 
|  | 6840 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); | 
|  | 6841 | } | 
|  | 6842 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6843 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6844 | static int ssl_preset_default_hashes[] = { | 
|  | 6845 | #if defined(MBEDTLS_SHA512_C) | 
|  | 6846 | MBEDTLS_MD_SHA512, | 
|  | 6847 | MBEDTLS_MD_SHA384, | 
|  | 6848 | #endif | 
|  | 6849 | #if defined(MBEDTLS_SHA256_C) | 
|  | 6850 | MBEDTLS_MD_SHA256, | 
|  | 6851 | MBEDTLS_MD_SHA224, | 
|  | 6852 | #endif | 
| Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 6853 | #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] | 6854 | MBEDTLS_MD_SHA1, | 
|  | 6855 | #endif | 
|  | 6856 | MBEDTLS_MD_NONE | 
|  | 6857 | }; | 
| Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 6858 | #endif | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6859 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6860 | static int ssl_preset_suiteb_ciphersuites[] = { | 
|  | 6861 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | 
|  | 6862 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | 
|  | 6863 | 0 | 
|  | 6864 | }; | 
|  | 6865 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6866 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6867 | static int ssl_preset_suiteb_hashes[] = { | 
|  | 6868 | MBEDTLS_MD_SHA256, | 
|  | 6869 | MBEDTLS_MD_SHA384, | 
|  | 6870 | MBEDTLS_MD_NONE | 
|  | 6871 | }; | 
|  | 6872 | #endif | 
|  | 6873 |  | 
|  | 6874 | #if defined(MBEDTLS_ECP_C) | 
|  | 6875 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6876 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6877 | MBEDTLS_ECP_DP_SECP256R1, | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6878 | #endif | 
|  | 6879 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6880 | MBEDTLS_ECP_DP_SECP384R1, | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6881 | #endif | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6882 | MBEDTLS_ECP_DP_NONE | 
|  | 6883 | }; | 
|  | 6884 | #endif | 
|  | 6885 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6886 | /* | 
| Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 6887 | * Load default in mbedtls_ssl_config | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6888 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6889 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6890 | int endpoint, int transport, int preset ) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6891 | { | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6892 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6893 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6894 | #endif | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6895 |  | 
| Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 6896 | /* Use the functions here so that they are covered in tests, | 
|  | 6897 | * but otherwise access member directly for efficiency */ | 
|  | 6898 | mbedtls_ssl_conf_endpoint( conf, endpoint ); | 
|  | 6899 | mbedtls_ssl_conf_transport( conf, transport ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6900 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6901 | /* | 
|  | 6902 | * Things that are common to all presets | 
|  | 6903 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6904 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 6905 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6906 | { | 
|  | 6907 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; | 
|  | 6908 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 6909 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; | 
|  | 6910 | #endif | 
|  | 6911 | } | 
|  | 6912 | #endif | 
|  | 6913 |  | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 6914 | #if defined(MBEDTLS_ARC4_C) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6915 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 6916 | #endif | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6917 |  | 
|  | 6918 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 6919 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | 
|  | 6920 | #endif | 
|  | 6921 |  | 
|  | 6922 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 6923 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | 
|  | 6924 | #endif | 
|  | 6925 |  | 
| Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 6926 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
|  | 6927 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; | 
|  | 6928 | #endif | 
|  | 6929 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6930 | #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] | 6931 | conf->f_cookie_write = ssl_cookie_write_dummy; | 
|  | 6932 | conf->f_cookie_check = ssl_cookie_check_dummy; | 
|  | 6933 | #endif | 
|  | 6934 |  | 
|  | 6935 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 6936 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; | 
|  | 6937 | #endif | 
|  | 6938 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6939 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 6940 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; | 
|  | 6941 | #endif | 
|  | 6942 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6943 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6944 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; | 
|  | 6945 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; | 
|  | 6946 | #endif | 
|  | 6947 |  | 
|  | 6948 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6949 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; | 
| Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 6950 | memset( conf->renego_period,     0x00, 2 ); | 
|  | 6951 | memset( conf->renego_period + 2, 0xFF, 6 ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6952 | #endif | 
|  | 6953 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6954 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
|  | 6955 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 6956 | { | 
| Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 6957 | const unsigned char dhm_p[] = | 
|  | 6958 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; | 
|  | 6959 | const unsigned char dhm_g[] = | 
|  | 6960 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; | 
|  | 6961 |  | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 6962 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, | 
|  | 6963 | dhm_p, sizeof( dhm_p ), | 
|  | 6964 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6965 | { | 
|  | 6966 | return( ret ); | 
|  | 6967 | } | 
|  | 6968 | } | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 6969 | #endif | 
|  | 6970 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6971 | /* | 
|  | 6972 | * Preset-specific defaults | 
|  | 6973 | */ | 
|  | 6974 | switch( preset ) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6975 | { | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6976 | /* | 
|  | 6977 | * NSA Suite B | 
|  | 6978 | */ | 
|  | 6979 | case MBEDTLS_SSL_PRESET_SUITEB: | 
|  | 6980 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
|  | 6981 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ | 
|  | 6982 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; | 
|  | 6983 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; | 
|  | 6984 |  | 
|  | 6985 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = | 
|  | 6986 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = | 
|  | 6987 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = | 
|  | 6988 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = | 
|  | 6989 | ssl_preset_suiteb_ciphersuites; | 
|  | 6990 |  | 
|  | 6991 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 6992 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6993 | #endif | 
|  | 6994 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6995 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6996 | conf->sig_hashes = ssl_preset_suiteb_hashes; | 
|  | 6997 | #endif | 
|  | 6998 |  | 
|  | 6999 | #if defined(MBEDTLS_ECP_C) | 
|  | 7000 | conf->curve_list = ssl_preset_suiteb_curves; | 
|  | 7001 | #endif | 
| Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 7002 | break; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 7003 |  | 
|  | 7004 | /* | 
|  | 7005 | * Default | 
|  | 7006 | */ | 
|  | 7007 | default: | 
| Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 7008 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > | 
|  | 7009 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? | 
|  | 7010 | MBEDTLS_SSL_MIN_MAJOR_VERSION : | 
|  | 7011 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; | 
|  | 7012 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > | 
|  | 7013 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? | 
|  | 7014 | MBEDTLS_SSL_MIN_MINOR_VERSION : | 
|  | 7015 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 7016 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; | 
|  | 7017 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; | 
|  | 7018 |  | 
|  | 7019 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 7020 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 7021 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; | 
|  | 7022 | #endif | 
|  | 7023 |  | 
|  | 7024 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = | 
|  | 7025 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = | 
|  | 7026 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = | 
|  | 7027 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = | 
|  | 7028 | mbedtls_ssl_list_ciphersuites(); | 
|  | 7029 |  | 
|  | 7030 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7031 | conf->cert_profile = &mbedtls_x509_crt_profile_default; | 
|  | 7032 | #endif | 
|  | 7033 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7034 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 7035 | conf->sig_hashes = ssl_preset_default_hashes; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 7036 | #endif | 
|  | 7037 |  | 
|  | 7038 | #if defined(MBEDTLS_ECP_C) | 
|  | 7039 | conf->curve_list = mbedtls_ecp_grp_id_list(); | 
|  | 7040 | #endif | 
|  | 7041 |  | 
|  | 7042 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 7043 | conf->dhm_min_bitlen = 1024; | 
|  | 7044 | #endif | 
|  | 7045 | } | 
|  | 7046 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7047 | return( 0 ); | 
|  | 7048 | } | 
|  | 7049 |  | 
|  | 7050 | /* | 
|  | 7051 | * Free mbedtls_ssl_config | 
|  | 7052 | */ | 
|  | 7053 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) | 
|  | 7054 | { | 
|  | 7055 | #if defined(MBEDTLS_DHM_C) | 
|  | 7056 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 7057 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 7058 | #endif | 
|  | 7059 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7060 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7061 | if( conf->psk != NULL ) | 
|  | 7062 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7063 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7064 | mbedtls_free( conf->psk ); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 7065 | conf->psk = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7066 | conf->psk_len = 0; | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 7067 | } | 
|  | 7068 |  | 
|  | 7069 | if( conf->psk_identity != NULL ) | 
|  | 7070 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7071 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 7072 | mbedtls_free( conf->psk_identity ); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 7073 | conf->psk_identity = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7074 | conf->psk_identity_len = 0; | 
|  | 7075 | } | 
|  | 7076 | #endif | 
|  | 7077 |  | 
|  | 7078 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7079 | ssl_key_cert_free( conf->key_cert ); | 
|  | 7080 | #endif | 
|  | 7081 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7082 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7083 | } | 
|  | 7084 |  | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 7085 | #if defined(MBEDTLS_PK_C) && \ | 
|  | 7086 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7087 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7088 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7089 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7090 | 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] | 7091 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7092 | #if defined(MBEDTLS_RSA_C) | 
|  | 7093 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) | 
|  | 7094 | return( MBEDTLS_SSL_SIG_RSA ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7095 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7096 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 7097 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) | 
|  | 7098 | return( MBEDTLS_SSL_SIG_ECDSA ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7099 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7100 | return( MBEDTLS_SSL_SIG_ANON ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7101 | } | 
|  | 7102 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7103 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) | 
|  | 7104 | { | 
|  | 7105 | switch( type ) { | 
|  | 7106 | case MBEDTLS_PK_RSA: | 
|  | 7107 | return( MBEDTLS_SSL_SIG_RSA ); | 
|  | 7108 | case MBEDTLS_PK_ECDSA: | 
|  | 7109 | case MBEDTLS_PK_ECKEY: | 
|  | 7110 | return( MBEDTLS_SSL_SIG_ECDSA ); | 
|  | 7111 | default: | 
|  | 7112 | return( MBEDTLS_SSL_SIG_ANON ); | 
|  | 7113 | } | 
|  | 7114 | } | 
|  | 7115 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7116 | 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] | 7117 | { | 
|  | 7118 | switch( sig ) | 
|  | 7119 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7120 | #if defined(MBEDTLS_RSA_C) | 
|  | 7121 | case MBEDTLS_SSL_SIG_RSA: | 
|  | 7122 | return( MBEDTLS_PK_RSA ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7123 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7124 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 7125 | case MBEDTLS_SSL_SIG_ECDSA: | 
|  | 7126 | return( MBEDTLS_PK_ECDSA ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7127 | #endif | 
|  | 7128 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7129 | return( MBEDTLS_PK_NONE ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7130 | } | 
|  | 7131 | } | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 7132 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7133 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7134 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7135 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7136 |  | 
|  | 7137 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ | 
|  | 7138 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 7139 | mbedtls_pk_type_t sig_alg ) | 
|  | 7140 | { | 
|  | 7141 | switch( sig_alg ) | 
|  | 7142 | { | 
|  | 7143 | case MBEDTLS_PK_RSA: | 
|  | 7144 | return( set->rsa ); | 
|  | 7145 | case MBEDTLS_PK_ECDSA: | 
|  | 7146 | return( set->ecdsa ); | 
|  | 7147 | default: | 
|  | 7148 | return( MBEDTLS_MD_NONE ); | 
|  | 7149 | } | 
|  | 7150 | } | 
|  | 7151 |  | 
|  | 7152 | /* Add a signature-hash-pair to a signature-hash set */ | 
|  | 7153 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 7154 | mbedtls_pk_type_t sig_alg, | 
|  | 7155 | mbedtls_md_type_t md_alg ) | 
|  | 7156 | { | 
|  | 7157 | switch( sig_alg ) | 
|  | 7158 | { | 
|  | 7159 | case MBEDTLS_PK_RSA: | 
|  | 7160 | if( set->rsa == MBEDTLS_MD_NONE ) | 
|  | 7161 | set->rsa = md_alg; | 
|  | 7162 | break; | 
|  | 7163 |  | 
|  | 7164 | case MBEDTLS_PK_ECDSA: | 
|  | 7165 | if( set->ecdsa == MBEDTLS_MD_NONE ) | 
|  | 7166 | set->ecdsa = md_alg; | 
|  | 7167 | break; | 
|  | 7168 |  | 
|  | 7169 | default: | 
|  | 7170 | break; | 
|  | 7171 | } | 
|  | 7172 | } | 
|  | 7173 |  | 
|  | 7174 | /* Allow exactly one hash algorithm for each signature. */ | 
|  | 7175 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 7176 | mbedtls_md_type_t md_alg ) | 
|  | 7177 | { | 
|  | 7178 | set->rsa   = md_alg; | 
|  | 7179 | set->ecdsa = md_alg; | 
|  | 7180 | } | 
|  | 7181 |  | 
|  | 7182 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7183 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7184 |  | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 7185 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7186 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 7187 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7188 | 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] | 7189 | { | 
|  | 7190 | switch( hash ) | 
|  | 7191 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7192 | #if defined(MBEDTLS_MD5_C) | 
|  | 7193 | case MBEDTLS_SSL_HASH_MD5: | 
|  | 7194 | return( MBEDTLS_MD_MD5 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7195 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7196 | #if defined(MBEDTLS_SHA1_C) | 
|  | 7197 | case MBEDTLS_SSL_HASH_SHA1: | 
|  | 7198 | return( MBEDTLS_MD_SHA1 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7199 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7200 | #if defined(MBEDTLS_SHA256_C) | 
|  | 7201 | case MBEDTLS_SSL_HASH_SHA224: | 
|  | 7202 | return( MBEDTLS_MD_SHA224 ); | 
|  | 7203 | case MBEDTLS_SSL_HASH_SHA256: | 
|  | 7204 | return( MBEDTLS_MD_SHA256 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7205 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7206 | #if defined(MBEDTLS_SHA512_C) | 
|  | 7207 | case MBEDTLS_SSL_HASH_SHA384: | 
|  | 7208 | return( MBEDTLS_MD_SHA384 ); | 
|  | 7209 | case MBEDTLS_SSL_HASH_SHA512: | 
|  | 7210 | return( MBEDTLS_MD_SHA512 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7211 | #endif | 
|  | 7212 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7213 | return( MBEDTLS_MD_NONE ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7214 | } | 
|  | 7215 | } | 
|  | 7216 |  | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7217 | /* | 
|  | 7218 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX | 
|  | 7219 | */ | 
|  | 7220 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) | 
|  | 7221 | { | 
|  | 7222 | switch( md ) | 
|  | 7223 | { | 
|  | 7224 | #if defined(MBEDTLS_MD5_C) | 
|  | 7225 | case MBEDTLS_MD_MD5: | 
|  | 7226 | return( MBEDTLS_SSL_HASH_MD5 ); | 
|  | 7227 | #endif | 
|  | 7228 | #if defined(MBEDTLS_SHA1_C) | 
|  | 7229 | case MBEDTLS_MD_SHA1: | 
|  | 7230 | return( MBEDTLS_SSL_HASH_SHA1 ); | 
|  | 7231 | #endif | 
|  | 7232 | #if defined(MBEDTLS_SHA256_C) | 
|  | 7233 | case MBEDTLS_MD_SHA224: | 
|  | 7234 | return( MBEDTLS_SSL_HASH_SHA224 ); | 
|  | 7235 | case MBEDTLS_MD_SHA256: | 
|  | 7236 | return( MBEDTLS_SSL_HASH_SHA256 ); | 
|  | 7237 | #endif | 
|  | 7238 | #if defined(MBEDTLS_SHA512_C) | 
|  | 7239 | case MBEDTLS_MD_SHA384: | 
|  | 7240 | return( MBEDTLS_SSL_HASH_SHA384 ); | 
|  | 7241 | case MBEDTLS_MD_SHA512: | 
|  | 7242 | return( MBEDTLS_SSL_HASH_SHA512 ); | 
|  | 7243 | #endif | 
|  | 7244 | default: | 
|  | 7245 | return( MBEDTLS_SSL_HASH_NONE ); | 
|  | 7246 | } | 
|  | 7247 | } | 
|  | 7248 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7249 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7250 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7251 | * 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] | 7252 | * 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] | 7253 | */ | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7254 | 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] | 7255 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7256 | const mbedtls_ecp_group_id *gid; | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7257 |  | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7258 | if( ssl->conf->curve_list == NULL ) | 
|  | 7259 | return( -1 ); | 
|  | 7260 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7261 | 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] | 7262 | if( *gid == grp_id ) | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7263 | return( 0 ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7264 |  | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7265 | return( -1 ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7266 | } | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7267 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7268 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7269 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7270 | /* | 
|  | 7271 | * Check if a hash proposed by the peer is in our list. | 
|  | 7272 | * Return 0 if we're willing to use it, -1 otherwise. | 
|  | 7273 | */ | 
|  | 7274 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, | 
|  | 7275 | mbedtls_md_type_t md ) | 
|  | 7276 | { | 
|  | 7277 | const int *cur; | 
|  | 7278 |  | 
|  | 7279 | if( ssl->conf->sig_hashes == NULL ) | 
|  | 7280 | return( -1 ); | 
|  | 7281 |  | 
|  | 7282 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) | 
|  | 7283 | if( *cur == (int) md ) | 
|  | 7284 | return( 0 ); | 
|  | 7285 |  | 
|  | 7286 | return( -1 ); | 
|  | 7287 | } | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7288 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7289 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7290 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 7291 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, | 
|  | 7292 | const mbedtls_ssl_ciphersuite_t *ciphersuite, | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7293 | int cert_endpoint, | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7294 | uint32_t *flags ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7295 | { | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7296 | int ret = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7297 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7298 | int usage = 0; | 
|  | 7299 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7300 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7301 | const char *ext_oid; | 
|  | 7302 | size_t ext_len; | 
|  | 7303 | #endif | 
|  | 7304 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7305 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) &&          \ | 
|  | 7306 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7307 | ((void) cert); | 
|  | 7308 | ((void) cert_endpoint); | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7309 | ((void) flags); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7310 | #endif | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7311 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7312 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) | 
|  | 7313 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7314 | { | 
|  | 7315 | /* Server part of the key exchange */ | 
|  | 7316 | switch( ciphersuite->key_exchange ) | 
|  | 7317 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7318 | case MBEDTLS_KEY_EXCHANGE_RSA: | 
|  | 7319 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7320 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7321 | break; | 
|  | 7322 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7323 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: | 
|  | 7324 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: | 
|  | 7325 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: | 
|  | 7326 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7327 | break; | 
|  | 7328 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7329 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: | 
|  | 7330 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7331 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7332 | break; | 
|  | 7333 |  | 
|  | 7334 | /* 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] | 7335 | case MBEDTLS_KEY_EXCHANGE_NONE: | 
|  | 7336 | case MBEDTLS_KEY_EXCHANGE_PSK: | 
|  | 7337 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: | 
|  | 7338 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: | 
| Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 7339 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7340 | usage = 0; | 
|  | 7341 | } | 
|  | 7342 | } | 
|  | 7343 | else | 
|  | 7344 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7345 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ | 
|  | 7346 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7347 | } | 
|  | 7348 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7349 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7350 | { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7351 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7352 | ret = -1; | 
|  | 7353 | } | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7354 | #else | 
|  | 7355 | ((void) ciphersuite); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7356 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7357 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7358 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) | 
|  | 7359 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7360 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7361 | ext_oid = MBEDTLS_OID_SERVER_AUTH; | 
|  | 7362 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7363 | } | 
|  | 7364 | else | 
|  | 7365 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7366 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; | 
|  | 7367 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7368 | } | 
|  | 7369 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7370 | 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] | 7371 | { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7372 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7373 | ret = -1; | 
|  | 7374 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7375 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7376 |  | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7377 | return( ret ); | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7378 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7379 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 7380 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7381 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) | 
|  | 7382 | { | 
|  | 7383 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 7384 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 7385 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; | 
|  | 7386 |  | 
|  | 7387 | switch( md ) | 
|  | 7388 | { | 
|  | 7389 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 7390 | #if defined(MBEDTLS_MD5_C) | 
|  | 7391 | case MBEDTLS_SSL_HASH_MD5: | 
| Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 7392 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7393 | #endif | 
|  | 7394 | #if defined(MBEDTLS_SHA1_C) | 
|  | 7395 | case MBEDTLS_SSL_HASH_SHA1: | 
|  | 7396 | ssl->handshake->calc_verify = ssl_calc_verify_tls; | 
|  | 7397 | break; | 
|  | 7398 | #endif | 
|  | 7399 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 7400 | #if defined(MBEDTLS_SHA512_C) | 
|  | 7401 | case MBEDTLS_SSL_HASH_SHA384: | 
|  | 7402 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; | 
|  | 7403 | break; | 
|  | 7404 | #endif | 
|  | 7405 | #if defined(MBEDTLS_SHA256_C) | 
|  | 7406 | case MBEDTLS_SSL_HASH_SHA256: | 
|  | 7407 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; | 
|  | 7408 | break; | 
|  | 7409 | #endif | 
|  | 7410 | default: | 
|  | 7411 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; | 
|  | 7412 | } | 
|  | 7413 |  | 
|  | 7414 | return 0; | 
|  | 7415 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 7416 | (void) ssl; | 
|  | 7417 | (void) md; | 
|  | 7418 |  | 
|  | 7419 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; | 
|  | 7420 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 7421 | } | 
|  | 7422 |  | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7423 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 7424 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 7425 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, | 
|  | 7426 | unsigned char *output, | 
|  | 7427 | unsigned char *data, size_t data_len ) | 
|  | 7428 | { | 
|  | 7429 | int ret = 0; | 
|  | 7430 | mbedtls_md5_context mbedtls_md5; | 
|  | 7431 | mbedtls_sha1_context mbedtls_sha1; | 
|  | 7432 |  | 
|  | 7433 | mbedtls_md5_init( &mbedtls_md5 ); | 
|  | 7434 | mbedtls_sha1_init( &mbedtls_sha1 ); | 
|  | 7435 |  | 
|  | 7436 | /* | 
|  | 7437 | * digitally-signed struct { | 
|  | 7438 | *     opaque md5_hash[16]; | 
|  | 7439 | *     opaque sha_hash[20]; | 
|  | 7440 | * }; | 
|  | 7441 | * | 
|  | 7442 | * md5_hash | 
|  | 7443 | *     MD5(ClientHello.random + ServerHello.random | 
|  | 7444 | *                            + ServerParams); | 
|  | 7445 | * sha_hash | 
|  | 7446 | *     SHA(ClientHello.random + ServerHello.random | 
|  | 7447 | *                            + ServerParams); | 
|  | 7448 | */ | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7449 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7450 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7451 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7452 | goto exit; | 
|  | 7453 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7454 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7455 | ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 7456 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7457 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7458 | goto exit; | 
|  | 7459 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7460 | 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] | 7461 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7462 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7463 | goto exit; | 
|  | 7464 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7465 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7466 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7467 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7468 | goto exit; | 
|  | 7469 | } | 
|  | 7470 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7471 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7472 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7473 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7474 | goto exit; | 
|  | 7475 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7476 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7477 | ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 7478 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7479 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7480 | goto exit; | 
|  | 7481 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7482 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7483 | data_len ) ) != 0 ) | 
|  | 7484 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7485 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7486 | goto exit; | 
|  | 7487 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7488 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7489 | output + 16 ) ) != 0 ) | 
|  | 7490 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7491 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7492 | goto exit; | 
|  | 7493 | } | 
|  | 7494 |  | 
|  | 7495 | exit: | 
|  | 7496 | mbedtls_md5_free( &mbedtls_md5 ); | 
|  | 7497 | mbedtls_sha1_free( &mbedtls_sha1 ); | 
|  | 7498 |  | 
|  | 7499 | if( ret != 0 ) | 
|  | 7500 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7501 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 7502 |  | 
|  | 7503 | return( ret ); | 
|  | 7504 |  | 
|  | 7505 | } | 
|  | 7506 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 7507 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 7508 |  | 
|  | 7509 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 7510 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7511 |  | 
|  | 7512 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 7513 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | 
|  | 7514 | unsigned char *hash, size_t *hashlen, | 
|  | 7515 | unsigned char *data, size_t data_len, | 
|  | 7516 | mbedtls_md_type_t md_alg ) | 
|  | 7517 | { | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7518 | psa_status_t status; | 
| Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7519 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7520 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); | 
|  | 7521 |  | 
| Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7522 | 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] | 7523 |  | 
|  | 7524 | if( ( status = psa_hash_setup( &hash_operation, | 
|  | 7525 | hash_alg ) ) != PSA_SUCCESS ) | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7526 | { | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7527 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7528 | goto exit; | 
|  | 7529 | } | 
|  | 7530 |  | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7531 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, | 
|  | 7532 | 64 ) ) != PSA_SUCCESS ) | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7533 | { | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7534 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7535 | goto exit; | 
|  | 7536 | } | 
|  | 7537 |  | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7538 | if( ( status = psa_hash_update( &hash_operation, | 
|  | 7539 | data, data_len ) ) != PSA_SUCCESS ) | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7540 | { | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7541 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7542 | goto exit; | 
|  | 7543 | } | 
|  | 7544 |  | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7545 | if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, | 
|  | 7546 | hashlen ) ) != PSA_SUCCESS ) | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7547 | { | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7548 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7549 | goto exit; | 
|  | 7550 | } | 
|  | 7551 |  | 
|  | 7552 | exit: | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7553 | if( status != PSA_SUCCESS ) | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7554 | { | 
|  | 7555 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7556 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7557 | switch( status ) | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7558 | { | 
|  | 7559 | case PSA_ERROR_NOT_SUPPORTED: | 
|  | 7560 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); | 
| Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7561 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7562 | case PSA_ERROR_BUFFER_TOO_SMALL: | 
|  | 7563 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); | 
|  | 7564 | case PSA_ERROR_INSUFFICIENT_MEMORY: | 
|  | 7565 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); | 
|  | 7566 | default: | 
|  | 7567 | return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED ); | 
|  | 7568 | } | 
|  | 7569 | } | 
|  | 7570 | return( 0 ); | 
|  | 7571 | } | 
|  | 7572 |  | 
|  | 7573 | #else | 
|  | 7574 |  | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7575 | 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] | 7576 | unsigned char *hash, size_t *hashlen, | 
|  | 7577 | unsigned char *data, size_t data_len, | 
|  | 7578 | mbedtls_md_type_t md_alg ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7579 | { | 
|  | 7580 | int ret = 0; | 
|  | 7581 | mbedtls_md_context_t ctx; | 
|  | 7582 | 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] | 7583 | *hashlen = mbedtls_md_get_size( md_info ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7584 |  | 
| Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7585 | 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] | 7586 |  | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7587 | mbedtls_md_init( &ctx ); | 
|  | 7588 |  | 
|  | 7589 | /* | 
|  | 7590 | * digitally-signed struct { | 
|  | 7591 | *     opaque client_random[32]; | 
|  | 7592 | *     opaque server_random[32]; | 
|  | 7593 | *     ServerDHParams params; | 
|  | 7594 | * }; | 
|  | 7595 | */ | 
|  | 7596 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) | 
|  | 7597 | { | 
|  | 7598 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | 
|  | 7599 | goto exit; | 
|  | 7600 | } | 
|  | 7601 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) | 
|  | 7602 | { | 
|  | 7603 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); | 
|  | 7604 | goto exit; | 
|  | 7605 | } | 
|  | 7606 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 7607 | { | 
|  | 7608 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | 
|  | 7609 | goto exit; | 
|  | 7610 | } | 
|  | 7611 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) | 
|  | 7612 | { | 
|  | 7613 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | 
|  | 7614 | goto exit; | 
|  | 7615 | } | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7616 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7617 | { | 
|  | 7618 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); | 
|  | 7619 | goto exit; | 
|  | 7620 | } | 
|  | 7621 |  | 
|  | 7622 | exit: | 
|  | 7623 | mbedtls_md_free( &ctx ); | 
|  | 7624 |  | 
|  | 7625 | if( ret != 0 ) | 
|  | 7626 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7627 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 7628 |  | 
|  | 7629 | return( ret ); | 
|  | 7630 | } | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7631 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 7632 |  | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7633 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 7634 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 7635 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7636 | #endif /* MBEDTLS_SSL_TLS_C */ |