Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 client-side 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 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 32 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 34 | #define mbedtls_calloc calloc |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 36 | #endif |
| 37 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #include "mbedtls/debug.h" |
| 39 | #include "mbedtls/ssl.h" |
| 40 | #include "mbedtls/ssl_internal.h" |
| 41 | |
| 42 | #include <string.h> |
| 43 | |
Manuel Pégourié-Gonnard | 9386664 | 2015-06-22 19:21:23 +0200 | [diff] [blame] | 44 | #include <stdint.h> |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_HAVE_TIME) |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 47 | #include "mbedtls/platform_time.h" |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 48 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 51 | #include "mbedtls/platform_util.h" |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 52 | #endif |
| 53 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 55 | static void ssl_write_hostname_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 56 | unsigned char *buf, |
| 57 | size_t *olen ) |
| 58 | { |
| 59 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 60 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 61 | size_t hostname_len; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 62 | |
| 63 | *olen = 0; |
| 64 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 65 | if( ssl->hostname == NULL ) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 66 | return; |
| 67 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s", |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 69 | ssl->hostname ) ); |
| 70 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 71 | hostname_len = strlen( ssl->hostname ); |
| 72 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 73 | if( end < p || (size_t)( end - p ) < hostname_len + 9 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 74 | { |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 75 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 79 | /* |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 80 | * Sect. 3, RFC 6066 (TLS Extensions Definitions) |
| 81 | * |
| 82 | * In order to provide any of the server names, clients MAY include an |
| 83 | * extension of type "server_name" in the (extended) client hello. The |
| 84 | * "extension_data" field of this extension SHALL contain |
| 85 | * "ServerNameList" where: |
| 86 | * |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 87 | * struct { |
| 88 | * NameType name_type; |
| 89 | * select (name_type) { |
| 90 | * case host_name: HostName; |
| 91 | * } name; |
| 92 | * } ServerName; |
| 93 | * |
| 94 | * enum { |
| 95 | * host_name(0), (255) |
| 96 | * } NameType; |
| 97 | * |
| 98 | * opaque HostName<1..2^16-1>; |
| 99 | * |
| 100 | * struct { |
| 101 | * ServerName server_name_list<1..2^16-1> |
| 102 | * } ServerNameList; |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 103 | * |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 104 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME >> 8 ) & 0xFF ); |
| 106 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 107 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 108 | *p++ = (unsigned char)( ( (hostname_len + 5) >> 8 ) & 0xFF ); |
| 109 | *p++ = (unsigned char)( ( (hostname_len + 5) ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 111 | *p++ = (unsigned char)( ( (hostname_len + 3) >> 8 ) & 0xFF ); |
| 112 | *p++ = (unsigned char)( ( (hostname_len + 3) ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF ); |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 115 | *p++ = (unsigned char)( ( hostname_len >> 8 ) & 0xFF ); |
| 116 | *p++ = (unsigned char)( ( hostname_len ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 117 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 118 | memcpy( p, ssl->hostname, hostname_len ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 119 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 120 | *olen = hostname_len + 9; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 121 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 125 | static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 126 | unsigned char *buf, |
| 127 | size_t *olen ) |
| 128 | { |
| 129 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 130 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 131 | |
| 132 | *olen = 0; |
| 133 | |
Hanno Becker | 40f8b51 | 2017-10-12 14:58:55 +0100 | [diff] [blame] | 134 | /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the |
| 135 | * initial ClientHello, in which case also adding the renegotiation |
| 136 | * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 138 | return; |
| 139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 141 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 142 | if( end < p || (size_t)( end - p ) < 5 + ssl->verify_data_len ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 143 | { |
| 144 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 145 | return; |
| 146 | } |
| 147 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 148 | /* |
| 149 | * Secure renegotiation |
| 150 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF ); |
| 152 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 153 | |
| 154 | *p++ = 0x00; |
| 155 | *p++ = ( ssl->verify_data_len + 1 ) & 0xFF; |
| 156 | *p++ = ssl->verify_data_len & 0xFF; |
| 157 | |
| 158 | memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); |
| 159 | |
| 160 | *olen = 5 + ssl->verify_data_len; |
| 161 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 163 | |
Manuel Pégourié-Gonnard | d942323 | 2014-12-02 11:57:29 +0100 | [diff] [blame] | 164 | /* |
| 165 | * Only if we handle at least one key exchange that needs signatures. |
| 166 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 168 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 169 | static void ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 170 | unsigned char *buf, |
| 171 | size_t *olen ) |
| 172 | { |
| 173 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 174 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 175 | size_t sig_alg_len = 0; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 176 | const int *md; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 5bfd968 | 2014-06-24 15:18:11 +0200 | [diff] [blame] | 178 | unsigned char *sig_alg_list = buf + 6; |
| 179 | #endif |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 180 | |
| 181 | *olen = 0; |
| 182 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 183 | if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 184 | return; |
| 185 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 187 | |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 188 | for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 189 | { |
| 190 | #if defined(MBEDTLS_ECDSA_C) |
| 191 | sig_alg_len += 2; |
| 192 | #endif |
| 193 | #if defined(MBEDTLS_RSA_C) |
| 194 | sig_alg_len += 2; |
| 195 | #endif |
| 196 | } |
| 197 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 198 | if( end < p || (size_t)( end - p ) < sig_alg_len + 6 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 199 | { |
| 200 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 201 | return; |
| 202 | } |
| 203 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 204 | /* |
| 205 | * Prepare signature_algorithms extension (TLS 1.2) |
| 206 | */ |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 207 | sig_alg_len = 0; |
| 208 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 209 | for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 212 | sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md ); |
| 213 | sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA; |
Manuel Pégourié-Gonnard | d11eb7c | 2013-08-22 15:57:15 +0200 | [diff] [blame] | 214 | #endif |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 215 | #if defined(MBEDTLS_RSA_C) |
| 216 | sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md ); |
| 217 | sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA; |
Manuel Pégourié-Gonnard | d11eb7c | 2013-08-22 15:57:15 +0200 | [diff] [blame] | 218 | #endif |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 219 | } |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 220 | |
| 221 | /* |
| 222 | * enum { |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 223 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 224 | * sha512(6), (255) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 225 | * } HashAlgorithm; |
| 226 | * |
| 227 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 228 | * SignatureAlgorithm; |
| 229 | * |
| 230 | * struct { |
| 231 | * HashAlgorithm hash; |
| 232 | * SignatureAlgorithm signature; |
| 233 | * } SignatureAndHashAlgorithm; |
| 234 | * |
| 235 | * SignatureAndHashAlgorithm |
| 236 | * supported_signature_algorithms<2..2^16-2>; |
| 237 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG >> 8 ) & 0xFF ); |
| 239 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 240 | |
| 241 | *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF ); |
| 242 | *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF ); |
| 243 | |
| 244 | *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF ); |
| 245 | *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF ); |
| 246 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 247 | *olen = 6 + sig_alg_len; |
| 248 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 249 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && |
| 250 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 251 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 252 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 253 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | static void ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 255 | unsigned char *buf, |
| 256 | size_t *olen ) |
| 257 | { |
| 258 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 259 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 8e205fc | 2014-01-23 17:27:10 +0100 | [diff] [blame] | 260 | unsigned char *elliptic_curve_list = p + 6; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 261 | size_t elliptic_curve_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | const mbedtls_ecp_curve_info *info; |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 263 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | const mbedtls_ecp_group_id *grp_id; |
Paul Bakker | 0910f32 | 2014-02-06 13:41:18 +0100 | [diff] [blame] | 265 | #else |
| 266 | ((void) ssl); |
Manuel Pégourié-Gonnard | cd49f76 | 2014-02-04 15:14:13 +0100 | [diff] [blame] | 267 | #endif |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 268 | |
| 269 | *olen = 0; |
| 270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 272 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 273 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 274 | for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ ) |
Manuel Pégourié-Gonnard | cd49f76 | 2014-02-04 15:14:13 +0100 | [diff] [blame] | 275 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | for( info = mbedtls_ecp_curve_list(); info->grp_id != MBEDTLS_ECP_DP_NONE; info++ ) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 277 | #endif |
Manuel Pégourié-Gonnard | cd49f76 | 2014-02-04 15:14:13 +0100 | [diff] [blame] | 278 | { |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 279 | #if defined(MBEDTLS_ECP_C) |
| 280 | info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); |
Manuel Pégourié-Gonnard | cd49f76 | 2014-02-04 15:14:13 +0100 | [diff] [blame] | 281 | #endif |
Janos Follath | 8a31705 | 2016-04-21 23:37:09 +0100 | [diff] [blame] | 282 | if( info == NULL ) |
| 283 | { |
| 284 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid curve in ssl configuration" ) ); |
| 285 | return; |
| 286 | } |
| 287 | |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 288 | elliptic_curve_len += 2; |
| 289 | } |
| 290 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 291 | if( end < p || (size_t)( end - p ) < 6 + elliptic_curve_len ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 292 | { |
| 293 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | elliptic_curve_len = 0; |
| 298 | |
| 299 | #if defined(MBEDTLS_ECP_C) |
| 300 | for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 301 | #else |
| 302 | for( info = mbedtls_ecp_curve_list(); info->grp_id != MBEDTLS_ECP_DP_NONE; info++ ) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 303 | #endif |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 304 | { |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 305 | #if defined(MBEDTLS_ECP_C) |
| 306 | info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 307 | #endif |
Manuel Pégourié-Gonnard | cd49f76 | 2014-02-04 15:14:13 +0100 | [diff] [blame] | 308 | elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8; |
| 309 | elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 310 | } |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 311 | |
| 312 | if( elliptic_curve_len == 0 ) |
| 313 | return; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF ); |
| 316 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 317 | |
| 318 | *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF ); |
| 319 | *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF ); |
| 320 | |
| 321 | *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF ); |
| 322 | *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF ); |
| 323 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 324 | *olen = 6 + elliptic_curve_len; |
| 325 | } |
| 326 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 328 | unsigned char *buf, |
| 329 | size_t *olen ) |
| 330 | { |
| 331 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 332 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 333 | |
| 334 | *olen = 0; |
| 335 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 336 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 337 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 338 | if( end < p || (size_t)( end - p ) < 6 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 339 | { |
| 340 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 341 | return; |
| 342 | } |
| 343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 344 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF ); |
| 345 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF ); |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 346 | |
| 347 | *p++ = 0x00; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 348 | *p++ = 2; |
Manuel Pégourié-Gonnard | 6b8846d | 2013-08-15 17:42:02 +0200 | [diff] [blame] | 349 | |
| 350 | *p++ = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 351 | *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 352 | |
Manuel Pégourié-Gonnard | 6b8846d | 2013-08-15 17:42:02 +0200 | [diff] [blame] | 353 | *olen = 6; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 354 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 355 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 356 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 357 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 358 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 359 | static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, |
| 360 | unsigned char *buf, |
| 361 | size_t *olen ) |
| 362 | { |
| 363 | int ret; |
| 364 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 365 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 366 | size_t kkpp_len; |
| 367 | |
| 368 | *olen = 0; |
| 369 | |
| 370 | /* Skip costly extension if we can't use EC J-PAKE anyway */ |
| 371 | if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 372 | return; |
| 373 | |
| 374 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding ecjpake_kkpp extension" ) ); |
| 375 | |
| 376 | if( end - p < 4 ) |
| 377 | { |
| 378 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF ); |
| 383 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF ); |
| 384 | |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 385 | /* |
| 386 | * We may need to send ClientHello multiple times for Hello verification. |
| 387 | * We don't want to compute fresh values every time (both for performance |
| 388 | * and consistency reasons), so cache the extension content. |
| 389 | */ |
| 390 | if( ssl->handshake->ecjpake_cache == NULL || |
| 391 | ssl->handshake->ecjpake_cache_len == 0 ) |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 392 | { |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 393 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) ); |
| 394 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 395 | ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, |
| 396 | p + 2, end - p - 2, &kkpp_len, |
| 397 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 398 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 399 | { |
| 400 | MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret ); |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len ); |
| 405 | if( ssl->handshake->ecjpake_cache == NULL ) |
| 406 | { |
| 407 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) ); |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len ); |
| 412 | ssl->handshake->ecjpake_cache_len = kkpp_len; |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) ); |
| 417 | |
| 418 | kkpp_len = ssl->handshake->ecjpake_cache_len; |
| 419 | |
| 420 | if( (size_t)( end - p - 2 ) < kkpp_len ) |
| 421 | { |
| 422 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len ); |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF ); |
| 430 | *p++ = (unsigned char)( ( kkpp_len ) & 0xFF ); |
| 431 | |
| 432 | *olen = kkpp_len + 4; |
| 433 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 434 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 435 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 436 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 39ec525 | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 437 | static void ssl_write_cid_ext( mbedtls_ssl_context *ssl, |
| 438 | unsigned char *buf, |
| 439 | size_t *olen ) |
| 440 | { |
| 441 | unsigned char *p = buf; |
| 442 | size_t ext_len; |
| 443 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 444 | |
| 445 | /* |
Hanno Becker | 3cdf8fe | 2019-05-15 10:26:32 +0100 | [diff] [blame] | 446 | * Quoting draft-ietf-tls-dtls-connection-id-05 |
| 447 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 |
Hanno Becker | 39ec525 | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 448 | * |
| 449 | * struct { |
| 450 | * opaque cid<0..2^8-1>; |
| 451 | * } ConnectionId; |
| 452 | */ |
| 453 | |
| 454 | *olen = 0; |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 455 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) || |
Hanno Becker | 39ec525 | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 456 | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 457 | { |
| 458 | return; |
| 459 | } |
| 460 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) ); |
| 461 | |
| 462 | /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX |
| 463 | * which is at most 255, so the increment cannot overflow. */ |
| 464 | if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) ) |
| 465 | { |
| 466 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | /* Add extension ID + size */ |
| 471 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF ); |
| 472 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF ); |
| 473 | ext_len = (size_t) ssl->own_cid_len + 1; |
| 474 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); |
| 475 | *p++ = (unsigned char)( ( ext_len ) & 0xFF ); |
| 476 | |
| 477 | *p++ = (uint8_t) ssl->own_cid_len; |
| 478 | memcpy( p, ssl->own_cid, ssl->own_cid_len ); |
| 479 | |
| 480 | *olen = ssl->own_cid_len + 5; |
| 481 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 482 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 39ec525 | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 483 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 484 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 485 | static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 486 | unsigned char *buf, |
| 487 | size_t *olen ) |
| 488 | { |
| 489 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 490 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 491 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 492 | *olen = 0; |
| 493 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 494 | if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) { |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 498 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) ); |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 499 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 500 | if( end < p || (size_t)( end - p ) < 5 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 501 | { |
| 502 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 503 | return; |
| 504 | } |
| 505 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 506 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF ); |
| 507 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF ); |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 508 | |
| 509 | *p++ = 0x00; |
| 510 | *p++ = 1; |
| 511 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 512 | *p++ = ssl->conf->mfl_code; |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 513 | |
| 514 | *olen = 5; |
| 515 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 516 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 517 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 519 | static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 520 | unsigned char *buf, size_t *olen ) |
| 521 | { |
| 522 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 523 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 524 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 525 | *olen = 0; |
| 526 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 527 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 528 | { |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 529 | return; |
| 530 | } |
| 531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 533 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 534 | if( end < p || (size_t)( end - p ) < 4 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 535 | { |
| 536 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 537 | return; |
| 538 | } |
| 539 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 540 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF ); |
| 541 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 542 | |
| 543 | *p++ = 0x00; |
| 544 | *p++ = 0x00; |
| 545 | |
| 546 | *olen = 4; |
| 547 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 548 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 549 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 550 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 551 | static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 552 | unsigned char *buf, size_t *olen ) |
| 553 | { |
| 554 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 555 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 556 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 557 | *olen = 0; |
| 558 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 559 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
| 560 | ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 561 | { |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac " |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 566 | "extension" ) ); |
| 567 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 568 | if( end < p || (size_t)( end - p ) < 4 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 569 | { |
| 570 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 571 | return; |
| 572 | } |
| 573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); |
| 575 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 576 | |
| 577 | *p++ = 0x00; |
| 578 | *p++ = 0x00; |
| 579 | |
| 580 | *olen = 4; |
| 581 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 582 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 583 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 585 | static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 586 | unsigned char *buf, size_t *olen ) |
| 587 | { |
| 588 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 589 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 590 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 591 | *olen = 0; |
| 592 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 593 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
| 594 | ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 595 | { |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret " |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 600 | "extension" ) ); |
| 601 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 602 | if( end < p || (size_t)( end - p ) < 4 ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 603 | { |
| 604 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 605 | return; |
| 606 | } |
| 607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 608 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF ); |
| 609 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 610 | |
| 611 | *p++ = 0x00; |
| 612 | *p++ = 0x00; |
| 613 | |
| 614 | *olen = 4; |
| 615 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 616 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 619 | static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 620 | unsigned char *buf, size_t *olen ) |
| 621 | { |
| 622 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 623 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 624 | size_t tlen = ssl->session_negotiate->ticket_len; |
| 625 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 626 | *olen = 0; |
| 627 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 628 | if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ) |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 629 | { |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 630 | return; |
| 631 | } |
| 632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 633 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 634 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 635 | if( end < p || (size_t)( end - p ) < 4 + tlen ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 636 | { |
| 637 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 638 | return; |
| 639 | } |
| 640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 641 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF ); |
| 642 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 643 | |
| 644 | *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF ); |
| 645 | *p++ = (unsigned char)( ( tlen ) & 0xFF ); |
| 646 | |
| 647 | *olen = 4; |
| 648 | |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 649 | if( ssl->session_negotiate->ticket == NULL || tlen == 0 ) |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 650 | { |
| 651 | return; |
| 652 | } |
| 653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 655 | |
| 656 | memcpy( p, ssl->session_negotiate->ticket, tlen ); |
| 657 | |
| 658 | *olen += tlen; |
| 659 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 660 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 662 | #if defined(MBEDTLS_SSL_ALPN) |
| 663 | static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 664 | unsigned char *buf, size_t *olen ) |
| 665 | { |
| 666 | unsigned char *p = buf; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 667 | const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN; |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 668 | size_t alpnlen = 0; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 669 | const char **cur; |
| 670 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 671 | *olen = 0; |
| 672 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 673 | if( ssl->conf->alpn_list == NULL ) |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 674 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 675 | return; |
| 676 | } |
| 677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 678 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 679 | |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 680 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
Simon Butcher | 04799a4 | 2015-09-29 00:31:09 +0100 | [diff] [blame] | 681 | alpnlen += (unsigned char)( strlen( *cur ) & 0xFF ) + 1; |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 682 | |
Simon Butcher | 0fc94e9 | 2015-09-28 20:52:04 +0100 | [diff] [blame] | 683 | if( end < p || (size_t)( end - p ) < 6 + alpnlen ) |
Simon Butcher | ed99766 | 2015-09-28 02:14:30 +0100 | [diff] [blame] | 684 | { |
| 685 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); |
| 686 | return; |
| 687 | } |
| 688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 689 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF ); |
| 690 | *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | * opaque ProtocolName<1..2^8-1>; |
| 694 | * |
| 695 | * struct { |
| 696 | * ProtocolName protocol_name_list<2..2^16-1> |
| 697 | * } ProtocolNameList; |
| 698 | */ |
| 699 | |
| 700 | /* Skip writing extension and list length for now */ |
| 701 | p += 4; |
| 702 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 703 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 704 | { |
| 705 | *p = (unsigned char)( strlen( *cur ) & 0xFF ); |
| 706 | memcpy( p + 1, *cur, *p ); |
| 707 | p += 1 + *p; |
| 708 | } |
| 709 | |
| 710 | *olen = p - buf; |
| 711 | |
| 712 | /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */ |
| 713 | buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF ); |
| 714 | buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF ); |
| 715 | |
| 716 | /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */ |
| 717 | buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF ); |
| 718 | buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF ); |
| 719 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 721 | |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 722 | /* |
| 723 | * Generate random bytes for ClientHello |
| 724 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 725 | static int ssl_generate_random( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 726 | { |
| 727 | int ret; |
| 728 | unsigned char *p = ssl->handshake->randbytes; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 729 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 730 | mbedtls_time_t t; |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 731 | #endif |
| 732 | |
Manuel Pégourié-Gonnard | fb2d223 | 2014-07-22 15:59:14 +0200 | [diff] [blame] | 733 | /* |
| 734 | * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1) |
| 735 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 736 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 737 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | fb2d223 | 2014-07-22 15:59:14 +0200 | [diff] [blame] | 738 | ssl->handshake->verify_cookie != NULL ) |
| 739 | { |
| 740 | return( 0 ); |
| 741 | } |
| 742 | #endif |
| 743 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 745 | t = mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 746 | *p++ = (unsigned char)( t >> 24 ); |
| 747 | *p++ = (unsigned char)( t >> 16 ); |
| 748 | *p++ = (unsigned char)( t >> 8 ); |
| 749 | *p++ = (unsigned char)( t ); |
| 750 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 751 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) ); |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 752 | #else |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 753 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 754 | return( ret ); |
| 755 | |
| 756 | p += 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | #endif /* MBEDTLS_HAVE_TIME */ |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 758 | |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 759 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 760 | return( ret ); |
| 761 | |
| 762 | return( 0 ); |
| 763 | } |
| 764 | |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 765 | /** |
| 766 | * \brief Validate cipher suite against config in SSL context. |
| 767 | * |
| 768 | * \param suite_info cipher suite to validate |
| 769 | * \param ssl SSL context |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 770 | * \param min_minor_ver Minimal minor version to accept a cipher suite |
| 771 | * \param max_minor_ver Maximal minor version to accept a cipher suite |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 772 | * |
| 773 | * \return 0 if valid, else 1 |
| 774 | */ |
| 775 | static int ssl_validate_ciphersuite( const mbedtls_ssl_ciphersuite_t * suite_info, |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 776 | const mbedtls_ssl_context * ssl, |
| 777 | int min_minor_ver, int max_minor_ver ) |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 778 | { |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 779 | (void) ssl; |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 780 | if( suite_info == NULL ) |
| 781 | return( 1 ); |
| 782 | |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 783 | if( suite_info->min_minor_ver > max_minor_ver || |
| 784 | suite_info->max_minor_ver < min_minor_ver ) |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 785 | return( 1 ); |
| 786 | |
| 787 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 788 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 789 | ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) |
| 790 | return( 1 ); |
| 791 | #endif |
| 792 | |
| 793 | #if defined(MBEDTLS_ARC4_C) |
| 794 | if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && |
| 795 | suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) |
| 796 | return( 1 ); |
| 797 | #endif |
| 798 | |
| 799 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 800 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 801 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 802 | return( 1 ); |
| 803 | #endif |
| 804 | |
| 805 | return( 0 ); |
| 806 | } |
| 807 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 808 | static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 809 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 810 | int ret; |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 811 | size_t i, n, olen, ext_len = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 812 | unsigned char *buf; |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 813 | unsigned char *p, *q; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 814 | unsigned char offer_compress; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 815 | const int *ciphersuites; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 817 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 818 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 819 | int uses_ec = 0; |
| 820 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 821 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 822 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 823 | |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 824 | if( ssl->conf->f_rng == NULL ) |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 825 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 826 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") ); |
| 827 | return( MBEDTLS_ERR_SSL_NO_RNG ); |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 828 | } |
| 829 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 830 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 831 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 832 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 833 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 834 | ssl->major_ver = ssl->conf->min_major_ver; |
| 835 | ssl->minor_ver = ssl->conf->min_minor_ver; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 836 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 837 | |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 838 | if( ssl->conf->max_major_ver == 0 ) |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 839 | { |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 840 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, " |
| 841 | "consider using mbedtls_ssl_config_defaults()" ) ); |
| 842 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 843 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 844 | |
| 845 | /* |
| 846 | * 0 . 0 handshake type |
| 847 | * 1 . 3 handshake length |
| 848 | * 4 . 5 highest version supported |
| 849 | * 6 . 9 current UNIX time |
| 850 | * 10 . 37 random bytes |
| 851 | */ |
| 852 | buf = ssl->out_msg; |
| 853 | p = buf + 4; |
| 854 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 855 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver, |
| 856 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 857 | p += 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 858 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 859 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 860 | buf[4], buf[5] ) ); |
| 861 | |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 862 | if( ( ret = ssl_generate_random( ssl ) ) != 0 ) |
| 863 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 864 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 865 | return( ret ); |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 866 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 867 | |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 868 | memcpy( p, ssl->handshake->randbytes, 32 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 869 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 ); |
Manuel Pégourié-Gonnard | b760f00 | 2014-07-22 15:53:27 +0200 | [diff] [blame] | 870 | p += 32; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 871 | |
| 872 | /* |
| 873 | * 38 . 38 session id length |
| 874 | * 39 . 39+n session id |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 875 | * 39+n . 39+n DTLS only: cookie length (1 byte) |
| 876 | * 40+n . .. DTSL only: cookie |
| 877 | * .. . .. ciphersuitelist length (2 bytes) |
| 878 | * .. . .. ciphersuitelist |
| 879 | * .. . .. compression methods length (1 byte) |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 880 | * .. . .. compression methods |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 881 | * .. . .. extensions length (2 bytes) |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 882 | * .. . .. extensions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 883 | */ |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 884 | n = ssl->session_negotiate->id_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 885 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 886 | if( n < 16 || n > 32 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 887 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 888 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 889 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 890 | ssl->handshake->resume == 0 ) |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 891 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 892 | n = 0; |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 893 | } |
| 894 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 895 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 896 | /* |
| 897 | * RFC 5077 section 3.4: "When presenting a ticket, the client MAY |
| 898 | * generate and include a Session ID in the TLS ClientHello." |
| 899 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 900 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 901 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 902 | #endif |
Manuel Pégourié-Gonnard | d2b35ec | 2015-03-10 11:40:43 +0000 | [diff] [blame] | 903 | { |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 904 | if( ssl->session_negotiate->ticket != NULL && |
| 905 | ssl->session_negotiate->ticket_len != 0 ) |
| 906 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 907 | ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id, 32 ); |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 908 | |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 909 | if( ret != 0 ) |
| 910 | return( ret ); |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 911 | |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 912 | ssl->session_negotiate->id_len = n = 32; |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 913 | } |
Manuel Pégourié-Gonnard | 6377e41 | 2013-07-31 16:31:33 +0200 | [diff] [blame] | 914 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 915 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 916 | |
| 917 | *p++ = (unsigned char) n; |
| 918 | |
| 919 | for( i = 0; i < n; i++ ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 920 | *p++ = ssl->session_negotiate->id[i]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 921 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 922 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) ); |
| 923 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 924 | |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 925 | /* |
| 926 | * DTLS cookie |
| 927 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 928 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 929 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 930 | { |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 931 | if( ssl->handshake->verify_cookie == NULL ) |
| 932 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 933 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 934 | *p++ = 0; |
| 935 | } |
| 936 | else |
| 937 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 938 | MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie", |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 939 | ssl->handshake->verify_cookie, |
| 940 | ssl->handshake->verify_cookie_len ); |
| 941 | |
| 942 | *p++ = ssl->handshake->verify_cookie_len; |
| 943 | memcpy( p, ssl->handshake->verify_cookie, |
| 944 | ssl->handshake->verify_cookie_len ); |
| 945 | p += ssl->handshake->verify_cookie_len; |
| 946 | } |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 947 | } |
| 948 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 949 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 950 | /* |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 951 | * Ciphersuite list |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 952 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 953 | ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; |
Manuel Pégourié-Gonnard | 4128aa7 | 2014-03-21 09:40:12 +0100 | [diff] [blame] | 954 | |
| 955 | /* Skip writing ciphersuite length for now */ |
| 956 | n = 0; |
| 957 | q = p; |
| 958 | p += 2; |
| 959 | |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 960 | for( i = 0; ciphersuites[i] != 0; i++ ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 961 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 962 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 963 | |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 964 | if( ssl_validate_ciphersuite( ciphersuite_info, ssl, |
| 965 | ssl->conf->min_minor_ver, |
| 966 | ssl->conf->max_minor_ver ) != 0 ) |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 967 | continue; |
| 968 | |
Manuel Pégourié-Gonnard | 60884a1 | 2015-09-16 11:13:41 +0200 | [diff] [blame] | 969 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x", |
| 970 | ciphersuites[i] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 971 | |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 972 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 973 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 974 | uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info ); |
| 975 | #endif |
| 976 | |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 977 | n++; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 978 | *p++ = (unsigned char)( ciphersuites[i] >> 8 ); |
| 979 | *p++ = (unsigned char)( ciphersuites[i] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Ron Eldor | 4a2fb4c | 2017-09-10 17:03:50 +0300 | [diff] [blame] | 982 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites (excluding SCSVs)", n ) ); |
Ron Eldor | 714785d | 2017-08-28 13:55:55 +0300 | [diff] [blame] | 983 | |
Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 984 | /* |
| 985 | * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 986 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 987 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 988 | if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 989 | #endif |
| 990 | { |
Ron Eldor | 4a2fb4c | 2017-09-10 17:03:50 +0300 | [diff] [blame] | 991 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 992 | *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO >> 8 ); |
| 993 | *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO ); |
Manuel Pégourié-Gonnard | 5d9cde2 | 2015-01-22 10:49:41 +0000 | [diff] [blame] | 994 | n++; |
| 995 | } |
| 996 | |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 997 | /* Some versions of OpenSSL don't handle it correctly if not at end */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 998 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 999 | if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK ) |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1000 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1001 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) ); |
| 1002 | *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ); |
| 1003 | *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ); |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1004 | n++; |
| 1005 | } |
| 1006 | #endif |
| 1007 | |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1008 | *q++ = (unsigned char)( n >> 7 ); |
| 1009 | *q++ = (unsigned char)( n << 1 ); |
| 1010 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1012 | offer_compress = 1; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1013 | #else |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1014 | offer_compress = 0; |
| 1015 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1016 | |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1017 | /* |
Johannes H | 4e5d23f | 2018-01-06 09:46:57 +0100 | [diff] [blame] | 1018 | * We don't support compression with DTLS right now: if many records come |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1019 | * in the same datagram, uncompressing one could overwrite the next one. |
| 1020 | * We don't want to add complexity for handling that case unless there is |
| 1021 | * an actual need for it. |
| 1022 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1023 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1024 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1025 | offer_compress = 0; |
| 1026 | #endif |
| 1027 | |
| 1028 | if( offer_compress ) |
| 1029 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1030 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) ); |
| 1031 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d", |
| 1032 | MBEDTLS_SSL_COMPRESS_DEFLATE, MBEDTLS_SSL_COMPRESS_NULL ) ); |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1033 | |
| 1034 | *p++ = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1035 | *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE; |
| 1036 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1037 | } |
| 1038 | else |
| 1039 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1040 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) ); |
| 1041 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", |
| 1042 | MBEDTLS_SSL_COMPRESS_NULL ) ); |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1043 | |
| 1044 | *p++ = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1045 | *p++ = MBEDTLS_SSL_COMPRESS_NULL; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1046 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1047 | |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1048 | // First write extensions, then the total length |
| 1049 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1050 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1051 | ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen ); |
| 1052 | ext_len += olen; |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 1053 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1054 | |
Hanno Becker | 40f8b51 | 2017-10-12 14:58:55 +0100 | [diff] [blame] | 1055 | /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added |
| 1056 | * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1057 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1058 | ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen ); |
| 1059 | ext_len += olen; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1060 | #endif |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1061 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1062 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1063 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Paul Bakker | d3edc86 | 2013-03-20 16:07:17 +0100 | [diff] [blame] | 1064 | ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen ); |
| 1065 | ext_len += olen; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1066 | #endif |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1067 | |
Manuel Pégourié-Gonnard | f472179 | 2015-09-15 10:53:51 +0200 | [diff] [blame] | 1068 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1069 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1070 | if( uses_ec ) |
| 1071 | { |
| 1072 | ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen ); |
| 1073 | ext_len += olen; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1074 | |
Ron Eldor | 755bb6a | 2018-02-14 19:30:48 +0200 | [diff] [blame] | 1075 | ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen ); |
| 1076 | ext_len += olen; |
| 1077 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1078 | #endif |
| 1079 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1080 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 294139b | 2015-09-15 16:55:05 +0200 | [diff] [blame] | 1081 | ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen ); |
| 1082 | ext_len += olen; |
| 1083 | #endif |
| 1084 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1085 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 39ec525 | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 1086 | ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen ); |
| 1087 | ext_len += olen; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1088 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 39ec525 | 2019-04-25 16:55:15 +0100 | [diff] [blame] | 1089 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1090 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 1091 | ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen ); |
| 1092 | ext_len += olen; |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1093 | #endif |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 1094 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1095 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1096 | ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen ); |
| 1097 | ext_len += olen; |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 1098 | #endif |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1099 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1100 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1101 | ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); |
| 1102 | ext_len += olen; |
| 1103 | #endif |
| 1104 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1106 | ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); |
| 1107 | ext_len += olen; |
| 1108 | #endif |
| 1109 | |
Simon Butcher | 5624ec8 | 2015-09-29 01:06:06 +0100 | [diff] [blame] | 1110 | #if defined(MBEDTLS_SSL_ALPN) |
| 1111 | ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1112 | ext_len += olen; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1113 | #endif |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1114 | |
Simon Butcher | 5624ec8 | 2015-09-29 01:06:06 +0100 | [diff] [blame] | 1115 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1116 | ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1117 | ext_len += olen; |
| 1118 | #endif |
| 1119 | |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1120 | /* olen unused if all extensions are disabled */ |
| 1121 | ((void) olen); |
| 1122 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1123 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d", |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 1124 | ext_len ) ); |
| 1125 | |
Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 1126 | if( ext_len > 0 ) |
| 1127 | { |
| 1128 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); |
| 1129 | *p++ = (unsigned char)( ( ext_len ) & 0xFF ); |
| 1130 | p += ext_len; |
| 1131 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1132 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1133 | ssl->out_msglen = p - buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1134 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 1135 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1136 | |
| 1137 | ssl->state++; |
| 1138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1140 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1141 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 1142 | #endif |
| 1143 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1144 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1145 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1146 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1147 | return( ret ); |
| 1148 | } |
| 1149 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1150 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1151 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1152 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 1153 | { |
| 1154 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 1155 | return( ret ); |
| 1156 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 1157 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1159 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1160 | |
| 1161 | return( 0 ); |
| 1162 | } |
| 1163 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1164 | static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | e048b67 | 2013-07-19 12:47:00 +0200 | [diff] [blame] | 1165 | const unsigned char *buf, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1166 | size_t len ) |
| 1167 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1168 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1169 | if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1170 | { |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1171 | /* Check verify-data in constant-time. The length OTOH is no secret */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1172 | if( len != 1 + ssl->verify_data_len * 2 || |
| 1173 | buf[0] != ssl->verify_data_len * 2 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1174 | mbedtls_ssl_safer_memcmp( buf + 1, |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1175 | ssl->own_verify_data, ssl->verify_data_len ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1176 | mbedtls_ssl_safer_memcmp( buf + 1 + ssl->verify_data_len, |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 1177 | ssl->peer_verify_data, ssl->verify_data_len ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1178 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1179 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1180 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1181 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1182 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1183 | } |
| 1184 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1185 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1186 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1187 | { |
| 1188 | if( len != 1 || buf[0] != 0x00 ) |
| 1189 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1190 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1191 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1192 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1193 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1196 | ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1197 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1198 | |
| 1199 | return( 0 ); |
| 1200 | } |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1202 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1203 | static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | e048b67 | 2013-07-19 12:47:00 +0200 | [diff] [blame] | 1204 | const unsigned char *buf, |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1205 | size_t len ) |
| 1206 | { |
| 1207 | /* |
| 1208 | * server should use the extension only if we did, |
| 1209 | * and if so the server's value should match ours (and len is always 1) |
| 1210 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1211 | if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE || |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1212 | len != 1 || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1213 | buf[0] != ssl->conf->mfl_code ) |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1214 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1215 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching max fragment length extension" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1216 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1217 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1218 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | return( 0 ); |
| 1222 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1223 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1225 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1226 | static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1227 | const unsigned char *buf, |
| 1228 | size_t len ) |
| 1229 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1230 | if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED || |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1231 | len != 0 ) |
| 1232 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1233 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching truncated HMAC extension" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1234 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1235 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1236 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | ((void) buf); |
| 1240 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1241 | ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1242 | |
| 1243 | return( 0 ); |
| 1244 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1245 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1246 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1247 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1248 | static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl, |
| 1249 | const unsigned char *buf, |
| 1250 | size_t len ) |
| 1251 | { |
| 1252 | size_t peer_cid_len; |
| 1253 | |
| 1254 | if( /* CID extension only makes sense in DTLS */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1255 | MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) || |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1256 | /* The server must only send the CID extension if we have offered it. */ |
Hanno Becker | 8f68f87 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1257 | ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1258 | { |
Hanno Becker | 8f68f87 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1259 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) ); |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1260 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | 8f68f87 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1261 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
| 1262 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1263 | } |
| 1264 | |
| 1265 | if( len == 0 ) |
| 1266 | { |
| 1267 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
| 1268 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1269 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1270 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1271 | } |
| 1272 | |
| 1273 | peer_cid_len = *buf++; |
| 1274 | len--; |
| 1275 | |
| 1276 | if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX ) |
| 1277 | { |
Hanno Becker | 8f68f87 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1278 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1279 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | 8f68f87 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1280 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1281 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1282 | } |
| 1283 | |
| 1284 | if( len != peer_cid_len ) |
| 1285 | { |
Hanno Becker | 8f68f87 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1286 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) ); |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1287 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Hanno Becker | 8f68f87 | 2019-05-03 12:46:59 +0100 | [diff] [blame] | 1288 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1289 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1290 | } |
| 1291 | |
Hanno Becker | f885d3b | 2019-05-03 12:47:49 +0100 | [diff] [blame] | 1292 | ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED; |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1293 | ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len; |
| 1294 | memcpy( ssl->handshake->peer_cid, buf, peer_cid_len ); |
| 1295 | |
| 1296 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) ); |
| 1297 | MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len ); |
| 1298 | |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1299 | return( 0 ); |
| 1300 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1301 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1303 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1304 | static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1305 | const unsigned char *buf, |
| 1306 | size_t len ) |
| 1307 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1308 | if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1309 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1310 | len != 0 ) |
| 1311 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1312 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching encrypt-then-MAC extension" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1313 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1314 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1315 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | ((void) buf); |
| 1319 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1320 | ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1321 | |
| 1322 | return( 0 ); |
| 1323 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1324 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1326 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1327 | static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1328 | const unsigned char *buf, |
| 1329 | size_t len ) |
| 1330 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1331 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1332 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1333 | len != 0 ) |
| 1334 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1335 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching extended master secret extension" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1336 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1337 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1338 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | ((void) buf); |
| 1342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1343 | ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1344 | |
| 1345 | return( 0 ); |
| 1346 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1347 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1349 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1350 | static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1351 | const unsigned char *buf, |
| 1352 | size_t len ) |
| 1353 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1354 | if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED || |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1355 | len != 0 ) |
| 1356 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1357 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching session ticket extension" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1358 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1359 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1360 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1361 | } |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1362 | |
| 1363 | ((void) buf); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 1364 | |
| 1365 | ssl->handshake->new_session_ticket = 1; |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1366 | |
| 1367 | return( 0 ); |
| 1368 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1369 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1370 | |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1371 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1372 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1373 | static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1374 | const unsigned char *buf, |
| 1375 | size_t len ) |
| 1376 | { |
| 1377 | size_t list_size; |
| 1378 | const unsigned char *p; |
| 1379 | |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1380 | if( len == 0 || (size_t)( buf[0] + 1 ) != len ) |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1381 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1382 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1383 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1384 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1385 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1386 | } |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1387 | list_size = buf[0]; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1388 | |
Manuel Pégourié-Gonnard | fd35af1 | 2014-06-23 14:10:13 +0200 | [diff] [blame] | 1389 | p = buf + 1; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1390 | while( list_size > 0 ) |
| 1391 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1392 | if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED || |
| 1393 | p[0] == MBEDTLS_ECP_PF_COMPRESSED ) |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1394 | { |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1395 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 1396 | ssl->handshake->ecdh_ctx.point_format = p[0]; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 1397 | #endif |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1398 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 1399 | ssl->handshake->ecjpake_ctx.point_format = p[0]; |
| 1400 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1401 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1402 | return( 0 ); |
| 1403 | } |
| 1404 | |
| 1405 | list_size--; |
| 1406 | p++; |
| 1407 | } |
| 1408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1409 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1410 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1411 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1412 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1413 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 1414 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 1415 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 1416 | |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1417 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 1418 | static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, |
| 1419 | const unsigned char *buf, |
| 1420 | size_t len ) |
| 1421 | { |
| 1422 | int ret; |
| 1423 | |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1424 | if( ssl->handshake->ciphersuite_info->key_exchange != |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1425 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 1426 | { |
| 1427 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); |
| 1428 | return( 0 ); |
| 1429 | } |
| 1430 | |
Manuel Pégourié-Gonnard | d0d8cb3 | 2015-09-17 14:16:30 +0200 | [diff] [blame] | 1431 | /* If we got here, we no longer need our cached extension */ |
| 1432 | mbedtls_free( ssl->handshake->ecjpake_cache ); |
| 1433 | ssl->handshake->ecjpake_cache = NULL; |
| 1434 | ssl->handshake->ecjpake_cache_len = 0; |
| 1435 | |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1436 | if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx, |
| 1437 | buf, len ) ) != 0 ) |
| 1438 | { |
| 1439 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1440 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1441 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 1442 | return( ret ); |
| 1443 | } |
| 1444 | |
| 1445 | return( 0 ); |
| 1446 | } |
| 1447 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | #if defined(MBEDTLS_SSL_ALPN) |
| 1450 | static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1451 | const unsigned char *buf, size_t len ) |
| 1452 | { |
| 1453 | size_t list_len, name_len; |
| 1454 | const char **p; |
| 1455 | |
| 1456 | /* If we didn't send it, the server shouldn't send it */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1457 | if( ssl->conf->alpn_list == NULL ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1458 | { |
| 1459 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1460 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1461 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1462 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1463 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1464 | |
| 1465 | /* |
| 1466 | * opaque ProtocolName<1..2^8-1>; |
| 1467 | * |
| 1468 | * struct { |
| 1469 | * ProtocolName protocol_name_list<2..2^16-1> |
| 1470 | * } ProtocolNameList; |
| 1471 | * |
| 1472 | * the "ProtocolNameList" MUST contain exactly one "ProtocolName" |
| 1473 | */ |
| 1474 | |
| 1475 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ |
| 1476 | if( len < 4 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1477 | { |
| 1478 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1479 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1480 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1481 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1482 | |
| 1483 | list_len = ( buf[0] << 8 ) | buf[1]; |
| 1484 | if( list_len != len - 2 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1485 | { |
| 1486 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1487 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1488 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1489 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1490 | |
| 1491 | name_len = buf[2]; |
| 1492 | if( name_len != list_len - 1 ) |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1493 | { |
| 1494 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1495 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1496 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1497 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1498 | |
| 1499 | /* Check that the server chosen protocol was in our list and save it */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1500 | for( p = ssl->conf->alpn_list; *p != NULL; p++ ) |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1501 | { |
| 1502 | if( name_len == strlen( *p ) && |
| 1503 | memcmp( buf + 3, *p, name_len ) == 0 ) |
| 1504 | { |
| 1505 | ssl->alpn_chosen = *p; |
| 1506 | return( 0 ); |
| 1507 | } |
| 1508 | } |
| 1509 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1510 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) ); |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 1511 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1512 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1513 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1514 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1515 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1516 | |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1517 | /* |
| 1518 | * Parse HelloVerifyRequest. Only called after verifying the HS type. |
| 1519 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1520 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1521 | static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1522 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1523 | const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1524 | int major_ver, minor_ver; |
| 1525 | unsigned char cookie_len; |
| 1526 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1527 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1528 | |
| 1529 | /* |
| 1530 | * struct { |
| 1531 | * ProtocolVersion server_version; |
| 1532 | * opaque cookie<0..2^8-1>; |
| 1533 | * } HelloVerifyRequest; |
| 1534 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1535 | MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 ); |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1536 | mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1537 | p += 2; |
| 1538 | |
Manuel Pégourié-Gonnard | b35fe56 | 2014-08-09 17:00:46 +0200 | [diff] [blame] | 1539 | /* |
| 1540 | * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1) |
| 1541 | * even is lower than our min version. |
| 1542 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1543 | if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 1544 | minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1545 | major_ver > ssl->conf->max_major_ver || |
| 1546 | minor_ver > ssl->conf->max_minor_ver ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1547 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1548 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1549 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1550 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1551 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | cookie_len = *p++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1557 | MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1558 | |
Andres AG | 5a87c93 | 2016-09-26 14:53:05 +0100 | [diff] [blame] | 1559 | if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len ) |
| 1560 | { |
| 1561 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 1562 | ( "cookie length does not match incoming message size" ) ); |
| 1563 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1564 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 1565 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1566 | } |
| 1567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1568 | mbedtls_free( ssl->handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1569 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1570 | ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1571 | if( ssl->handshake->verify_cookie == NULL ) |
| 1572 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 1573 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1574 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | memcpy( ssl->handshake->verify_cookie, p, cookie_len ); |
| 1578 | ssl->handshake->verify_cookie_len = cookie_len; |
| 1579 | |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 1580 | /* Start over at ClientHello */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1581 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 1582 | mbedtls_ssl_reset_checksum( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1583 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1584 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1585 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1586 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1587 | |
| 1588 | return( 0 ); |
| 1589 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1590 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1592 | static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1593 | { |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1594 | int ret, i; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1595 | size_t n; |
Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 1596 | size_t ext_len; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1597 | unsigned char *buf, *ext; |
Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 1598 | unsigned char comp; |
| 1599 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1600 | int accept_comp; |
| 1601 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1602 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1603 | int renegotiation_info_seen = 0; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1604 | #endif |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1605 | int handshake_failure = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1606 | const mbedtls_ssl_ciphersuite_t *suite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1608 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1609 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 1610 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1611 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1612 | /* No alert on a read error. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1613 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1614 | return( ret ); |
| 1615 | } |
| 1616 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 1617 | buf = ssl->in_msg; |
| 1618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1619 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1620 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1621 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1622 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 1623 | { |
Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 1624 | ssl->renego_records_seen++; |
| 1625 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1626 | if( ssl->conf->renego_max_records >= 0 && |
| 1627 | ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 1628 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1629 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 1630 | "but not honored by server" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1631 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 44ade65 | 2014-08-19 13:58:40 +0200 | [diff] [blame] | 1632 | } |
| 1633 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1634 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 1635 | |
| 1636 | ssl->keep_current_message = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1637 | return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO ); |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 1638 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1639 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 1640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1641 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1642 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1643 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1644 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1647 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 1648 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1649 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1650 | if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1651 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1652 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) ); |
| 1653 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1654 | return( ssl_parse_hello_verify_request( ssl ) ); |
| 1655 | } |
| 1656 | else |
| 1657 | { |
| 1658 | /* We made it through the verification process */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1659 | mbedtls_free( ssl->handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 1660 | ssl->handshake->verify_cookie = NULL; |
| 1661 | ssl->handshake->verify_cookie_len = 0; |
| 1662 | } |
| 1663 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1665 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1666 | if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) || |
| 1667 | buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1668 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1669 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1670 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1671 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1672 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1675 | /* |
| 1676 | * 0 . 1 server_version |
| 1677 | * 2 . 33 random (maybe including 4 bytes of Unix time) |
| 1678 | * 34 . 34 session_id length = n |
| 1679 | * 35 . 34+n session_id |
| 1680 | * 35+n . 36+n cipher_suite |
| 1681 | * 37+n . 37+n compression_method |
| 1682 | * |
| 1683 | * 38+n . 39+n extensions length (optional) |
| 1684 | * 40+n . .. extensions |
| 1685 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1686 | buf += mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1688 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 ); |
| 1689 | mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1690 | ssl->conf->transport, buf + 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1691 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1692 | if( ssl->major_ver < ssl->conf->min_major_ver || |
| 1693 | ssl->minor_ver < ssl->conf->min_minor_ver || |
| 1694 | ssl->major_ver > ssl->conf->max_major_ver || |
| 1695 | ssl->minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1696 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1697 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server version out of bounds - " |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1698 | " min: [%d:%d], server: [%d:%d], max: [%d:%d]", |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1699 | ssl->conf->min_major_ver, ssl->conf->min_minor_ver, |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 1700 | ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1701 | ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1703 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1704 | MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1705 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1706 | return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Andres Amaya Garcia | 6bce9cb | 2017-09-06 15:33:34 +0100 | [diff] [blame] | 1709 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", |
| 1710 | ( (uint32_t) buf[2] << 24 ) | |
| 1711 | ( (uint32_t) buf[3] << 16 ) | |
| 1712 | ( (uint32_t) buf[4] << 8 ) | |
| 1713 | ( (uint32_t) buf[5] ) ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1714 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1715 | memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1716 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1717 | n = buf[34]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1718 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1719 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1720 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1721 | if( n > 32 ) |
| 1722 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1723 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1724 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1725 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1726 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Manuel Pégourié-Gonnard | a6e5bd5 | 2015-07-23 12:14:13 +0200 | [diff] [blame] | 1729 | if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1730 | { |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1731 | ext_len = ( ( buf[38 + n] << 8 ) |
| 1732 | | ( buf[39 + n] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1733 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1734 | if( ( ext_len > 0 && ext_len < 4 ) || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1735 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1736 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1737 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1738 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1739 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1740 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1741 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1742 | } |
Manuel Pégourié-Gonnard | a6e5bd5 | 2015-07-23 12:14:13 +0200 | [diff] [blame] | 1743 | else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n ) |
Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 1744 | { |
| 1745 | ext_len = 0; |
| 1746 | } |
| 1747 | else |
| 1748 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1749 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1750 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1751 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1752 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Manuel Pégourié-Gonnard | f7cdbc0 | 2014-10-17 17:02:10 +0200 | [diff] [blame] | 1753 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1754 | |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1755 | /* ciphersuite (used later) */ |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1756 | i = ( buf[35 + n] << 8 ) | buf[36 + n]; |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1757 | |
| 1758 | /* |
| 1759 | * Read and check compression |
| 1760 | */ |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1761 | comp = buf[37 + n]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1762 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1763 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1764 | /* See comments in ssl_write_client_hello() */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 1765 | accept_comp = MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ); |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1766 | |
Manuel Pégourié-Gonnard | 1cf7b30 | 2015-06-24 22:28:19 +0200 | [diff] [blame] | 1767 | if( comp != MBEDTLS_SSL_COMPRESS_NULL && |
| 1768 | ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) ) |
| 1769 | #else /* MBEDTLS_ZLIB_SUPPORT */ |
| 1770 | if( comp != MBEDTLS_SSL_COMPRESS_NULL ) |
| 1771 | #endif/* MBEDTLS_ZLIB_SUPPORT */ |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1772 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1773 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1774 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1775 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1776 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | a0e1632 | 2014-07-14 17:38:41 +0200 | [diff] [blame] | 1777 | } |
| 1778 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1779 | /* |
| 1780 | * Initialize update checksum functions |
| 1781 | */ |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1782 | ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i ); |
| 1783 | if( ssl->handshake->ciphersuite_info == NULL ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1784 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1785 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1786 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1787 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1788 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1789 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1790 | |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1791 | mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info ); |
Manuel Pégourié-Gonnard | 3c599f1 | 2014-03-10 13:25:07 +0100 | [diff] [blame] | 1792 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1793 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); |
| 1794 | MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1795 | |
| 1796 | /* |
| 1797 | * Check if the session can be resumed |
| 1798 | */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1799 | if( ssl->handshake->resume == 0 || n == 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1800 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1801 | ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE || |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1802 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1803 | ssl->session_negotiate->ciphersuite != i || |
| 1804 | ssl->session_negotiate->compression != comp || |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1805 | ssl->session_negotiate->id_len != n || |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1806 | memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1807 | { |
| 1808 | ssl->state++; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1809 | ssl->handshake->resume = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1810 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 1811 | ssl->session_negotiate->start = mbedtls_time( NULL ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 1812 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1813 | ssl->session_negotiate->ciphersuite = i; |
| 1814 | ssl->session_negotiate->compression = comp; |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 1815 | ssl->session_negotiate->id_len = n; |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1816 | memcpy( ssl->session_negotiate->id, buf + 35, n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1817 | } |
| 1818 | else |
| 1819 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1820 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1821 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1822 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1823 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1824 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1825 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1826 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1827 | return( ret ); |
| 1828 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1831 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1832 | ssl->handshake->resume ? "a" : "no" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1833 | |
Manuel Pégourié-Gonnard | 60884a1 | 2015-09-16 11:13:41 +0200 | [diff] [blame] | 1834 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", i ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1835 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1836 | |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 1837 | /* |
| 1838 | * Perform cipher suite validation in same way as in ssl_write_client_hello. |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1839 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1840 | i = 0; |
| 1841 | while( 1 ) |
| 1842 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1843 | if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1844 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1845 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1846 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1847 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1848 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1851 | if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] == |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1852 | ssl->session_negotiate->ciphersuite ) |
| 1853 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1854 | break; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1855 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1858 | suite_info = mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ); |
Andrzej Kurek | 03bac44 | 2018-04-25 05:06:07 -0400 | [diff] [blame] | 1859 | if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver, ssl->minor_ver ) != 0 ) |
Mohammad Azim Khan | 1d3b508 | 2018-04-18 19:35:00 +0100 | [diff] [blame] | 1860 | { |
| 1861 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 1862 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1863 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
| 1864 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
| 1865 | } |
| 1866 | |
| 1867 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", suite_info->name ) ); |
| 1868 | |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1869 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 1870 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA && |
| 1871 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 1872 | { |
| 1873 | ssl->handshake->ecrs_enabled = 1; |
| 1874 | } |
| 1875 | #endif |
| 1876 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1877 | if( comp != MBEDTLS_SSL_COMPRESS_NULL |
| 1878 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1879 | && comp != MBEDTLS_SSL_COMPRESS_DEFLATE |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1880 | #endif |
| 1881 | ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1882 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1883 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1884 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1885 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1886 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1887 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1888 | ssl->session_negotiate->compression = comp; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1889 | |
Manuel Pégourié-Gonnard | 0b3400d | 2014-09-10 21:23:41 +0200 | [diff] [blame] | 1890 | ext = buf + 40 + n; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1891 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1892 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) ); |
Manuel Pégourié-Gonnard | a052849 | 2013-07-16 17:26:28 +0200 | [diff] [blame] | 1893 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1894 | while( ext_len ) |
| 1895 | { |
| 1896 | unsigned int ext_id = ( ( ext[0] << 8 ) |
| 1897 | | ( ext[1] ) ); |
| 1898 | unsigned int ext_size = ( ( ext[2] << 8 ) |
| 1899 | | ( ext[3] ) ); |
| 1900 | |
| 1901 | if( ext_size + 4 > ext_len ) |
| 1902 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1903 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1904 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1905 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1906 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | switch( ext_id ) |
| 1910 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1911 | case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO: |
| 1912 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); |
| 1913 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1914 | renegotiation_info_seen = 1; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1915 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1916 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1917 | if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4, |
| 1918 | ext_size ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1919 | return( ret ); |
| 1920 | |
| 1921 | break; |
| 1922 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1923 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1924 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
| 1925 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) ); |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1926 | |
| 1927 | if( ( ret = ssl_parse_max_fragment_length_ext( ssl, |
| 1928 | ext + 4, ext_size ) ) != 0 ) |
| 1929 | { |
| 1930 | return( ret ); |
| 1931 | } |
| 1932 | |
| 1933 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1934 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | de600e5 | 2013-07-17 10:14:38 +0200 | [diff] [blame] | 1935 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1936 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1937 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
| 1938 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) ); |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1939 | |
| 1940 | if( ( ret = ssl_parse_truncated_hmac_ext( ssl, |
| 1941 | ext + 4, ext_size ) ) != 0 ) |
| 1942 | { |
| 1943 | return( ret ); |
| 1944 | } |
| 1945 | |
| 1946 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1947 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1948 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1949 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1950 | case MBEDTLS_TLS_EXT_CID: |
| 1951 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) ); |
| 1952 | |
| 1953 | if( ( ret = ssl_parse_cid_ext( ssl, |
| 1954 | ext + 4, |
| 1955 | ext_size ) ) != 0 ) |
| 1956 | { |
| 1957 | return( ret ); |
| 1958 | } |
| 1959 | |
| 1960 | break; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1961 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 1ba81f6 | 2019-04-26 15:37:26 +0100 | [diff] [blame] | 1962 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1963 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1964 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
| 1965 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1966 | |
| 1967 | if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl, |
| 1968 | ext + 4, ext_size ) ) != 0 ) |
| 1969 | { |
| 1970 | return( ret ); |
| 1971 | } |
| 1972 | |
| 1973 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1974 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1975 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1976 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1977 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
| 1978 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1979 | |
| 1980 | if( ( ret = ssl_parse_extended_ms_ext( ssl, |
| 1981 | ext + 4, ext_size ) ) != 0 ) |
| 1982 | { |
| 1983 | return( ret ); |
| 1984 | } |
| 1985 | |
| 1986 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1987 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1988 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1989 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1990 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
| 1991 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) ); |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 1992 | |
| 1993 | if( ( ret = ssl_parse_session_ticket_ext( ssl, |
| 1994 | ext + 4, ext_size ) ) != 0 ) |
| 1995 | { |
| 1996 | return( ret ); |
| 1997 | } |
| 1998 | |
| 1999 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2000 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 60182ef | 2013-08-02 14:44:54 +0200 | [diff] [blame] | 2001 | |
Robert Cragie | 136884c | 2015-10-02 13:34:31 +0100 | [diff] [blame] | 2002 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2003 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2004 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
| 2005 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) ); |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2006 | |
| 2007 | if( ( ret = ssl_parse_supported_point_formats_ext( ssl, |
| 2008 | ext + 4, ext_size ) ) != 0 ) |
| 2009 | { |
| 2010 | return( ret ); |
| 2011 | } |
| 2012 | |
| 2013 | break; |
Robert Cragie | ae8535d | 2015-10-06 17:11:18 +0100 | [diff] [blame] | 2014 | #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 2015 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2016 | |
Manuel Pégourié-Gonnard | 0a1324a | 2015-09-16 16:01:00 +0200 | [diff] [blame] | 2017 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2018 | case MBEDTLS_TLS_EXT_ECJPAKE_KKPP: |
| 2019 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) ); |
| 2020 | |
| 2021 | if( ( ret = ssl_parse_ecjpake_kkpp( ssl, |
| 2022 | ext + 4, ext_size ) ) != 0 ) |
| 2023 | { |
| 2024 | return( ret ); |
| 2025 | } |
| 2026 | |
| 2027 | break; |
| 2028 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2029 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2030 | #if defined(MBEDTLS_SSL_ALPN) |
| 2031 | case MBEDTLS_TLS_EXT_ALPN: |
| 2032 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2033 | |
| 2034 | if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 ) |
| 2035 | return( ret ); |
| 2036 | |
| 2037 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2038 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2039 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2040 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2041 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)", |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2042 | ext_id ) ); |
| 2043 | } |
| 2044 | |
| 2045 | ext_len -= 4 + ext_size; |
| 2046 | ext += 4 + ext_size; |
| 2047 | |
| 2048 | if( ext_len > 0 && ext_len < 4 ) |
| 2049 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2050 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
| 2051 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | /* |
| 2056 | * Renegotiation security checks |
| 2057 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2058 | if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2059 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2060 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2061 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2062 | handshake_failure = 1; |
| 2063 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2064 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2065 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2066 | ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION && |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2067 | renegotiation_info_seen == 0 ) |
| 2068 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2069 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2070 | handshake_failure = 1; |
| 2071 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2072 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2073 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2074 | ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2076 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2077 | handshake_failure = 1; |
| 2078 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2079 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 2080 | ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2081 | renegotiation_info_seen == 1 ) |
| 2082 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2083 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2084 | handshake_failure = 1; |
| 2085 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2086 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2087 | |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2088 | /* |
| 2089 | * Check if extended master secret is being enforced |
| 2090 | */ |
| 2091 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 2092 | if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED && |
| 2093 | ssl->conf->enforce_extended_master_secret == |
| 2094 | MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED && |
Jarno Lamsa | 20095af | 2019-06-11 17:16:58 +0300 | [diff] [blame] | 2095 | ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ) |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 2096 | { |
| 2097 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Peer not offering extended master " |
| 2098 | "secret, while it is enforced") ); |
| 2099 | handshake_failure = 1; |
| 2100 | } |
| 2101 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 2102 | |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 2103 | if( handshake_failure == 1 ) |
| 2104 | { |
Gilles Peskine | c94f735 | 2017-05-10 16:37:56 +0200 | [diff] [blame] | 2105 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2106 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2107 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2108 | } |
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, ( "<= parse server hello" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2111 | |
| 2112 | return( 0 ); |
| 2113 | } |
| 2114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2115 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2116 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 2117 | static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, unsigned char **p, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2118 | unsigned char *end ) |
| 2119 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2120 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2121 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2122 | /* |
| 2123 | * Ephemeral DH parameters: |
| 2124 | * |
| 2125 | * struct { |
| 2126 | * opaque dh_p<1..2^16-1>; |
| 2127 | * opaque dh_g<1..2^16-1>; |
| 2128 | * opaque dh_Ys<1..2^16-1>; |
| 2129 | * } ServerDHParams; |
| 2130 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2131 | if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2132 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2133 | MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2134 | return( ret ); |
| 2135 | } |
| 2136 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2137 | if( ssl->handshake->dhm_ctx.len * 8 < ssl->conf->dhm_min_bitlen ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2138 | { |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2139 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %d < %d", |
| 2140 | ssl->handshake->dhm_ctx.len * 8, |
| 2141 | ssl->conf->dhm_min_bitlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2142 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2143 | } |
| 2144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2145 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P ); |
| 2146 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G ); |
| 2147 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2148 | |
| 2149 | return( ret ); |
| 2150 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2151 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 2152 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2154 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2155 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 2156 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
| 2157 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2158 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2159 | static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2160 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2161 | const mbedtls_ecp_curve_info *curve_info; |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 2162 | mbedtls_ecp_group_id grp_id; |
| 2163 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 2164 | grp_id = ssl->handshake->ecdh_ctx.grp.id; |
| 2165 | #else |
| 2166 | grp_id = ssl->handshake->ecdh_ctx.grp_id; |
| 2167 | #endif |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2168 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 2169 | curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id ); |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2170 | if( curve_info == NULL ) |
| 2171 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2172 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2173 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2174 | } |
| 2175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2176 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2177 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2178 | #if defined(MBEDTLS_ECP_C) |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 2179 | if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2180 | #else |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2181 | if( ssl->handshake->ecdh_ctx.grp.nbits < 163 || |
| 2182 | ssl->handshake->ecdh_ctx.grp.nbits > 521 ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2183 | #endif |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2184 | return( -1 ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2185 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 2186 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 2187 | MBEDTLS_DEBUG_ECDH_QP ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2188 | |
| 2189 | return( 0 ); |
| 2190 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2191 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2192 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 2193 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || |
| 2194 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 2195 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2196 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2197 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2198 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 2199 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 2200 | static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2201 | unsigned char **p, |
| 2202 | unsigned char *end ) |
| 2203 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2204 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2205 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2206 | /* |
| 2207 | * Ephemeral ECDH parameters: |
| 2208 | * |
| 2209 | * struct { |
| 2210 | * ECParameters curve_params; |
| 2211 | * ECPoint public; |
| 2212 | * } ServerECDHParams; |
| 2213 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2214 | if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2215 | (const unsigned char **) p, end ) ) != 0 ) |
| 2216 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2217 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret ); |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2218 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1c1c20e | 2018-09-12 10:34:43 +0200 | [diff] [blame] | 2219 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2220 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2221 | #endif |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2222 | return( ret ); |
| 2223 | } |
| 2224 | |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2225 | if( ssl_check_server_ecdh_params( ssl ) != 0 ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2226 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2227 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) ); |
| 2228 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2229 | } |
| 2230 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2231 | return( ret ); |
| 2232 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2233 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2234 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 2235 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2237 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 2238 | static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl, |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2239 | unsigned char **p, |
| 2240 | unsigned char *end ) |
| 2241 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2242 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2243 | size_t len; |
Paul Bakker | c5a79cc | 2013-06-26 15:08:35 +0200 | [diff] [blame] | 2244 | ((void) ssl); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2245 | |
| 2246 | /* |
| 2247 | * PSK parameters: |
| 2248 | * |
| 2249 | * opaque psk_identity_hint<0..2^16-1>; |
| 2250 | */ |
Hanno Becker | 0c161d1 | 2018-10-08 13:40:50 +0100 | [diff] [blame] | 2251 | if( end - (*p) < 2 ) |
Krzysztof Stachowiak | 740b218 | 2018-03-13 11:31:14 +0100 | [diff] [blame] | 2252 | { |
| 2253 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message " |
| 2254 | "(psk_identity_hint length)" ) ); |
| 2255 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2256 | } |
Manuel Pégourié-Gonnard | 59b9fe2 | 2013-10-15 11:55:33 +0200 | [diff] [blame] | 2257 | len = (*p)[0] << 8 | (*p)[1]; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2258 | *p += 2; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2259 | |
Hanno Becker | 8df1023 | 2018-10-10 15:48:39 +0100 | [diff] [blame] | 2260 | if( end - (*p) < (int) len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2261 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2262 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message " |
| 2263 | "(psk_identity_hint length)" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2264 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2265 | } |
| 2266 | |
Manuel Pégourié-Gonnard | 9d62412 | 2016-02-22 11:10:14 +0100 | [diff] [blame] | 2267 | /* |
| 2268 | * Note: we currently ignore the PKS identity hint, as we only allow one |
| 2269 | * PSK to be provisionned on the client. This could be changed later if |
| 2270 | * someone needs that feature. |
| 2271 | */ |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2272 | *p += len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2273 | ret = 0; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2274 | |
| 2275 | return( ret ); |
| 2276 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2277 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2278 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2279 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 2280 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2281 | /* |
| 2282 | * Generate a pre-master secret and encrypt it with the server's RSA key |
| 2283 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2284 | static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2285 | size_t offset, size_t *olen, |
| 2286 | size_t pms_offset ) |
| 2287 | { |
| 2288 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2289 | size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2; |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2290 | unsigned char *p = ssl->handshake->premaster + pms_offset; |
Hanno Becker | 374800a | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2291 | mbedtls_pk_context * peer_pk; |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2292 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2293 | if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2294 | { |
| 2295 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) ); |
| 2296 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2297 | } |
| 2298 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2299 | /* |
| 2300 | * Generate (part of) the pre-master as |
| 2301 | * struct { |
| 2302 | * ProtocolVersion client_version; |
| 2303 | * opaque random[46]; |
| 2304 | * } PreMasterSecret; |
| 2305 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2306 | mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver, |
| 2307 | ssl->conf->transport, p ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2308 | |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2309 | if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2310 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2311 | MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2312 | return( ret ); |
| 2313 | } |
| 2314 | |
| 2315 | ssl->handshake->pmslen = 48; |
| 2316 | |
Hanno Becker | 374800a | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2317 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2318 | peer_pk = &ssl->handshake->peer_pubkey; |
| 2319 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2320 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 2321 | { |
Hanno Becker | f02d550 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2322 | /* Should never happen */ |
Hanno Becker | e9839c0 | 2019-02-26 11:51:06 +0000 | [diff] [blame] | 2323 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | f02d550 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2324 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2325 | } |
Hanno Becker | 374800a | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2326 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 2327 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2328 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2329 | /* |
| 2330 | * Now write it out, encrypted |
| 2331 | */ |
Hanno Becker | 374800a | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2332 | if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2333 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2334 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) ); |
| 2335 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2336 | } |
| 2337 | |
Hanno Becker | 374800a | 2019-02-06 16:49:54 +0000 | [diff] [blame] | 2338 | if( ( ret = mbedtls_pk_encrypt( peer_pk, |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2339 | p, ssl->handshake->pmslen, |
| 2340 | ssl->out_msg + offset + len_bytes, olen, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2341 | MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 2342 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2343 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2344 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret ); |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2345 | return( ret ); |
| 2346 | } |
| 2347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2348 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2349 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2350 | if( len_bytes == 2 ) |
| 2351 | { |
| 2352 | ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 ); |
| 2353 | ssl->out_msg[offset+1] = (unsigned char)( *olen ); |
| 2354 | *olen += 2; |
| 2355 | } |
| 2356 | #endif |
| 2357 | |
Hanno Becker | 6c83db7 | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 2358 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2359 | /* We don't need the peer's public key anymore. Free it. */ |
| 2360 | mbedtls_pk_free( peer_pk ); |
| 2361 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2362 | return( 0 ); |
| 2363 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2364 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED || |
| 2365 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2366 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2367 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 5c2a7ca | 2015-10-23 08:48:41 +0200 | [diff] [blame] | 2368 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2369 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2370 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2371 | static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl, |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2372 | unsigned char **p, |
| 2373 | unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2374 | mbedtls_md_type_t *md_alg, |
| 2375 | mbedtls_pk_type_t *pk_alg ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2376 | { |
Paul Bakker | c5a79cc | 2013-06-26 15:08:35 +0200 | [diff] [blame] | 2377 | ((void) ssl); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2378 | *md_alg = MBEDTLS_MD_NONE; |
| 2379 | *pk_alg = MBEDTLS_PK_NONE; |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2380 | |
| 2381 | /* Only in TLS 1.2 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2382 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2383 | { |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2384 | return( 0 ); |
| 2385 | } |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2386 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2387 | if( (*p) + 2 > end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2388 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2389 | |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2390 | /* |
| 2391 | * Get hash algorithm |
| 2392 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2393 | if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) == MBEDTLS_MD_NONE ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2394 | { |
Manuel Pégourié-Gonnard | d805324 | 2015-12-08 09:53:51 +0100 | [diff] [blame] | 2395 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server used unsupported " |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 2396 | "HashAlgorithm %d", *(p)[0] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2397 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2398 | } |
| 2399 | |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2400 | /* |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2401 | * Get signature algorithm |
| 2402 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2403 | if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) == MBEDTLS_PK_NONE ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2404 | { |
Manuel Pégourié-Gonnard | d805324 | 2015-12-08 09:53:51 +0100 | [diff] [blame] | 2405 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used unsupported " |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 2406 | "SignatureAlgorithm %d", (*p)[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2407 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2408 | } |
| 2409 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 2410 | /* |
| 2411 | * Check if the hash is acceptable |
| 2412 | */ |
| 2413 | if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 ) |
| 2414 | { |
Gilles Peskine | cd3c845 | 2017-05-09 14:57:45 +0200 | [diff] [blame] | 2415 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used HashAlgorithm %d that was not offered", |
| 2416 | *(p)[0] ) ); |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 2417 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2418 | } |
| 2419 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2420 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) ); |
| 2421 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2422 | *p += 2; |
| 2423 | |
| 2424 | return( 0 ); |
| 2425 | } |
Manuel Pégourié-Gonnard | 5c2a7ca | 2015-10-23 08:48:41 +0200 | [diff] [blame] | 2426 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 2427 | MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2428 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2429 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2430 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2431 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2432 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2433 | static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2434 | { |
| 2435 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2436 | const mbedtls_ecp_keypair *peer_key; |
Hanno Becker | 53b6b7e | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2437 | mbedtls_pk_context * peer_pk; |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2438 | |
Hanno Becker | 53b6b7e | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2439 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2440 | peer_pk = &ssl->handshake->peer_pubkey; |
| 2441 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2442 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 2443 | { |
Hanno Becker | f02d550 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2444 | /* Should never happen */ |
Hanno Becker | c39e23e | 2019-02-26 12:36:01 +0000 | [diff] [blame^] | 2445 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | f02d550 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2446 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2447 | } |
Hanno Becker | 53b6b7e | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2448 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 2449 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2450 | |
Hanno Becker | 53b6b7e | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2451 | if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2452 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2453 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 2454 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2455 | } |
| 2456 | |
Hanno Becker | 53b6b7e | 2019-02-06 17:44:07 +0000 | [diff] [blame] | 2457 | peer_key = mbedtls_pk_ec( *peer_pk ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2458 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2459 | if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key, |
| 2460 | MBEDTLS_ECDH_THEIRS ) ) != 0 ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2461 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2462 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2463 | return( ret ); |
| 2464 | } |
| 2465 | |
| 2466 | if( ssl_check_server_ecdh_params( ssl ) != 0 ) |
| 2467 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2468 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) ); |
| 2469 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2470 | } |
| 2471 | |
Hanno Becker | 6c83db7 | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 2472 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2473 | /* We don't need the peer's public key anymore. Free it, |
| 2474 | * so that more RAM is available for upcoming expensive |
| 2475 | * operations like ECDHE. */ |
| 2476 | mbedtls_pk_free( peer_pk ); |
| 2477 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2478 | |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2479 | return( ret ); |
| 2480 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2481 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || |
| 2482 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2483 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2484 | static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2485 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2486 | int ret; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2487 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2488 | ssl->handshake->ciphersuite_info; |
Andres Amaya Garcia | 53c77cc | 2017-06-27 16:15:06 +0100 | [diff] [blame] | 2489 | unsigned char *p = NULL, *end = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2490 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2491 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2492 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2493 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 2494 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2495 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2496 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2497 | ssl->state++; |
| 2498 | return( 0 ); |
| 2499 | } |
Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 2500 | ((void) p); |
| 2501 | ((void) end); |
| 2502 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2503 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2504 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2505 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2506 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 2507 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2508 | { |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2509 | if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 ) |
| 2510 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2511 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2512 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2513 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 2514 | return( ret ); |
| 2515 | } |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2516 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2517 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2518 | ssl->state++; |
| 2519 | return( 0 ); |
| 2520 | } |
| 2521 | ((void) p); |
| 2522 | ((void) end); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2523 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 2524 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | d18cc57 | 2013-12-11 17:45:46 +0100 | [diff] [blame] | 2525 | |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2526 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2527 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2528 | ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing ) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2529 | { |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2530 | goto start_processing; |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2531 | } |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2532 | #endif |
| 2533 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2534 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2535 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2536 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2537 | return( ret ); |
| 2538 | } |
| 2539 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2540 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2541 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2542 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2543 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2544 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2545 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2548 | /* |
| 2549 | * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server |
| 2550 | * doesn't use a psk_identity_hint |
| 2551 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2552 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2553 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2554 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2555 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Paul Bakker | 188c8de | 2013-04-19 09:13:37 +0200 | [diff] [blame] | 2556 | { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2557 | /* Current message is probably either |
| 2558 | * CertificateRequest or ServerHelloDone */ |
| 2559 | ssl->keep_current_message = 1; |
Paul Bakker | 188c8de | 2013-04-19 09:13:37 +0200 | [diff] [blame] | 2560 | goto exit; |
| 2561 | } |
| 2562 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2563 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key exchange message must " |
| 2564 | "not be skipped" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2565 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2566 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2568 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2571 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 2572 | if( ssl->handshake->ecrs_enabled ) |
| 2573 | ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing; |
| 2574 | |
| 2575 | start_processing: |
| 2576 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2577 | p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Paul Bakker | 3b6a07b | 2013-03-21 11:56:50 +0100 | [diff] [blame] | 2578 | end = ssl->in_msg + ssl->in_hslen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2579 | MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p ); |
Paul Bakker | 3b6a07b | 2013-03-21 11:56:50 +0100 | [diff] [blame] | 2580 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2581 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 2582 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2583 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || |
| 2584 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 2585 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2586 | { |
| 2587 | if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 ) |
| 2588 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2589 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2590 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2591 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2592 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2593 | } |
| 2594 | } /* FALLTROUGH */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2595 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2597 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ |
| 2598 | defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 2599 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 2600 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2601 | ; /* nothing more to do */ |
| 2602 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2603 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || |
| 2604 | MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 2605 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2606 | defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 2607 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA || |
| 2608 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2609 | { |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2610 | if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2611 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2612 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2613 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2614 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2615 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2616 | } |
| 2617 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2618 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2619 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 2620 | MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 2621 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2622 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
| 2623 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
| 2624 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 2625 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 2626 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2627 | { |
| 2628 | if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 ) |
| 2629 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2630 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2631 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2632 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2633 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2634 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2635 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2636 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2637 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2638 | MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || |
| 2639 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2640 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2641 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 2642 | { |
| 2643 | ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, |
| 2644 | p, end - p ); |
| 2645 | if( ret != 0 ) |
| 2646 | { |
| 2647 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2648 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2649 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 2650 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2651 | } |
| 2652 | } |
| 2653 | else |
| 2654 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2655 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2656 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2657 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2658 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2659 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2660 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) |
| 2661 | if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2662 | { |
Manuel Pégourié-Gonnard | d92d6a1 | 2014-09-10 15:25:02 +0000 | [diff] [blame] | 2663 | size_t sig_len, hashlen; |
| 2664 | unsigned char hash[64]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2665 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; |
| 2666 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
| 2667 | unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | d92d6a1 | 2014-09-10 15:25:02 +0000 | [diff] [blame] | 2668 | size_t params_len = p - params; |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2669 | void *rs_ctx = NULL; |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2670 | |
Hanno Becker | 69fad13 | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 2671 | mbedtls_pk_context * peer_pk; |
| 2672 | |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2673 | /* |
| 2674 | * Handle the digitally-signed structure |
| 2675 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2676 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2677 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2678 | { |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2679 | if( ssl_parse_signature_algorithm( ssl, &p, end, |
| 2680 | &md_alg, &pk_alg ) != 0 ) |
| 2681 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2682 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2683 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2684 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2685 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2686 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2688 | if( pk_alg != mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2689 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2690 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2691 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2692 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2693 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2694 | } |
| 2695 | } |
Manuel Pégourié-Gonnard | 09edda8 | 2013-08-19 13:50:33 +0200 | [diff] [blame] | 2696 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2697 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2698 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2699 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 2700 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 09edda8 | 2013-08-19 13:50:33 +0200 | [diff] [blame] | 2701 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2702 | pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2703 | |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2704 | /* Default hash for ECDSA is SHA-1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2705 | if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE ) |
| 2706 | md_alg = MBEDTLS_MD_SHA1; |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2707 | } |
| 2708 | else |
| 2709 | #endif |
| 2710 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2711 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2712 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2713 | } |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2714 | |
| 2715 | /* |
| 2716 | * Read signature |
| 2717 | */ |
Krzysztof Stachowiak | a1098f8 | 2018-03-13 11:28:49 +0100 | [diff] [blame] | 2718 | |
| 2719 | if( p > end - 2 ) |
| 2720 | { |
| 2721 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
| 2722 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2723 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2724 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
| 2725 | } |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2726 | sig_len = ( p[0] << 8 ) | p[1]; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2727 | p += 2; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2728 | |
Krzysztof Stachowiak | 027f84c | 2018-03-13 11:29:24 +0100 | [diff] [blame] | 2729 | if( p != end - sig_len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2730 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2731 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2732 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2733 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2734 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2735 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2737 | MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len ); |
Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 2738 | |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2739 | /* |
| 2740 | * Compute the hash that has been signed |
| 2741 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2742 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 2743 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 2744 | if( md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 2745 | { |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2746 | hashlen = 36; |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 2747 | ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, params, |
| 2748 | params_len ); |
| 2749 | if( ret != 0 ) |
Andres Amaya Garcia | f0e521e | 2017-06-28 12:11:42 +0100 | [diff] [blame] | 2750 | return( ret ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2751 | } |
| 2752 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2753 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 2754 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 2755 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2756 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2757 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2758 | { |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 2759 | ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen, |
| 2760 | params, params_len, |
| 2761 | md_alg ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 2762 | if( ret != 0 ) |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2763 | return( ret ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2764 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2765 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2766 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2767 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2768 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2769 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2770 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2771 | } |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2772 | |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 2773 | MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen ); |
Paul Bakker | 29e1f12 | 2013-04-16 13:07:56 +0200 | [diff] [blame] | 2774 | |
Hanno Becker | 69fad13 | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 2775 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2776 | peer_pk = &ssl->handshake->peer_pubkey; |
| 2777 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2778 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 2779 | { |
Hanno Becker | f02d550 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2780 | /* Should never happen */ |
Hanno Becker | c39e23e | 2019-02-26 12:36:01 +0000 | [diff] [blame^] | 2781 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Hanno Becker | f02d550 | 2019-02-06 17:37:32 +0000 | [diff] [blame] | 2782 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2783 | } |
Hanno Becker | 69fad13 | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 2784 | peer_pk = &ssl->session_negotiate->peer_cert->pk; |
| 2785 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 7f2f062 | 2015-09-03 10:44:32 +0200 | [diff] [blame] | 2786 | |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2787 | /* |
| 2788 | * Verify signature |
| 2789 | */ |
Hanno Becker | 69fad13 | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 2790 | if( !mbedtls_pk_can_do( peer_pk, pk_alg ) ) |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2791 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2792 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2793 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2794 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2795 | return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2796 | } |
| 2797 | |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2798 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 2799 | if( ssl->handshake->ecrs_enabled ) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 2800 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2801 | #endif |
| 2802 | |
Hanno Becker | 69fad13 | 2019-02-06 18:26:03 +0000 | [diff] [blame] | 2803 | if( ( ret = mbedtls_pk_verify_restartable( peer_pk, |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2804 | md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | efebb0a | 2013-08-19 12:06:38 +0200 | [diff] [blame] | 2805 | { |
Manuel Pégourié-Gonnard | 1f1f2a1 | 2017-05-18 11:27:06 +0200 | [diff] [blame] | 2806 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 2807 | if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2808 | #endif |
| 2809 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2810 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2811 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret ); |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2812 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 2813 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2814 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 2815 | #endif |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2816 | return( ret ); |
Paul Bakker | c3f177a | 2012-04-11 16:11:49 +0000 | [diff] [blame] | 2817 | } |
Hanno Becker | 6c83db7 | 2019-02-08 14:06:00 +0000 | [diff] [blame] | 2818 | |
| 2819 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2820 | /* We don't need the peer's public key anymore. Free it, |
| 2821 | * so that more RAM is available for upcoming expensive |
| 2822 | * operations like ECDHE. */ |
| 2823 | mbedtls_pk_free( peer_pk ); |
| 2824 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2825 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2826 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2827 | |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2828 | exit: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2829 | ssl->state++; |
| 2830 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2831 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2832 | |
| 2833 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2836 | #if ! defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2837 | static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2838 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2839 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2840 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2841 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2842 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2843 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2844 | if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2845 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2846 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2847 | ssl->state++; |
| 2848 | return( 0 ); |
| 2849 | } |
| 2850 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2851 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2852 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2853 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2854 | #else /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2855 | static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2856 | { |
| 2857 | int ret; |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2858 | unsigned char *buf; |
| 2859 | size_t n = 0; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2860 | size_t cert_type_len = 0, dn_len = 0; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2861 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2862 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2863 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2864 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2865 | |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 2866 | if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2867 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2868 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) ); |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2869 | ssl->state++; |
| 2870 | return( 0 ); |
| 2871 | } |
| 2872 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2873 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2874 | { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2875 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 2876 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2879 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 2880 | { |
| 2881 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 2882 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2883 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 2884 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 2885 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2886 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2887 | ssl->state++; |
| 2888 | ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2889 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2890 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2891 | ssl->client_auth ? "a" : "no" ) ); |
| 2892 | |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2893 | if( ssl->client_auth == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2894 | { |
| 2895 | /* Current message is probably the ServerHelloDone */ |
| 2896 | ssl->keep_current_message = 1; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2897 | goto exit; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 2898 | } |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2899 | |
Manuel Pégourié-Gonnard | 04c1b4e | 2014-09-10 19:25:43 +0200 | [diff] [blame] | 2900 | /* |
| 2901 | * struct { |
| 2902 | * ClientCertificateType certificate_types<1..2^8-1>; |
| 2903 | * SignatureAndHashAlgorithm |
| 2904 | * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only |
| 2905 | * DistinguishedName certificate_authorities<0..2^16-1>; |
| 2906 | * } CertificateRequest; |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2907 | * |
| 2908 | * Since we only support a single certificate on clients, let's just |
| 2909 | * ignore all the information that's supposed to help us pick a |
| 2910 | * certificate. |
| 2911 | * |
| 2912 | * We could check that our certificate matches the request, and bail out |
| 2913 | * if it doesn't, but it's simpler to just send the certificate anyway, |
| 2914 | * and give the server the opportunity to decide if it should terminate |
| 2915 | * the connection when it doesn't like our certificate. |
| 2916 | * |
| 2917 | * Same goes for the hash in TLS 1.2's signature_algorithms: at this |
| 2918 | * point we only have one hash available (see comments in |
Simon Butcher | c0957bd | 2016-03-01 13:16:57 +0000 | [diff] [blame] | 2919 | * write_certificate_verify), so let's just use what we have. |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2920 | * |
| 2921 | * However, we still minimally parse the message to check it is at least |
| 2922 | * superficially sane. |
Manuel Pégourié-Gonnard | 04c1b4e | 2014-09-10 19:25:43 +0200 | [diff] [blame] | 2923 | */ |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2924 | buf = ssl->in_msg; |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2925 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2926 | /* certificate_types */ |
Krzysztof Stachowiak | 73b183c | 2018-04-05 10:20:09 +0200 | [diff] [blame] | 2927 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 2928 | { |
| 2929 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 2930 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2931 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2932 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
| 2933 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2934 | cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )]; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2935 | n = cert_type_len; |
| 2936 | |
Krzysztof Stachowiak | bc145f7 | 2018-03-20 11:19:50 +0100 | [diff] [blame] | 2937 | /* |
Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 2938 | * In the subsequent code there are two paths that read from buf: |
Krzysztof Stachowiak | bc145f7 | 2018-03-20 11:19:50 +0100 | [diff] [blame] | 2939 | * * the length of the signature algorithms field (if minor version of |
| 2940 | * SSL is 3), |
| 2941 | * * distinguished name length otherwise. |
| 2942 | * Both reach at most the index: |
| 2943 | * ...hdr_len + 2 + n, |
| 2944 | * therefore the buffer length at this point must be greater than that |
| 2945 | * regardless of the actual code path. |
| 2946 | */ |
| 2947 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2948 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2949 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2950 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2951 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2952 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2953 | } |
| 2954 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2955 | /* supported_signature_algorithms */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2956 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2957 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2958 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2959 | size_t sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 ) |
| 2960 | | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2961 | #if defined(MBEDTLS_DEBUG_C) |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 2962 | unsigned char* sig_alg; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2963 | size_t i; |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 2964 | #endif |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2965 | |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 2966 | /* |
Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 2967 | * The furthest access in buf is in the loop few lines below: |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 2968 | * sig_alg[i + 1], |
| 2969 | * where: |
| 2970 | * sig_alg = buf + ...hdr_len + 3 + n, |
| 2971 | * max(i) = sig_alg_len - 1. |
Krzysztof Stachowiak | 94d4997 | 2018-04-05 14:48:55 +0200 | [diff] [blame] | 2972 | * Therefore the furthest access is: |
Krzysztof Stachowiak | bc231cc | 2018-03-20 14:09:53 +0100 | [diff] [blame] | 2973 | * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1], |
| 2974 | * which reduces to: |
| 2975 | * buf[...hdr_len + 3 + n + sig_alg_len], |
| 2976 | * which is one less than we need the buf to be. |
| 2977 | */ |
| 2978 | if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len ) |
| 2979 | { |
| 2980 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
| 2981 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2982 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 2983 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
| 2984 | } |
| 2985 | |
| 2986 | #if defined(MBEDTLS_DEBUG_C) |
| 2987 | sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2988 | for( i = 0; i < sig_alg_len; i += 2 ) |
| 2989 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 2990 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Signature Algorithm found: %d" |
| 2991 | ",%d", sig_alg[i], sig_alg[i + 1] ) ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2992 | } |
| 2993 | #endif |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2994 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2995 | n += 2 + sig_alg_len; |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2996 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2997 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2998 | |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 2999 | /* certificate_authorities */ |
| 3000 | dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 ) |
| 3001 | | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3002 | |
| 3003 | n += dn_len; |
Manuel Pégourié-Gonnard | d1b7f2b | 2016-02-24 14:13:22 +0000 | [diff] [blame] | 3004 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3005 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3006 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3007 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3008 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3009 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3013 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3014 | |
| 3015 | return( 0 ); |
| 3016 | } |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3017 | #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3018 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3019 | static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3020 | { |
| 3021 | int ret; |
| 3022 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3023 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3024 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3025 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3026 | { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3027 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 3028 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3029 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3030 | |
| 3031 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 3032 | { |
| 3033 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
| 3034 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 3035 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3036 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3037 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) || |
| 3038 | ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3039 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3040 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3041 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3042 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3043 | return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | ssl->state++; |
| 3047 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3048 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3049 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3050 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3051 | #endif |
| 3052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3053 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3054 | |
| 3055 | return( 0 ); |
| 3056 | } |
| 3057 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3058 | static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3059 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3060 | int ret; |
| 3061 | size_t i, n; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3062 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3063 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3064 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3065 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) ); |
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 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) |
| 3068 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3069 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3070 | /* |
| 3071 | * DHM key exchange -- send G^X mod P |
| 3072 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3073 | n = ssl->handshake->dhm_ctx.len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3074 | |
| 3075 | ssl->out_msg[4] = (unsigned char)( n >> 8 ); |
| 3076 | ssl->out_msg[5] = (unsigned char)( n ); |
| 3077 | i = 6; |
| 3078 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3079 | ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, |
| 3080 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3081 | &ssl->out_msg[i], n, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3082 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3083 | if( ret != 0 ) |
| 3084 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3085 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3086 | return( ret ); |
| 3087 | } |
| 3088 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3089 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X ); |
| 3090 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3091 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3092 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3093 | ssl->handshake->premaster, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 3094 | MBEDTLS_PREMASTER_SIZE, |
Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 3095 | &ssl->handshake->pmslen, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3096 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3097 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3098 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3099 | return( ret ); |
| 3100 | } |
| 3101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3102 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3103 | } |
| 3104 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3105 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
| 3106 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 3107 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 3108 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 3109 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 3110 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || |
| 3111 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || |
| 3112 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || |
| 3113 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3114 | { |
| 3115 | /* |
| 3116 | * ECDH key exchange -- send client public value |
| 3117 | */ |
| 3118 | i = 4; |
| 3119 | |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3120 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3121 | if( ssl->handshake->ecrs_enabled ) |
| 3122 | { |
Manuel Pégourié-Gonnard | c37423f | 2018-10-16 10:28:17 +0200 | [diff] [blame] | 3123 | if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret ) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3124 | goto ecdh_calc_secret; |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 3125 | |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3126 | mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx ); |
| 3127 | } |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3128 | #endif |
| 3129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3130 | ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3131 | &n, |
| 3132 | &ssl->out_msg[i], 1000, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3133 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3134 | if( ret != 0 ) |
| 3135 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3136 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3137 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 3138 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3139 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3140 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3141 | return( ret ); |
| 3142 | } |
| 3143 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 3144 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3145 | MBEDTLS_DEBUG_ECDH_Q ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3146 | |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3147 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3148 | if( ssl->handshake->ecrs_enabled ) |
| 3149 | { |
| 3150 | ssl->handshake->ecrs_n = n; |
Manuel Pégourié-Gonnard | c37423f | 2018-10-16 10:28:17 +0200 | [diff] [blame] | 3151 | ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret; |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3152 | } |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3153 | |
| 3154 | ecdh_calc_secret: |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3155 | if( ssl->handshake->ecrs_enabled ) |
| 3156 | n = ssl->handshake->ecrs_n; |
Manuel Pégourié-Gonnard | 2350b4e | 2017-05-16 09:26:48 +0200 | [diff] [blame] | 3157 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3158 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3159 | &ssl->handshake->pmslen, |
| 3160 | ssl->handshake->premaster, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3161 | MBEDTLS_MPI_MAX_SIZE, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3162 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3163 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3164 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3165 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 3166 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3167 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3168 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3169 | return( ret ); |
| 3170 | } |
| 3171 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 3172 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3173 | MBEDTLS_DEBUG_ECDH_Z ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3174 | } |
| 3175 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3176 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 3177 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 3178 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 3179 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
| 3180 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Hanno Becker | 1aa267c | 2017-04-28 17:08:27 +0100 | [diff] [blame] | 3181 | if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3182 | { |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3183 | /* |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3184 | * opaque psk_identity<0..2^16-1>; |
| 3185 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3186 | if( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3187 | { |
| 3188 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for PSK" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3189 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3190 | } |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3191 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3192 | i = 4; |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3193 | n = ssl->conf->psk_identity_len; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3194 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3195 | if( i + 2 + n > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3196 | { |
| 3197 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity too long or " |
| 3198 | "SSL buffer too short" ) ); |
| 3199 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3200 | } |
| 3201 | |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3202 | ssl->out_msg[i++] = (unsigned char)( n >> 8 ); |
| 3203 | ssl->out_msg[i++] = (unsigned char)( n ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3204 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3205 | memcpy( ssl->out_msg + i, ssl->conf->psk_identity, ssl->conf->psk_identity_len ); |
| 3206 | i += ssl->conf->psk_identity_len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3208 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 3209 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3210 | { |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3211 | n = 0; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3212 | } |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3213 | else |
| 3214 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3215 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 3216 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3217 | { |
| 3218 | if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 ) |
| 3219 | return( ret ); |
| 3220 | } |
| 3221 | else |
| 3222 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3223 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3224 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3225 | { |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3226 | /* |
| 3227 | * ClientDiffieHellmanPublic public (DHM send G^X mod P) |
| 3228 | */ |
| 3229 | n = ssl->handshake->dhm_ctx.len; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3230 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3231 | if( i + 2 + n > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3232 | { |
| 3233 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long" |
| 3234 | " or SSL buffer too short" ) ); |
| 3235 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3236 | } |
| 3237 | |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3238 | ssl->out_msg[i++] = (unsigned char)( n >> 8 ); |
| 3239 | ssl->out_msg[i++] = (unsigned char)( n ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3240 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3241 | ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, |
| 3242 | (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ), |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3243 | &ssl->out_msg[i], n, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3244 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3245 | if( ret != 0 ) |
| 3246 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3247 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3248 | return( ret ); |
| 3249 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3250 | } |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3251 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3252 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 3253 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 3254 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3255 | { |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3256 | /* |
| 3257 | * ClientECDiffieHellmanPublic public; |
| 3258 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3259 | ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, &n, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3260 | &ssl->out_msg[i], MBEDTLS_SSL_OUT_CONTENT_LEN - i, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3261 | ssl->conf->f_rng, ssl->conf->p_rng ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3262 | if( ret != 0 ) |
| 3263 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3264 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3265 | return( ret ); |
| 3266 | } |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3267 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 3268 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 3269 | MBEDTLS_DEBUG_ECDH_Q ); |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3270 | } |
| 3271 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3272 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 72fb62d | 2013-10-14 14:01:58 +0200 | [diff] [blame] | 3273 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3274 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3275 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3276 | } |
| 3277 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3278 | if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3279 | ciphersuite_info->key_exchange ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3280 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3281 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3282 | return( ret ); |
| 3283 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3284 | } |
| 3285 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3286 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
| 3287 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 3288 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3289 | { |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3290 | i = 4; |
| 3291 | if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 3292 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3293 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3294 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3295 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3296 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 3297 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 3298 | { |
| 3299 | i = 4; |
| 3300 | |
| 3301 | ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3302 | ssl->out_msg + i, MBEDTLS_SSL_OUT_CONTENT_LEN - i, &n, |
Manuel Pégourié-Gonnard | 0f1660a | 2015-09-16 22:41:06 +0200 | [diff] [blame] | 3303 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3304 | if( ret != 0 ) |
| 3305 | { |
| 3306 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret ); |
| 3307 | return( ret ); |
| 3308 | } |
| 3309 | |
| 3310 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, |
| 3311 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, |
| 3312 | ssl->conf->f_rng, ssl->conf->p_rng ); |
| 3313 | if( ret != 0 ) |
| 3314 | { |
| 3315 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); |
| 3316 | return( ret ); |
| 3317 | } |
| 3318 | } |
| 3319 | else |
| 3320 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3321 | { |
| 3322 | ((void) ciphersuite_info); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3323 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3324 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3325 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3326 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3327 | ssl->out_msglen = i + n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3328 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3329 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3330 | |
| 3331 | ssl->state++; |
| 3332 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3333 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3334 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3335 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3336 | return( ret ); |
| 3337 | } |
| 3338 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3339 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3340 | |
| 3341 | return( 0 ); |
| 3342 | } |
| 3343 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3344 | #if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3345 | static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3346 | { |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3347 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3348 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3349 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3350 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3351 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3353 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3354 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3355 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3356 | return( ret ); |
| 3357 | } |
| 3358 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3359 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3360 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3361 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3362 | ssl->state++; |
| 3363 | return( 0 ); |
| 3364 | } |
| 3365 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3366 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3367 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3368 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3369 | #else /* !MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3370 | static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3371 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3372 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | 0d0cd4b | 2017-05-11 14:06:43 +0100 | [diff] [blame] | 3373 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3374 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3375 | size_t n = 0, offset = 0; |
| 3376 | unsigned char hash[48]; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3377 | unsigned char *hash_start = hash; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3378 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 3379 | size_t hashlen; |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3380 | void *rs_ctx = NULL; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3381 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3382 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3383 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3384 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3385 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3386 | ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign ) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3387 | { |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3388 | goto sign; |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3389 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3390 | #endif |
| 3391 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3392 | if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3393 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3394 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 3395 | return( ret ); |
| 3396 | } |
| 3397 | |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3398 | if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3399 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3400 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3401 | ssl->state++; |
| 3402 | return( 0 ); |
| 3403 | } |
| 3404 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3405 | if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3406 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3407 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3408 | ssl->state++; |
| 3409 | return( 0 ); |
| 3410 | } |
| 3411 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3412 | if( mbedtls_ssl_own_key( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3413 | { |
Manuel Pégourié-Gonnard | b4b19f3 | 2015-07-07 11:41:21 +0200 | [diff] [blame] | 3414 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3415 | return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | /* |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3419 | * Make a signature of the handshake digests |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3420 | */ |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 3421 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 3422 | if( ssl->handshake->ecrs_enabled ) |
| 3423 | ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign; |
| 3424 | |
| 3425 | sign: |
| 3426 | #endif |
| 3427 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 3428 | ssl->handshake->calc_verify( ssl, hash, &hashlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3429 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3430 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 3431 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 3432 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3433 | { |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3434 | /* |
| 3435 | * digitally-signed struct { |
| 3436 | * opaque md5_hash[16]; |
| 3437 | * opaque sha_hash[20]; |
| 3438 | * }; |
| 3439 | * |
| 3440 | * md5_hash |
| 3441 | * MD5(handshake_messages); |
| 3442 | * |
| 3443 | * sha_hash |
| 3444 | * SHA(handshake_messages); |
| 3445 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3446 | md_alg = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3447 | |
| 3448 | /* |
| 3449 | * For ECDSA, default hash is SHA-1 only |
| 3450 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3451 | if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) ) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3452 | { |
| 3453 | hash_start += 16; |
| 3454 | hashlen -= 16; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3455 | md_alg = MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3456 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3457 | } |
| 3458 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3459 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 3460 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 3461 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3462 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3463 | { |
| 3464 | /* |
| 3465 | * digitally-signed struct { |
| 3466 | * opaque handshake_messages[handshake_messages_length]; |
| 3467 | * }; |
| 3468 | * |
| 3469 | * Taking shortcut here. We assume that the server always allows the |
| 3470 | * PRF Hash function and has sent it in the allowed signature |
| 3471 | * algorithms list received in the Certificate Request message. |
| 3472 | * |
| 3473 | * Until we encounter a server that does not, we will take this |
| 3474 | * shortcut. |
| 3475 | * |
| 3476 | * Reason: Otherwise we should have running hashes for SHA512 and SHA224 |
| 3477 | * in order to satisfy 'weird' needs from the server side. |
| 3478 | */ |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3479 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3480 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3481 | md_alg = MBEDTLS_MD_SHA384; |
| 3482 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3483 | } |
| 3484 | else |
| 3485 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3486 | md_alg = MBEDTLS_MD_SHA256; |
| 3487 | ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3488 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3489 | ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3490 | |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 3491 | /* Info from md_alg will be used instead */ |
| 3492 | hashlen = 0; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3493 | offset = 2; |
| 3494 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3495 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3496 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3497 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3498 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3499 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3500 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3501 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3502 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | d27d1a5 | 2017-08-15 11:49:08 +0200 | [diff] [blame] | 3503 | if( ssl->handshake->ecrs_enabled ) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 3504 | rs_ctx = &ssl->handshake->ecrs_ctx.pk; |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3505 | #endif |
| 3506 | |
| 3507 | if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ), |
| 3508 | md_alg, hash_start, hashlen, |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 3509 | ssl->out_msg + 6 + offset, &n, |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3510 | ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 76c18a1 | 2013-08-20 16:50:40 +0200 | [diff] [blame] | 3511 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3512 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret ); |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 3513 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 3514 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 3515 | ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 3516 | #endif |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 3517 | return( ret ); |
Manuel Pégourié-Gonnard | 76c18a1 | 2013-08-20 16:50:40 +0200 | [diff] [blame] | 3518 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3519 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3520 | ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 ); |
| 3521 | ssl->out_msg[5 + offset] = (unsigned char)( n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3522 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3523 | ssl->out_msglen = 6 + n + offset; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3524 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3525 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3526 | |
| 3527 | ssl->state++; |
| 3528 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3529 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3530 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3531 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3532 | return( ret ); |
| 3533 | } |
| 3534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3535 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3536 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3537 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3538 | } |
Hanno Becker | ae39b9e | 2019-02-07 12:32:43 +0000 | [diff] [blame] | 3539 | #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3540 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3541 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3542 | static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3543 | { |
| 3544 | int ret; |
| 3545 | uint32_t lifetime; |
| 3546 | size_t ticket_len; |
| 3547 | unsigned char *ticket; |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3548 | const unsigned char *msg; |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3549 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3550 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3551 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3552 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3553 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3554 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3555 | return( ret ); |
| 3556 | } |
| 3557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3558 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3559 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3560 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3561 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3562 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3563 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | /* |
| 3567 | * struct { |
| 3568 | * uint32 ticket_lifetime_hint; |
| 3569 | * opaque ticket<0..2^16-1>; |
| 3570 | * } NewSessionTicket; |
| 3571 | * |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3572 | * 0 . 3 ticket_lifetime_hint |
| 3573 | * 4 . 5 ticket_len (n) |
| 3574 | * 6 . 5+n ticket content |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3575 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3576 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET || |
| 3577 | ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3578 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3579 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3580 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3581 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3582 | return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3583 | } |
| 3584 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3585 | msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3586 | |
Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 3587 | lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) | |
| 3588 | ( msg[2] << 8 ) | ( msg[3] ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3589 | |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3590 | ticket_len = ( msg[4] << 8 ) | ( msg[5] ); |
| 3591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3592 | if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3593 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3594 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3595 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3596 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3597 | return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3598 | } |
| 3599 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3600 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3601 | |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 3602 | /* We're not waiting for a NewSessionTicket message any more */ |
| 3603 | ssl->handshake->new_session_ticket = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3604 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 3605 | |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3606 | /* |
| 3607 | * Zero-length ticket means the server changed his mind and doesn't want |
| 3608 | * to send a ticket after all, so just forget it |
| 3609 | */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3610 | if( ticket_len == 0 ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3611 | return( 0 ); |
| 3612 | |
Hanno Becker | 3d699e4 | 2019-01-30 14:46:35 +0000 | [diff] [blame] | 3613 | if( ssl->session != NULL && ssl->session->ticket != NULL ) |
| 3614 | { |
| 3615 | mbedtls_platform_zeroize( ssl->session->ticket, |
| 3616 | ssl->session->ticket_len ); |
| 3617 | mbedtls_free( ssl->session->ticket ); |
| 3618 | ssl->session->ticket = NULL; |
| 3619 | ssl->session->ticket_len = 0; |
| 3620 | } |
| 3621 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3622 | mbedtls_platform_zeroize( ssl->session_negotiate->ticket, |
| 3623 | ssl->session_negotiate->ticket_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3624 | mbedtls_free( ssl->session_negotiate->ticket ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3625 | ssl->session_negotiate->ticket = NULL; |
| 3626 | ssl->session_negotiate->ticket_len = 0; |
| 3627 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3628 | if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3629 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3630 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 3631 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3632 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3633 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3634 | } |
| 3635 | |
Manuel Pégourié-Gonnard | 000d5ae | 2014-09-10 21:52:12 +0200 | [diff] [blame] | 3636 | memcpy( ticket, msg + 6, ticket_len ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3637 | |
| 3638 | ssl->session_negotiate->ticket = ticket; |
| 3639 | ssl->session_negotiate->ticket_len = ticket_len; |
| 3640 | ssl->session_negotiate->ticket_lifetime = lifetime; |
| 3641 | |
| 3642 | /* |
| 3643 | * RFC 5077 section 3.4: |
| 3644 | * "If the client receives a session ticket from the server, then it |
| 3645 | * discards any Session ID that was sent in the ServerHello." |
| 3646 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3647 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) ); |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 3648 | ssl->session_negotiate->id_len = 0; |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3650 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) ); |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3651 | |
| 3652 | return( 0 ); |
| 3653 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3654 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3655 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3656 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3657 | * SSL handshake -- client side -- single step |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3658 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3659 | int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3660 | { |
| 3661 | int ret = 0; |
| 3662 | |
Manuel Pégourié-Gonnard | dba460f | 2015-06-24 22:59:30 +0200 | [diff] [blame] | 3663 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3664 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3665 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3666 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3667 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3668 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3669 | return( ret ); |
| 3670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3671 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3672 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3673 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3674 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3675 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3676 | return( ret ); |
| 3677 | } |
Hanno Becker | bc2498a | 2018-08-28 10:13:29 +0100 | [diff] [blame] | 3678 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3679 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3680 | /* Change state now, so that it is right in mbedtls_ssl_read_record(), used |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 3681 | * by DTLS for dropping out-of-sequence ChangeCipherSpec records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3682 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3683 | if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC && |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 3684 | ssl->handshake->new_session_ticket != 0 ) |
| 3685 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3686 | ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET; |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 3687 | } |
| 3688 | #endif |
| 3689 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3690 | switch( ssl->state ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3691 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3692 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3693 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3694 | break; |
| 3695 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3696 | /* |
| 3697 | * ==> ClientHello |
| 3698 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3699 | case MBEDTLS_SSL_CLIENT_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3700 | ret = ssl_write_client_hello( ssl ); |
| 3701 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3702 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3703 | /* |
| 3704 | * <== ServerHello |
| 3705 | * Certificate |
| 3706 | * ( ServerKeyExchange ) |
| 3707 | * ( CertificateRequest ) |
| 3708 | * ServerHelloDone |
| 3709 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3710 | case MBEDTLS_SSL_SERVER_HELLO: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3711 | ret = ssl_parse_server_hello( ssl ); |
| 3712 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3713 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3714 | case MBEDTLS_SSL_SERVER_CERTIFICATE: |
| 3715 | ret = mbedtls_ssl_parse_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3716 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3717 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3718 | case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3719 | ret = ssl_parse_server_key_exchange( ssl ); |
| 3720 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3721 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3722 | case MBEDTLS_SSL_CERTIFICATE_REQUEST: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3723 | ret = ssl_parse_certificate_request( ssl ); |
| 3724 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3725 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3726 | case MBEDTLS_SSL_SERVER_HELLO_DONE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3727 | ret = ssl_parse_server_hello_done( ssl ); |
| 3728 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3729 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3730 | /* |
| 3731 | * ==> ( Certificate/Alert ) |
| 3732 | * ClientKeyExchange |
| 3733 | * ( CertificateVerify ) |
| 3734 | * ChangeCipherSpec |
| 3735 | * Finished |
| 3736 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3737 | case MBEDTLS_SSL_CLIENT_CERTIFICATE: |
| 3738 | ret = mbedtls_ssl_write_certificate( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3739 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3740 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3741 | case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3742 | ret = ssl_write_client_key_exchange( ssl ); |
| 3743 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3745 | case MBEDTLS_SSL_CERTIFICATE_VERIFY: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3746 | ret = ssl_write_certificate_verify( ssl ); |
| 3747 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3748 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3749 | case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 3750 | ret = mbedtls_ssl_write_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3751 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3753 | case MBEDTLS_SSL_CLIENT_FINISHED: |
| 3754 | ret = mbedtls_ssl_write_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3755 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3756 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3757 | /* |
Manuel Pégourié-Gonnard | a5cc602 | 2013-07-31 12:58:16 +0200 | [diff] [blame] | 3758 | * <== ( NewSessionTicket ) |
| 3759 | * ChangeCipherSpec |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3760 | * Finished |
| 3761 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3762 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3763 | case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET: |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 3764 | ret = ssl_parse_new_session_ticket( ssl ); |
| 3765 | break; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3766 | #endif |
Manuel Pégourié-Gonnard | cd32a50 | 2014-09-20 13:54:12 +0200 | [diff] [blame] | 3767 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3768 | case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 3769 | ret = mbedtls_ssl_parse_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3770 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3772 | case MBEDTLS_SSL_SERVER_FINISHED: |
| 3773 | ret = mbedtls_ssl_parse_finished( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3774 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3775 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3776 | case MBEDTLS_SSL_FLUSH_BUFFERS: |
| 3777 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 3778 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3779 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3780 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3781 | case MBEDTLS_SSL_HANDSHAKE_WRAPUP: |
| 3782 | mbedtls_ssl_handshake_wrapup( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3783 | break; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3784 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3785 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3786 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 3787 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3788 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3789 | |
| 3790 | return( ret ); |
| 3791 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3792 | #endif /* MBEDTLS_SSL_CLI_C */ |