Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 shared functions |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
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 | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | /* |
| 22 | * The SSL 3.0 specification was drafted by Netscape in 1996, |
| 23 | * and became an IETF standard in 1999. |
| 24 | * |
| 25 | * http://wp.netscape.com/eng/ssl3/ |
| 26 | * http://www.ietf.org/rfc/rfc2246.txt |
| 27 | * http://www.ietf.org/rfc/rfc4346.txt |
| 28 | */ |
| 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #if defined(MBEDTLS_PLATFORM_C) |
| 39 | #include "mbedtls/platform.h" |
| 40 | #else |
| 41 | #include <stdlib.h> |
| 42 | #define mbedtls_calloc calloc |
| 43 | #define mbedtls_free free |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 44 | #endif |
| 45 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 47 | #include "mbedtls/ssl_internal.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 48 | #include "mbedtls/debug.h" |
| 49 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 50 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 51 | #include "mbedtls/version.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 52 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 53 | #include <string.h> |
| 54 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 55 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 56 | #include "mbedtls/psa_util.h" |
| 57 | #include "psa/crypto.h" |
| 58 | #endif |
| 59 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 60 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 61 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 62 | #endif |
| 63 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 64 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 65 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 66 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 67 | /* Top-level Connection ID API */ |
| 68 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 69 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 70 | size_t len, |
| 71 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 72 | { |
| 73 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 74 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 75 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 76 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 77 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 78 | { |
| 79 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 80 | } |
| 81 | |
| 82 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 83 | conf->cid_len = len; |
| 84 | return( 0 ); |
| 85 | } |
| 86 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 87 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 88 | int enable, |
| 89 | unsigned char const *own_cid, |
| 90 | size_t own_cid_len ) |
| 91 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 92 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 93 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 94 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 95 | ssl->negotiate_cid = enable; |
| 96 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 97 | { |
| 98 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 99 | return( 0 ); |
| 100 | } |
| 101 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 102 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 103 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 104 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 105 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 106 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 107 | (unsigned) own_cid_len, |
| 108 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 109 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 110 | } |
| 111 | |
| 112 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 113 | /* Truncation is not an issue here because |
| 114 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 115 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 116 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 117 | return( 0 ); |
| 118 | } |
| 119 | |
| 120 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 121 | int *enabled, |
| 122 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 123 | size_t *peer_cid_len ) |
| 124 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 125 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 126 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 127 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 128 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 129 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 130 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 131 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 132 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 133 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 134 | * were used, but client and server requested the empty CID. |
| 135 | * This is indistinguishable from not using the CID extension |
| 136 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 137 | if( ssl->transform_in->in_cid_len == 0 && |
| 138 | ssl->transform_in->out_cid_len == 0 ) |
| 139 | { |
| 140 | return( 0 ); |
| 141 | } |
| 142 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 143 | if( peer_cid_len != NULL ) |
| 144 | { |
| 145 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 146 | if( peer_cid != NULL ) |
| 147 | { |
| 148 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 149 | ssl->transform_in->out_cid_len ); |
| 150 | } |
| 151 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 152 | |
| 153 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 154 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 155 | return( 0 ); |
| 156 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 157 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 160 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 162 | /* |
| 163 | * Convert max_fragment_length codes to length. |
| 164 | * RFC 6066 says: |
| 165 | * enum{ |
| 166 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 167 | * } MaxFragmentLength; |
| 168 | * and we add 0 -> extension unused |
| 169 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 170 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 171 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 172 | switch( mfl ) |
| 173 | { |
| 174 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 175 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 176 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 177 | return 512; |
| 178 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 179 | return 1024; |
| 180 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 181 | return 2048; |
| 182 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 183 | return 4096; |
| 184 | default: |
| 185 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 186 | } |
| 187 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 189 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 190 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 191 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 192 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | mbedtls_ssl_session_free( dst ); |
| 194 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 197 | |
| 198 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 199 | if( src->peer_cert != NULL ) |
| 200 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 201 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 202 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 203 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 204 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 205 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 206 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 208 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | 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] | 210 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 211 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 213 | dst->peer_cert = NULL; |
| 214 | return( ret ); |
| 215 | } |
| 216 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 217 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 218 | if( src->peer_cert_digest != NULL ) |
| 219 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 220 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 221 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 222 | if( dst->peer_cert_digest == NULL ) |
| 223 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 224 | |
| 225 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 226 | src->peer_cert_digest_len ); |
| 227 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 228 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 229 | } |
| 230 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 231 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 233 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 234 | #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] | 235 | if( src->ticket != NULL ) |
| 236 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 237 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 238 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 239 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 240 | |
| 241 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 242 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 243 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 244 | |
| 245 | return( 0 ); |
| 246 | } |
| 247 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 248 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 249 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
| 250 | { |
| 251 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); |
| 252 | if( resized_buffer == NULL ) |
| 253 | return -1; |
| 254 | |
| 255 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 256 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 257 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 258 | * lost, are done outside of this function. */ |
| 259 | memcpy( resized_buffer, *buffer, |
| 260 | ( len_new < *len_old ) ? len_new : *len_old ); |
| 261 | mbedtls_platform_zeroize( *buffer, *len_old ); |
| 262 | mbedtls_free( *buffer ); |
| 263 | |
| 264 | *buffer = resized_buffer; |
| 265 | *len_old = len_new; |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 270 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 271 | /* |
| 272 | * Key material generation |
| 273 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 275 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 276 | const char *label, |
| 277 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 278 | unsigned char *dstbuf, size_t dlen ) |
| 279 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 280 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 281 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 282 | mbedtls_md5_context md5; |
| 283 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 284 | unsigned char padding[16]; |
| 285 | unsigned char sha1sum[20]; |
| 286 | ((void)label); |
| 287 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 288 | mbedtls_md5_init( &md5 ); |
| 289 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 290 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 291 | /* |
| 292 | * SSLv3: |
| 293 | * block = |
| 294 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 295 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 296 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 297 | * ... |
| 298 | */ |
| 299 | for( i = 0; i < dlen / 16; i++ ) |
| 300 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 301 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 302 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 303 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 304 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 305 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 306 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 307 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 308 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 309 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 310 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 311 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 312 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 313 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 314 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 315 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 316 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 317 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 318 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 319 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 320 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 321 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 324 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 325 | mbedtls_md5_free( &md5 ); |
| 326 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 327 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 328 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 329 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 330 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 331 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 332 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 333 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 336 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 337 | const char *label, |
| 338 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 339 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 340 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 341 | size_t nb, hs; |
| 342 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 343 | const unsigned char *S1, *S2; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 344 | unsigned char *tmp; |
| 345 | size_t tmp_len = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 346 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | const mbedtls_md_info_t *md_info; |
| 348 | mbedtls_md_context_t md_ctx; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 349 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 350 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 351 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 352 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 353 | tmp_len = 20 + strlen( label ) + rlen; |
| 354 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 355 | if( tmp == NULL ) |
| 356 | { |
| 357 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 358 | goto exit; |
| 359 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 360 | |
| 361 | hs = ( slen + 1 ) / 2; |
| 362 | S1 = secret; |
| 363 | S2 = secret + slen - hs; |
| 364 | |
| 365 | nb = strlen( label ); |
| 366 | memcpy( tmp + 20, label, nb ); |
| 367 | memcpy( tmp + 20 + nb, random, rlen ); |
| 368 | nb += rlen; |
| 369 | |
| 370 | /* |
| 371 | * First compute P_md5(secret,label+random)[0..dlen] |
| 372 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 373 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 374 | { |
| 375 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 376 | goto exit; |
| 377 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 378 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 379 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 380 | { |
| 381 | goto exit; |
| 382 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 383 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 384 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 385 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 386 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 387 | |
| 388 | for( i = 0; i < dlen; i += 16 ) |
| 389 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 390 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 391 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 392 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 393 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 395 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 396 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 397 | |
| 398 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 399 | |
| 400 | for( j = 0; j < k; j++ ) |
| 401 | dstbuf[i + j] = h_i[j]; |
| 402 | } |
| 403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 405 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 406 | /* |
| 407 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 408 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 410 | { |
| 411 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 412 | goto exit; |
| 413 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 414 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 416 | { |
| 417 | goto exit; |
| 418 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 419 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 420 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 421 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 422 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 423 | |
| 424 | for( i = 0; i < dlen; i += 20 ) |
| 425 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 427 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 428 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 429 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 430 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 431 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 432 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 433 | |
| 434 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 435 | |
| 436 | for( j = 0; j < k; j++ ) |
| 437 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 438 | } |
| 439 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 440 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 441 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 442 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 443 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 444 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 445 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 446 | mbedtls_free( tmp ); |
| 447 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 448 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 449 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 450 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 451 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 452 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 453 | |
| 454 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 455 | psa_key_handle_t slot, |
| 456 | psa_algorithm_t alg, |
| 457 | const unsigned char* seed, size_t seed_length, |
| 458 | const unsigned char* label, size_t label_length, |
| 459 | size_t capacity ) |
| 460 | { |
| 461 | psa_status_t status; |
| 462 | |
| 463 | status = psa_key_derivation_setup( derivation, alg ); |
| 464 | if( status != PSA_SUCCESS ) |
| 465 | return( status ); |
| 466 | |
| 467 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 468 | { |
| 469 | status = psa_key_derivation_input_bytes( derivation, |
| 470 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 471 | seed, seed_length ); |
| 472 | if( status != PSA_SUCCESS ) |
| 473 | return( status ); |
| 474 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 475 | if( slot == 0 ) |
| 476 | { |
| 477 | status = psa_key_derivation_input_bytes( |
| 478 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 479 | NULL, 0 ); |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | status = psa_key_derivation_input_key( |
| 484 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 485 | slot ); |
| 486 | } |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 487 | if( status != PSA_SUCCESS ) |
| 488 | return( status ); |
| 489 | |
| 490 | status = psa_key_derivation_input_bytes( derivation, |
| 491 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 492 | label, label_length ); |
| 493 | if( status != PSA_SUCCESS ) |
| 494 | return( status ); |
| 495 | } |
| 496 | else |
| 497 | { |
| 498 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 499 | } |
| 500 | |
| 501 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 502 | if( status != PSA_SUCCESS ) |
| 503 | return( status ); |
| 504 | |
| 505 | return( PSA_SUCCESS ); |
| 506 | } |
| 507 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 508 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 509 | const unsigned char *secret, size_t slen, |
| 510 | const char *label, |
| 511 | const unsigned char *random, size_t rlen, |
| 512 | unsigned char *dstbuf, size_t dlen ) |
| 513 | { |
| 514 | psa_status_t status; |
| 515 | psa_algorithm_t alg; |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 516 | psa_key_handle_t master_slot = 0; |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 517 | psa_key_derivation_operation_t derivation = |
Janos Follath | 8dee877 | 2019-07-30 12:53:32 +0100 | [diff] [blame] | 518 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 519 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 520 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 521 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 522 | else |
| 523 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 524 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 525 | /* Normally a "secret" should be long enough to be impossible to |
| 526 | * find by brute force, and in particular should not be empty. But |
| 527 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 528 | * and for this use case it makes sense to have a 0-length "secret". |
| 529 | * Since the key API doesn't allow importing a key of length 0, |
| 530 | * keep master_slot=0, which setup_psa_key_derivation() understands |
| 531 | * to mean a 0-length "secret" input. */ |
| 532 | if( slen != 0 ) |
| 533 | { |
| 534 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 535 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 536 | psa_set_key_algorithm( &key_attributes, alg ); |
| 537 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 538 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 539 | status = psa_import_key( &key_attributes, secret, slen, &master_slot ); |
| 540 | if( status != PSA_SUCCESS ) |
| 541 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 542 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 543 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 544 | status = setup_psa_key_derivation( &derivation, |
| 545 | master_slot, alg, |
| 546 | random, rlen, |
| 547 | (unsigned char const *) label, |
| 548 | (size_t) strlen( label ), |
| 549 | dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 550 | if( status != PSA_SUCCESS ) |
| 551 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 552 | psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 553 | psa_destroy_key( master_slot ); |
| 554 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 555 | } |
| 556 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 557 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 558 | if( status != PSA_SUCCESS ) |
| 559 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 560 | psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 561 | psa_destroy_key( master_slot ); |
| 562 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 563 | } |
| 564 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 565 | status = psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 566 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 567 | { |
| 568 | psa_destroy_key( master_slot ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 569 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 570 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 571 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 572 | if( master_slot != 0 ) |
| 573 | status = psa_destroy_key( master_slot ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 574 | if( status != PSA_SUCCESS ) |
| 575 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 576 | |
Andrzej Kurek | 3317126 | 2019-01-15 03:25:18 -0500 | [diff] [blame] | 577 | return( 0 ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 582 | 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] | 583 | const unsigned char *secret, size_t slen, |
| 584 | const char *label, |
| 585 | const unsigned char *random, size_t rlen, |
| 586 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 587 | { |
| 588 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 589 | size_t i, j, k, md_len; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 590 | unsigned char *tmp; |
| 591 | size_t tmp_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 593 | const mbedtls_md_info_t *md_info; |
| 594 | mbedtls_md_context_t md_ctx; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 595 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 597 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 600 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 603 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 604 | tmp_len = md_len + strlen( label ) + rlen; |
| 605 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 606 | if( tmp == NULL ) |
| 607 | { |
| 608 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 609 | goto exit; |
| 610 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 611 | |
| 612 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 613 | memcpy( tmp + md_len, label, nb ); |
| 614 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 615 | nb += rlen; |
| 616 | |
| 617 | /* |
| 618 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 619 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 620 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 621 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 624 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 625 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 626 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 627 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 628 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 629 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 630 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 631 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 633 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 634 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 635 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 636 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 637 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 638 | |
| 639 | for( j = 0; j < k; j++ ) |
| 640 | dstbuf[i + j] = h_i[j]; |
| 641 | } |
| 642 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 643 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 644 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 645 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 646 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 647 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 648 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 649 | mbedtls_free( tmp ); |
| 650 | |
| 651 | return( ret ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 652 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 653 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 655 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 656 | const char *label, |
| 657 | const unsigned char *random, size_t rlen, |
| 658 | unsigned char *dstbuf, size_t dlen ) |
| 659 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 660 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 661 | label, random, rlen, dstbuf, dlen ) ); |
| 662 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 666 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 667 | const char *label, |
| 668 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 669 | unsigned char *dstbuf, size_t dlen ) |
| 670 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 672 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 673 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | #endif /* MBEDTLS_SHA512_C */ |
| 675 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | 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] | 678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 679 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 680 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 681 | 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] | 682 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 683 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 684 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 685 | 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] | 686 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 687 | #endif |
| 688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 689 | #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] | 690 | 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] | 691 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 692 | #endif |
| 693 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 695 | #if defined(MBEDTLS_SHA256_C) |
| 696 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 697 | 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] | 698 | 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] | 699 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 700 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | #if defined(MBEDTLS_SHA512_C) |
| 702 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 703 | 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] | 704 | 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] | 705 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 706 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 707 | |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 708 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 709 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 710 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 711 | { |
| 712 | if( ssl->conf->f_psk != NULL ) |
| 713 | { |
| 714 | /* If we've used a callback to select the PSK, |
| 715 | * the static configuration is irrelevant. */ |
| 716 | if( ssl->handshake->psk_opaque != 0 ) |
| 717 | return( 1 ); |
| 718 | |
| 719 | return( 0 ); |
| 720 | } |
| 721 | |
| 722 | if( ssl->conf->psk_opaque != 0 ) |
| 723 | return( 1 ); |
| 724 | |
| 725 | return( 0 ); |
| 726 | } |
| 727 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 728 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 729 | |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 730 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 731 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 732 | { |
| 733 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 734 | if( tls_prf == ssl3_prf ) |
| 735 | { |
Ron Eldor | 0810f0b | 2019-05-15 12:32:32 +0300 | [diff] [blame] | 736 | return( MBEDTLS_SSL_TLS_PRF_SSL3 ); |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 737 | } |
| 738 | else |
| 739 | #endif |
| 740 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 741 | if( tls_prf == tls1_prf ) |
| 742 | { |
| 743 | return( MBEDTLS_SSL_TLS_PRF_TLS1 ); |
| 744 | } |
| 745 | else |
| 746 | #endif |
| 747 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 748 | #if defined(MBEDTLS_SHA512_C) |
| 749 | if( tls_prf == tls_prf_sha384 ) |
| 750 | { |
| 751 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 752 | } |
| 753 | else |
| 754 | #endif |
| 755 | #if defined(MBEDTLS_SHA256_C) |
| 756 | if( tls_prf == tls_prf_sha256 ) |
| 757 | { |
| 758 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 759 | } |
| 760 | else |
| 761 | #endif |
| 762 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 763 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 764 | } |
| 765 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 766 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 767 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 768 | const unsigned char *secret, size_t slen, |
| 769 | const char *label, |
| 770 | const unsigned char *random, size_t rlen, |
| 771 | unsigned char *dstbuf, size_t dlen ) |
| 772 | { |
| 773 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 774 | |
| 775 | switch( prf ) |
| 776 | { |
| 777 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 778 | case MBEDTLS_SSL_TLS_PRF_SSL3: |
| 779 | tls_prf = ssl3_prf; |
| 780 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 781 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 782 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 783 | case MBEDTLS_SSL_TLS_PRF_TLS1: |
| 784 | tls_prf = tls1_prf; |
| 785 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 786 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 787 | |
| 788 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 789 | #if defined(MBEDTLS_SHA512_C) |
| 790 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 791 | tls_prf = tls_prf_sha384; |
| 792 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 793 | #endif /* MBEDTLS_SHA512_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 794 | #if defined(MBEDTLS_SHA256_C) |
| 795 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 796 | tls_prf = tls_prf_sha256; |
| 797 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 798 | #endif /* MBEDTLS_SHA256_C */ |
| 799 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 800 | default: |
| 801 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 802 | } |
| 803 | |
| 804 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 805 | } |
| 806 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 807 | /* Type for the TLS PRF */ |
| 808 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 809 | const unsigned char *, size_t, |
| 810 | unsigned char *, size_t); |
| 811 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 812 | /* |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 813 | * Populate a transform structure with session keys and all the other |
| 814 | * necessary information. |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 815 | * |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 816 | * Parameters: |
| 817 | * - [in/out]: transform: structure to populate |
| 818 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 819 | * [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] | 820 | * - [in] ciphersuite |
| 821 | * - [in] master |
| 822 | * - [in] encrypt_then_mac |
| 823 | * - [in] trunc_hmac |
| 824 | * - [in] compression |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 825 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 826 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 827 | * - [in] minor_ver: SSL/TLS minor version |
| 828 | * - [in] endpoint: client or server |
| 829 | * - [in] ssl: optionally used for: |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 830 | * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 831 | * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys |
| 832 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 833 | */ |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 834 | static int ssl_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 835 | int ciphersuite, |
| 836 | const unsigned char master[48], |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 837 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 838 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 839 | int encrypt_then_mac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 840 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 841 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 842 | int trunc_hmac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 843 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 844 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 845 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 846 | int compression, |
| 847 | #endif |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 848 | ssl_tls_prf_t tls_prf, |
| 849 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 850 | int minor_ver, |
| 851 | unsigned endpoint, |
Manuel Pégourié-Gonnard | 7ae6ed4 | 2020-03-13 11:28:19 +0100 | [diff] [blame] | 852 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 853 | const |
| 854 | #endif |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 855 | mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 856 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 857 | int ret = 0; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 858 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 859 | int psa_fallthrough; |
| 860 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 861 | unsigned char keyblk[256]; |
| 862 | unsigned char *key1; |
| 863 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 864 | unsigned char *mac_enc; |
| 865 | unsigned char *mac_dec; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 866 | size_t mac_key_len; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 867 | size_t iv_copy_len; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 868 | unsigned keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 869 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 870 | const mbedtls_cipher_info_t *cipher_info; |
| 871 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 872 | |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 873 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \ |
| 874 | !defined(MBEDTLS_SSL_EXPORT_KEYS) && \ |
| 875 | !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 876 | 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] | 877 | (void) ssl; |
| 878 | #endif |
| 879 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 880 | /* |
| 881 | * Some data just needs copying into the structure |
| 882 | */ |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 883 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 884 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 885 | transform->encrypt_then_mac = encrypt_then_mac; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 886 | #endif |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 887 | transform->minor_ver = minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 888 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 889 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 890 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 891 | #endif |
| 892 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 893 | /* |
| 894 | * Get various info structures |
| 895 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 896 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 897 | if( ciphersuite_info == NULL ) |
| 898 | { |
| 899 | 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] | 900 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 901 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 902 | } |
| 903 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 904 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 905 | if( cipher_info == NULL ) |
| 906 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 907 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 908 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 909 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 910 | } |
| 911 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 912 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 913 | if( md_info == NULL ) |
| 914 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 915 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 916 | ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 917 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 918 | } |
| 919 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 920 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 921 | /* Copy own and peer's CID if the use of the CID |
| 922 | * extension has been negotiated. */ |
| 923 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 924 | { |
| 925 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 926 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 927 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 928 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 929 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 930 | transform->in_cid_len ); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 931 | |
| 932 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 933 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 934 | ssl->handshake->peer_cid_len ); |
| 935 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 936 | transform->out_cid_len ); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 937 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 938 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 939 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 940 | /* |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 941 | * Compute key block using the PRF |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 942 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 943 | 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] | 944 | if( ret != 0 ) |
| 945 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 946 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 947 | return( ret ); |
| 948 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 950 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 951 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 952 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 953 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 954 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 955 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 956 | /* |
| 957 | * Determine the appropriate key, IV and MAC length. |
| 958 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 959 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 960 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 961 | |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 962 | #if defined(MBEDTLS_GCM_C) || \ |
| 963 | defined(MBEDTLS_CCM_C) || \ |
| 964 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 965 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 966 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 967 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 968 | { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 969 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 970 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 971 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 972 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 973 | transform->taglen = |
| 974 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 975 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 976 | /* All modes haves 96-bit IVs; |
| 977 | * GCM and CCM has 4 implicit and 8 explicit bytes |
| 978 | * ChachaPoly has all 12 bytes implicit |
| 979 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 980 | transform->ivlen = 12; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 981 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 982 | transform->fixed_ivlen = 12; |
| 983 | else |
| 984 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 985 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 986 | /* Minimum length of encrypted record */ |
| 987 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 988 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 989 | } |
| 990 | else |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 991 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 992 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 993 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 994 | cipher_info->mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 995 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 996 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 997 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 998 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 999 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1000 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1001 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1002 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1003 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1004 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1005 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 1006 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1008 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1009 | /* |
| 1010 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 1011 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 1012 | * so we only need to adjust the length here. |
| 1013 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1014 | if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1015 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1017 | |
| 1018 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 1019 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 1020 | * HMAC implementation which also truncates the key |
| 1021 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1022 | mac_key_len = transform->maclen; |
| 1023 | #endif |
| 1024 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1025 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1026 | |
| 1027 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1028 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1029 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1030 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1031 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1032 | transform->minlen = transform->maclen; |
| 1033 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1034 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1035 | /* |
| 1036 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1037 | * 1. if EtM is in use: one block plus MAC |
| 1038 | * otherwise: * first multiple of blocklen greater than maclen |
| 1039 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1040 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1041 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1042 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1043 | { |
| 1044 | transform->minlen = transform->maclen |
| 1045 | + cipher_info->block_size; |
| 1046 | } |
| 1047 | else |
| 1048 | #endif |
| 1049 | { |
| 1050 | transform->minlen = transform->maclen |
| 1051 | + cipher_info->block_size |
| 1052 | - transform->maclen % cipher_info->block_size; |
| 1053 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1054 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1055 | #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] | 1056 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 1057 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1058 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1059 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1060 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1061 | #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] | 1062 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 1063 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1064 | { |
| 1065 | transform->minlen += transform->ivlen; |
| 1066 | } |
| 1067 | else |
| 1068 | #endif |
| 1069 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1070 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1071 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1072 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1073 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1074 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1075 | } |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1076 | else |
| 1077 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1078 | { |
| 1079 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1080 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1081 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1082 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1083 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 1084 | (unsigned) keylen, |
| 1085 | (unsigned) transform->minlen, |
| 1086 | (unsigned) transform->ivlen, |
| 1087 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1088 | |
| 1089 | /* |
| 1090 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 1091 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1093 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1094 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1095 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1096 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1097 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1098 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1099 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1100 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1101 | /* |
| 1102 | * This is not used in TLS v1.1. |
| 1103 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1104 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1105 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1106 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 1107 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1108 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1109 | } |
| 1110 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1111 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1112 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1113 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1114 | { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1115 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1116 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1117 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1118 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1119 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1120 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1121 | /* |
| 1122 | * This is not used in TLS v1.1. |
| 1123 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1124 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1125 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1126 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 1127 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1128 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1129 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1130 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1131 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1132 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1133 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1134 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1135 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1136 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1137 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1138 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1140 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1141 | { |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1142 | if( mac_key_len > sizeof( transform->mac_enc ) ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1143 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1144 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1145 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1146 | goto end; |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1147 | } |
| 1148 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1149 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1150 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1151 | } |
| 1152 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1153 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1154 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1155 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1156 | if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1157 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1158 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1159 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1160 | if( mac_key_len != 0 ) |
| 1161 | { |
| 1162 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1163 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1164 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1165 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1166 | else |
| 1167 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1168 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1169 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1170 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1171 | goto end; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1172 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1173 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1176 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1177 | { |
| 1178 | int ret = 0; |
| 1179 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1180 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1181 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1182 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1183 | transform->iv_enc, transform->iv_dec, |
| 1184 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1185 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1186 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1187 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1188 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1189 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1190 | goto end; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1191 | } |
| 1192 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1193 | #else |
| 1194 | ((void) mac_dec); |
| 1195 | ((void) mac_enc); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1196 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1197 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1198 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1199 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1200 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1201 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1202 | master, keyblk, |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1203 | mac_key_len, keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1204 | iv_copy_len ); |
| 1205 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1206 | |
| 1207 | if( ssl->conf->f_export_keys_ext != NULL ) |
| 1208 | { |
| 1209 | 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] | 1210 | master, keyblk, |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1211 | mac_key_len, keylen, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1212 | iv_copy_len, |
Jaeden Amero | fa63645 | 2019-09-12 10:47:37 +0100 | [diff] [blame] | 1213 | randbytes + 32, |
| 1214 | randbytes, |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1215 | tls_prf_get_type( tls_prf ) ); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1216 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1217 | #endif |
| 1218 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1219 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1220 | |
| 1221 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1222 | * That's relevant at least for TLS-1.0, where |
| 1223 | * we assume that mbedtls_cipher_crypt() updates |
| 1224 | * the structure field for the IV, which the PSA-based |
| 1225 | * implementation currently doesn't. */ |
| 1226 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1227 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1228 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1229 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1230 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1231 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1232 | { |
| 1233 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1234 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | if( ret == 0 ) |
| 1238 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1239 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1240 | psa_fallthrough = 0; |
| 1241 | } |
| 1242 | else |
| 1243 | { |
| 1244 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) ); |
| 1245 | psa_fallthrough = 1; |
| 1246 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1247 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1248 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1249 | psa_fallthrough = 1; |
| 1250 | #else |
| 1251 | psa_fallthrough = 1; |
| 1252 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1253 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1254 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1255 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1256 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1257 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1258 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1259 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1260 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1261 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1262 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1263 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1264 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1265 | * That's relevant at least for TLS-1.0, where |
| 1266 | * we assume that mbedtls_cipher_crypt() updates |
| 1267 | * the structure field for the IV, which the PSA-based |
| 1268 | * implementation currently doesn't. */ |
| 1269 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1270 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1271 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1272 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1273 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1274 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1275 | { |
| 1276 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1277 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | if( ret == 0 ) |
| 1281 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1282 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1283 | psa_fallthrough = 0; |
| 1284 | } |
| 1285 | else |
| 1286 | { |
| 1287 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) ); |
| 1288 | psa_fallthrough = 1; |
| 1289 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1290 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1291 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1292 | psa_fallthrough = 1; |
| 1293 | #else |
| 1294 | psa_fallthrough = 1; |
| 1295 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1296 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1297 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1298 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1299 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1300 | cipher_info ) ) != 0 ) |
| 1301 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1302 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1303 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1304 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1305 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1306 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1307 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1308 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1309 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1310 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1311 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1312 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1313 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1314 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1315 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1316 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1317 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1318 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1319 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1320 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1322 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1323 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1324 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1325 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1326 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1327 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1328 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1329 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1330 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1331 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1332 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1333 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1334 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1335 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1336 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1337 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1338 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1339 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1340 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1341 | |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1342 | /* Initialize Zlib contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1343 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1344 | if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1345 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1346 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1347 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1348 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1349 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1350 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1351 | if( deflateInit( &transform->ctx_deflate, |
| 1352 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1353 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1354 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1355 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1356 | ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED; |
| 1357 | goto end; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1358 | } |
| 1359 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1360 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1361 | |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1362 | end: |
Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 1363 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1364 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1367 | /* |
Manuel Pégourié-Gonnard | 47e33e1 | 2019-05-20 10:10:17 +0200 | [diff] [blame] | 1368 | * 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] | 1369 | * |
| 1370 | * Inputs: |
| 1371 | * - SSL/TLS minor version |
| 1372 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1373 | * |
Manuel Pégourié-Gonnard | 31d3ef1 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1374 | * Outputs: |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1375 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1376 | */ |
| 1377 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1378 | int minor_ver, |
| 1379 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1380 | { |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1381 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) |
| 1382 | (void) hash; |
| 1383 | #endif |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1384 | |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1385 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1386 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1387 | { |
| 1388 | handshake->tls_prf = ssl3_prf; |
| 1389 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 1390 | handshake->calc_finished = ssl_calc_finished_ssl; |
| 1391 | } |
| 1392 | else |
| 1393 | #endif |
| 1394 | #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] | 1395 | if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1396 | { |
| 1397 | handshake->tls_prf = tls1_prf; |
| 1398 | handshake->calc_verify = ssl_calc_verify_tls; |
| 1399 | handshake->calc_finished = ssl_calc_finished_tls; |
| 1400 | } |
| 1401 | else |
| 1402 | #endif |
| 1403 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1404 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1405 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1406 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1407 | { |
| 1408 | handshake->tls_prf = tls_prf_sha384; |
| 1409 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1410 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1411 | } |
| 1412 | else |
| 1413 | #endif |
| 1414 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1415 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1416 | { |
| 1417 | handshake->tls_prf = tls_prf_sha256; |
| 1418 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1419 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1420 | } |
| 1421 | else |
| 1422 | #endif |
| 1423 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1424 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1425 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1426 | } |
| 1427 | |
| 1428 | return( 0 ); |
| 1429 | } |
| 1430 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1431 | /* |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1432 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1433 | * |
| 1434 | * Parameters: |
| 1435 | * [in/out] handshake |
| 1436 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1437 | * (PSA-PSK) ciphersuite_info, psk_opaque |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1438 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1439 | * [out] master |
| 1440 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 1441 | * debug: conf->f_dbg, conf->p_dbg |
| 1442 | * EMS: passed to calc_verify (debug + (SSL3) session_negotiate) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1443 | * PSA-PSA: minor_ver, conf |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1444 | */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1445 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1446 | unsigned char *master, |
Manuel Pégourié-Gonnard | 0d56aaa | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1447 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1448 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1449 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1450 | |
| 1451 | /* cf. RFC 5246, Section 8.1: |
| 1452 | * "The master secret is always exactly 48 bytes in length." */ |
| 1453 | size_t const master_secret_len = 48; |
| 1454 | |
| 1455 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1456 | unsigned char session_hash[48]; |
| 1457 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 1458 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1459 | /* The label for the KDF used for key expansion. |
| 1460 | * This is either "master secret" or "extended master secret" |
| 1461 | * depending on whether the Extended Master Secret extension |
| 1462 | * is used. */ |
| 1463 | char const *lbl = "master secret"; |
| 1464 | |
| 1465 | /* The salt for the KDF used for key expansion. |
| 1466 | * - If the Extended Master Secret extension is not used, |
| 1467 | * this is ClientHello.Random + ServerHello.Random |
| 1468 | * (see Sect. 8.1 in RFC 5246). |
| 1469 | * - If the Extended Master Secret extension is used, |
| 1470 | * this is the transcript of the handshake so far. |
| 1471 | * (see Sect. 4 in RFC 7627). */ |
| 1472 | unsigned char const *salt = handshake->randbytes; |
| 1473 | size_t salt_len = 64; |
| 1474 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1475 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 1476 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 1477 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1478 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1479 | 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] | 1480 | (void) ssl; |
| 1481 | #endif |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1482 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1483 | if( handshake->resume != 0 ) |
| 1484 | { |
| 1485 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1486 | return( 0 ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1487 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1488 | |
| 1489 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1490 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1491 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1492 | lbl = "extended master secret"; |
| 1493 | salt = session_hash; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1494 | handshake->calc_verify( ssl, session_hash, &salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1495 | |
Manuel Pégourié-Gonnard | 8faa70e | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1496 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1497 | session_hash, salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1498 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1499 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 1500 | |
| 1501 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1502 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1503 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1504 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1505 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 1506 | { |
| 1507 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1508 | psa_status_t status; |
| 1509 | psa_algorithm_t alg; |
| 1510 | psa_key_handle_t psk; |
| 1511 | psa_key_derivation_operation_t derivation = |
| 1512 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1513 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1514 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1515 | 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] | 1516 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1517 | psk = ssl->conf->psk_opaque; |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1518 | if( handshake->psk_opaque != 0 ) |
| 1519 | psk = handshake->psk_opaque; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1520 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1521 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1522 | 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] | 1523 | else |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1524 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1525 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 1526 | status = setup_psa_key_derivation( &derivation, psk, alg, |
| 1527 | salt, salt_len, |
| 1528 | (unsigned char const *) lbl, |
| 1529 | (size_t) strlen( lbl ), |
| 1530 | master_secret_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1531 | if( status != PSA_SUCCESS ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1532 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1533 | psa_key_derivation_abort( &derivation ); |
| 1534 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1535 | } |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1536 | |
| 1537 | status = psa_key_derivation_output_bytes( &derivation, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1538 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1539 | master_secret_len ); |
| 1540 | if( status != PSA_SUCCESS ) |
| 1541 | { |
| 1542 | psa_key_derivation_abort( &derivation ); |
| 1543 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1544 | } |
| 1545 | |
| 1546 | status = psa_key_derivation_abort( &derivation ); |
| 1547 | if( status != PSA_SUCCESS ) |
| 1548 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1549 | } |
| 1550 | else |
| 1551 | #endif |
| 1552 | { |
| 1553 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1554 | lbl, salt, salt_len, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1555 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1556 | master_secret_len ); |
| 1557 | if( ret != 0 ) |
| 1558 | { |
| 1559 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1560 | return( ret ); |
| 1561 | } |
| 1562 | |
| 1563 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 1564 | handshake->premaster, |
| 1565 | handshake->pmslen ); |
| 1566 | |
| 1567 | mbedtls_platform_zeroize( handshake->premaster, |
| 1568 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | return( 0 ); |
| 1572 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1573 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1574 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1575 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1576 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1577 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1578 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1579 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1580 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1581 | |
| 1582 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1583 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1584 | ssl->minor_ver, |
| 1585 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1586 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1587 | { |
| 1588 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1589 | return( ret ); |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1590 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1591 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1592 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1593 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1594 | ssl->session_negotiate->master, |
| 1595 | ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1596 | if( ret != 0 ) |
| 1597 | { |
| 1598 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1599 | return( ret ); |
| 1600 | } |
| 1601 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1602 | /* Swap the client and server random values: |
| 1603 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1604 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1605 | { |
| 1606 | unsigned char tmp[64]; |
| 1607 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1608 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1609 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1610 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1611 | } |
| 1612 | |
| 1613 | /* Populate transform structure */ |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1614 | ret = ssl_populate_transform( ssl->transform_negotiate, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1615 | ssl->session_negotiate->ciphersuite, |
| 1616 | ssl->session_negotiate->master, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1617 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1618 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1619 | ssl->session_negotiate->encrypt_then_mac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1620 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1621 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1622 | ssl->session_negotiate->trunc_hmac, |
Jarno Lamsa | c84bd24 | 2019-08-16 12:06:56 +0300 | [diff] [blame] | 1623 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 1624 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1625 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1626 | ssl->session_negotiate->compression, |
| 1627 | #endif |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1628 | ssl->handshake->tls_prf, |
| 1629 | ssl->handshake->randbytes, |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1630 | ssl->minor_ver, |
| 1631 | ssl->conf->endpoint, |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1632 | ssl ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1633 | if( ret != 0 ) |
| 1634 | { |
| 1635 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); |
| 1636 | return( ret ); |
| 1637 | } |
| 1638 | |
| 1639 | /* We no longer need Server/ClientHello.random values */ |
| 1640 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1641 | sizeof( ssl->handshake->randbytes ) ); |
| 1642 | |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1643 | /* Allocate compression buffer */ |
| 1644 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
jiblime | 9f25b8d | 2019-12-18 21:40:01 -0800 | [diff] [blame] | 1645 | if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE && |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1646 | ssl->compress_buf == NULL ) |
| 1647 | { |
| 1648 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
| 1649 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
| 1650 | if( ssl->compress_buf == NULL ) |
| 1651 | { |
| 1652 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1653 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Manuel Pégourié-Gonnard | d73b47f | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1654 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1655 | } |
| 1656 | } |
| 1657 | #endif |
| 1658 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1659 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1660 | |
| 1661 | return( 0 ); |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1662 | } |
| 1663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1665 | void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, |
| 1666 | unsigned char hash[36], |
| 1667 | size_t *hlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1668 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1669 | mbedtls_md5_context md5; |
| 1670 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1671 | unsigned char pad_1[48]; |
| 1672 | unsigned char pad_2[48]; |
| 1673 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1674 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1675 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1676 | mbedtls_md5_init( &md5 ); |
| 1677 | mbedtls_sha1_init( &sha1 ); |
| 1678 | |
| 1679 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1680 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1681 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1682 | memset( pad_1, 0x36, 48 ); |
| 1683 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1684 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1685 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1686 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1687 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1688 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1689 | mbedtls_md5_starts_ret( &md5 ); |
| 1690 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1691 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1692 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1693 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1694 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1695 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1696 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1697 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1698 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1699 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1700 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1701 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1702 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1703 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1704 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1705 | *hlen = 36; |
| 1706 | |
| 1707 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1708 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1709 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1710 | mbedtls_md5_free( &md5 ); |
| 1711 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1712 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1713 | return; |
| 1714 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1715 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1717 | #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] | 1718 | void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, |
| 1719 | unsigned char hash[36], |
| 1720 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1721 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1722 | mbedtls_md5_context md5; |
| 1723 | mbedtls_sha1_context sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1725 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1726 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1727 | mbedtls_md5_init( &md5 ); |
| 1728 | mbedtls_sha1_init( &sha1 ); |
| 1729 | |
| 1730 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1731 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1732 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1733 | mbedtls_md5_finish_ret( &md5, hash ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1734 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1735 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1736 | *hlen = 36; |
| 1737 | |
| 1738 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1739 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1740 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1741 | mbedtls_md5_free( &md5 ); |
| 1742 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1743 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1744 | return; |
| 1745 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1746 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1748 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1749 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1750 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 1751 | unsigned char hash[32], |
| 1752 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1753 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1754 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1755 | size_t hash_size; |
| 1756 | psa_status_t status; |
| 1757 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1758 | |
| 1759 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 1760 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 1761 | if( status != PSA_SUCCESS ) |
| 1762 | { |
| 1763 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1764 | return; |
| 1765 | } |
| 1766 | |
| 1767 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 1768 | if( status != PSA_SUCCESS ) |
| 1769 | { |
| 1770 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1771 | return; |
| 1772 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1773 | |
| 1774 | *hlen = 32; |
| 1775 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1776 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1777 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1778 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1779 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1780 | mbedtls_sha256_init( &sha256 ); |
| 1781 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1782 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1783 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1784 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1785 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1786 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1787 | *hlen = 32; |
| 1788 | |
| 1789 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1790 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1791 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1792 | mbedtls_sha256_free( &sha256 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1793 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1794 | return; |
| 1795 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1796 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1797 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1798 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1799 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 1800 | unsigned char hash[48], |
| 1801 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1802 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1803 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1804 | size_t hash_size; |
| 1805 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1806 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1807 | |
| 1808 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1809 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1810 | if( status != PSA_SUCCESS ) |
| 1811 | { |
| 1812 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1813 | return; |
| 1814 | } |
| 1815 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1816 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1817 | if( status != PSA_SUCCESS ) |
| 1818 | { |
| 1819 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1820 | return; |
| 1821 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1822 | |
| 1823 | *hlen = 48; |
| 1824 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1825 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1826 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1827 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1828 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1829 | mbedtls_sha512_init( &sha512 ); |
| 1830 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1831 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1832 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1833 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1834 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1835 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1836 | *hlen = 48; |
| 1837 | |
| 1838 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1839 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1840 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1841 | mbedtls_sha512_free( &sha512 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1842 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1843 | return; |
| 1844 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1845 | #endif /* MBEDTLS_SHA512_C */ |
| 1846 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1847 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1848 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1849 | 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] | 1850 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1851 | unsigned char *p = ssl->handshake->premaster; |
| 1852 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1853 | const unsigned char *psk = ssl->conf->psk; |
| 1854 | size_t psk_len = ssl->conf->psk_len; |
| 1855 | |
| 1856 | /* If the psk callback was called, use its result */ |
| 1857 | if( ssl->handshake->psk != NULL ) |
| 1858 | { |
| 1859 | psk = ssl->handshake->psk; |
| 1860 | psk_len = ssl->handshake->psk_len; |
| 1861 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1862 | |
| 1863 | /* |
| 1864 | * PMS = struct { |
| 1865 | * opaque other_secret<0..2^16-1>; |
| 1866 | * opaque psk<0..2^16-1>; |
| 1867 | * }; |
| 1868 | * with "other_secret" depending on the particular key exchange |
| 1869 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1870 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1871 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1872 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1873 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1874 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1875 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1876 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1877 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1878 | |
| 1879 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1880 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1881 | |
| 1882 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1883 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1884 | } |
| 1885 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1886 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1887 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1888 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1889 | { |
| 1890 | /* |
| 1891 | * other_secret already set by the ClientKeyExchange message, |
| 1892 | * and is 48 bytes long |
| 1893 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1894 | if( end - p < 2 ) |
| 1895 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1896 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1897 | *p++ = 0; |
| 1898 | *p++ = 48; |
| 1899 | p += 48; |
| 1900 | } |
| 1901 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1902 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1903 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1904 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1905 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1906 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1907 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1908 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1909 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1910 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1911 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1912 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1913 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1914 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1915 | return( ret ); |
| 1916 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1917 | *(p++) = (unsigned char)( len >> 8 ); |
| 1918 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1919 | p += len; |
| 1920 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1921 | 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] | 1922 | } |
| 1923 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1924 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1925 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1926 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1927 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1928 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1929 | size_t zlen; |
| 1930 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1931 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1932 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1933 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1934 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1935 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1936 | return( ret ); |
| 1937 | } |
| 1938 | |
| 1939 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 1940 | *(p++) = (unsigned char)( zlen ); |
| 1941 | p += zlen; |
| 1942 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1943 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1944 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1945 | } |
| 1946 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1947 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1948 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1949 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1950 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1954 | if( end - p < 2 ) |
| 1955 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1956 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1957 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1958 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1959 | |
| 1960 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1961 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1962 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1963 | memcpy( p, psk, psk_len ); |
| 1964 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1965 | |
| 1966 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1967 | |
| 1968 | return( 0 ); |
| 1969 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1970 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1972 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1973 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1974 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1975 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 1976 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1977 | { |
| 1978 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 1979 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1980 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1981 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1982 | 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] | 1983 | unsigned char doublings = 1; |
| 1984 | |
| 1985 | while( ratio != 0 ) |
| 1986 | { |
| 1987 | ++doublings; |
| 1988 | ratio >>= 1; |
| 1989 | } |
| 1990 | |
| 1991 | if( ++ssl->renego_records_seen > doublings ) |
| 1992 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 1993 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1994 | return( 0 ); |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | return( ssl_write_hello_request( ssl ) ); |
| 1999 | } |
| 2000 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2001 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2002 | |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 2003 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2004 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 2005 | { |
| 2006 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2007 | if( session->peer_cert != NULL ) |
| 2008 | { |
| 2009 | mbedtls_x509_crt_free( session->peer_cert ); |
| 2010 | mbedtls_free( session->peer_cert ); |
| 2011 | session->peer_cert = NULL; |
| 2012 | } |
| 2013 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2014 | if( session->peer_cert_digest != NULL ) |
| 2015 | { |
| 2016 | /* Zeroization is not necessary. */ |
| 2017 | mbedtls_free( session->peer_cert_digest ); |
| 2018 | session->peer_cert_digest = NULL; |
| 2019 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 2020 | session->peer_cert_digest_len = 0; |
| 2021 | } |
| 2022 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2023 | } |
| 2024 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 2025 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2026 | /* |
| 2027 | * Handshake functions |
| 2028 | */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2029 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 2030 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2031 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2032 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2033 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 2034 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2035 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2036 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2037 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 2038 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2039 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2040 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2041 | ssl->state++; |
| 2042 | return( 0 ); |
| 2043 | } |
| 2044 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2045 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2046 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2047 | } |
| 2048 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2049 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2050 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2051 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 2052 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2053 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2054 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2055 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 2056 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2057 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2058 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2059 | ssl->state++; |
| 2060 | return( 0 ); |
| 2061 | } |
| 2062 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2063 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2064 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2065 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 2066 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2067 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 2068 | /* Some certificate support -> implement write and parse */ |
| 2069 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2070 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2071 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2072 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2073 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2074 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2075 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 2076 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2077 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2078 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2079 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 2080 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2081 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2082 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2083 | ssl->state++; |
| 2084 | return( 0 ); |
| 2085 | } |
| 2086 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2087 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2088 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2089 | { |
| 2090 | if( ssl->client_auth == 0 ) |
| 2091 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2092 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2093 | ssl->state++; |
| 2094 | return( 0 ); |
| 2095 | } |
| 2096 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2097 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2098 | /* |
| 2099 | * If using SSLv3 and got no cert, send an Alert message |
| 2100 | * (otherwise an empty Certificate message will be sent). |
| 2101 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2102 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
| 2103 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2104 | { |
| 2105 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2106 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 2107 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 2108 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2109 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2110 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2111 | goto write_msg; |
| 2112 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2113 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2114 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2115 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2116 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2117 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2118 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2119 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2120 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2121 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 2122 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2123 | } |
| 2124 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2125 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2127 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2128 | |
| 2129 | /* |
| 2130 | * 0 . 0 handshake type |
| 2131 | * 1 . 3 handshake length |
| 2132 | * 4 . 6 length of all certs |
| 2133 | * 7 . 9 length of cert. 1 |
| 2134 | * 10 . n-1 peer certificate |
| 2135 | * n . n+2 length of cert. 2 |
| 2136 | * n+3 . ... upper level cert, etc. |
| 2137 | */ |
| 2138 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2139 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2140 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 2141 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2142 | { |
| 2143 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2144 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2145 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2146 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2147 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2148 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 2152 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 2153 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 2154 | |
| 2155 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 2156 | i += n; crt = crt->next; |
| 2157 | } |
| 2158 | |
| 2159 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 2160 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 2161 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 2162 | |
| 2163 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2164 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2165 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2166 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2167 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2168 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2169 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2170 | |
| 2171 | ssl->state++; |
| 2172 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2173 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2174 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2175 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2176 | return( ret ); |
| 2177 | } |
| 2178 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2179 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2180 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 2181 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 2184 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 2185 | |
| 2186 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2187 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 2188 | unsigned char *crt_buf, |
| 2189 | size_t crt_buf_len ) |
| 2190 | { |
| 2191 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 2192 | |
| 2193 | if( peer_crt == NULL ) |
| 2194 | return( -1 ); |
| 2195 | |
| 2196 | if( peer_crt->raw.len != crt_buf_len ) |
| 2197 | return( -1 ); |
| 2198 | |
k-stachowiak | 95b68ef | 2019-09-16 12:21:00 +0200 | [diff] [blame] | 2199 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2200 | } |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 2201 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2202 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 2203 | unsigned char *crt_buf, |
| 2204 | size_t crt_buf_len ) |
| 2205 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2206 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 2207 | unsigned char const * const peer_cert_digest = |
| 2208 | ssl->session->peer_cert_digest; |
| 2209 | mbedtls_md_type_t const peer_cert_digest_type = |
| 2210 | ssl->session->peer_cert_digest_type; |
| 2211 | mbedtls_md_info_t const * const digest_info = |
| 2212 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 2213 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 2214 | size_t digest_len; |
| 2215 | |
| 2216 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 2217 | return( -1 ); |
| 2218 | |
| 2219 | digest_len = mbedtls_md_get_size( digest_info ); |
| 2220 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 2221 | return( -1 ); |
| 2222 | |
| 2223 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 2224 | if( ret != 0 ) |
| 2225 | return( -1 ); |
| 2226 | |
| 2227 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 2228 | } |
| 2229 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 2230 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2231 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2232 | /* |
| 2233 | * Once the certificate message is read, parse it into a cert chain and |
| 2234 | * perform basic checks, but leave actual verification to the caller |
| 2235 | */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2236 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 2237 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2238 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2239 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2240 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 2241 | int crt_cnt=0; |
| 2242 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2243 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 2244 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2245 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2246 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2247 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2248 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2249 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2250 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2251 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2254 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 2255 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2256 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2257 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2258 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2259 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2260 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2263 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 2264 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2265 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2266 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2267 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 2268 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2269 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 2270 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2271 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2272 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2273 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2274 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2275 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2276 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2279 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 2280 | i += 3; |
| 2281 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2282 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2283 | while( i < ssl->in_hslen ) |
| 2284 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2285 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 2286 | if ( i + 3 > ssl->in_hslen ) { |
| 2287 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 2288 | mbedtls_ssl_send_alert_message( ssl, |
| 2289 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2290 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 2291 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 2292 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2293 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 2294 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2295 | if( ssl->in_msg[i] != 0 ) |
| 2296 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2297 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 2298 | mbedtls_ssl_send_alert_message( ssl, |
| 2299 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2300 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2301 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2304 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2305 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 2306 | | (unsigned int) ssl->in_msg[i + 2]; |
| 2307 | i += 3; |
| 2308 | |
| 2309 | if( n < 128 || i + n > ssl->in_hslen ) |
| 2310 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2311 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 2312 | mbedtls_ssl_send_alert_message( ssl, |
| 2313 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2314 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2315 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2318 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2319 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 2320 | if( crt_cnt++ == 0 && |
| 2321 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2322 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2323 | { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 2324 | /* During client-side renegotiation, check that the server's |
| 2325 | * end-CRTs hasn't changed compared to the initial handshake, |
| 2326 | * mitigating the triple handshake attack. On success, reuse |
| 2327 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2328 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 2329 | if( ssl_check_peer_crt_unchanged( ssl, |
| 2330 | &ssl->in_msg[i], |
| 2331 | n ) != 0 ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2332 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2333 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 2334 | mbedtls_ssl_send_alert_message( ssl, |
| 2335 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2336 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 2337 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2338 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2339 | |
| 2340 | /* Now we can safely free the original chain. */ |
| 2341 | ssl_clear_peer_cert( ssl->session ); |
| 2342 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2343 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 2344 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2345 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2346 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2347 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2348 | #else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 2349 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2350 | * it in-place from the input buffer instead of making a copy. */ |
| 2351 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 2352 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2353 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2354 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2355 | case 0: /*ok*/ |
| 2356 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 2357 | /* Ignore certificate with an unknown algorithm: maybe a |
| 2358 | prior certificate was already trusted. */ |
| 2359 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2360 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2361 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 2362 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 2363 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2364 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2365 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 2366 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2367 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2368 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2369 | default: |
| 2370 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 2371 | crt_parse_der_failed: |
| 2372 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 2373 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 2374 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | i += n; |
| 2378 | } |
| 2379 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2380 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2381 | return( 0 ); |
| 2382 | } |
| 2383 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2384 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2385 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 2386 | { |
| 2387 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2388 | return( -1 ); |
| 2389 | |
| 2390 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2391 | /* |
| 2392 | * Check if the client sent an empty certificate |
| 2393 | */ |
| 2394 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 2395 | { |
| 2396 | if( ssl->in_msglen == 2 && |
| 2397 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 2398 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 2399 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 2400 | { |
| 2401 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
| 2402 | return( 0 ); |
| 2403 | } |
| 2404 | |
| 2405 | return( -1 ); |
| 2406 | } |
| 2407 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2408 | |
| 2409 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2410 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2411 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 2412 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2413 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 2414 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 2415 | { |
| 2416 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 2417 | return( 0 ); |
| 2418 | } |
| 2419 | |
| 2420 | return( -1 ); |
| 2421 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2422 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2423 | } |
| 2424 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2425 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2426 | /* Check if a certificate message is expected. |
| 2427 | * Return either |
| 2428 | * - SSL_CERTIFICATE_EXPECTED, or |
| 2429 | * - SSL_CERTIFICATE_SKIP |
| 2430 | * indicating whether a Certificate message is expected or not. |
| 2431 | */ |
| 2432 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 2433 | #define SSL_CERTIFICATE_SKIP 1 |
| 2434 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 2435 | int authmode ) |
| 2436 | { |
| 2437 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2438 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2439 | |
| 2440 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 2441 | return( SSL_CERTIFICATE_SKIP ); |
| 2442 | |
| 2443 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2444 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2445 | { |
| 2446 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 2447 | return( SSL_CERTIFICATE_SKIP ); |
| 2448 | |
| 2449 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 2450 | { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2451 | ssl->session_negotiate->verify_result = |
| 2452 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 2453 | return( SSL_CERTIFICATE_SKIP ); |
| 2454 | } |
| 2455 | } |
Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 2456 | #else |
| 2457 | ((void) authmode); |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2458 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2459 | |
| 2460 | return( SSL_CERTIFICATE_EXPECTED ); |
| 2461 | } |
| 2462 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2463 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 2464 | int authmode, |
| 2465 | mbedtls_x509_crt *chain, |
| 2466 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2467 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2468 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2469 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2470 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2471 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2472 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2473 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 2474 | void *p_vrfy; |
| 2475 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2476 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 2477 | return( 0 ); |
| 2478 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2479 | if( ssl->f_vrfy != NULL ) |
| 2480 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2481 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2482 | f_vrfy = ssl->f_vrfy; |
| 2483 | p_vrfy = ssl->p_vrfy; |
| 2484 | } |
| 2485 | else |
| 2486 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2487 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2488 | f_vrfy = ssl->conf->f_vrfy; |
| 2489 | p_vrfy = ssl->conf->p_vrfy; |
| 2490 | } |
| 2491 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2492 | /* |
| 2493 | * Main check: verify certificate |
| 2494 | */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2495 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 2496 | if( ssl->conf->f_ca_cb != NULL ) |
| 2497 | { |
| 2498 | ((void) rs_ctx); |
| 2499 | have_ca_chain = 1; |
| 2500 | |
| 2501 | 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] | 2502 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2503 | chain, |
| 2504 | ssl->conf->f_ca_cb, |
| 2505 | ssl->conf->p_ca_cb, |
| 2506 | ssl->conf->cert_profile, |
| 2507 | ssl->hostname, |
| 2508 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2509 | f_vrfy, p_vrfy ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2510 | } |
| 2511 | else |
| 2512 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 2513 | { |
| 2514 | mbedtls_x509_crt *ca_chain; |
| 2515 | mbedtls_x509_crl *ca_crl; |
| 2516 | |
| 2517 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2518 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 2519 | { |
| 2520 | ca_chain = ssl->handshake->sni_ca_chain; |
| 2521 | ca_crl = ssl->handshake->sni_ca_crl; |
| 2522 | } |
| 2523 | else |
| 2524 | #endif |
| 2525 | { |
| 2526 | ca_chain = ssl->conf->ca_chain; |
| 2527 | ca_crl = ssl->conf->ca_crl; |
| 2528 | } |
| 2529 | |
| 2530 | if( ca_chain != NULL ) |
| 2531 | have_ca_chain = 1; |
| 2532 | |
| 2533 | ret = mbedtls_x509_crt_verify_restartable( |
| 2534 | chain, |
| 2535 | ca_chain, ca_crl, |
| 2536 | ssl->conf->cert_profile, |
| 2537 | ssl->hostname, |
| 2538 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2539 | f_vrfy, p_vrfy, rs_ctx ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2540 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2541 | |
| 2542 | if( ret != 0 ) |
| 2543 | { |
| 2544 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 2545 | } |
| 2546 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2547 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2548 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2549 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 2550 | #endif |
| 2551 | |
| 2552 | /* |
| 2553 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 2554 | */ |
| 2555 | |
| 2556 | #if defined(MBEDTLS_ECP_C) |
| 2557 | { |
| 2558 | const mbedtls_pk_context *pk = &chain->pk; |
| 2559 | |
| 2560 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 2561 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 2562 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 2563 | { |
| 2564 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 2565 | |
| 2566 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 2567 | if( ret == 0 ) |
| 2568 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 2569 | } |
| 2570 | } |
| 2571 | #endif /* MBEDTLS_ECP_C */ |
| 2572 | |
| 2573 | if( mbedtls_ssl_check_cert_usage( chain, |
| 2574 | ciphersuite_info, |
| 2575 | ! ssl->conf->endpoint, |
| 2576 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 2577 | { |
| 2578 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 2579 | if( ret == 0 ) |
| 2580 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 2581 | } |
| 2582 | |
| 2583 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 2584 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 2585 | * with details encoded in the verification flags. All other kinds |
| 2586 | * of error codes, including those from the user provided f_vrfy |
| 2587 | * functions, are treated as fatal and lead to a failure of |
| 2588 | * ssl_parse_certificate even if verification was optional. */ |
| 2589 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 2590 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 2591 | ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
| 2592 | { |
| 2593 | ret = 0; |
| 2594 | } |
| 2595 | |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2596 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2597 | { |
| 2598 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 2599 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 2600 | } |
| 2601 | |
| 2602 | if( ret != 0 ) |
| 2603 | { |
| 2604 | uint8_t alert; |
| 2605 | |
| 2606 | /* The certificate may have been rejected for several reasons. |
| 2607 | Pick one and send the corresponding alert. Which alert to send |
| 2608 | may be a subject of debate in some cases. */ |
| 2609 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 2610 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 2611 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 2612 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 2613 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 2614 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2615 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 2616 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2617 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 2618 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2619 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 2620 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2621 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 2622 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2623 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 2624 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 2625 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 2626 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 2627 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 2628 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 2629 | else |
| 2630 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 2631 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2632 | alert ); |
| 2633 | } |
| 2634 | |
| 2635 | #if defined(MBEDTLS_DEBUG_C) |
| 2636 | if( ssl->session_negotiate->verify_result != 0 ) |
| 2637 | { |
| 2638 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", |
| 2639 | ssl->session_negotiate->verify_result ) ); |
| 2640 | } |
| 2641 | else |
| 2642 | { |
| 2643 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 2644 | } |
| 2645 | #endif /* MBEDTLS_DEBUG_C */ |
| 2646 | |
| 2647 | return( ret ); |
| 2648 | } |
| 2649 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2650 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2651 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 2652 | unsigned char *start, size_t len ) |
| 2653 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2654 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2655 | /* Remember digest of the peer's end-CRT. */ |
| 2656 | ssl->session_negotiate->peer_cert_digest = |
| 2657 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 2658 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 2659 | { |
| 2660 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
irwir | 40883e9 | 2019-09-21 17:55:33 +0300 | [diff] [blame] | 2661 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2662 | mbedtls_ssl_send_alert_message( ssl, |
| 2663 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2664 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 2665 | |
| 2666 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2667 | } |
| 2668 | |
| 2669 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 2670 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 2671 | start, len, |
| 2672 | ssl->session_negotiate->peer_cert_digest ); |
| 2673 | |
| 2674 | ssl->session_negotiate->peer_cert_digest_type = |
| 2675 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 2676 | ssl->session_negotiate->peer_cert_digest_len = |
| 2677 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 2678 | |
| 2679 | return( ret ); |
| 2680 | } |
| 2681 | |
| 2682 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 2683 | unsigned char *start, size_t len ) |
| 2684 | { |
| 2685 | unsigned char *end = start + len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2686 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2687 | |
| 2688 | /* Make a copy of the peer's raw public key. */ |
| 2689 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 2690 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 2691 | &ssl->handshake->peer_pubkey ); |
| 2692 | if( ret != 0 ) |
| 2693 | { |
| 2694 | /* We should have parsed the public key before. */ |
| 2695 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2696 | } |
| 2697 | |
| 2698 | return( 0 ); |
| 2699 | } |
| 2700 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2701 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2702 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 2703 | { |
| 2704 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2705 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2706 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2707 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 2708 | ? ssl->handshake->sni_authmode |
| 2709 | : ssl->conf->authmode; |
| 2710 | #else |
| 2711 | const int authmode = ssl->conf->authmode; |
| 2712 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2713 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2714 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2715 | |
| 2716 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 2717 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2718 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 2719 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2720 | { |
| 2721 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2722 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2723 | } |
| 2724 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2725 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2726 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2727 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2728 | { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2729 | chain = ssl->handshake->ecrs_peer_cert; |
| 2730 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2731 | goto crt_verify; |
| 2732 | } |
| 2733 | #endif |
| 2734 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 2735 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2736 | { |
| 2737 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 2738 | let it decide whether to alert. */ |
| 2739 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2740 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2741 | } |
| 2742 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2743 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2744 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 2745 | { |
| 2746 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2747 | |
| 2748 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2749 | ret = 0; |
| 2750 | else |
| 2751 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2752 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2753 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2754 | } |
| 2755 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2756 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2757 | /* Clear existing peer CRT structure in case we tried to |
| 2758 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2759 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2760 | |
| 2761 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 2762 | if( chain == NULL ) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2763 | { |
| 2764 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 2765 | sizeof( mbedtls_x509_crt ) ) ); |
| 2766 | mbedtls_ssl_send_alert_message( ssl, |
| 2767 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2768 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2769 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2770 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2771 | goto exit; |
| 2772 | } |
| 2773 | mbedtls_x509_crt_init( chain ); |
| 2774 | |
| 2775 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2776 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2777 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2778 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2779 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2780 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2781 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2782 | |
| 2783 | crt_verify: |
| 2784 | if( ssl->handshake->ecrs_enabled) |
| 2785 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 2786 | #endif |
| 2787 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2788 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2789 | chain, rs_ctx ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2790 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2791 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2792 | |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2793 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2794 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2795 | unsigned char *crt_start, *pk_start; |
| 2796 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2797 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2798 | /* We parse the CRT chain without copying, so |
| 2799 | * these pointers point into the input buffer, |
| 2800 | * and are hence still valid after freeing the |
| 2801 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2802 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2803 | crt_start = chain->raw.p; |
| 2804 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2805 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2806 | pk_start = chain->pk_raw.p; |
| 2807 | pk_len = chain->pk_raw.len; |
| 2808 | |
| 2809 | /* Free the CRT structures before computing |
| 2810 | * digest and copying the peer's public key. */ |
| 2811 | mbedtls_x509_crt_free( chain ); |
| 2812 | mbedtls_free( chain ); |
| 2813 | chain = NULL; |
| 2814 | |
| 2815 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2816 | if( ret != 0 ) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2817 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2818 | |
| 2819 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 2820 | if( ret != 0 ) |
| 2821 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2822 | } |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2823 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2824 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2825 | ssl->session_negotiate->peer_cert = chain; |
| 2826 | chain = NULL; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2827 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2829 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2830 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2831 | exit: |
| 2832 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2833 | if( ret == 0 ) |
| 2834 | ssl->state++; |
| 2835 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2836 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2837 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 2838 | { |
| 2839 | ssl->handshake->ecrs_peer_cert = chain; |
| 2840 | chain = NULL; |
| 2841 | } |
| 2842 | #endif |
| 2843 | |
| 2844 | if( chain != NULL ) |
| 2845 | { |
| 2846 | mbedtls_x509_crt_free( chain ); |
| 2847 | mbedtls_free( chain ); |
| 2848 | } |
| 2849 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2850 | return( ret ); |
| 2851 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2852 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2854 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 2855 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2856 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 2857 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2858 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2859 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2860 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 2861 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2862 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2863 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2864 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2865 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2866 | #if defined(MBEDTLS_SHA512_C) |
| 2867 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2868 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 2869 | else |
| 2870 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2871 | #if defined(MBEDTLS_SHA256_C) |
| 2872 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2873 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2874 | else |
| 2875 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2876 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2877 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2878 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2879 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2880 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2881 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2882 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2883 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2884 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2885 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2886 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2887 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 2888 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2889 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2890 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2891 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2892 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 2893 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2894 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 2895 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2896 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2897 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2898 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2899 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2900 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 2901 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2902 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2903 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2904 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2905 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2906 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2907 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2908 | } |
| 2909 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2910 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2911 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2912 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2913 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2914 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2915 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 2916 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2917 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2918 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2919 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2920 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2921 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 2922 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2923 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2924 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2925 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2926 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2927 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2928 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2929 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2930 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2931 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2932 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2933 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2936 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2937 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 2938 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2939 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2940 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2941 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 2942 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2943 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2944 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2945 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2946 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2947 | #if defined(MBEDTLS_SHA256_C) |
| 2948 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2949 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2950 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2951 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2952 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 2953 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2954 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2955 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2956 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2957 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2958 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2959 | #if defined(MBEDTLS_SHA512_C) |
| 2960 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2961 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2962 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2963 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2964 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2965 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2966 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2967 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2968 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2969 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2970 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2972 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2973 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2974 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2975 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2976 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2977 | mbedtls_md5_context md5; |
| 2978 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2979 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2980 | unsigned char padbuf[48]; |
| 2981 | unsigned char md5sum[16]; |
| 2982 | unsigned char sha1sum[20]; |
| 2983 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2984 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2985 | if( !session ) |
| 2986 | session = ssl->session; |
| 2987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2988 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2989 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2990 | mbedtls_md5_init( &md5 ); |
| 2991 | mbedtls_sha1_init( &sha1 ); |
| 2992 | |
| 2993 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 2994 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2995 | |
| 2996 | /* |
| 2997 | * SSLv3: |
| 2998 | * hash = |
| 2999 | * MD5( master + pad2 + |
| 3000 | * MD5( handshake + sender + master + pad1 ) ) |
| 3001 | * + SHA1( master + pad2 + |
| 3002 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3003 | */ |
| 3004 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3005 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3006 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 3007 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3008 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3009 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3010 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3011 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 3012 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3013 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3014 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3015 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3016 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3017 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3018 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3019 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3020 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 3021 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 3022 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 3023 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3024 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3025 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 3026 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 3027 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 3028 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3029 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3030 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3031 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3032 | mbedtls_md5_starts_ret( &md5 ); |
| 3033 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 3034 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 3035 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 3036 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3037 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3038 | mbedtls_sha1_starts_ret( &sha1 ); |
| 3039 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 3040 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 3041 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 3042 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3043 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3044 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3045 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3046 | mbedtls_md5_free( &md5 ); |
| 3047 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3048 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3049 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 3050 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 3051 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3053 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3054 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3055 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3056 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3057 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3058 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3059 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3060 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3061 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3062 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3063 | mbedtls_md5_context md5; |
| 3064 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3065 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3066 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3067 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3068 | if( !session ) |
| 3069 | session = ssl->session; |
| 3070 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3071 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3072 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3073 | mbedtls_md5_init( &md5 ); |
| 3074 | mbedtls_sha1_init( &sha1 ); |
| 3075 | |
| 3076 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 3077 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3078 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3079 | /* |
| 3080 | * TLSv1: |
| 3081 | * hash = PRF( master, finished_label, |
| 3082 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 3083 | */ |
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 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3086 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 3087 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3088 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3089 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3090 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3091 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 3092 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3093 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3094 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3095 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3096 | ? "client finished" |
| 3097 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3098 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3099 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 3100 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3101 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3102 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3103 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3104 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3105 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3106 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3107 | mbedtls_md5_free( &md5 ); |
| 3108 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3109 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3110 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3112 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3113 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3114 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3115 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3116 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3117 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3118 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3119 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3120 | { |
| 3121 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3122 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3123 | unsigned char padbuf[32]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3124 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3125 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 3126 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3127 | psa_status_t status; |
| 3128 | #else |
| 3129 | mbedtls_sha256_context sha256; |
| 3130 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3131 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3132 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3133 | if( !session ) |
| 3134 | session = ssl->session; |
| 3135 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3136 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 3137 | ? "client finished" |
| 3138 | : "server finished"; |
| 3139 | |
| 3140 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3141 | sha256_psa = psa_hash_operation_init(); |
| 3142 | |
| 3143 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 3144 | |
| 3145 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 3146 | if( status != PSA_SUCCESS ) |
| 3147 | { |
| 3148 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 3149 | return; |
| 3150 | } |
| 3151 | |
| 3152 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 3153 | if( status != PSA_SUCCESS ) |
| 3154 | { |
| 3155 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 3156 | return; |
| 3157 | } |
| 3158 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 3159 | #else |
| 3160 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3161 | mbedtls_sha256_init( &sha256 ); |
| 3162 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3163 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3164 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3165 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3166 | |
| 3167 | /* |
| 3168 | * TLSv1.2: |
| 3169 | * hash = PRF( master, finished_label, |
| 3170 | * Hash( handshake ) )[0.11] |
| 3171 | */ |
| 3172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3173 | #if !defined(MBEDTLS_SHA256_ALT) |
| 3174 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3175 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3176 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3177 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3178 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3179 | mbedtls_sha256_free( &sha256 ); |
| 3180 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3181 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3182 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3183 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3184 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3185 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3186 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3187 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3189 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3190 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3191 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3192 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3193 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3194 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3195 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3196 | { |
| 3197 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 3198 | const char *sender; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3199 | unsigned char padbuf[48]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3200 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3201 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 3202 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3203 | psa_status_t status; |
| 3204 | #else |
| 3205 | mbedtls_sha512_context sha512; |
| 3206 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3208 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3209 | if( !session ) |
| 3210 | session = ssl->session; |
| 3211 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3212 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 3213 | ? "client finished" |
| 3214 | : "server finished"; |
| 3215 | |
| 3216 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3217 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3218 | |
| 3219 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 3220 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3221 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3222 | if( status != PSA_SUCCESS ) |
| 3223 | { |
| 3224 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 3225 | return; |
| 3226 | } |
| 3227 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3228 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3229 | if( status != PSA_SUCCESS ) |
| 3230 | { |
| 3231 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 3232 | return; |
| 3233 | } |
| 3234 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 3235 | #else |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3236 | mbedtls_sha512_init( &sha512 ); |
| 3237 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3238 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3239 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 3240 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3241 | |
| 3242 | /* |
| 3243 | * TLSv1.2: |
| 3244 | * hash = PRF( master, finished_label, |
| 3245 | * Hash( handshake ) )[0.11] |
| 3246 | */ |
| 3247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3248 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 3249 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 3250 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 3251 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3252 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3253 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3254 | mbedtls_sha512_free( &sha512 ); |
| 3255 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3256 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 3257 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3258 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3260 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3261 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3262 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3264 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3265 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3266 | #endif /* MBEDTLS_SHA512_C */ |
| 3267 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3268 | |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 3269 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3270 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3271 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3272 | |
| 3273 | /* |
| 3274 | * Free our handshake params |
| 3275 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3276 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3277 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3278 | ssl->handshake = NULL; |
| 3279 | |
| 3280 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3281 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3282 | */ |
| 3283 | if( ssl->transform ) |
| 3284 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3285 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3286 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3287 | } |
| 3288 | ssl->transform = ssl->transform_negotiate; |
| 3289 | ssl->transform_negotiate = NULL; |
| 3290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3291 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3292 | } |
| 3293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3294 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3295 | { |
| 3296 | int resume = ssl->handshake->resume; |
| 3297 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3298 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3300 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3301 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3302 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3303 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3304 | ssl->renego_records_seen = 0; |
| 3305 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3306 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3307 | |
| 3308 | /* |
| 3309 | * Free the previous session and switch in the current one |
| 3310 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3311 | if( ssl->session ) |
| 3312 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3313 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 3314 | /* RFC 7366 3.1: keep the EtM state */ |
| 3315 | ssl->session_negotiate->encrypt_then_mac = |
| 3316 | ssl->session->encrypt_then_mac; |
| 3317 | #endif |
| 3318 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3319 | mbedtls_ssl_session_free( ssl->session ); |
| 3320 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3321 | } |
| 3322 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3323 | ssl->session_negotiate = NULL; |
| 3324 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3325 | /* |
| 3326 | * Add cache entry |
| 3327 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3328 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 3329 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 3330 | resume == 0 ) |
| 3331 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 3332 | 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] | 3333 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 3334 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3335 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3336 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3337 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3338 | ssl->handshake->flight != NULL ) |
| 3339 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3340 | /* Cancel handshake timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3341 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3342 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3343 | /* Keep last flight around in case we need to resend it: |
| 3344 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3345 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3346 | } |
| 3347 | else |
| 3348 | #endif |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 3349 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3350 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3351 | ssl->state++; |
| 3352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3353 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3356 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3357 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3358 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3360 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3361 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3362 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 3363 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3364 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3365 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 3366 | /* |
| 3367 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 3368 | * may define some other value. Currently (early 2016), no defined |
| 3369 | * ciphersuite does this (and this is unlikely to change as activity has |
| 3370 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 3371 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3372 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3374 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3375 | ssl->verify_data_len = hash_len; |
| 3376 | 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] | 3377 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3378 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3379 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3380 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3381 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3382 | |
| 3383 | /* |
| 3384 | * In case of session resuming, invert the client and server |
| 3385 | * ChangeCipherSpec messages order. |
| 3386 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3387 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3388 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3389 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3390 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3391 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3392 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3393 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3394 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3395 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3396 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3397 | } |
| 3398 | else |
| 3399 | ssl->state++; |
| 3400 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3401 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 3402 | * Switch to our negotiated transform and session parameters for outbound |
| 3403 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3404 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3405 | 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] | 3406 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3407 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3408 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3409 | { |
| 3410 | unsigned char i; |
| 3411 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3412 | /* Remember current epoch settings for resending */ |
| 3413 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3414 | 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] | 3415 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3416 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3417 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3418 | |
| 3419 | /* Increment epoch */ |
| 3420 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3421 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3422 | break; |
| 3423 | |
| 3424 | /* The loop goes to its end iff the counter is wrapping */ |
| 3425 | if( i == 0 ) |
| 3426 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3427 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 3428 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3429 | } |
| 3430 | } |
| 3431 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3432 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3433 | memset( ssl->cur_out_ctr, 0, 8 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3434 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3435 | ssl->transform_out = ssl->transform_negotiate; |
| 3436 | ssl->session_out = ssl->session_negotiate; |
| 3437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3438 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3439 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 3440 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3441 | 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] | 3442 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3443 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3444 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 3445 | } |
| 3446 | } |
| 3447 | #endif |
| 3448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3449 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3450 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3451 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3452 | #endif |
| 3453 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3454 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3455 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3456 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3457 | return( ret ); |
| 3458 | } |
| 3459 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3460 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3461 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3462 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 3463 | { |
| 3464 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 3465 | return( ret ); |
| 3466 | } |
| 3467 | #endif |
| 3468 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3469 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3470 | |
| 3471 | return( 0 ); |
| 3472 | } |
| 3473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3474 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3475 | #define SSL_MAX_HASH_LEN 36 |
| 3476 | #else |
| 3477 | #define SSL_MAX_HASH_LEN 12 |
| 3478 | #endif |
| 3479 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3480 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3481 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3482 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 3483 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3484 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3486 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3487 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3488 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3489 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3490 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3491 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3492 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3493 | return( ret ); |
| 3494 | } |
| 3495 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3496 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3497 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3498 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3499 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3500 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3501 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3504 | /* 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] | 3505 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 3506 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 3507 | hash_len = 36; |
| 3508 | else |
| 3509 | #endif |
| 3510 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3511 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3512 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 3513 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3514 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3515 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3516 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3517 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3518 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3521 | 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] | 3522 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3523 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3524 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3525 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3526 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3527 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3530 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3531 | ssl->verify_data_len = hash_len; |
| 3532 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3533 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3534 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3535 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3536 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3537 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3538 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3539 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3540 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3541 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3542 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3543 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3544 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3545 | } |
| 3546 | else |
| 3547 | ssl->state++; |
| 3548 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3549 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3550 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3551 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3552 | #endif |
| 3553 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3554 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3555 | |
| 3556 | return( 0 ); |
| 3557 | } |
| 3558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3559 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3560 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3561 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3562 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3563 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3564 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3565 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 3566 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3567 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 3568 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3569 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3570 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3571 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3572 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3573 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 3574 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 3575 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3576 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3577 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3578 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3579 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3580 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3581 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3582 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 3583 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3584 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3585 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 3586 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3587 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3588 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3589 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3590 | |
| 3591 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3592 | |
| 3593 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3594 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 3595 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 3596 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3597 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3598 | #if defined(MBEDTLS_DHM_C) |
| 3599 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3600 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3601 | #if defined(MBEDTLS_ECDH_C) |
| 3602 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3603 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3604 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3605 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3606 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3607 | handshake->ecjpake_cache = NULL; |
| 3608 | handshake->ecjpake_cache_len = 0; |
| 3609 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3610 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3611 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3612 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3613 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3614 | #endif |
| 3615 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3616 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3617 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 3618 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3619 | |
| 3620 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3621 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3622 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 3623 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3624 | } |
| 3625 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3626 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3628 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 3629 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3630 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 3631 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 3632 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 3633 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3634 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 3635 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 3636 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3637 | } |
| 3638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3639 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3640 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3641 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3642 | } |
| 3643 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3644 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3645 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3646 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3647 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3648 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3649 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3650 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3651 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3652 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3653 | |
| 3654 | /* |
| 3655 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 3656 | * Now allocate missing structures. |
| 3657 | */ |
| 3658 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3659 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3660 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3661 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3662 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3663 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3664 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3665 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3666 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3667 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 3668 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3669 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3670 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3671 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3672 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3673 | /* If the buffers are too small - reallocate */ |
| 3674 | { |
| 3675 | int modified = 0; |
| 3676 | if( ssl->in_buf_len < MBEDTLS_SSL_IN_BUFFER_LEN ) |
| 3677 | { |
| 3678 | if( resize_buffer( &ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 3679 | &ssl->in_buf_len ) != 0 ) |
| 3680 | { |
| 3681 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 3682 | } |
| 3683 | else |
| 3684 | { |
| 3685 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %d", MBEDTLS_SSL_IN_BUFFER_LEN ) ); |
| 3686 | modified = 1; |
| 3687 | } |
| 3688 | } |
| 3689 | if( ssl->out_buf_len < MBEDTLS_SSL_OUT_BUFFER_LEN ) |
| 3690 | { |
| 3691 | if( resize_buffer( &ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN, |
| 3692 | &ssl->out_buf_len ) != 0 ) |
| 3693 | { |
| 3694 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 3695 | } |
| 3696 | else |
| 3697 | { |
| 3698 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %d", MBEDTLS_SSL_OUT_BUFFER_LEN ) ); |
| 3699 | modified = 1; |
| 3700 | } |
| 3701 | } |
| 3702 | if( modified ) |
| 3703 | { |
| 3704 | /* Update pointers here to avoid doing it twice. */ |
| 3705 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 3706 | } |
| 3707 | } |
| 3708 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3709 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3710 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3711 | if( ssl->handshake == NULL || |
| 3712 | ssl->transform_negotiate == NULL || |
| 3713 | ssl->session_negotiate == NULL ) |
| 3714 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3715 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3717 | mbedtls_free( ssl->handshake ); |
| 3718 | mbedtls_free( ssl->transform_negotiate ); |
| 3719 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3720 | |
| 3721 | ssl->handshake = NULL; |
| 3722 | ssl->transform_negotiate = NULL; |
| 3723 | ssl->session_negotiate = NULL; |
| 3724 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3725 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3728 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3729 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3730 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 3731 | ssl_handshake_params_init( ssl->handshake ); |
| 3732 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3733 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3734 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3735 | { |
| 3736 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3737 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3738 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 3739 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 3740 | else |
| 3741 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3742 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3743 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3744 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3745 | #endif |
| 3746 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3747 | return( 0 ); |
| 3748 | } |
| 3749 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3750 | #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] | 3751 | /* Dummy cookie callbacks for defaults */ |
| 3752 | static int ssl_cookie_write_dummy( void *ctx, |
| 3753 | unsigned char **p, unsigned char *end, |
| 3754 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3755 | { |
| 3756 | ((void) ctx); |
| 3757 | ((void) p); |
| 3758 | ((void) end); |
| 3759 | ((void) cli_id); |
| 3760 | ((void) cli_id_len); |
| 3761 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3762 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | static int ssl_cookie_check_dummy( void *ctx, |
| 3766 | const unsigned char *cookie, size_t cookie_len, |
| 3767 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3768 | { |
| 3769 | ((void) ctx); |
| 3770 | ((void) cookie); |
| 3771 | ((void) cookie_len); |
| 3772 | ((void) cli_id); |
| 3773 | ((void) cli_id_len); |
| 3774 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3775 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3776 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3777 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3778 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3779 | /* |
| 3780 | * Initialize an SSL context |
| 3781 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3782 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 3783 | { |
| 3784 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 3785 | } |
| 3786 | |
| 3787 | /* |
| 3788 | * Setup an SSL context |
| 3789 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 3790 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3791 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 3792 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3793 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3794 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3795 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3796 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3797 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3798 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3799 | |
| 3800 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3801 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3802 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 3803 | |
| 3804 | /* Set to NULL in case of an error condition */ |
| 3805 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3806 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3807 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3808 | ssl->in_buf_len = in_buf_len; |
| 3809 | #endif |
| 3810 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3811 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3812 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3813 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", in_buf_len ) ); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3814 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3815 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3816 | } |
| 3817 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3818 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3819 | ssl->out_buf_len = out_buf_len; |
| 3820 | #endif |
| 3821 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3822 | if( ssl->out_buf == NULL ) |
| 3823 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3824 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", out_buf_len ) ); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3825 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3826 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3829 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3830 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3831 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3832 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3833 | |
| 3834 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3835 | |
| 3836 | error: |
| 3837 | mbedtls_free( ssl->in_buf ); |
| 3838 | mbedtls_free( ssl->out_buf ); |
| 3839 | |
| 3840 | ssl->conf = NULL; |
| 3841 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3842 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3843 | ssl->in_buf_len = 0; |
| 3844 | ssl->out_buf_len = 0; |
| 3845 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3846 | ssl->in_buf = NULL; |
| 3847 | ssl->out_buf = NULL; |
| 3848 | |
| 3849 | ssl->in_hdr = NULL; |
| 3850 | ssl->in_ctr = NULL; |
| 3851 | ssl->in_len = NULL; |
| 3852 | ssl->in_iv = NULL; |
| 3853 | ssl->in_msg = NULL; |
| 3854 | |
| 3855 | ssl->out_hdr = NULL; |
| 3856 | ssl->out_ctr = NULL; |
| 3857 | ssl->out_len = NULL; |
| 3858 | ssl->out_iv = NULL; |
| 3859 | ssl->out_msg = NULL; |
| 3860 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3861 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3865 | * Reset an initialized and used SSL context for re-use while retaining |
| 3866 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3867 | * |
| 3868 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 3869 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3870 | */ |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3871 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3872 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3873 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3874 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3875 | size_t in_buf_len = ssl->in_buf_len; |
| 3876 | size_t out_buf_len = ssl->out_buf_len; |
| 3877 | #else |
| 3878 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3879 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 3880 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3881 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 3882 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 3883 | !defined(MBEDTLS_SSL_SRV_C) |
| 3884 | ((void) partial); |
| 3885 | #endif |
| 3886 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3887 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3888 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3889 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3890 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3891 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3892 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3893 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3894 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3895 | |
| 3896 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3897 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 3898 | 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] | 3899 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3900 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3901 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3902 | ssl->in_offt = NULL; |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3903 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3904 | |
| 3905 | ssl->in_msgtype = 0; |
| 3906 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3907 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3908 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 3909 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3910 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3911 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 7e8e6a6 | 2020-02-05 10:45:48 +0000 | [diff] [blame] | 3912 | mbedtls_ssl_dtls_replay_reset( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3913 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3914 | |
| 3915 | ssl->in_hslen = 0; |
| 3916 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3917 | |
| 3918 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3919 | |
| 3920 | ssl->out_msgtype = 0; |
| 3921 | ssl->out_msglen = 0; |
| 3922 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3923 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 3924 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 3925 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 3926 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3927 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3928 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 3929 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3930 | ssl->transform_in = NULL; |
| 3931 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3932 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 3933 | ssl->session_in = NULL; |
| 3934 | ssl->session_out = NULL; |
| 3935 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3936 | memset( ssl->out_buf, 0, out_buf_len ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3937 | |
| 3938 | #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] | 3939 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3940 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 3941 | { |
| 3942 | ssl->in_left = 0; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3943 | memset( ssl->in_buf, 0, in_buf_len ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3944 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3945 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3946 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3947 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3948 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3949 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 3950 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3951 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3952 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 3953 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3954 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3955 | } |
| 3956 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3957 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3958 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3959 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3960 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3961 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3962 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3963 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3964 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3965 | if( ssl->session ) |
| 3966 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3967 | mbedtls_ssl_session_free( ssl->session ); |
| 3968 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3969 | ssl->session = NULL; |
| 3970 | } |
| 3971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3972 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3973 | ssl->alpn_chosen = NULL; |
| 3974 | #endif |
| 3975 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3976 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3977 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3978 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3979 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3980 | { |
| 3981 | mbedtls_free( ssl->cli_id ); |
| 3982 | ssl->cli_id = NULL; |
| 3983 | ssl->cli_id_len = 0; |
| 3984 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 3985 | #endif |
| 3986 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3987 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3988 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3989 | |
| 3990 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 3993 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3994 | * Reset an initialized and used SSL context for re-use while retaining |
| 3995 | * all application-set variables, function pointers and data. |
| 3996 | */ |
| 3997 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 3998 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3999 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4003 | * SSL set accessors |
| 4004 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4005 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4006 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4007 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4010 | 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] | 4011 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4012 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 4013 | } |
| 4014 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4015 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4016 | 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] | 4017 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4018 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4019 | } |
| 4020 | #endif |
| 4021 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4022 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4023 | 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] | 4024 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4025 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 4026 | } |
| 4027 | #endif |
| 4028 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4029 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4030 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 4031 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 4032 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4033 | { |
| 4034 | ssl->disable_datagram_packing = !allow_packing; |
| 4035 | } |
| 4036 | |
| 4037 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 4038 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 4039 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4040 | conf->hs_timeout_min = min; |
| 4041 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 4042 | } |
| 4043 | #endif |
| 4044 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4045 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4046 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4047 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4050 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4051 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4052 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4053 | void *p_vrfy ) |
| 4054 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4055 | conf->f_vrfy = f_vrfy; |
| 4056 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4057 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4058 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 4059 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4060 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 4061 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4062 | void *p_rng ) |
| 4063 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 4064 | conf->f_rng = f_rng; |
| 4065 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4068 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 4069 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4070 | void *p_dbg ) |
| 4071 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4072 | conf->f_dbg = f_dbg; |
| 4073 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4076 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 4077 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 4078 | mbedtls_ssl_send_t *f_send, |
| 4079 | mbedtls_ssl_recv_t *f_recv, |
| 4080 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 4081 | { |
| 4082 | ssl->p_bio = p_bio; |
| 4083 | ssl->f_send = f_send; |
| 4084 | ssl->f_recv = f_recv; |
| 4085 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 4086 | } |
| 4087 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 4088 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4089 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 4090 | { |
| 4091 | ssl->mtu = mtu; |
| 4092 | } |
| 4093 | #endif |
| 4094 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4095 | 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] | 4096 | { |
| 4097 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 4098 | } |
| 4099 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 4100 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 4101 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 4102 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 4103 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 4104 | { |
| 4105 | ssl->p_timer = p_timer; |
| 4106 | ssl->f_set_timer = f_set_timer; |
| 4107 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 4108 | |
| 4109 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 4110 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 4111 | } |
| 4112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4113 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4114 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 4115 | void *p_cache, |
| 4116 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 4117 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4118 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 4119 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4120 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4121 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4122 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4123 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4125 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4126 | 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] | 4127 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4128 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4129 | |
| 4130 | if( ssl == NULL || |
| 4131 | session == NULL || |
| 4132 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4133 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4134 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4135 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4136 | } |
| 4137 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 4138 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 4139 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4140 | return( ret ); |
| 4141 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4142 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 4143 | |
| 4144 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4145 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4146 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4147 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4148 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4149 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4150 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4151 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 4152 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 4153 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 4154 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 4155 | } |
| 4156 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4157 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 4158 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 4159 | int major, int minor ) |
| 4160 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4161 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 4162 | return; |
| 4163 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4164 | 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] | 4165 | return; |
| 4166 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4167 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4170 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 4171 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 4172 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 4173 | { |
| 4174 | conf->cert_profile = profile; |
| 4175 | } |
| 4176 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4177 | /* Append a new keycert entry to a (possibly empty) list */ |
| 4178 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 4179 | mbedtls_x509_crt *cert, |
| 4180 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4181 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4182 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4183 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4184 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 4185 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4186 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4187 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4188 | new_cert->cert = cert; |
| 4189 | new_cert->key = key; |
| 4190 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4191 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4192 | /* Update head is the list was null, else add to the end */ |
| 4193 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 4194 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4195 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 4196 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4197 | else |
| 4198 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4199 | mbedtls_ssl_key_cert *cur = *head; |
| 4200 | while( cur->next != NULL ) |
| 4201 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 4202 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 4203 | } |
| 4204 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4205 | return( 0 ); |
| 4206 | } |
| 4207 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4208 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 4209 | mbedtls_x509_crt *own_cert, |
| 4210 | mbedtls_pk_context *pk_key ) |
| 4211 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 4212 | 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] | 4213 | } |
| 4214 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4215 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4216 | mbedtls_x509_crt *ca_chain, |
| 4217 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4218 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4219 | conf->ca_chain = ca_chain; |
| 4220 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 4221 | |
| 4222 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 4223 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 4224 | * cannot be used together. */ |
| 4225 | conf->f_ca_cb = NULL; |
| 4226 | conf->p_ca_cb = NULL; |
| 4227 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4228 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 4229 | |
| 4230 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 4231 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 4232 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 4233 | void *p_ca_cb ) |
| 4234 | { |
| 4235 | conf->f_ca_cb = f_ca_cb; |
| 4236 | conf->p_ca_cb = p_ca_cb; |
| 4237 | |
| 4238 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 4239 | * cannot be used together. */ |
| 4240 | conf->ca_chain = NULL; |
| 4241 | conf->ca_crl = NULL; |
| 4242 | } |
| 4243 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4244 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 4245 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 4246 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4247 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 4248 | mbedtls_x509_crt *own_cert, |
| 4249 | mbedtls_pk_context *pk_key ) |
| 4250 | { |
| 4251 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 4252 | own_cert, pk_key ) ); |
| 4253 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 4254 | |
| 4255 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 4256 | mbedtls_x509_crt *ca_chain, |
| 4257 | mbedtls_x509_crl *ca_crl ) |
| 4258 | { |
| 4259 | ssl->handshake->sni_ca_chain = ca_chain; |
| 4260 | ssl->handshake->sni_ca_crl = ca_crl; |
| 4261 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 4262 | |
| 4263 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 4264 | int authmode ) |
| 4265 | { |
| 4266 | ssl->handshake->sni_authmode = authmode; |
| 4267 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 4268 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 4269 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 4270 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4271 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 4272 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 4273 | void *p_vrfy ) |
| 4274 | { |
| 4275 | ssl->f_vrfy = f_vrfy; |
| 4276 | ssl->p_vrfy = p_vrfy; |
| 4277 | } |
| 4278 | #endif |
| 4279 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4280 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 4281 | /* |
| 4282 | * Set EC J-PAKE password for current handshake |
| 4283 | */ |
| 4284 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 4285 | const unsigned char *pw, |
| 4286 | size_t pw_len ) |
| 4287 | { |
| 4288 | mbedtls_ecjpake_role role; |
| 4289 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 4290 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 4291 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4292 | |
| 4293 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4294 | role = MBEDTLS_ECJPAKE_SERVER; |
| 4295 | else |
| 4296 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 4297 | |
| 4298 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 4299 | role, |
| 4300 | MBEDTLS_MD_SHA256, |
| 4301 | MBEDTLS_ECP_DP_SECP256R1, |
| 4302 | pw, pw_len ) ); |
| 4303 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4304 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 4305 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4306 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4307 | |
| 4308 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 4309 | { |
| 4310 | /* Remove reference to existing PSK, if any. */ |
| 4311 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4312 | if( conf->psk_opaque != 0 ) |
| 4313 | { |
| 4314 | /* The maintenance of the PSK key slot is the |
| 4315 | * user's responsibility. */ |
| 4316 | conf->psk_opaque = 0; |
| 4317 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 4318 | /* This and the following branch should never |
| 4319 | * be taken simultaenously as we maintain the |
| 4320 | * invariant that raw and opaque PSKs are never |
| 4321 | * configured simultaneously. As a safeguard, |
| 4322 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4323 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4324 | if( conf->psk != NULL ) |
| 4325 | { |
| 4326 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 4327 | |
| 4328 | mbedtls_free( conf->psk ); |
| 4329 | conf->psk = NULL; |
| 4330 | conf->psk_len = 0; |
| 4331 | } |
| 4332 | |
| 4333 | /* Remove reference to PSK identity, if any. */ |
| 4334 | if( conf->psk_identity != NULL ) |
| 4335 | { |
| 4336 | mbedtls_free( conf->psk_identity ); |
| 4337 | conf->psk_identity = NULL; |
| 4338 | conf->psk_identity_len = 0; |
| 4339 | } |
| 4340 | } |
| 4341 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4342 | /* This function assumes that PSK identity in the SSL config is unset. |
| 4343 | * It checks that the provided identity is well-formed and attempts |
| 4344 | * to make a copy of it in the SSL config. |
| 4345 | * On failure, the PSK identity in the config remains unset. */ |
| 4346 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 4347 | unsigned char const *psk_identity, |
| 4348 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 4349 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 4350 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4351 | if( psk_identity == NULL || |
| 4352 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4353 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 4354 | { |
| 4355 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4356 | } |
| 4357 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4358 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 4359 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4360 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4361 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 4362 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 4363 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 4364 | |
| 4365 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4366 | } |
| 4367 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4368 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 4369 | const unsigned char *psk, size_t psk_len, |
| 4370 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 4371 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4372 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4373 | /* Remove opaque/raw PSK + PSK Identity */ |
| 4374 | ssl_conf_remove_psk( conf ); |
| 4375 | |
| 4376 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 4377 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4378 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 4379 | if( psk_len == 0 ) |
| 4380 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4381 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 4382 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4383 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4384 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 4385 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 4386 | conf->psk_len = psk_len; |
| 4387 | memcpy( conf->psk, psk, conf->psk_len ); |
| 4388 | |
| 4389 | /* Check and set PSK Identity */ |
| 4390 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 4391 | if( ret != 0 ) |
| 4392 | ssl_conf_remove_psk( conf ); |
| 4393 | |
| 4394 | return( ret ); |
| 4395 | } |
| 4396 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4397 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 4398 | { |
| 4399 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4400 | if( ssl->handshake->psk_opaque != 0 ) |
| 4401 | { |
| 4402 | ssl->handshake->psk_opaque = 0; |
| 4403 | } |
| 4404 | else |
| 4405 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4406 | if( ssl->handshake->psk != NULL ) |
| 4407 | { |
| 4408 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 4409 | ssl->handshake->psk_len ); |
| 4410 | mbedtls_free( ssl->handshake->psk ); |
| 4411 | ssl->handshake->psk_len = 0; |
| 4412 | } |
| 4413 | } |
| 4414 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4415 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 4416 | const unsigned char *psk, size_t psk_len ) |
| 4417 | { |
| 4418 | if( psk == NULL || ssl->handshake == NULL ) |
| 4419 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4420 | |
| 4421 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 4422 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4423 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4424 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4425 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4426 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4427 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4428 | |
| 4429 | ssl->handshake->psk_len = psk_len; |
| 4430 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 4431 | |
| 4432 | return( 0 ); |
| 4433 | } |
| 4434 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4435 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4436 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 4437 | psa_key_handle_t psk_slot, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4438 | const unsigned char *psk_identity, |
| 4439 | size_t psk_identity_len ) |
| 4440 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4441 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4442 | /* Clear opaque/raw PSK + PSK Identity, if present. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4443 | ssl_conf_remove_psk( conf ); |
| 4444 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4445 | /* Check and set opaque PSK */ |
| 4446 | if( psk_slot == 0 ) |
| 4447 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4448 | conf->psk_opaque = psk_slot; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4449 | |
| 4450 | /* Check and set PSK Identity */ |
| 4451 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 4452 | psk_identity_len ); |
| 4453 | if( ret != 0 ) |
| 4454 | ssl_conf_remove_psk( conf ); |
| 4455 | |
| 4456 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 4460 | psa_key_handle_t psk_slot ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4461 | { |
| 4462 | if( psk_slot == 0 || ssl->handshake == NULL ) |
| 4463 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4464 | |
| 4465 | ssl_remove_psk( ssl ); |
| 4466 | ssl->handshake->psk_opaque = psk_slot; |
| 4467 | return( 0 ); |
| 4468 | } |
| 4469 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4470 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4471 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4472 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 4473 | size_t), |
| 4474 | void *p_psk ) |
| 4475 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4476 | conf->f_psk = f_psk; |
| 4477 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 4478 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4479 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 4480 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4481 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 4482 | |
| 4483 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4484 | 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] | 4485 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4486 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4487 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4488 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 4489 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 4490 | { |
| 4491 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4492 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4493 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4494 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4495 | |
| 4496 | return( 0 ); |
| 4497 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 4498 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4499 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4500 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 4501 | const unsigned char *dhm_P, size_t P_len, |
| 4502 | const unsigned char *dhm_G, size_t G_len ) |
| 4503 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4504 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4505 | |
| 4506 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 4507 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 4508 | { |
| 4509 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4510 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4511 | return( ret ); |
| 4512 | } |
| 4513 | |
| 4514 | return( 0 ); |
| 4515 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4516 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4517 | 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] | 4518 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4519 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4520 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4521 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 4522 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 4523 | { |
| 4524 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4525 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4526 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4527 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4528 | |
| 4529 | return( 0 ); |
| 4530 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4531 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4532 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4533 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4534 | /* |
| 4535 | * Set the minimum length for Diffie-Hellman parameters |
| 4536 | */ |
| 4537 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 4538 | unsigned int bitlen ) |
| 4539 | { |
| 4540 | conf->dhm_min_bitlen = bitlen; |
| 4541 | } |
| 4542 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 4543 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4544 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 4545 | /* |
| 4546 | * Set allowed/preferred hashes for handshake signatures |
| 4547 | */ |
| 4548 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 4549 | const int *hashes ) |
| 4550 | { |
| 4551 | conf->sig_hashes = hashes; |
| 4552 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4553 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 4554 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4555 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4556 | /* |
| 4557 | * Set the allowed elliptic curves |
| 4558 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4559 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4560 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4561 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4562 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4563 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4564 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4565 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4566 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4567 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4568 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4569 | /* Initialize to suppress unnecessary compiler warning */ |
| 4570 | size_t hostname_len = 0; |
| 4571 | |
| 4572 | /* Check if new hostname is valid before |
| 4573 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4574 | if( hostname != NULL ) |
| 4575 | { |
| 4576 | hostname_len = strlen( hostname ); |
| 4577 | |
| 4578 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 4579 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4580 | } |
| 4581 | |
| 4582 | /* Now it's clear that we will overwrite the old hostname, |
| 4583 | * so we can free it safely */ |
| 4584 | |
| 4585 | if( ssl->hostname != NULL ) |
| 4586 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4587 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4588 | mbedtls_free( ssl->hostname ); |
| 4589 | } |
| 4590 | |
| 4591 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 4592 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4593 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4594 | { |
| 4595 | ssl->hostname = NULL; |
| 4596 | } |
| 4597 | else |
| 4598 | { |
| 4599 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4600 | if( ssl->hostname == NULL ) |
| 4601 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4602 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4603 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4604 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4605 | ssl->hostname[hostname_len] = '\0'; |
| 4606 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4607 | |
| 4608 | return( 0 ); |
| 4609 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 4610 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4611 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4612 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4613 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4614 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4615 | const unsigned char *, size_t), |
| 4616 | void *p_sni ) |
| 4617 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4618 | conf->f_sni = f_sni; |
| 4619 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4620 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4621 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4623 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4624 | 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] | 4625 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4626 | size_t cur_len, tot_len; |
| 4627 | const char **p; |
| 4628 | |
| 4629 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4630 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 4631 | * MUST NOT be truncated." |
| 4632 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4633 | */ |
| 4634 | tot_len = 0; |
| 4635 | for( p = protos; *p != NULL; p++ ) |
| 4636 | { |
| 4637 | cur_len = strlen( *p ); |
| 4638 | tot_len += cur_len; |
| 4639 | |
| 4640 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4641 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4642 | } |
| 4643 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4644 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4645 | |
| 4646 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4647 | } |
| 4648 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4649 | 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] | 4650 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4651 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4652 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4653 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4654 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4655 | 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] | 4656 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4657 | conf->max_major_ver = major; |
| 4658 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4661 | 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] | 4662 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4663 | conf->min_major_ver = major; |
| 4664 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4667 | #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] | 4668 | 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] | 4669 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 4670 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 4671 | } |
| 4672 | #endif |
| 4673 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4674 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4675 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 4676 | char cert_req_ca_list ) |
| 4677 | { |
| 4678 | conf->cert_req_ca_list = cert_req_ca_list; |
| 4679 | } |
| 4680 | #endif |
| 4681 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4682 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4683 | 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] | 4684 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4685 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 4686 | } |
| 4687 | #endif |
| 4688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4689 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4690 | 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] | 4691 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4692 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 4693 | } |
| 4694 | #endif |
| 4695 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 4696 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4697 | 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] | 4698 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4699 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 4700 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 4701 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 4702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4703 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4704 | 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] | 4705 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4706 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4707 | 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] | 4708 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4709 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4710 | } |
| 4711 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 4712 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4713 | |
| 4714 | return( 0 ); |
| 4715 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4716 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4717 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4718 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4719 | 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] | 4720 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4721 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 4722 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4723 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 4724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4725 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4726 | 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] | 4727 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 4728 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 4729 | } |
| 4730 | #endif |
| 4731 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4732 | 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] | 4733 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4734 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4737 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4738 | 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] | 4739 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4740 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 4741 | } |
| 4742 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4743 | 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] | 4744 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4745 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 4746 | } |
| 4747 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4748 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4749 | const unsigned char period[8] ) |
| 4750 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4751 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4752 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4753 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4754 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4755 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4756 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4757 | 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] | 4758 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 4759 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4760 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4761 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4762 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4763 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4764 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 4765 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 4766 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 4767 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4768 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4769 | conf->f_ticket_write = f_ticket_write; |
| 4770 | conf->f_ticket_parse = f_ticket_parse; |
| 4771 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4772 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4773 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4774 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4775 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4776 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 4777 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 4778 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 4779 | void *p_export_keys ) |
| 4780 | { |
| 4781 | conf->f_export_keys = f_export_keys; |
| 4782 | conf->p_export_keys = p_export_keys; |
| 4783 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 4784 | |
| 4785 | void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf, |
| 4786 | mbedtls_ssl_export_keys_ext_t *f_export_keys_ext, |
| 4787 | void *p_export_keys ) |
| 4788 | { |
| 4789 | conf->f_export_keys_ext = f_export_keys_ext; |
| 4790 | conf->p_export_keys = p_export_keys; |
| 4791 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4792 | #endif |
| 4793 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4794 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4795 | void mbedtls_ssl_conf_async_private_cb( |
| 4796 | mbedtls_ssl_config *conf, |
| 4797 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 4798 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 4799 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 4800 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4801 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4802 | { |
| 4803 | conf->f_async_sign_start = f_async_sign; |
| 4804 | conf->f_async_decrypt_start = f_async_decrypt; |
| 4805 | conf->f_async_resume = f_async_resume; |
| 4806 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4807 | conf->p_async_config_data = async_config_data; |
| 4808 | } |
| 4809 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 4810 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 4811 | { |
| 4812 | return( conf->p_async_config_data ); |
| 4813 | } |
| 4814 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4815 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4816 | { |
| 4817 | if( ssl->handshake == NULL ) |
| 4818 | return( NULL ); |
| 4819 | else |
| 4820 | return( ssl->handshake->user_async_ctx ); |
| 4821 | } |
| 4822 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4823 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4824 | void *ctx ) |
| 4825 | { |
| 4826 | if( ssl->handshake != NULL ) |
| 4827 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4828 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4829 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4830 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4831 | /* |
| 4832 | * SSL get accessors |
| 4833 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4834 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4835 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 4836 | if( ssl->session != NULL ) |
| 4837 | return( ssl->session->verify_result ); |
| 4838 | |
| 4839 | if( ssl->session_negotiate != NULL ) |
| 4840 | return( ssl->session_negotiate->verify_result ); |
| 4841 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 4842 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4845 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4846 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4847 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4848 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4849 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4850 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4851 | } |
| 4852 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4853 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4854 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4855 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4856 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4857 | { |
| 4858 | switch( ssl->minor_ver ) |
| 4859 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4860 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4861 | return( "DTLSv1.0" ); |
| 4862 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4863 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4864 | return( "DTLSv1.2" ); |
| 4865 | |
| 4866 | default: |
| 4867 | return( "unknown (DTLS)" ); |
| 4868 | } |
| 4869 | } |
| 4870 | #endif |
| 4871 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4872 | switch( ssl->minor_ver ) |
| 4873 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4874 | case MBEDTLS_SSL_MINOR_VERSION_0: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4875 | return( "SSLv3.0" ); |
| 4876 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4877 | case MBEDTLS_SSL_MINOR_VERSION_1: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4878 | return( "TLSv1.0" ); |
| 4879 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4880 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4881 | return( "TLSv1.1" ); |
| 4882 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4883 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4884 | return( "TLSv1.2" ); |
| 4885 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4886 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4887 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4888 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4891 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame^] | 4892 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 4893 | { |
| 4894 | size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN; |
| 4895 | size_t read_mfl; |
| 4896 | |
| 4897 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 4898 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4899 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 4900 | { |
| 4901 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 4902 | } |
| 4903 | |
| 4904 | /* Check if a smaller max length was negotiated */ |
| 4905 | if( ssl->session_out != NULL ) |
| 4906 | { |
| 4907 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 4908 | if( read_mfl < max_len ) |
| 4909 | { |
| 4910 | max_len = read_mfl; |
| 4911 | } |
| 4912 | } |
| 4913 | |
| 4914 | // During a handshake, use the value being negotiated |
| 4915 | if( ssl->session_negotiate != NULL ) |
| 4916 | { |
| 4917 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 4918 | if( read_mfl < max_len ) |
| 4919 | { |
| 4920 | max_len = read_mfl; |
| 4921 | } |
| 4922 | } |
| 4923 | |
| 4924 | return( max_len ); |
| 4925 | } |
| 4926 | |
| 4927 | 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] | 4928 | { |
| 4929 | size_t max_len; |
| 4930 | |
| 4931 | /* |
| 4932 | * Assume mfl_code is correct since it was checked when set |
| 4933 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4934 | 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] | 4935 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4936 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4937 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4938 | 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] | 4939 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4940 | 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] | 4941 | } |
| 4942 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4943 | /* During a handshake, use the value being negotiated */ |
| 4944 | if( ssl->session_negotiate != NULL && |
| 4945 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 4946 | { |
| 4947 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 4948 | } |
| 4949 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4950 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4951 | } |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame^] | 4952 | |
| 4953 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4954 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 4955 | { |
| 4956 | return mbedtls_ssl_get_output_max_frag_len( ssl ); |
| 4957 | } |
| 4958 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4959 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4960 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4961 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4962 | 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] | 4963 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 4964 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 4965 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4966 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 4967 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 4968 | return ( 0 ); |
| 4969 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4970 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 4971 | return( ssl->mtu ); |
| 4972 | |
| 4973 | if( ssl->mtu == 0 ) |
| 4974 | return( ssl->handshake->mtu ); |
| 4975 | |
| 4976 | return( ssl->mtu < ssl->handshake->mtu ? |
| 4977 | ssl->mtu : ssl->handshake->mtu ); |
| 4978 | } |
| 4979 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4980 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4981 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 4982 | { |
| 4983 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 4984 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 4985 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4986 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4987 | (void) ssl; |
| 4988 | #endif |
| 4989 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4990 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame^] | 4991 | 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] | 4992 | |
| 4993 | if( max_len > mfl ) |
| 4994 | max_len = mfl; |
| 4995 | #endif |
| 4996 | |
| 4997 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4998 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4999 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 5000 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5001 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 5002 | const size_t overhead = (size_t) ret; |
| 5003 | |
| 5004 | if( ret < 0 ) |
| 5005 | return( ret ); |
| 5006 | |
| 5007 | if( mtu <= overhead ) |
| 5008 | { |
| 5009 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 5010 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5011 | } |
| 5012 | |
| 5013 | if( max_len > mtu - overhead ) |
| 5014 | max_len = mtu - overhead; |
| 5015 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 5016 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5017 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 5018 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 5019 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5020 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5021 | #endif |
| 5022 | |
| 5023 | return( (int) max_len ); |
| 5024 | } |
| 5025 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5026 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5027 | 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] | 5028 | { |
| 5029 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 5030 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5031 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 5032 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 5033 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 5034 | #else |
| 5035 | return( NULL ); |
| 5036 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5037 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5038 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5040 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 5041 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 5042 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5043 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5044 | if( ssl == NULL || |
| 5045 | dst == NULL || |
| 5046 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5047 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5048 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5049 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5050 | } |
| 5051 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 5052 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5053 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5054 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5055 | |
Manuel Pégourié-Gonnard | b5e4e0a | 2019-05-20 11:12:28 +0200 | [diff] [blame] | 5056 | const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ) |
| 5057 | { |
| 5058 | if( ssl == NULL ) |
| 5059 | return( NULL ); |
| 5060 | |
| 5061 | return( ssl->session ); |
| 5062 | } |
| 5063 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5064 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5065 | * Define ticket header determining Mbed TLS version |
| 5066 | * and structure of the ticket. |
| 5067 | */ |
| 5068 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5069 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5070 | * Define bitflag determining compile-time settings influencing |
| 5071 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5072 | */ |
| 5073 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5074 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5075 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5076 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5077 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5078 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5079 | |
| 5080 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5081 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5082 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5083 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5084 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5085 | |
| 5086 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5087 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5088 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5089 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5090 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 5091 | |
| 5092 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5093 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5094 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5095 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5096 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 5097 | |
| 5098 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5099 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5100 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5101 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5102 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 5103 | |
| 5104 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5105 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5106 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5107 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5108 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 5109 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5110 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5111 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 5112 | #else |
| 5113 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 5114 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 5115 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5116 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 5117 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 5118 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 5119 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
| 5120 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4 |
| 5121 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5 |
| 5122 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5123 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5124 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 5125 | ( (uint16_t) ( \ |
| 5126 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 5127 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 5128 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 5129 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
| 5130 | ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \ |
| 5131 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 5132 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5133 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 5134 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 5135 | MBEDTLS_VERSION_MAJOR, |
| 5136 | MBEDTLS_VERSION_MINOR, |
| 5137 | MBEDTLS_VERSION_PATCH, |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5138 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 5139 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 5140 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5141 | |
| 5142 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5143 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5144 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5145 | * |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5146 | * opaque mbedtls_version[3]; // major, minor, patch |
| 5147 | * opaque session_format[2]; // version-specific 16-bit field determining |
| 5148 | * // the format of the remaining |
| 5149 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 5150 | * |
| 5151 | * Note: When updating the format, remember to keep |
| 5152 | * these version+format bytes. |
| 5153 | * |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 5154 | * // In this version, `session_format` determines |
| 5155 | * // the setting of those compile-time |
| 5156 | * // configuration options which influence |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5157 | * // the structure of mbedtls_ssl_session. |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5158 | * uint64 start_time; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5159 | * uint8 ciphersuite[2]; // defined by the standard |
| 5160 | * uint8 compression; // 0 or 1 |
| 5161 | * uint8 session_id_len; // at most 32 |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5162 | * opaque session_id[32]; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5163 | * opaque master[48]; // fixed length in the standard |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5164 | * uint32 verify_result; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5165 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 5166 | * 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] | 5167 | * uint32 ticket_lifetime; |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 5168 | * uint8 mfl_code; // up to 255 according to standard |
| 5169 | * uint8 trunc_hmac; // 0 or 1 |
| 5170 | * uint8 encrypt_then_mac; // 0 or 1 |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5171 | * |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5172 | * The order is the same as in the definition of the structure, except |
| 5173 | * verify_result is put before peer_cert so that all mandatory fields come |
| 5174 | * together in one block. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5175 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5176 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 5177 | unsigned char omit_header, |
| 5178 | unsigned char *buf, |
| 5179 | size_t buf_len, |
| 5180 | size_t *olen ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5181 | { |
| 5182 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5183 | size_t used = 0; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5184 | #if defined(MBEDTLS_HAVE_TIME) |
| 5185 | uint64_t start; |
| 5186 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5187 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5188 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5189 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5190 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5191 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5192 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5193 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5194 | if( !omit_header ) |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5195 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5196 | /* |
| 5197 | * Add version identifier |
| 5198 | */ |
| 5199 | |
| 5200 | used += sizeof( ssl_serialized_session_header ); |
| 5201 | |
| 5202 | if( used <= buf_len ) |
| 5203 | { |
| 5204 | memcpy( p, ssl_serialized_session_header, |
| 5205 | sizeof( ssl_serialized_session_header ) ); |
| 5206 | p += sizeof( ssl_serialized_session_header ); |
| 5207 | } |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5208 | } |
| 5209 | |
| 5210 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5211 | * Time |
| 5212 | */ |
| 5213 | #if defined(MBEDTLS_HAVE_TIME) |
| 5214 | used += 8; |
| 5215 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5216 | if( used <= buf_len ) |
| 5217 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5218 | start = (uint64_t) session->start; |
| 5219 | |
| 5220 | *p++ = (unsigned char)( ( start >> 56 ) & 0xFF ); |
| 5221 | *p++ = (unsigned char)( ( start >> 48 ) & 0xFF ); |
| 5222 | *p++ = (unsigned char)( ( start >> 40 ) & 0xFF ); |
| 5223 | *p++ = (unsigned char)( ( start >> 32 ) & 0xFF ); |
| 5224 | *p++ = (unsigned char)( ( start >> 24 ) & 0xFF ); |
| 5225 | *p++ = (unsigned char)( ( start >> 16 ) & 0xFF ); |
| 5226 | *p++ = (unsigned char)( ( start >> 8 ) & 0xFF ); |
| 5227 | *p++ = (unsigned char)( ( start ) & 0xFF ); |
| 5228 | } |
| 5229 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5230 | |
| 5231 | /* |
| 5232 | * Basic mandatory fields |
| 5233 | */ |
| 5234 | used += 2 /* ciphersuite */ |
| 5235 | + 1 /* compression */ |
| 5236 | + 1 /* id_len */ |
| 5237 | + sizeof( session->id ) |
| 5238 | + sizeof( session->master ) |
| 5239 | + 4; /* verify_result */ |
| 5240 | |
| 5241 | if( used <= buf_len ) |
| 5242 | { |
| 5243 | *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF ); |
| 5244 | *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF ); |
| 5245 | |
| 5246 | *p++ = (unsigned char)( session->compression & 0xFF ); |
| 5247 | |
| 5248 | *p++ = (unsigned char)( session->id_len & 0xFF ); |
| 5249 | memcpy( p, session->id, 32 ); |
| 5250 | p += 32; |
| 5251 | |
| 5252 | memcpy( p, session->master, 48 ); |
| 5253 | p += 48; |
| 5254 | |
| 5255 | *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF ); |
| 5256 | *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF ); |
| 5257 | *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF ); |
| 5258 | *p++ = (unsigned char)( ( session->verify_result ) & 0xFF ); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5259 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5260 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5261 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5262 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5263 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5264 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5265 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5266 | if( session->peer_cert == NULL ) |
| 5267 | cert_len = 0; |
| 5268 | else |
| 5269 | cert_len = session->peer_cert->raw.len; |
| 5270 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5271 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5272 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5273 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5274 | { |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5275 | *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); |
| 5276 | *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); |
| 5277 | *p++ = (unsigned char)( ( cert_len ) & 0xFF ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5278 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5279 | if( session->peer_cert != NULL ) |
| 5280 | { |
| 5281 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 5282 | p += cert_len; |
| 5283 | } |
| 5284 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5285 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5286 | if( session->peer_cert_digest != NULL ) |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5287 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5288 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 5289 | if( used <= buf_len ) |
| 5290 | { |
| 5291 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 5292 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 5293 | memcpy( p, session->peer_cert_digest, |
| 5294 | session->peer_cert_digest_len ); |
| 5295 | p += session->peer_cert_digest_len; |
| 5296 | } |
| 5297 | } |
| 5298 | else |
| 5299 | { |
| 5300 | used += 2; |
| 5301 | if( used <= buf_len ) |
| 5302 | { |
| 5303 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 5304 | *p++ = 0; |
| 5305 | } |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5306 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5307 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5308 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5309 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5310 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5311 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5312 | */ |
| 5313 | #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] | 5314 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5315 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5316 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5317 | { |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5318 | *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF ); |
| 5319 | *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF ); |
| 5320 | *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF ); |
| 5321 | |
| 5322 | if( session->ticket != NULL ) |
| 5323 | { |
| 5324 | memcpy( p, session->ticket, session->ticket_len ); |
| 5325 | p += session->ticket_len; |
| 5326 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5327 | |
| 5328 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF ); |
| 5329 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF ); |
| 5330 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF ); |
| 5331 | *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF ); |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5332 | } |
| 5333 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5334 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5335 | /* |
| 5336 | * Misc extension-related info |
| 5337 | */ |
| 5338 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 5339 | used += 1; |
| 5340 | |
| 5341 | if( used <= buf_len ) |
| 5342 | *p++ = session->mfl_code; |
| 5343 | #endif |
| 5344 | |
| 5345 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 5346 | used += 1; |
| 5347 | |
| 5348 | if( used <= buf_len ) |
| 5349 | *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF ); |
| 5350 | #endif |
| 5351 | |
| 5352 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5353 | used += 1; |
| 5354 | |
| 5355 | if( used <= buf_len ) |
| 5356 | *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF ); |
| 5357 | #endif |
| 5358 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5359 | /* Done */ |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5360 | *olen = used; |
| 5361 | |
| 5362 | if( used > buf_len ) |
| 5363 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5364 | |
| 5365 | return( 0 ); |
| 5366 | } |
| 5367 | |
| 5368 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5369 | * Public wrapper for ssl_session_save() |
| 5370 | */ |
| 5371 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 5372 | unsigned char *buf, |
| 5373 | size_t buf_len, |
| 5374 | size_t *olen ) |
| 5375 | { |
| 5376 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 5377 | } |
| 5378 | |
| 5379 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5380 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5381 | * |
| 5382 | * 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] | 5383 | * case of error, and has an extra option omit_header. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5384 | */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5385 | static int ssl_session_load( mbedtls_ssl_session *session, |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5386 | unsigned char omit_header, |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5387 | const unsigned char *buf, |
| 5388 | size_t len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5389 | { |
| 5390 | const unsigned char *p = buf; |
| 5391 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5392 | #if defined(MBEDTLS_HAVE_TIME) |
| 5393 | uint64_t start; |
| 5394 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5395 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5396 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5397 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5398 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5399 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5400 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5401 | if( !omit_header ) |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5402 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5403 | /* |
| 5404 | * Check version identifier |
| 5405 | */ |
| 5406 | |
| 5407 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 5408 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5409 | |
| 5410 | if( memcmp( p, ssl_serialized_session_header, |
| 5411 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 5412 | { |
| 5413 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 5414 | } |
| 5415 | p += sizeof( ssl_serialized_session_header ); |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5416 | } |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5417 | |
| 5418 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5419 | * Time |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5420 | */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5421 | #if defined(MBEDTLS_HAVE_TIME) |
| 5422 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5423 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5424 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5425 | start = ( (uint64_t) p[0] << 56 ) | |
| 5426 | ( (uint64_t) p[1] << 48 ) | |
| 5427 | ( (uint64_t) p[2] << 40 ) | |
| 5428 | ( (uint64_t) p[3] << 32 ) | |
| 5429 | ( (uint64_t) p[4] << 24 ) | |
| 5430 | ( (uint64_t) p[5] << 16 ) | |
| 5431 | ( (uint64_t) p[6] << 8 ) | |
| 5432 | ( (uint64_t) p[7] ); |
| 5433 | p += 8; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5434 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5435 | session->start = (time_t) start; |
| 5436 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5437 | |
| 5438 | /* |
| 5439 | * Basic mandatory fields |
| 5440 | */ |
| 5441 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 5442 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5443 | |
| 5444 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 5445 | p += 2; |
| 5446 | |
| 5447 | session->compression = *p++; |
| 5448 | |
| 5449 | session->id_len = *p++; |
| 5450 | memcpy( session->id, p, 32 ); |
| 5451 | p += 32; |
| 5452 | |
| 5453 | memcpy( session->master, p, 48 ); |
| 5454 | p += 48; |
| 5455 | |
| 5456 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 5457 | ( (uint32_t) p[1] << 16 ) | |
| 5458 | ( (uint32_t) p[2] << 8 ) | |
| 5459 | ( (uint32_t) p[3] ); |
| 5460 | p += 4; |
| 5461 | |
| 5462 | /* Immediately clear invalid pointer values that have been read, in case |
| 5463 | * we exit early before we replaced them with valid ones. */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5464 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5465 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5466 | session->peer_cert = NULL; |
| 5467 | #else |
| 5468 | session->peer_cert_digest = NULL; |
| 5469 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5470 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5471 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5472 | session->ticket = NULL; |
| 5473 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5474 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5475 | /* |
| 5476 | * Peer certificate |
| 5477 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5478 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5479 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5480 | /* Deserialize CRT from the end of the ticket. */ |
| 5481 | if( 3 > (size_t)( end - p ) ) |
| 5482 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5483 | |
| 5484 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5485 | p += 3; |
| 5486 | |
| 5487 | if( cert_len != 0 ) |
| 5488 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5489 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5490 | |
| 5491 | if( cert_len > (size_t)( end - p ) ) |
| 5492 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5493 | |
| 5494 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 5495 | |
| 5496 | if( session->peer_cert == NULL ) |
| 5497 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5498 | |
| 5499 | mbedtls_x509_crt_init( session->peer_cert ); |
| 5500 | |
| 5501 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 5502 | p, cert_len ) ) != 0 ) |
| 5503 | { |
| 5504 | mbedtls_x509_crt_free( session->peer_cert ); |
| 5505 | mbedtls_free( session->peer_cert ); |
| 5506 | session->peer_cert = NULL; |
| 5507 | return( ret ); |
| 5508 | } |
| 5509 | |
| 5510 | p += cert_len; |
| 5511 | } |
| 5512 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5513 | /* Deserialize CRT digest from the end of the ticket. */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5514 | if( 2 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5515 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5516 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5517 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 5518 | session->peer_cert_digest_len = (size_t) *p++; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5519 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5520 | if( session->peer_cert_digest_len != 0 ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5521 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5522 | const mbedtls_md_info_t *md_info = |
| 5523 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 5524 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5525 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5526 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 5527 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5528 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5529 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 5530 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5531 | |
| 5532 | session->peer_cert_digest = |
| 5533 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5534 | if( session->peer_cert_digest == NULL ) |
| 5535 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5536 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5537 | memcpy( session->peer_cert_digest, p, |
| 5538 | session->peer_cert_digest_len ); |
| 5539 | p += session->peer_cert_digest_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5540 | } |
| 5541 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5542 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5543 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5544 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5545 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5546 | */ |
| 5547 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5548 | if( 3 > (size_t)( end - p ) ) |
| 5549 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5550 | |
| 5551 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5552 | p += 3; |
| 5553 | |
| 5554 | if( session->ticket_len != 0 ) |
| 5555 | { |
| 5556 | if( session->ticket_len > (size_t)( end - p ) ) |
| 5557 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5558 | |
| 5559 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 5560 | if( session->ticket == NULL ) |
| 5561 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5562 | |
| 5563 | memcpy( session->ticket, p, session->ticket_len ); |
| 5564 | p += session->ticket_len; |
| 5565 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5566 | |
| 5567 | if( 4 > (size_t)( end - p ) ) |
| 5568 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5569 | |
| 5570 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 5571 | ( (uint32_t) p[1] << 16 ) | |
| 5572 | ( (uint32_t) p[2] << 8 ) | |
| 5573 | ( (uint32_t) p[3] ); |
| 5574 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5575 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5576 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5577 | /* |
| 5578 | * Misc extension-related info |
| 5579 | */ |
| 5580 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 5581 | if( 1 > (size_t)( end - p ) ) |
| 5582 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5583 | |
| 5584 | session->mfl_code = *p++; |
| 5585 | #endif |
| 5586 | |
| 5587 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 5588 | if( 1 > (size_t)( end - p ) ) |
| 5589 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5590 | |
| 5591 | session->trunc_hmac = *p++; |
| 5592 | #endif |
| 5593 | |
| 5594 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5595 | if( 1 > (size_t)( end - p ) ) |
| 5596 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5597 | |
| 5598 | session->encrypt_then_mac = *p++; |
| 5599 | #endif |
| 5600 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5601 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5602 | if( p != end ) |
| 5603 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5604 | |
| 5605 | return( 0 ); |
| 5606 | } |
| 5607 | |
| 5608 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5609 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5610 | */ |
| 5611 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 5612 | const unsigned char *buf, |
| 5613 | size_t len ) |
| 5614 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5615 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5616 | |
| 5617 | if( ret != 0 ) |
| 5618 | mbedtls_ssl_session_free( session ); |
| 5619 | |
| 5620 | return( ret ); |
| 5621 | } |
| 5622 | |
| 5623 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5624 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5625 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5626 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5628 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5629 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5630 | if( ssl == NULL || ssl->conf == NULL ) |
| 5631 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5633 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5634 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5635 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5636 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5637 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5638 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5639 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5640 | #endif |
| 5641 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5642 | return( ret ); |
| 5643 | } |
| 5644 | |
| 5645 | /* |
| 5646 | * Perform the SSL handshake |
| 5647 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5648 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5649 | { |
| 5650 | int ret = 0; |
| 5651 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5652 | if( ssl == NULL || ssl->conf == NULL ) |
| 5653 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5655 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5657 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5658 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5659 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5660 | |
| 5661 | if( ret != 0 ) |
| 5662 | break; |
| 5663 | } |
| 5664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5665 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5666 | |
| 5667 | return( ret ); |
| 5668 | } |
| 5669 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5670 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5671 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5672 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5673 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5674 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5675 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5676 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5677 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5679 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5680 | |
| 5681 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5682 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5683 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5684 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5685 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5686 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5687 | 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] | 5688 | return( ret ); |
| 5689 | } |
| 5690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5691 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5692 | |
| 5693 | return( 0 ); |
| 5694 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5695 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5696 | |
| 5697 | /* |
| 5698 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5699 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 5700 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 5701 | * - 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] | 5702 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5703 | * 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] | 5704 | * 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] | 5705 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5706 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5707 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5708 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5710 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5711 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5712 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 5713 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5714 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5715 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 5716 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5717 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5718 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5719 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5720 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5721 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 5722 | ssl->handshake->out_msg_seq = 1; |
| 5723 | else |
| 5724 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5725 | } |
| 5726 | #endif |
| 5727 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5728 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 5729 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5730 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5731 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5732 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5733 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5734 | return( ret ); |
| 5735 | } |
| 5736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5737 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5738 | |
| 5739 | return( 0 ); |
| 5740 | } |
| 5741 | |
| 5742 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5743 | * Renegotiate current connection on client, |
| 5744 | * or request renegotiation on server |
| 5745 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5746 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5747 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5748 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5749 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5750 | if( ssl == NULL || ssl->conf == NULL ) |
| 5751 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5753 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5754 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5755 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5756 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5757 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5758 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5759 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5760 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5761 | |
| 5762 | /* Did we already try/start sending HelloRequest? */ |
| 5763 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5764 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5765 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5766 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5767 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5768 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5769 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5770 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5771 | /* |
| 5772 | * On client, either start the renegotiation process or, |
| 5773 | * if already in progress, continue the handshake |
| 5774 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5775 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5776 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5777 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5778 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5779 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5780 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5781 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5782 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5783 | return( ret ); |
| 5784 | } |
| 5785 | } |
| 5786 | else |
| 5787 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5788 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5789 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5790 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5791 | return( ret ); |
| 5792 | } |
| 5793 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5794 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5795 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 5796 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5797 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5798 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5800 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5801 | 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] | 5802 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5803 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5804 | |
| 5805 | while( cur != NULL ) |
| 5806 | { |
| 5807 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5808 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5809 | cur = next; |
| 5810 | } |
| 5811 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5812 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5813 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5814 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5815 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5816 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 5817 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5818 | if( handshake == NULL ) |
| 5819 | return; |
| 5820 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5821 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 5822 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 5823 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 5824 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5825 | handshake->async_in_progress = 0; |
| 5826 | } |
| 5827 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 5828 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5829 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 5830 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 5831 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 5832 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 5833 | #endif |
| 5834 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5835 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5836 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5837 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 5838 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5839 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 5840 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5841 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5842 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5843 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 5844 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5845 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5846 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 5847 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5848 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5849 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5850 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5851 | #if defined(MBEDTLS_DHM_C) |
| 5852 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5853 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5854 | #if defined(MBEDTLS_ECDH_C) |
| 5855 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5856 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 5857 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 5858 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 5859 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5860 | mbedtls_free( handshake->ecjpake_cache ); |
| 5861 | handshake->ecjpake_cache = NULL; |
| 5862 | handshake->ecjpake_cache_len = 0; |
| 5863 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 5864 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5865 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 5866 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 5867 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 5868 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5869 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 5870 | #endif |
| 5871 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 5872 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5873 | if( handshake->psk != NULL ) |
| 5874 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5875 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5876 | mbedtls_free( handshake->psk ); |
| 5877 | } |
| 5878 | #endif |
| 5879 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5880 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5881 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5882 | /* |
| 5883 | * Free only the linked list wrapper, not the keys themselves |
| 5884 | * since the belong to the SNI callback |
| 5885 | */ |
| 5886 | if( handshake->sni_key_cert != NULL ) |
| 5887 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5888 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5889 | |
| 5890 | while( cur != NULL ) |
| 5891 | { |
| 5892 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5893 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5894 | cur = next; |
| 5895 | } |
| 5896 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5897 | #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] | 5898 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 5899 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 5900 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 5901 | if( handshake->ecrs_peer_cert != NULL ) |
| 5902 | { |
| 5903 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 5904 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 5905 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 5906 | #endif |
| 5907 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 5908 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5909 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5910 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 5911 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5912 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5913 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5914 | mbedtls_free( handshake->verify_cookie ); |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 5915 | mbedtls_ssl_flight_free( handshake->flight ); |
| 5916 | mbedtls_ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 5917 | #endif |
| 5918 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 5919 | #if defined(MBEDTLS_ECDH_C) && \ |
| 5920 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5921 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 5922 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 5923 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5924 | mbedtls_platform_zeroize( handshake, |
| 5925 | sizeof( mbedtls_ssl_handshake_params ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5926 | |
| 5927 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5928 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 5929 | * processes datagrams and the fact that a datagram is allowed to have |
| 5930 | * several records in it, it is possible that the I/O buffers are not |
| 5931 | * empty at this stage */ |
| 5932 | { |
| 5933 | int modified = 0; |
| 5934 | uint32_t buf_len = mbedtls_ssl_get_input_buflen( ssl ); |
| 5935 | size_t written_in = 0; |
| 5936 | size_t written_out = 0; |
| 5937 | if( ssl->in_buf != NULL && |
| 5938 | ssl->in_buf_len > buf_len && |
| 5939 | ssl->in_left < buf_len ) |
| 5940 | { |
| 5941 | written_in = ssl->in_msg - ssl->in_buf; |
| 5942 | if( resize_buffer( &ssl->in_buf, buf_len, &ssl->in_buf_len ) != 0 ) |
| 5943 | { |
| 5944 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 5945 | } |
| 5946 | else |
| 5947 | { |
| 5948 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %d", buf_len ) ); |
| 5949 | modified = 1; |
| 5950 | } |
| 5951 | } |
| 5952 | |
| 5953 | buf_len = mbedtls_ssl_get_output_buflen( ssl ); |
| 5954 | if( ssl->out_buf != NULL && |
| 5955 | ssl->out_buf_len > mbedtls_ssl_get_output_buflen( ssl ) && |
| 5956 | ssl->out_left < buf_len ) |
| 5957 | { |
| 5958 | written_out = ssl->out_msg - ssl->out_buf; |
| 5959 | if( resize_buffer( &ssl->out_buf, buf_len, &ssl->out_buf_len ) != 0 ) |
| 5960 | { |
| 5961 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 5962 | } |
| 5963 | else |
| 5964 | { |
| 5965 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %d", buf_len ) ); |
| 5966 | modified = 1; |
| 5967 | } |
| 5968 | } |
| 5969 | if( modified ) |
| 5970 | { |
| 5971 | /* Update pointers here to avoid doing it twice. */ |
| 5972 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 5973 | /* Fields below might not be properly updated with record |
| 5974 | * splitting, so they are manually updated here. */ |
| 5975 | ssl->out_msg = ssl->out_buf + written_out; |
| 5976 | ssl->in_msg = ssl->in_buf + written_in; |
| 5977 | } |
| 5978 | } |
| 5979 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5980 | } |
| 5981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5982 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5983 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5984 | if( session == NULL ) |
| 5985 | return; |
| 5986 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5987 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 5988 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 5989 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 5990 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 5991 | #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] | 5992 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 5993 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 5994 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5995 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 5998 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5999 | |
| 6000 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6001 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 6002 | #else |
| 6003 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 6004 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6005 | |
| 6006 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 6007 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
| 6008 | #else |
| 6009 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u |
| 6010 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 6011 | |
| 6012 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6013 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 6014 | #else |
| 6015 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 6016 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6017 | |
| 6018 | #if defined(MBEDTLS_SSL_ALPN) |
| 6019 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 6020 | #else |
| 6021 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 6022 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6023 | |
| 6024 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 6025 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 6026 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 6027 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 6028 | |
| 6029 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 6030 | ( (uint32_t) ( \ |
| 6031 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 6032 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 6033 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 6034 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 6035 | 0u ) ) |
| 6036 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6037 | static unsigned char ssl_serialized_context_header[] = { |
| 6038 | MBEDTLS_VERSION_MAJOR, |
| 6039 | MBEDTLS_VERSION_MINOR, |
| 6040 | MBEDTLS_VERSION_PATCH, |
| 6041 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 6042 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 6043 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF, |
| 6044 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 6045 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6046 | }; |
| 6047 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6048 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6049 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 6050 | * |
| 6051 | * The format of the serialized data is: |
| 6052 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 6053 | * |
| 6054 | * // header |
| 6055 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6056 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 6057 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6058 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 6059 | * Note: When updating the format, remember to keep these |
| 6060 | * 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] | 6061 | * |
| 6062 | * // session sub-structure |
| 6063 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 6064 | * // transform sub-structure |
| 6065 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 6066 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 6067 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 6068 | * // fields from ssl_context |
| 6069 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 6070 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 6071 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 6072 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 6073 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 6074 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 6075 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 6076 | * |
| 6077 | * Note that many fields of the ssl_context or sub-structures are not |
| 6078 | * serialized, as they fall in one of the following categories: |
| 6079 | * |
| 6080 | * 1. forced value (eg in_left must be 0) |
| 6081 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 6082 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 6083 | * 4. value was temporary (eg content of input buffer) |
| 6084 | * 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] | 6085 | */ |
| 6086 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 6087 | unsigned char *buf, |
| 6088 | size_t buf_len, |
| 6089 | size_t *olen ) |
| 6090 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6091 | unsigned char *p = buf; |
| 6092 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6093 | size_t session_len; |
| 6094 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6095 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6096 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6097 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 6098 | * this function's documentation. |
| 6099 | * |
| 6100 | * These are due to assumptions/limitations in the implementation. Some of |
| 6101 | * them are likely to stay (no handshake in progress) some might go away |
| 6102 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6103 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6104 | /* The initial handshake must be over */ |
| 6105 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6106 | { |
| 6107 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6109 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6110 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6111 | { |
| 6112 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6113 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6114 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6115 | /* Double-check that sub-structures are indeed ready */ |
| 6116 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6117 | { |
| 6118 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6119 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6120 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6121 | /* There must be no pending incoming or outgoing data */ |
| 6122 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6123 | { |
| 6124 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6125 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6126 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6127 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6128 | { |
| 6129 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6130 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6131 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6132 | /* Protocol must be DLTS, not TLS */ |
| 6133 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6134 | { |
| 6135 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6136 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6137 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6138 | /* Version must be 1.2 */ |
| 6139 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6140 | { |
| 6141 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6142 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6143 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6144 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6145 | { |
| 6146 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6147 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6148 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6149 | /* We must be using an AEAD ciphersuite */ |
| 6150 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6151 | { |
| 6152 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6153 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6154 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6155 | /* Renegotiation must not be enabled */ |
| 6156 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6157 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6158 | { |
| 6159 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6160 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6161 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6162 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6163 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6164 | /* |
| 6165 | * Version and format identifier |
| 6166 | */ |
| 6167 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6168 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6169 | if( used <= buf_len ) |
| 6170 | { |
| 6171 | memcpy( p, ssl_serialized_context_header, |
| 6172 | sizeof( ssl_serialized_context_header ) ); |
| 6173 | p += sizeof( ssl_serialized_context_header ); |
| 6174 | } |
| 6175 | |
| 6176 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6177 | * Session (length + data) |
| 6178 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6179 | 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] | 6180 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 6181 | return( ret ); |
| 6182 | |
| 6183 | used += 4 + session_len; |
| 6184 | if( used <= buf_len ) |
| 6185 | { |
| 6186 | *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF ); |
| 6187 | *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF ); |
| 6188 | *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF ); |
| 6189 | *p++ = (unsigned char)( ( session_len ) & 0xFF ); |
| 6190 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6191 | ret = ssl_session_save( ssl->session, 1, |
| 6192 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6193 | if( ret != 0 ) |
| 6194 | return( ret ); |
| 6195 | |
| 6196 | p += session_len; |
| 6197 | } |
| 6198 | |
| 6199 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6200 | * Transform |
| 6201 | */ |
| 6202 | used += sizeof( ssl->transform->randbytes ); |
| 6203 | if( used <= buf_len ) |
| 6204 | { |
| 6205 | memcpy( p, ssl->transform->randbytes, |
| 6206 | sizeof( ssl->transform->randbytes ) ); |
| 6207 | p += sizeof( ssl->transform->randbytes ); |
| 6208 | } |
| 6209 | |
| 6210 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6211 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 6212 | if( used <= buf_len ) |
| 6213 | { |
| 6214 | *p++ = ssl->transform->in_cid_len; |
| 6215 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 6216 | p += ssl->transform->in_cid_len; |
| 6217 | |
| 6218 | *p++ = ssl->transform->out_cid_len; |
| 6219 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 6220 | p += ssl->transform->out_cid_len; |
| 6221 | } |
| 6222 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6223 | |
| 6224 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6225 | * Saved fields from top-level ssl_context structure |
| 6226 | */ |
| 6227 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 6228 | used += 4; |
| 6229 | if( used <= buf_len ) |
| 6230 | { |
| 6231 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF ); |
| 6232 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF ); |
| 6233 | *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF ); |
| 6234 | *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF ); |
| 6235 | } |
| 6236 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 6237 | |
| 6238 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6239 | used += 16; |
| 6240 | if( used <= buf_len ) |
| 6241 | { |
| 6242 | *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF ); |
| 6243 | *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF ); |
| 6244 | *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF ); |
| 6245 | *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF ); |
| 6246 | *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF ); |
| 6247 | *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF ); |
| 6248 | *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF ); |
| 6249 | *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF ); |
| 6250 | |
| 6251 | *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF ); |
| 6252 | *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF ); |
| 6253 | *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF ); |
| 6254 | *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF ); |
| 6255 | *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF ); |
| 6256 | *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF ); |
| 6257 | *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF ); |
| 6258 | *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF ); |
| 6259 | } |
| 6260 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6261 | |
| 6262 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6263 | used += 1; |
| 6264 | if( used <= buf_len ) |
| 6265 | { |
| 6266 | *p++ = ssl->disable_datagram_packing; |
| 6267 | } |
| 6268 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6269 | |
| 6270 | used += 8; |
| 6271 | if( used <= buf_len ) |
| 6272 | { |
| 6273 | memcpy( p, ssl->cur_out_ctr, 8 ); |
| 6274 | p += 8; |
| 6275 | } |
| 6276 | |
| 6277 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6278 | used += 2; |
| 6279 | if( used <= buf_len ) |
| 6280 | { |
| 6281 | *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF ); |
| 6282 | *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF ); |
| 6283 | } |
| 6284 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6285 | |
| 6286 | #if defined(MBEDTLS_SSL_ALPN) |
| 6287 | { |
| 6288 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 6289 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6290 | : 0; |
| 6291 | |
| 6292 | used += 1 + alpn_len; |
| 6293 | if( used <= buf_len ) |
| 6294 | { |
| 6295 | *p++ = alpn_len; |
| 6296 | |
| 6297 | if( ssl->alpn_chosen != NULL ) |
| 6298 | { |
| 6299 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 6300 | p += alpn_len; |
| 6301 | } |
| 6302 | } |
| 6303 | } |
| 6304 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6305 | |
| 6306 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6307 | * Done |
| 6308 | */ |
| 6309 | *olen = used; |
| 6310 | |
| 6311 | if( used > buf_len ) |
| 6312 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6313 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6314 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 6315 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6316 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6317 | } |
| 6318 | |
| 6319 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6320 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 6321 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 6322 | */ |
| 6323 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 6324 | const char *label, |
| 6325 | const unsigned char *random, size_t rlen, |
| 6326 | unsigned char *dstbuf, size_t dlen ); |
| 6327 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 6328 | { |
Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 6329 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6330 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6331 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 6332 | |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6333 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 6334 | return( tls_prf_sha384 ); |
Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 6335 | #else |
| 6336 | (void) ciphersuite_id; |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6337 | #endif |
| 6338 | return( tls_prf_sha256 ); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6339 | } |
| 6340 | |
| 6341 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6342 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6343 | * |
| 6344 | * This internal version is wrapped by a public function that cleans up in |
| 6345 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6346 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6347 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 6348 | const unsigned char *buf, |
| 6349 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6350 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6351 | const unsigned char *p = buf; |
| 6352 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6353 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6354 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6355 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6356 | /* |
| 6357 | * The context should have been freshly setup or reset. |
| 6358 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 6359 | * (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] | 6360 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 6361 | */ |
| 6362 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 6363 | ssl->session != NULL ) |
| 6364 | { |
| 6365 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6366 | } |
| 6367 | |
| 6368 | /* |
| 6369 | * We can't check that the config matches the initial one, but we can at |
| 6370 | * least check it matches the requirements for serializing. |
| 6371 | */ |
| 6372 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 6373 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 6374 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 6375 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || |
| 6376 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || |
| 6377 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6378 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6379 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6380 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6381 | { |
| 6382 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6383 | } |
| 6384 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6385 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 6386 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6387 | /* |
| 6388 | * Check version identifier |
| 6389 | */ |
| 6390 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 6391 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6392 | |
| 6393 | if( memcmp( p, ssl_serialized_context_header, |
| 6394 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 6395 | { |
| 6396 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 6397 | } |
| 6398 | p += sizeof( ssl_serialized_context_header ); |
| 6399 | |
| 6400 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6401 | * Session |
| 6402 | */ |
| 6403 | if( (size_t)( end - p ) < 4 ) |
| 6404 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6405 | |
| 6406 | session_len = ( (size_t) p[0] << 24 ) | |
| 6407 | ( (size_t) p[1] << 16 ) | |
| 6408 | ( (size_t) p[2] << 8 ) | |
| 6409 | ( (size_t) p[3] ); |
| 6410 | p += 4; |
| 6411 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6412 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6413 | * 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] | 6414 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6415 | ssl->session_in = ssl->session; |
| 6416 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6417 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6418 | |
| 6419 | if( (size_t)( end - p ) < session_len ) |
| 6420 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6421 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6422 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6423 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6424 | { |
| 6425 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6426 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6427 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6428 | |
| 6429 | p += session_len; |
| 6430 | |
| 6431 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6432 | * Transform |
| 6433 | */ |
| 6434 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6435 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6436 | * 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] | 6437 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6438 | ssl->transform_in = ssl->transform; |
| 6439 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6440 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6441 | |
| 6442 | /* Read random bytes and populate structure */ |
| 6443 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 6444 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6445 | |
| 6446 | ret = ssl_populate_transform( ssl->transform, |
| 6447 | ssl->session->ciphersuite, |
| 6448 | ssl->session->master, |
| 6449 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 6450 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6451 | ssl->session->encrypt_then_mac, |
| 6452 | #endif |
| 6453 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 6454 | ssl->session->trunc_hmac, |
| 6455 | #endif |
| 6456 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 6457 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 6458 | ssl->session->compression, |
| 6459 | #endif |
| 6460 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 6461 | p, /* currently pointing to randbytes */ |
| 6462 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 6463 | ssl->conf->endpoint, |
| 6464 | ssl ); |
| 6465 | if( ret != 0 ) |
| 6466 | return( ret ); |
| 6467 | |
| 6468 | p += sizeof( ssl->transform->randbytes ); |
| 6469 | |
| 6470 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6471 | /* Read connection IDs and store them */ |
| 6472 | if( (size_t)( end - p ) < 1 ) |
| 6473 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6474 | |
| 6475 | ssl->transform->in_cid_len = *p++; |
| 6476 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 6477 | 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] | 6478 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6479 | |
| 6480 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 6481 | p += ssl->transform->in_cid_len; |
| 6482 | |
| 6483 | ssl->transform->out_cid_len = *p++; |
| 6484 | |
| 6485 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 6486 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6487 | |
| 6488 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 6489 | p += ssl->transform->out_cid_len; |
| 6490 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6491 | |
| 6492 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6493 | * Saved fields from top-level ssl_context structure |
| 6494 | */ |
| 6495 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
| 6496 | if( (size_t)( end - p ) < 4 ) |
| 6497 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6498 | |
| 6499 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 6500 | ( (uint32_t) p[1] << 16 ) | |
| 6501 | ( (uint32_t) p[2] << 8 ) | |
| 6502 | ( (uint32_t) p[3] ); |
| 6503 | p += 4; |
| 6504 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ |
| 6505 | |
| 6506 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6507 | if( (size_t)( end - p ) < 16 ) |
| 6508 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6509 | |
| 6510 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 6511 | ( (uint64_t) p[1] << 48 ) | |
| 6512 | ( (uint64_t) p[2] << 40 ) | |
| 6513 | ( (uint64_t) p[3] << 32 ) | |
| 6514 | ( (uint64_t) p[4] << 24 ) | |
| 6515 | ( (uint64_t) p[5] << 16 ) | |
| 6516 | ( (uint64_t) p[6] << 8 ) | |
| 6517 | ( (uint64_t) p[7] ); |
| 6518 | p += 8; |
| 6519 | |
| 6520 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 6521 | ( (uint64_t) p[1] << 48 ) | |
| 6522 | ( (uint64_t) p[2] << 40 ) | |
| 6523 | ( (uint64_t) p[3] << 32 ) | |
| 6524 | ( (uint64_t) p[4] << 24 ) | |
| 6525 | ( (uint64_t) p[5] << 16 ) | |
| 6526 | ( (uint64_t) p[6] << 8 ) | |
| 6527 | ( (uint64_t) p[7] ); |
| 6528 | p += 8; |
| 6529 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6530 | |
| 6531 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6532 | if( (size_t)( end - p ) < 1 ) |
| 6533 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6534 | |
| 6535 | ssl->disable_datagram_packing = *p++; |
| 6536 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6537 | |
| 6538 | if( (size_t)( end - p ) < 8 ) |
| 6539 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6540 | |
| 6541 | memcpy( ssl->cur_out_ctr, p, 8 ); |
| 6542 | p += 8; |
| 6543 | |
| 6544 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6545 | if( (size_t)( end - p ) < 2 ) |
| 6546 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6547 | |
| 6548 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 6549 | p += 2; |
| 6550 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6551 | |
| 6552 | #if defined(MBEDTLS_SSL_ALPN) |
| 6553 | { |
| 6554 | uint8_t alpn_len; |
| 6555 | const char **cur; |
| 6556 | |
| 6557 | if( (size_t)( end - p ) < 1 ) |
| 6558 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6559 | |
| 6560 | alpn_len = *p++; |
| 6561 | |
| 6562 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 6563 | { |
| 6564 | /* alpn_chosen should point to an item in the configured list */ |
| 6565 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 6566 | { |
| 6567 | if( strlen( *cur ) == alpn_len && |
| 6568 | memcmp( p, cur, alpn_len ) == 0 ) |
| 6569 | { |
| 6570 | ssl->alpn_chosen = *cur; |
| 6571 | break; |
| 6572 | } |
| 6573 | } |
| 6574 | } |
| 6575 | |
| 6576 | /* can only happen on conf mismatch */ |
| 6577 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 6578 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6579 | |
| 6580 | p += alpn_len; |
| 6581 | } |
| 6582 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6583 | |
| 6584 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6585 | * Forced fields from top-level ssl_context structure |
| 6586 | * |
| 6587 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 6588 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 6589 | */ |
| 6590 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 6591 | |
| 6592 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6593 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 6594 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6595 | /* Adjust pointers for header fields of outgoing records to |
| 6596 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 6597 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6598 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6599 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6600 | ssl->in_epoch = 1; |
| 6601 | #endif |
| 6602 | |
| 6603 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 6604 | * 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] | 6605 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 6606 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6607 | if( ssl->handshake != NULL ) |
| 6608 | { |
| 6609 | mbedtls_ssl_handshake_free( ssl ); |
| 6610 | mbedtls_free( ssl->handshake ); |
| 6611 | ssl->handshake = NULL; |
| 6612 | } |
| 6613 | |
| 6614 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6615 | * Done - should have consumed entire buffer |
| 6616 | */ |
| 6617 | if( p != end ) |
| 6618 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6619 | |
| 6620 | return( 0 ); |
| 6621 | } |
| 6622 | |
| 6623 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6624 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6625 | */ |
| 6626 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 6627 | const unsigned char *buf, |
| 6628 | size_t len ) |
| 6629 | { |
| 6630 | int ret = ssl_context_load( context, buf, len ); |
| 6631 | |
| 6632 | if( ret != 0 ) |
| 6633 | mbedtls_ssl_free( context ); |
| 6634 | |
| 6635 | return( ret ); |
| 6636 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 6637 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6638 | |
| 6639 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6640 | * Free an SSL context |
| 6641 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6642 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6643 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6644 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6645 | size_t in_buf_len = ssl->in_buf_len; |
| 6646 | size_t out_buf_len = ssl->out_buf_len; |
| 6647 | #else |
| 6648 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 6649 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 6650 | #endif |
| 6651 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6652 | if( ssl == NULL ) |
| 6653 | return; |
| 6654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6655 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6656 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6657 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6658 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6659 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6660 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6661 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6664 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6665 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6666 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6667 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6668 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6669 | } |
| 6670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6671 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 6672 | if( ssl->compress_buf != NULL ) |
| 6673 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6674 | 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] | 6675 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 6676 | } |
| 6677 | #endif |
| 6678 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6679 | if( ssl->transform ) |
| 6680 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6681 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6682 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
| 6685 | if( ssl->handshake ) |
| 6686 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6687 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6688 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 6689 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6691 | mbedtls_free( ssl->handshake ); |
| 6692 | mbedtls_free( ssl->transform_negotiate ); |
| 6693 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6694 | } |
| 6695 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6696 | if( ssl->session ) |
| 6697 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6698 | mbedtls_ssl_session_free( ssl->session ); |
| 6699 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6700 | } |
| 6701 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 6702 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 6703 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6704 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6705 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6706 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6707 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 6708 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6710 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 6711 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 6712 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6713 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 6714 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 6715 | } |
| 6716 | #endif |
| 6717 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6718 | #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] | 6719 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 6720 | #endif |
| 6721 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6722 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 6723 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 6724 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6725 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6726 | } |
| 6727 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6728 | /* |
| 6729 | * Initialze mbedtls_ssl_config |
| 6730 | */ |
| 6731 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 6732 | { |
| 6733 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 6734 | } |
| 6735 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6736 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6737 | static int ssl_preset_default_hashes[] = { |
| 6738 | #if defined(MBEDTLS_SHA512_C) |
| 6739 | MBEDTLS_MD_SHA512, |
| 6740 | MBEDTLS_MD_SHA384, |
| 6741 | #endif |
| 6742 | #if defined(MBEDTLS_SHA256_C) |
| 6743 | MBEDTLS_MD_SHA256, |
| 6744 | MBEDTLS_MD_SHA224, |
| 6745 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 6746 | #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] | 6747 | MBEDTLS_MD_SHA1, |
| 6748 | #endif |
| 6749 | MBEDTLS_MD_NONE |
| 6750 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 6751 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6752 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6753 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 6754 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 6755 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 6756 | 0 |
| 6757 | }; |
| 6758 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6759 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6760 | static int ssl_preset_suiteb_hashes[] = { |
| 6761 | MBEDTLS_MD_SHA256, |
| 6762 | MBEDTLS_MD_SHA384, |
| 6763 | MBEDTLS_MD_NONE |
| 6764 | }; |
| 6765 | #endif |
| 6766 | |
| 6767 | #if defined(MBEDTLS_ECP_C) |
| 6768 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6769 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6770 | MBEDTLS_ECP_DP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6771 | #endif |
| 6772 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6773 | MBEDTLS_ECP_DP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6774 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6775 | MBEDTLS_ECP_DP_NONE |
| 6776 | }; |
| 6777 | #endif |
| 6778 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6779 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 6780 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6781 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6782 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6783 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6784 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6785 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6786 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6787 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6788 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 6789 | /* Use the functions here so that they are covered in tests, |
| 6790 | * but otherwise access member directly for efficiency */ |
| 6791 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 6792 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6793 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6794 | /* |
| 6795 | * Things that are common to all presets |
| 6796 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6797 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6798 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6799 | { |
| 6800 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 6801 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 6802 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 6803 | #endif |
| 6804 | } |
| 6805 | #endif |
| 6806 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 6807 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6808 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 6809 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6810 | |
| 6811 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6812 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 6813 | #endif |
| 6814 | |
| 6815 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6816 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 6817 | #endif |
| 6818 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 6819 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 6820 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 6821 | #endif |
| 6822 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6823 | #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] | 6824 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 6825 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 6826 | #endif |
| 6827 | |
| 6828 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6829 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 6830 | #endif |
| 6831 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6832 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6833 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
| 6834 | #endif |
| 6835 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6836 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6837 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 6838 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 6839 | #endif |
| 6840 | |
| 6841 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6842 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 6843 | memset( conf->renego_period, 0x00, 2 ); |
| 6844 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6845 | #endif |
| 6846 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6847 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 6848 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6849 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 6850 | const unsigned char dhm_p[] = |
| 6851 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 6852 | const unsigned char dhm_g[] = |
| 6853 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 6854 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 6855 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 6856 | dhm_p, sizeof( dhm_p ), |
| 6857 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6858 | { |
| 6859 | return( ret ); |
| 6860 | } |
| 6861 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 6862 | #endif |
| 6863 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6864 | /* |
| 6865 | * Preset-specific defaults |
| 6866 | */ |
| 6867 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6868 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6869 | /* |
| 6870 | * NSA Suite B |
| 6871 | */ |
| 6872 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 6873 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6874 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 6875 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6876 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6877 | |
| 6878 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 6879 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 6880 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 6881 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 6882 | ssl_preset_suiteb_ciphersuites; |
| 6883 | |
| 6884 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6885 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6886 | #endif |
| 6887 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6888 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6889 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 6890 | #endif |
| 6891 | |
| 6892 | #if defined(MBEDTLS_ECP_C) |
| 6893 | conf->curve_list = ssl_preset_suiteb_curves; |
| 6894 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 6895 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6896 | |
| 6897 | /* |
| 6898 | * Default |
| 6899 | */ |
| 6900 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 6901 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 6902 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 6903 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 6904 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 6905 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 6906 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 6907 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 6908 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6909 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6910 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6911 | |
| 6912 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6913 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6914 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 6915 | #endif |
| 6916 | |
| 6917 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 6918 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 6919 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 6920 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 6921 | mbedtls_ssl_list_ciphersuites(); |
| 6922 | |
| 6923 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6924 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 6925 | #endif |
| 6926 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6927 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6928 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6929 | #endif |
| 6930 | |
| 6931 | #if defined(MBEDTLS_ECP_C) |
| 6932 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 6933 | #endif |
| 6934 | |
| 6935 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 6936 | conf->dhm_min_bitlen = 1024; |
| 6937 | #endif |
| 6938 | } |
| 6939 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6940 | return( 0 ); |
| 6941 | } |
| 6942 | |
| 6943 | /* |
| 6944 | * Free mbedtls_ssl_config |
| 6945 | */ |
| 6946 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 6947 | { |
| 6948 | #if defined(MBEDTLS_DHM_C) |
| 6949 | mbedtls_mpi_free( &conf->dhm_P ); |
| 6950 | mbedtls_mpi_free( &conf->dhm_G ); |
| 6951 | #endif |
| 6952 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6953 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6954 | if( conf->psk != NULL ) |
| 6955 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6956 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6957 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6958 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6959 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 6960 | } |
| 6961 | |
| 6962 | if( conf->psk_identity != NULL ) |
| 6963 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6964 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 6965 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6966 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6967 | conf->psk_identity_len = 0; |
| 6968 | } |
| 6969 | #endif |
| 6970 | |
| 6971 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6972 | ssl_key_cert_free( conf->key_cert ); |
| 6973 | #endif |
| 6974 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6975 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6976 | } |
| 6977 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 6978 | #if defined(MBEDTLS_PK_C) && \ |
| 6979 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6980 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6981 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6982 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6983 | 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] | 6984 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6985 | #if defined(MBEDTLS_RSA_C) |
| 6986 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 6987 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6988 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6989 | #if defined(MBEDTLS_ECDSA_C) |
| 6990 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 6991 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6992 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6993 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6994 | } |
| 6995 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6996 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 6997 | { |
| 6998 | switch( type ) { |
| 6999 | case MBEDTLS_PK_RSA: |
| 7000 | return( MBEDTLS_SSL_SIG_RSA ); |
| 7001 | case MBEDTLS_PK_ECDSA: |
| 7002 | case MBEDTLS_PK_ECKEY: |
| 7003 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 7004 | default: |
| 7005 | return( MBEDTLS_SSL_SIG_ANON ); |
| 7006 | } |
| 7007 | } |
| 7008 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7009 | 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] | 7010 | { |
| 7011 | switch( sig ) |
| 7012 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7013 | #if defined(MBEDTLS_RSA_C) |
| 7014 | case MBEDTLS_SSL_SIG_RSA: |
| 7015 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7016 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7017 | #if defined(MBEDTLS_ECDSA_C) |
| 7018 | case MBEDTLS_SSL_SIG_ECDSA: |
| 7019 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7020 | #endif |
| 7021 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7022 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7023 | } |
| 7024 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 7025 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7026 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7027 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7028 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7029 | |
| 7030 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 7031 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 7032 | mbedtls_pk_type_t sig_alg ) |
| 7033 | { |
| 7034 | switch( sig_alg ) |
| 7035 | { |
| 7036 | case MBEDTLS_PK_RSA: |
| 7037 | return( set->rsa ); |
| 7038 | case MBEDTLS_PK_ECDSA: |
| 7039 | return( set->ecdsa ); |
| 7040 | default: |
| 7041 | return( MBEDTLS_MD_NONE ); |
| 7042 | } |
| 7043 | } |
| 7044 | |
| 7045 | /* Add a signature-hash-pair to a signature-hash set */ |
| 7046 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 7047 | mbedtls_pk_type_t sig_alg, |
| 7048 | mbedtls_md_type_t md_alg ) |
| 7049 | { |
| 7050 | switch( sig_alg ) |
| 7051 | { |
| 7052 | case MBEDTLS_PK_RSA: |
| 7053 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 7054 | set->rsa = md_alg; |
| 7055 | break; |
| 7056 | |
| 7057 | case MBEDTLS_PK_ECDSA: |
| 7058 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 7059 | set->ecdsa = md_alg; |
| 7060 | break; |
| 7061 | |
| 7062 | default: |
| 7063 | break; |
| 7064 | } |
| 7065 | } |
| 7066 | |
| 7067 | /* Allow exactly one hash algorithm for each signature. */ |
| 7068 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 7069 | mbedtls_md_type_t md_alg ) |
| 7070 | { |
| 7071 | set->rsa = md_alg; |
| 7072 | set->ecdsa = md_alg; |
| 7073 | } |
| 7074 | |
| 7075 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7076 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7077 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 7078 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7079 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 7080 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7081 | 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] | 7082 | { |
| 7083 | switch( hash ) |
| 7084 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7085 | #if defined(MBEDTLS_MD5_C) |
| 7086 | case MBEDTLS_SSL_HASH_MD5: |
| 7087 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7088 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7089 | #if defined(MBEDTLS_SHA1_C) |
| 7090 | case MBEDTLS_SSL_HASH_SHA1: |
| 7091 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7092 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7093 | #if defined(MBEDTLS_SHA256_C) |
| 7094 | case MBEDTLS_SSL_HASH_SHA224: |
| 7095 | return( MBEDTLS_MD_SHA224 ); |
| 7096 | case MBEDTLS_SSL_HASH_SHA256: |
| 7097 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7098 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7099 | #if defined(MBEDTLS_SHA512_C) |
| 7100 | case MBEDTLS_SSL_HASH_SHA384: |
| 7101 | return( MBEDTLS_MD_SHA384 ); |
| 7102 | case MBEDTLS_SSL_HASH_SHA512: |
| 7103 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7104 | #endif |
| 7105 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7106 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7107 | } |
| 7108 | } |
| 7109 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7110 | /* |
| 7111 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 7112 | */ |
| 7113 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 7114 | { |
| 7115 | switch( md ) |
| 7116 | { |
| 7117 | #if defined(MBEDTLS_MD5_C) |
| 7118 | case MBEDTLS_MD_MD5: |
| 7119 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 7120 | #endif |
| 7121 | #if defined(MBEDTLS_SHA1_C) |
| 7122 | case MBEDTLS_MD_SHA1: |
| 7123 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 7124 | #endif |
| 7125 | #if defined(MBEDTLS_SHA256_C) |
| 7126 | case MBEDTLS_MD_SHA224: |
| 7127 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 7128 | case MBEDTLS_MD_SHA256: |
| 7129 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 7130 | #endif |
| 7131 | #if defined(MBEDTLS_SHA512_C) |
| 7132 | case MBEDTLS_MD_SHA384: |
| 7133 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 7134 | case MBEDTLS_MD_SHA512: |
| 7135 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 7136 | #endif |
| 7137 | default: |
| 7138 | return( MBEDTLS_SSL_HASH_NONE ); |
| 7139 | } |
| 7140 | } |
| 7141 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7142 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7143 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7144 | * 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] | 7145 | * 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] | 7146 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7147 | 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] | 7148 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7149 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7150 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7151 | if( ssl->conf->curve_list == NULL ) |
| 7152 | return( -1 ); |
| 7153 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7154 | 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] | 7155 | if( *gid == grp_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7156 | return( 0 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7157 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7158 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7159 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7160 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7161 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7162 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7163 | /* |
| 7164 | * Check if a hash proposed by the peer is in our list. |
| 7165 | * Return 0 if we're willing to use it, -1 otherwise. |
| 7166 | */ |
| 7167 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 7168 | mbedtls_md_type_t md ) |
| 7169 | { |
| 7170 | const int *cur; |
| 7171 | |
| 7172 | if( ssl->conf->sig_hashes == NULL ) |
| 7173 | return( -1 ); |
| 7174 | |
| 7175 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 7176 | if( *cur == (int) md ) |
| 7177 | return( 0 ); |
| 7178 | |
| 7179 | return( -1 ); |
| 7180 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7181 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7183 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7184 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 7185 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7186 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7187 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7188 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7189 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7190 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7191 | int usage = 0; |
| 7192 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7193 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7194 | const char *ext_oid; |
| 7195 | size_t ext_len; |
| 7196 | #endif |
| 7197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7198 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 7199 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7200 | ((void) cert); |
| 7201 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7202 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7203 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7205 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 7206 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7207 | { |
| 7208 | /* Server part of the key exchange */ |
| 7209 | switch( ciphersuite->key_exchange ) |
| 7210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7211 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 7212 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7213 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7214 | break; |
| 7215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7216 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 7217 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 7218 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 7219 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7220 | break; |
| 7221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7222 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 7223 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7224 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7225 | break; |
| 7226 | |
| 7227 | /* 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] | 7228 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 7229 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 7230 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 7231 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 7232 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7233 | usage = 0; |
| 7234 | } |
| 7235 | } |
| 7236 | else |
| 7237 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7238 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 7239 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7240 | } |
| 7241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7242 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7243 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7244 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7245 | ret = -1; |
| 7246 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7247 | #else |
| 7248 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7249 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7251 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 7252 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7254 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 7255 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7256 | } |
| 7257 | else |
| 7258 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7259 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 7260 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7261 | } |
| 7262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7263 | 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] | 7264 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7265 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7266 | ret = -1; |
| 7267 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7268 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7269 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7270 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7271 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7272 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 7273 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7274 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 7275 | { |
| 7276 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7277 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 7278 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 7279 | |
| 7280 | switch( md ) |
| 7281 | { |
| 7282 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7283 | #if defined(MBEDTLS_MD5_C) |
| 7284 | case MBEDTLS_SSL_HASH_MD5: |
Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 7285 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7286 | #endif |
| 7287 | #if defined(MBEDTLS_SHA1_C) |
| 7288 | case MBEDTLS_SSL_HASH_SHA1: |
| 7289 | ssl->handshake->calc_verify = ssl_calc_verify_tls; |
| 7290 | break; |
| 7291 | #endif |
| 7292 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 7293 | #if defined(MBEDTLS_SHA512_C) |
| 7294 | case MBEDTLS_SSL_HASH_SHA384: |
| 7295 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 7296 | break; |
| 7297 | #endif |
| 7298 | #if defined(MBEDTLS_SHA256_C) |
| 7299 | case MBEDTLS_SSL_HASH_SHA256: |
| 7300 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7301 | break; |
| 7302 | #endif |
| 7303 | default: |
| 7304 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 7305 | } |
| 7306 | |
| 7307 | return 0; |
| 7308 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7309 | (void) ssl; |
| 7310 | (void) md; |
| 7311 | |
| 7312 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 7313 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7314 | } |
| 7315 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7316 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7317 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7318 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 7319 | unsigned char *output, |
| 7320 | unsigned char *data, size_t data_len ) |
| 7321 | { |
| 7322 | int ret = 0; |
| 7323 | mbedtls_md5_context mbedtls_md5; |
| 7324 | mbedtls_sha1_context mbedtls_sha1; |
| 7325 | |
| 7326 | mbedtls_md5_init( &mbedtls_md5 ); |
| 7327 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 7328 | |
| 7329 | /* |
| 7330 | * digitally-signed struct { |
| 7331 | * opaque md5_hash[16]; |
| 7332 | * opaque sha_hash[20]; |
| 7333 | * }; |
| 7334 | * |
| 7335 | * md5_hash |
| 7336 | * MD5(ClientHello.random + ServerHello.random |
| 7337 | * + ServerParams); |
| 7338 | * sha_hash |
| 7339 | * SHA(ClientHello.random + ServerHello.random |
| 7340 | * + ServerParams); |
| 7341 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7342 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7343 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7344 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7345 | goto exit; |
| 7346 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7347 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7348 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7349 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7350 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7351 | goto exit; |
| 7352 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7353 | 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] | 7354 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7355 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7356 | goto exit; |
| 7357 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7358 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7359 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7360 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7361 | goto exit; |
| 7362 | } |
| 7363 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7364 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7365 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7366 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7367 | goto exit; |
| 7368 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7369 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7370 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7371 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7372 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7373 | goto exit; |
| 7374 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7375 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7376 | data_len ) ) != 0 ) |
| 7377 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7378 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7379 | goto exit; |
| 7380 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7381 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7382 | output + 16 ) ) != 0 ) |
| 7383 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7384 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7385 | goto exit; |
| 7386 | } |
| 7387 | |
| 7388 | exit: |
| 7389 | mbedtls_md5_free( &mbedtls_md5 ); |
| 7390 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 7391 | |
| 7392 | if( ret != 0 ) |
| 7393 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7394 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7395 | |
| 7396 | return( ret ); |
| 7397 | |
| 7398 | } |
| 7399 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 7400 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 7401 | |
| 7402 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 7403 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7404 | |
| 7405 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7406 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7407 | unsigned char *hash, size_t *hashlen, |
| 7408 | unsigned char *data, size_t data_len, |
| 7409 | mbedtls_md_type_t md_alg ) |
| 7410 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7411 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7412 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7413 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 7414 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7415 | 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] | 7416 | |
| 7417 | if( ( status = psa_hash_setup( &hash_operation, |
| 7418 | hash_alg ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7419 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7420 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7421 | goto exit; |
| 7422 | } |
| 7423 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7424 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 7425 | 64 ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7426 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7427 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7428 | goto exit; |
| 7429 | } |
| 7430 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7431 | if( ( status = psa_hash_update( &hash_operation, |
| 7432 | data, data_len ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7433 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7434 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7435 | goto exit; |
| 7436 | } |
| 7437 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7438 | if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, |
| 7439 | hashlen ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7440 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7441 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7442 | goto exit; |
| 7443 | } |
| 7444 | |
| 7445 | exit: |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7446 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7447 | { |
| 7448 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7449 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7450 | switch( status ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7451 | { |
| 7452 | case PSA_ERROR_NOT_SUPPORTED: |
| 7453 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7454 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7455 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 7456 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 7457 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 7458 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 7459 | default: |
| 7460 | return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED ); |
| 7461 | } |
| 7462 | } |
| 7463 | return( 0 ); |
| 7464 | } |
| 7465 | |
| 7466 | #else |
| 7467 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7468 | 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] | 7469 | unsigned char *hash, size_t *hashlen, |
| 7470 | unsigned char *data, size_t data_len, |
| 7471 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7472 | { |
| 7473 | int ret = 0; |
| 7474 | mbedtls_md_context_t ctx; |
| 7475 | 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] | 7476 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7477 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7478 | 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] | 7479 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7480 | mbedtls_md_init( &ctx ); |
| 7481 | |
| 7482 | /* |
| 7483 | * digitally-signed struct { |
| 7484 | * opaque client_random[32]; |
| 7485 | * opaque server_random[32]; |
| 7486 | * ServerDHParams params; |
| 7487 | * }; |
| 7488 | */ |
| 7489 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7490 | { |
| 7491 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7492 | goto exit; |
| 7493 | } |
| 7494 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7495 | { |
| 7496 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7497 | goto exit; |
| 7498 | } |
| 7499 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7500 | { |
| 7501 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7502 | goto exit; |
| 7503 | } |
| 7504 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7505 | { |
| 7506 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7507 | goto exit; |
| 7508 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7509 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7510 | { |
| 7511 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7512 | goto exit; |
| 7513 | } |
| 7514 | |
| 7515 | exit: |
| 7516 | mbedtls_md_free( &ctx ); |
| 7517 | |
| 7518 | if( ret != 0 ) |
| 7519 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7520 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7521 | |
| 7522 | return( ret ); |
| 7523 | } |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7524 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7525 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7526 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 7527 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7529 | #endif /* MBEDTLS_SSL_TLS_C */ |