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