Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 server-side functions |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 30 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 31 | #include "polarssl/debug.h" |
| 32 | #include "polarssl/ssl.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 33 | |
| 34 | #include <string.h> |
| 35 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 36 | #if defined(POLARSSL_ECP_C) |
| 37 | #include "polarssl/ecp.h" |
| 38 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 | |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 40 | #if defined(POLARSSL_PLATFORM_C) |
| 41 | #include "polarssl/platform.h" |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 42 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 43 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 44 | #define polarssl_malloc malloc |
| 45 | #define polarssl_free free |
| 46 | #endif |
| 47 | |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 48 | #if defined(POLARSSL_HAVE_TIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 | #include <time.h> |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 50 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 51 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 52 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 53 | /* Implementation that should never be optimized out by the compiler */ |
| 54 | static void polarssl_zeroize( void *v, size_t n ) { |
| 55 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 56 | } |
| 57 | |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 58 | /* |
| 59 | * Serialize a session in the following format: |
| 60 | * 0 . n-1 session structure, n = sizeof(ssl_session) |
| 61 | * n . n+2 peer_cert length = m (0 if no certificate) |
| 62 | * n+3 . n+2+m peer cert ASN.1 |
| 63 | * |
| 64 | * Assumes ticket is NULL (always true on server side). |
| 65 | */ |
Manuel Pégourié-Gonnard | c6554aa | 2013-08-23 11:10:28 +0200 | [diff] [blame] | 66 | static int ssl_save_session( const ssl_session *session, |
| 67 | unsigned char *buf, size_t buf_len, |
| 68 | size_t *olen ) |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 69 | { |
| 70 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | c6554aa | 2013-08-23 11:10:28 +0200 | [diff] [blame] | 71 | size_t left = buf_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 72 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 73 | size_t cert_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 74 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 75 | |
Manuel Pégourié-Gonnard | c6554aa | 2013-08-23 11:10:28 +0200 | [diff] [blame] | 76 | if( left < sizeof( ssl_session ) ) |
| 77 | return( -1 ); |
| 78 | |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 79 | memcpy( p, session, sizeof( ssl_session ) ); |
| 80 | p += sizeof( ssl_session ); |
Manuel Pégourié-Gonnard | c6554aa | 2013-08-23 11:10:28 +0200 | [diff] [blame] | 81 | left -= sizeof( ssl_session ); |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 82 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 83 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 84 | if( session->peer_cert == NULL ) |
| 85 | cert_len = 0; |
| 86 | else |
| 87 | cert_len = session->peer_cert->raw.len; |
| 88 | |
Manuel Pégourié-Gonnard | c6554aa | 2013-08-23 11:10:28 +0200 | [diff] [blame] | 89 | if( left < 3 + cert_len ) |
| 90 | return( -1 ); |
| 91 | |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 92 | *p++ = (unsigned char)( cert_len >> 16 & 0xFF ); |
| 93 | *p++ = (unsigned char)( cert_len >> 8 & 0xFF ); |
| 94 | *p++ = (unsigned char)( cert_len & 0xFF ); |
| 95 | |
| 96 | if( session->peer_cert != NULL ) |
| 97 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 98 | |
| 99 | p += cert_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 100 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 101 | |
| 102 | *olen = p - buf; |
Manuel Pégourié-Gonnard | c6554aa | 2013-08-23 11:10:28 +0200 | [diff] [blame] | 103 | |
| 104 | return( 0 ); |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Unserialise session, see ssl_save_session() |
| 109 | */ |
| 110 | static int ssl_load_session( ssl_session *session, |
| 111 | const unsigned char *buf, size_t len ) |
| 112 | { |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 113 | const unsigned char *p = buf; |
| 114 | const unsigned char * const end = buf + len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 115 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 116 | size_t cert_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 117 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 118 | |
| 119 | if( p + sizeof( ssl_session ) > end ) |
| 120 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
| 121 | |
| 122 | memcpy( session, p, sizeof( ssl_session ) ); |
| 123 | p += sizeof( ssl_session ); |
| 124 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 125 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 126 | if( p + 3 > end ) |
| 127 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
| 128 | |
| 129 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 130 | p += 3; |
| 131 | |
| 132 | if( cert_len == 0 ) |
| 133 | { |
| 134 | session->peer_cert = NULL; |
| 135 | } |
| 136 | else |
| 137 | { |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 138 | int ret; |
| 139 | |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 140 | if( p + cert_len > end ) |
| 141 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
| 142 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 143 | session->peer_cert = polarssl_malloc( sizeof( x509_crt ) ); |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 144 | |
| 145 | if( session->peer_cert == NULL ) |
| 146 | return( POLARSSL_ERR_SSL_MALLOC_FAILED ); |
| 147 | |
Paul Bakker | b6b0956 | 2013-09-18 14:17:41 +0200 | [diff] [blame] | 148 | x509_crt_init( session->peer_cert ); |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 149 | |
Manuel Pégourié-Gonnard | 4d2a8eb | 2014-06-13 20:33:27 +0200 | [diff] [blame] | 150 | if( ( ret = x509_crt_parse_der( session->peer_cert, |
| 151 | p, cert_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 152 | { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 153 | x509_crt_free( session->peer_cert ); |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 154 | polarssl_free( session->peer_cert ); |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 155 | session->peer_cert = NULL; |
| 156 | return( ret ); |
| 157 | } |
| 158 | |
| 159 | p += cert_len; |
| 160 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 161 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 94f6a79 | 2013-08-01 14:33:49 +0200 | [diff] [blame] | 162 | |
| 163 | if( p != end ) |
| 164 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
| 165 | |
| 166 | return( 0 ); |
| 167 | } |
| 168 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 169 | /* |
| 170 | * Create session ticket, secured as recommended in RFC 5077 section 4: |
| 171 | * |
| 172 | * struct { |
| 173 | * opaque key_name[16]; |
| 174 | * opaque iv[16]; |
| 175 | * opaque encrypted_state<0..2^16-1>; |
| 176 | * opaque mac[32]; |
| 177 | * } ticket; |
| 178 | * |
| 179 | * (the internal state structure differs, however). |
| 180 | */ |
| 181 | static int ssl_write_ticket( ssl_context *ssl, size_t *tlen ) |
| 182 | { |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 183 | int ret; |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 184 | unsigned char * const start = ssl->out_msg + 10; |
| 185 | unsigned char *p = start; |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 186 | unsigned char *state; |
| 187 | unsigned char iv[16]; |
| 188 | size_t clear_len, enc_len, pad_len, i; |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 189 | |
Manuel Pégourié-Gonnard | 0a20171 | 2013-08-23 16:25:16 +0200 | [diff] [blame] | 190 | *tlen = 0; |
| 191 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 192 | if( ssl->ticket_keys == NULL ) |
| 193 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
| 194 | |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 195 | /* Write key name */ |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 196 | memcpy( p, ssl->ticket_keys->key_name, 16 ); |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 197 | p += 16; |
| 198 | |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 199 | /* Generate and write IV (with a copy for aes_crypt) */ |
| 200 | if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 ) |
| 201 | return( ret ); |
| 202 | memcpy( iv, p, 16 ); |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 203 | p += 16; |
| 204 | |
Manuel Pégourié-Gonnard | c6554aa | 2013-08-23 11:10:28 +0200 | [diff] [blame] | 205 | /* |
| 206 | * Dump session state |
| 207 | * |
| 208 | * After the session state itself, we still need room for 16 bytes of |
| 209 | * padding and 32 bytes of MAC, so there's only so much room left |
| 210 | */ |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 211 | state = p + 2; |
Manuel Pégourié-Gonnard | c6554aa | 2013-08-23 11:10:28 +0200 | [diff] [blame] | 212 | if( ssl_save_session( ssl->session_negotiate, state, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 213 | SSL_MAX_CONTENT_LEN - ( state - ssl->out_ctr ) - 48, |
Manuel Pégourié-Gonnard | c6554aa | 2013-08-23 11:10:28 +0200 | [diff] [blame] | 214 | &clear_len ) != 0 ) |
| 215 | { |
| 216 | return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
| 217 | } |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 218 | SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len ); |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 219 | |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 220 | /* Apply PKCS padding */ |
| 221 | pad_len = 16 - clear_len % 16; |
| 222 | enc_len = clear_len + pad_len; |
| 223 | for( i = clear_len; i < enc_len; i++ ) |
| 224 | state[i] = (unsigned char) pad_len; |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 225 | |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 226 | /* Encrypt */ |
| 227 | if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT, |
| 228 | enc_len, iv, state, state ) ) != 0 ) |
| 229 | { |
| 230 | return( ret ); |
| 231 | } |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 232 | |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 233 | /* Write length */ |
| 234 | *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF ); |
| 235 | *p++ = (unsigned char)( ( enc_len ) & 0xFF ); |
| 236 | p = state + enc_len; |
| 237 | |
Manuel Pégourié-Gonnard | 56dc9e8 | 2013-08-03 17:16:31 +0200 | [diff] [blame] | 238 | /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */ |
| 239 | sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 ); |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 240 | p += 32; |
| 241 | |
| 242 | *tlen = p - start; |
| 243 | |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 244 | SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen ); |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 245 | |
| 246 | return( 0 ); |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Load session ticket (see ssl_write_ticket for structure) |
| 251 | */ |
| 252 | static int ssl_parse_ticket( ssl_context *ssl, |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 253 | unsigned char *buf, |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 254 | size_t len ) |
| 255 | { |
| 256 | int ret; |
| 257 | ssl_session session; |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 258 | unsigned char *key_name = buf; |
| 259 | unsigned char *iv = buf + 16; |
| 260 | unsigned char *enc_len_p = iv + 16; |
| 261 | unsigned char *ticket = enc_len_p + 2; |
| 262 | unsigned char *mac; |
Manuel Pégourié-Gonnard | 34ced2d | 2013-09-20 11:37:39 +0200 | [diff] [blame] | 263 | unsigned char computed_mac[32]; |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 264 | size_t enc_len, clear_len, i; |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 265 | unsigned char pad_len, diff; |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 266 | |
| 267 | SSL_DEBUG_BUF( 3, "session ticket structure", buf, len ); |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 268 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 269 | if( len < 34 || ssl->ticket_keys == NULL ) |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 270 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
| 271 | |
| 272 | enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1]; |
| 273 | mac = ticket + enc_len; |
| 274 | |
| 275 | if( len != enc_len + 66 ) |
| 276 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
| 277 | |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 278 | /* Check name, in constant time though it's not a big secret */ |
| 279 | diff = 0; |
| 280 | for( i = 0; i < 16; i++ ) |
| 281 | diff |= key_name[i] ^ ssl->ticket_keys->key_name[i]; |
| 282 | /* don't return yet, check the MAC anyway */ |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 283 | |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 284 | /* Check mac, with constant-time buffer comparison */ |
Manuel Pégourié-Gonnard | 56dc9e8 | 2013-08-03 17:16:31 +0200 | [diff] [blame] | 285 | sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32, |
| 286 | computed_mac, 0 ); |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 287 | |
Manuel Pégourié-Gonnard | 56dc9e8 | 2013-08-03 17:16:31 +0200 | [diff] [blame] | 288 | for( i = 0; i < 32; i++ ) |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 289 | diff |= mac[i] ^ computed_mac[i]; |
| 290 | |
| 291 | /* Now return if ticket is not authentic, since we want to avoid |
| 292 | * decrypting arbitrary attacker-chosen data */ |
| 293 | if( diff != 0 ) |
| 294 | return( POLARSSL_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 295 | |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 296 | /* Decrypt */ |
| 297 | if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT, |
| 298 | enc_len, iv, ticket, ticket ) ) != 0 ) |
| 299 | { |
| 300 | return( ret ); |
| 301 | } |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 302 | |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 303 | /* Check PKCS padding */ |
| 304 | pad_len = ticket[enc_len - 1]; |
| 305 | |
| 306 | ret = 0; |
| 307 | for( i = 2; i < pad_len; i++ ) |
| 308 | if( ticket[enc_len - i] != pad_len ) |
| 309 | ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA; |
| 310 | if( ret != 0 ) |
| 311 | return( ret ); |
| 312 | |
| 313 | clear_len = enc_len - pad_len; |
| 314 | |
| 315 | SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len ); |
| 316 | |
| 317 | /* Actually load session */ |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 318 | if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 ) |
| 319 | { |
| 320 | SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) ); |
Manuel Pégourié-Gonnard | d701c9a | 2014-02-26 17:54:07 +0100 | [diff] [blame] | 321 | ssl_session_free( &session ); |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 322 | return( ret ); |
| 323 | } |
| 324 | |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 325 | #if defined(POLARSSL_HAVE_TIME) |
| 326 | /* Check if still valid */ |
| 327 | if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime ) |
| 328 | { |
| 329 | SSL_DEBUG_MSG( 1, ( "session ticket expired" ) ); |
Manuel Pégourié-Gonnard | d701c9a | 2014-02-26 17:54:07 +0100 | [diff] [blame] | 330 | ssl_session_free( &session ); |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 331 | return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED ); |
| 332 | } |
| 333 | #endif |
| 334 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 335 | /* |
| 336 | * Keep the session ID sent by the client, since we MUST send it back to |
| 337 | * inform him we're accepting the ticket (RFC 5077 section 3.4) |
| 338 | */ |
| 339 | session.length = ssl->session_negotiate->length; |
| 340 | memcpy( &session.id, ssl->session_negotiate->id, session.length ); |
| 341 | |
| 342 | ssl_session_free( ssl->session_negotiate ); |
| 343 | memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 344 | |
| 345 | /* Zeroize instead of free as we copied the content */ |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 346 | polarssl_zeroize( &session, sizeof( ssl_session ) ); |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 347 | |
| 348 | return( 0 ); |
| 349 | } |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 350 | #endif /* POLARSSL_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 351 | |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 352 | #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 353 | /* |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 354 | * Wrapper around f_sni, allowing use of ssl_set_own_cert() but |
| 355 | * making it act on ssl->hanshake->sni_key_cert instead. |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 356 | */ |
| 357 | static int ssl_sni_wrapper( ssl_context *ssl, |
| 358 | const unsigned char* name, size_t len ) |
| 359 | { |
| 360 | int ret; |
| 361 | ssl_key_cert *key_cert_ori = ssl->key_cert; |
| 362 | |
| 363 | ssl->key_cert = NULL; |
| 364 | ret = ssl->f_sni( ssl->p_sni, ssl, name, len ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 365 | ssl->handshake->sni_key_cert = ssl->key_cert; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 366 | |
| 367 | ssl->key_cert = key_cert_ori; |
| 368 | |
| 369 | return( ret ); |
| 370 | } |
| 371 | |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 372 | static int ssl_parse_servername_ext( ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 373 | const unsigned char *buf, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 374 | size_t len ) |
| 375 | { |
| 376 | int ret; |
| 377 | size_t servername_list_size, hostname_len; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 378 | const unsigned char *p; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 379 | |
Manuel Pégourié-Gonnard | 96ea2f2 | 2014-02-25 12:26:29 +0100 | [diff] [blame] | 380 | SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
| 381 | |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 382 | servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); |
| 383 | if( servername_list_size + 2 != len ) |
| 384 | { |
| 385 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 386 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 387 | } |
| 388 | |
| 389 | p = buf + 2; |
| 390 | while( servername_list_size > 0 ) |
| 391 | { |
| 392 | hostname_len = ( ( p[1] << 8 ) | p[2] ); |
| 393 | if( hostname_len + 3 > servername_list_size ) |
| 394 | { |
| 395 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 396 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 397 | } |
| 398 | |
| 399 | if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME ) |
| 400 | { |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 401 | ret = ssl_sni_wrapper( ssl, p + 3, hostname_len ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 402 | if( ret != 0 ) |
| 403 | { |
Manuel Pégourié-Gonnard | 96ea2f2 | 2014-02-25 12:26:29 +0100 | [diff] [blame] | 404 | SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 405 | ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL, |
| 406 | SSL_ALERT_MSG_UNRECOGNIZED_NAME ); |
| 407 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 408 | } |
Paul Bakker | 81420ab | 2012-10-23 10:31:15 +0000 | [diff] [blame] | 409 | return( 0 ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | servername_list_size -= hostname_len + 3; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 413 | p += hostname_len + 3; |
| 414 | } |
| 415 | |
| 416 | if( servername_list_size != 0 ) |
| 417 | { |
| 418 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 419 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return( 0 ); |
| 423 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 424 | #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 425 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 426 | static int ssl_parse_renegotiation_info( ssl_context *ssl, |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 427 | const unsigned char *buf, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 428 | size_t len ) |
| 429 | { |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 430 | int ret; |
| 431 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 432 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
| 433 | if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) |
| 434 | { |
| 435 | /* Check verify-data in constant-time. The length OTOH is no secret */ |
| 436 | if( len != 1 + ssl->verify_data_len || |
| 437 | buf[0] != ssl->verify_data_len || |
| 438 | safer_memcmp( buf + 1, ssl->peer_verify_data, |
| 439 | ssl->verify_data_len ) != 0 ) |
| 440 | { |
| 441 | SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) ); |
| 442 | |
| 443 | if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) |
| 444 | return( ret ); |
| 445 | |
| 446 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 447 | } |
| 448 | } |
| 449 | else |
| 450 | #endif /* POLARSSL_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 451 | { |
| 452 | if( len != 1 || buf[0] != 0x0 ) |
| 453 | { |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 454 | SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 455 | |
| 456 | if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) |
| 457 | return( ret ); |
| 458 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 459 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 460 | } |
| 461 | |
| 462 | ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION; |
| 463 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 464 | |
| 465 | return( 0 ); |
| 466 | } |
| 467 | |
Manuel Pégourié-Gonnard | d942323 | 2014-12-02 11:57:29 +0100 | [diff] [blame] | 468 | #if defined(POLARSSL_SSL_PROTO_TLS1_2) && \ |
| 469 | defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 470 | static int ssl_parse_signature_algorithms_ext( ssl_context *ssl, |
| 471 | const unsigned char *buf, |
| 472 | size_t len ) |
| 473 | { |
| 474 | size_t sig_alg_list_size; |
| 475 | const unsigned char *p; |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 476 | const unsigned char *end = buf + len; |
| 477 | const int *md_cur; |
| 478 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 479 | |
| 480 | sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); |
| 481 | if( sig_alg_list_size + 2 != len || |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 482 | sig_alg_list_size % 2 != 0 ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 483 | { |
| 484 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 485 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 486 | } |
| 487 | |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 488 | /* |
| 489 | * For now, ignore the SignatureAlgorithm part and rely on offered |
| 490 | * ciphersuites only for that part. To be fixed later. |
| 491 | * |
| 492 | * So, just look at the HashAlgorithm part. |
| 493 | */ |
| 494 | for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) { |
| 495 | for( p = buf + 2; p < end; p += 2 ) { |
| 496 | if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) { |
| 497 | ssl->handshake->sig_alg = p[0]; |
Manuel Pégourié-Gonnard | 480905d | 2014-08-21 19:38:32 +0200 | [diff] [blame] | 498 | goto have_sig_alg; |
Manuel Pégourié-Gonnard | 08e81e0 | 2014-07-08 12:56:25 +0200 | [diff] [blame] | 499 | } |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 500 | } |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Manuel Pégourié-Gonnard | 480905d | 2014-08-21 19:38:32 +0200 | [diff] [blame] | 503 | /* Some key echanges do not need signatures at all */ |
| 504 | SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) ); |
| 505 | return( 0 ); |
| 506 | |
| 507 | have_sig_alg: |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 508 | SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d", |
| 509 | ssl->handshake->sig_alg ) ); |
| 510 | |
| 511 | return( 0 ); |
| 512 | } |
Manuel Pégourié-Gonnard | d942323 | 2014-12-02 11:57:29 +0100 | [diff] [blame] | 513 | #endif /* POLARSSL_SSL_PROTO_TLS1_2 && |
| 514 | POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 515 | |
Manuel Pégourié-Gonnard | 0b27267 | 2013-08-15 19:38:07 +0200 | [diff] [blame] | 516 | #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 517 | static int ssl_parse_supported_elliptic_curves( ssl_context *ssl, |
| 518 | const unsigned char *buf, |
| 519 | size_t len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 520 | { |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 521 | size_t list_size, our_size; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 522 | const unsigned char *p; |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 523 | const ecp_curve_info *curve_info, **curves; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 524 | |
| 525 | list_size = ( ( buf[0] << 8 ) | ( buf[1] ) ); |
| 526 | if( list_size + 2 != len || |
| 527 | list_size % 2 != 0 ) |
| 528 | { |
| 529 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 530 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 531 | } |
| 532 | |
Manuel Pégourié-Gonnard | 43c3b28 | 2014-10-17 12:42:11 +0200 | [diff] [blame] | 533 | /* Should never happen unless client duplicates the extension */ |
| 534 | if( ssl->handshake->curves != NULL ) |
| 535 | { |
| 536 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 537 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 538 | } |
| 539 | |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 540 | /* Don't allow our peer to make us allocate too much memory, |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 541 | * and leave room for a final 0 */ |
| 542 | our_size = list_size / 2 + 1; |
| 543 | if( our_size > POLARSSL_ECP_DP_MAX ) |
| 544 | our_size = POLARSSL_ECP_DP_MAX; |
| 545 | |
| 546 | if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL ) |
| 547 | return( POLARSSL_ERR_SSL_MALLOC_FAILED ); |
| 548 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 549 | /* explicit void pointer cast for buggy MS compiler */ |
Paul Bakker | beccd9f | 2013-10-11 15:20:27 +0200 | [diff] [blame] | 550 | memset( (void *) curves, 0, our_size * sizeof( *curves ) ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 551 | ssl->handshake->curves = curves; |
| 552 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 553 | p = buf + 2; |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 554 | while( list_size > 0 && our_size > 1 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 555 | { |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 556 | curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] ); |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 557 | |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 558 | if( curve_info != NULL ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 559 | { |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 560 | *curves++ = curve_info; |
| 561 | our_size--; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | list_size -= 2; |
| 565 | p += 2; |
| 566 | } |
| 567 | |
| 568 | return( 0 ); |
| 569 | } |
| 570 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 571 | static int ssl_parse_supported_point_formats( ssl_context *ssl, |
| 572 | const unsigned char *buf, |
| 573 | size_t len ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 574 | { |
| 575 | size_t list_size; |
| 576 | const unsigned char *p; |
| 577 | |
| 578 | list_size = buf[0]; |
| 579 | if( list_size + 1 != len ) |
| 580 | { |
| 581 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 582 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 583 | } |
| 584 | |
| 585 | p = buf + 2; |
| 586 | while( list_size > 0 ) |
| 587 | { |
| 588 | if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED || |
| 589 | p[0] == POLARSSL_ECP_PF_COMPRESSED ) |
| 590 | { |
Manuel Pégourié-Gonnard | 5734b2d | 2013-08-15 19:04:02 +0200 | [diff] [blame] | 591 | ssl->handshake->ecdh_ctx.point_format = p[0]; |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 592 | SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 593 | return( 0 ); |
| 594 | } |
| 595 | |
| 596 | list_size--; |
| 597 | p++; |
| 598 | } |
| 599 | |
| 600 | return( 0 ); |
| 601 | } |
Manuel Pégourié-Gonnard | 0b27267 | 2013-08-15 19:38:07 +0200 | [diff] [blame] | 602 | #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 603 | |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 604 | #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 605 | static int ssl_parse_max_fragment_length_ext( ssl_context *ssl, |
| 606 | const unsigned char *buf, |
| 607 | size_t len ) |
| 608 | { |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 609 | if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID ) |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 610 | { |
| 611 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 612 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 613 | } |
| 614 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 615 | ssl->session_negotiate->mfl_code = buf[0]; |
| 616 | |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 617 | return( 0 ); |
| 618 | } |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 619 | #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 620 | |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 621 | #if defined(POLARSSL_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 622 | static int ssl_parse_truncated_hmac_ext( ssl_context *ssl, |
| 623 | const unsigned char *buf, |
| 624 | size_t len ) |
| 625 | { |
| 626 | if( len != 0 ) |
| 627 | { |
| 628 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 629 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 630 | } |
| 631 | |
| 632 | ((void) buf); |
| 633 | |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 634 | if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED ) |
| 635 | ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED; |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 636 | |
| 637 | return( 0 ); |
| 638 | } |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 639 | #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 640 | |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 641 | #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) |
| 642 | static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl, |
| 643 | const unsigned char *buf, |
| 644 | size_t len ) |
| 645 | { |
| 646 | if( len != 0 ) |
| 647 | { |
| 648 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 649 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 650 | } |
| 651 | |
| 652 | ((void) buf); |
| 653 | |
| 654 | if( ssl->encrypt_then_mac == SSL_ETM_ENABLED && |
| 655 | ssl->minor_ver != SSL_MINOR_VERSION_0 ) |
| 656 | { |
| 657 | ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED; |
| 658 | } |
| 659 | |
| 660 | return( 0 ); |
| 661 | } |
| 662 | #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ |
| 663 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 664 | #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) |
| 665 | static int ssl_parse_extended_ms_ext( ssl_context *ssl, |
| 666 | const unsigned char *buf, |
| 667 | size_t len ) |
| 668 | { |
| 669 | if( len != 0 ) |
| 670 | { |
| 671 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 672 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 673 | } |
| 674 | |
| 675 | ((void) buf); |
| 676 | |
Manuel Pégourié-Gonnard | b575b54 | 2014-10-24 15:12:31 +0200 | [diff] [blame] | 677 | if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED && |
| 678 | ssl->minor_ver != SSL_MINOR_VERSION_0 ) |
| 679 | { |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 680 | ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED; |
Manuel Pégourié-Gonnard | b575b54 | 2014-10-24 15:12:31 +0200 | [diff] [blame] | 681 | } |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 682 | |
| 683 | return( 0 ); |
| 684 | } |
| 685 | #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ |
| 686 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 687 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 688 | static int ssl_parse_session_ticket_ext( ssl_context *ssl, |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 689 | unsigned char *buf, |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 690 | size_t len ) |
| 691 | { |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 692 | int ret; |
| 693 | |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 694 | if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ) |
| 695 | return( 0 ); |
| 696 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 697 | /* Remember the client asked us to send a new ticket */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 698 | ssl->handshake->new_session_ticket = 1; |
| 699 | |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 700 | SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) ); |
| 701 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 702 | if( len == 0 ) |
| 703 | return( 0 ); |
| 704 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 705 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 706 | if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) |
| 707 | { |
| 708 | SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) ); |
| 709 | return( 0 ); |
| 710 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 711 | #endif /* POLARSSL_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 712 | |
| 713 | /* |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 714 | * Failures are ok: just ignore the ticket and proceed. |
| 715 | */ |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 716 | if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 ) |
| 717 | { |
| 718 | SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret ); |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 719 | return( 0 ); |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 720 | } |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 721 | |
| 722 | SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) ); |
| 723 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 724 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 725 | |
Manuel Pégourié-Gonnard | 306827e | 2013-08-02 18:05:14 +0200 | [diff] [blame] | 726 | /* Don't send a new ticket after all, this one is OK */ |
| 727 | ssl->handshake->new_session_ticket = 0; |
| 728 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 729 | return( 0 ); |
| 730 | } |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 731 | #endif /* POLARSSL_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 732 | |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 733 | #if defined(POLARSSL_SSL_ALPN) |
| 734 | static int ssl_parse_alpn_ext( ssl_context *ssl, |
Manuel Pégourié-Gonnard | 14beb08 | 2014-07-08 13:50:35 +0200 | [diff] [blame] | 735 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 736 | { |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 737 | size_t list_len, cur_len, ours_len; |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 738 | const unsigned char *theirs, *start, *end; |
| 739 | const char **ours; |
| 740 | |
| 741 | /* If ALPN not configured, just ignore the extension */ |
| 742 | if( ssl->alpn_list == NULL ) |
| 743 | return( 0 ); |
| 744 | |
| 745 | /* |
| 746 | * opaque ProtocolName<1..2^8-1>; |
| 747 | * |
| 748 | * struct { |
| 749 | * ProtocolName protocol_name_list<2..2^16-1> |
| 750 | * } ProtocolNameList; |
| 751 | */ |
| 752 | |
| 753 | /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */ |
| 754 | if( len < 4 ) |
| 755 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 756 | |
| 757 | list_len = ( buf[0] << 8 ) | buf[1]; |
| 758 | if( list_len != len - 2 ) |
| 759 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 760 | |
| 761 | /* |
| 762 | * Use our order of preference |
| 763 | */ |
| 764 | start = buf + 2; |
| 765 | end = buf + len; |
| 766 | for( ours = ssl->alpn_list; *ours != NULL; ours++ ) |
| 767 | { |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 768 | ours_len = strlen( *ours ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 769 | for( theirs = start; theirs != end; theirs += cur_len ) |
| 770 | { |
| 771 | /* If the list is well formed, we should get equality first */ |
| 772 | if( theirs > end ) |
| 773 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 774 | |
| 775 | cur_len = *theirs++; |
| 776 | |
| 777 | /* Empty strings MUST NOT be included */ |
| 778 | if( cur_len == 0 ) |
| 779 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 780 | |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 781 | if( cur_len == ours_len && |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 782 | memcmp( theirs, *ours, cur_len ) == 0 ) |
| 783 | { |
| 784 | ssl->alpn_chosen = *ours; |
| 785 | return( 0 ); |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* If we get there, no match was found */ |
| 791 | ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL, |
| 792 | SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL ); |
| 793 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 794 | } |
| 795 | #endif /* POLARSSL_SSL_ALPN */ |
| 796 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 797 | /* |
| 798 | * Auxiliary functions for ServerHello parsing and related actions |
| 799 | */ |
| 800 | |
| 801 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
| 802 | /* |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 803 | * Return 0 if the given key uses one of the acceptable curves, -1 otherwise |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 804 | */ |
| 805 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 806 | static int ssl_check_key_curve( pk_context *pk, |
| 807 | const ecp_curve_info **curves ) |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 808 | { |
| 809 | const ecp_curve_info **crv = curves; |
| 810 | ecp_group_id grp_id = pk_ec( *pk )->grp.id; |
| 811 | |
| 812 | while( *crv != NULL ) |
| 813 | { |
| 814 | if( (*crv)->grp_id == grp_id ) |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 815 | return( 0 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 816 | crv++; |
| 817 | } |
| 818 | |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 819 | return( -1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 820 | } |
| 821 | #endif /* POLARSSL_ECDSA_C */ |
| 822 | |
| 823 | /* |
| 824 | * Try picking a certificate for this ciphersuite, |
| 825 | * return 0 on success and -1 on failure. |
| 826 | */ |
| 827 | static int ssl_pick_cert( ssl_context *ssl, |
| 828 | const ssl_ciphersuite_t * ciphersuite_info ) |
| 829 | { |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 830 | ssl_key_cert *cur, *list, *fallback = NULL; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 831 | pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ); |
| 832 | |
| 833 | #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) |
| 834 | if( ssl->handshake->sni_key_cert != NULL ) |
| 835 | list = ssl->handshake->sni_key_cert; |
| 836 | else |
| 837 | #endif |
| 838 | list = ssl->handshake->key_cert; |
| 839 | |
| 840 | if( pk_alg == POLARSSL_PK_NONE ) |
| 841 | return( 0 ); |
| 842 | |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 843 | SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) ); |
| 844 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 845 | for( cur = list; cur != NULL; cur = cur->next ) |
| 846 | { |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 847 | SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate", |
| 848 | cur->cert ); |
| 849 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 850 | if( ! pk_can_do( cur->key, pk_alg ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 851 | { |
| 852 | SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 853 | continue; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 854 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 855 | |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 856 | /* |
| 857 | * This avoids sending the client a cert it'll reject based on |
| 858 | * keyUsage or other extensions. |
| 859 | * |
| 860 | * It also allows the user to provision different certificates for |
| 861 | * different uses based on keyUsage, eg if they want to avoid signing |
| 862 | * and decrypting with the same RSA key. |
| 863 | */ |
| 864 | if( ssl_check_cert_usage( cur->cert, ciphersuite_info, |
| 865 | SSL_IS_SERVER ) != 0 ) |
| 866 | { |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 867 | SSL_DEBUG_MSG( 3, ( "certificate mismatch: " |
| 868 | "(extended) key usage extension" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 869 | continue; |
| 870 | } |
| 871 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 872 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 873 | if( pk_alg == POLARSSL_PK_ECDSA && |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 874 | ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 875 | { |
| 876 | SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) ); |
Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 877 | continue; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 878 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 879 | #endif |
Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 880 | |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 881 | /* |
| 882 | * Try to select a SHA-1 certificate for pre-1.2 clients, but still |
| 883 | * present them a SHA-higher cert rather than failing if it's the only |
| 884 | * one we got that satisfies the other conditions. |
| 885 | */ |
| 886 | if( ssl->minor_ver < SSL_MINOR_VERSION_3 && |
| 887 | cur->cert->sig_md != POLARSSL_MD_SHA1 ) |
| 888 | { |
| 889 | if( fallback == NULL ) |
| 890 | fallback = cur; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 891 | { |
| 892 | SSL_DEBUG_MSG( 3, ( "certificate not preferred: " |
| 893 | "sha-2 with pre-TLS 1.2 client" ) ); |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 894 | continue; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 895 | } |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 896 | } |
| 897 | |
Manuel Pégourié-Gonnard | 846ba47 | 2015-01-08 13:54:38 +0100 | [diff] [blame] | 898 | /* If we get there, we got a winner */ |
| 899 | break; |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 902 | if( cur == NULL ) |
| 903 | cur = fallback; |
| 904 | |
| 905 | |
| 906 | /* Do not update ssl->handshake->key_cert unless the is a match */ |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 907 | if( cur != NULL ) |
| 908 | { |
| 909 | ssl->handshake->key_cert = cur; |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 910 | SSL_DEBUG_CRT( 3, "selected certificate chain, certificate", |
| 911 | ssl->handshake->key_cert->cert ); |
Manuel Pégourié-Gonnard | df331a5 | 2015-01-08 16:43:07 +0100 | [diff] [blame] | 912 | return( 0 ); |
| 913 | } |
| 914 | |
| 915 | return( -1 ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 916 | } |
| 917 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
| 918 | |
| 919 | /* |
| 920 | * Check if a given ciphersuite is suitable for use with our config/keys/etc |
| 921 | * Sets ciphersuite_info only if the suite matches. |
| 922 | */ |
| 923 | static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id, |
| 924 | const ssl_ciphersuite_t **ciphersuite_info ) |
| 925 | { |
| 926 | const ssl_ciphersuite_t *suite_info; |
| 927 | |
| 928 | suite_info = ssl_ciphersuite_from_id( suite_id ); |
| 929 | if( suite_info == NULL ) |
| 930 | { |
Manuel Pégourié-Gonnard | 6458e3b | 2015-01-08 14:16:56 +0100 | [diff] [blame] | 931 | SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 932 | return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 933 | } |
| 934 | |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 935 | SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) ); |
| 936 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 937 | if( suite_info->min_minor_ver > ssl->minor_ver || |
| 938 | suite_info->max_minor_ver < ssl->minor_ver ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 939 | { |
| 940 | SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 941 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 942 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 943 | |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 944 | if( ssl->arc4_disabled == SSL_ARC4_DISABLED && |
| 945 | suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 946 | { |
| 947 | SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) ); |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 948 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 949 | } |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 950 | |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 951 | #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C) |
| 952 | if( ssl_ciphersuite_uses_ec( suite_info ) && |
| 953 | ( ssl->handshake->curves == NULL || |
| 954 | ssl->handshake->curves[0] == NULL ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 955 | { |
| 956 | SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " |
| 957 | "no common elliptic curve" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 958 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 959 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 960 | #endif |
| 961 | |
| 962 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 963 | /* If the ciphersuite requires a pre-shared key and we don't |
| 964 | * have one, skip it now rather than failing later */ |
| 965 | if( ssl_ciphersuite_uses_psk( suite_info ) && |
| 966 | ssl->f_psk == NULL && |
| 967 | ( ssl->psk == NULL || ssl->psk_identity == NULL || |
| 968 | ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 969 | { |
| 970 | SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 971 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 972 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 973 | #endif |
| 974 | |
| 975 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
| 976 | /* |
| 977 | * Final check: if ciphersuite requires us to have a |
| 978 | * certificate/key of a particular type: |
| 979 | * - select the appropriate certificate if we have one, or |
| 980 | * - try the next ciphersuite if we don't |
| 981 | * This must be done last since we modify the key_cert list. |
| 982 | */ |
| 983 | if( ssl_pick_cert( ssl, suite_info ) != 0 ) |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 984 | { |
| 985 | SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: " |
| 986 | "no suitable certificate" ) ); |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 987 | return( 0 ); |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 988 | } |
Manuel Pégourié-Gonnard | 3252560 | 2013-11-30 17:50:32 +0100 | [diff] [blame] | 989 | #endif |
| 990 | |
| 991 | *ciphersuite_info = suite_info; |
| 992 | return( 0 ); |
| 993 | } |
| 994 | |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 995 | #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) |
| 996 | static int ssl_parse_client_hello_v2( ssl_context *ssl ) |
| 997 | { |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 998 | int ret, got_common_suite; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 999 | unsigned int i, j; |
| 1000 | size_t n; |
| 1001 | unsigned int ciph_len, sess_len, chal_len; |
| 1002 | unsigned char *buf, *p; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1003 | const int *ciphersuites; |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1004 | const ssl_ciphersuite_t *ciphersuite_info; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1005 | |
| 1006 | SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); |
| 1007 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1008 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1009 | if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) |
| 1010 | { |
| 1011 | SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) ); |
| 1012 | |
| 1013 | if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) |
| 1014 | return( ret ); |
| 1015 | |
| 1016 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1017 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1018 | #endif /* POLARSSL_SSL_RENEGOTIATION */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1019 | |
| 1020 | buf = ssl->in_hdr; |
| 1021 | |
| 1022 | SSL_DEBUG_BUF( 4, "record header", buf, 5 ); |
| 1023 | |
| 1024 | SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d", |
| 1025 | buf[2] ) ); |
| 1026 | SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d", |
| 1027 | ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) ); |
| 1028 | SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]", |
| 1029 | buf[3], buf[4] ) ); |
| 1030 | |
| 1031 | /* |
| 1032 | * SSLv2 Client Hello |
| 1033 | * |
| 1034 | * Record layer: |
| 1035 | * 0 . 1 message length |
| 1036 | * |
| 1037 | * SSL layer: |
| 1038 | * 2 . 2 message type |
| 1039 | * 3 . 4 protocol version |
| 1040 | */ |
| 1041 | if( buf[2] != SSL_HS_CLIENT_HELLO || |
| 1042 | buf[3] != SSL_MAJOR_VERSION_3 ) |
| 1043 | { |
| 1044 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1045 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1046 | } |
| 1047 | |
| 1048 | n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF; |
| 1049 | |
| 1050 | if( n < 17 || n > 512 ) |
| 1051 | { |
| 1052 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1053 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1054 | } |
| 1055 | |
| 1056 | ssl->major_ver = SSL_MAJOR_VERSION_3; |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1057 | ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver ) |
| 1058 | ? buf[4] : ssl->max_minor_ver; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1059 | |
| 1060 | if( ssl->minor_ver < ssl->min_minor_ver ) |
| 1061 | { |
| 1062 | SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum" |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1063 | " [%d:%d] < [%d:%d]", |
| 1064 | ssl->major_ver, ssl->minor_ver, |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1065 | ssl->min_major_ver, ssl->min_minor_ver ) ); |
| 1066 | |
| 1067 | ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL, |
| 1068 | SSL_ALERT_MSG_PROTOCOL_VERSION ); |
| 1069 | return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
| 1070 | } |
| 1071 | |
Paul Bakker | 2fbefde | 2013-06-29 16:01:15 +0200 | [diff] [blame] | 1072 | ssl->handshake->max_major_ver = buf[3]; |
| 1073 | ssl->handshake->max_minor_ver = buf[4]; |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1074 | |
| 1075 | if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 ) |
| 1076 | { |
| 1077 | SSL_DEBUG_RET( 1, "ssl_fetch_input", ret ); |
| 1078 | return( ret ); |
| 1079 | } |
| 1080 | |
| 1081 | ssl->handshake->update_checksum( ssl, buf + 2, n ); |
| 1082 | |
| 1083 | buf = ssl->in_msg; |
| 1084 | n = ssl->in_left - 5; |
| 1085 | |
| 1086 | /* |
| 1087 | * 0 . 1 ciphersuitelist length |
| 1088 | * 2 . 3 session id length |
| 1089 | * 4 . 5 challenge length |
| 1090 | * 6 . .. ciphersuitelist |
| 1091 | * .. . .. session id |
| 1092 | * .. . .. challenge |
| 1093 | */ |
| 1094 | SSL_DEBUG_BUF( 4, "record contents", buf, n ); |
| 1095 | |
| 1096 | ciph_len = ( buf[0] << 8 ) | buf[1]; |
| 1097 | sess_len = ( buf[2] << 8 ) | buf[3]; |
| 1098 | chal_len = ( buf[4] << 8 ) | buf[5]; |
| 1099 | |
| 1100 | SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d", |
| 1101 | ciph_len, sess_len, chal_len ) ); |
| 1102 | |
| 1103 | /* |
| 1104 | * Make sure each parameter length is valid |
| 1105 | */ |
| 1106 | if( ciph_len < 3 || ( ciph_len % 3 ) != 0 ) |
| 1107 | { |
| 1108 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1109 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1110 | } |
| 1111 | |
| 1112 | if( sess_len > 32 ) |
| 1113 | { |
| 1114 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1115 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1116 | } |
| 1117 | |
| 1118 | if( chal_len < 8 || chal_len > 32 ) |
| 1119 | { |
| 1120 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1121 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1122 | } |
| 1123 | |
| 1124 | if( n != 6 + ciph_len + sess_len + chal_len ) |
| 1125 | { |
| 1126 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1127 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1128 | } |
| 1129 | |
| 1130 | SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
| 1131 | buf + 6, ciph_len ); |
| 1132 | SSL_DEBUG_BUF( 3, "client hello, session id", |
| 1133 | buf + 6 + ciph_len, sess_len ); |
| 1134 | SSL_DEBUG_BUF( 3, "client hello, challenge", |
| 1135 | buf + 6 + ciph_len + sess_len, chal_len ); |
| 1136 | |
| 1137 | p = buf + 6 + ciph_len; |
| 1138 | ssl->session_negotiate->length = sess_len; |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1139 | memset( ssl->session_negotiate->id, 0, |
| 1140 | sizeof( ssl->session_negotiate->id ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1141 | memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length ); |
| 1142 | |
| 1143 | p += sess_len; |
| 1144 | memset( ssl->handshake->randbytes, 0, 64 ); |
| 1145 | memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len ); |
| 1146 | |
| 1147 | /* |
| 1148 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 1149 | */ |
| 1150 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) |
| 1151 | { |
| 1152 | if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO ) |
| 1153 | { |
| 1154 | SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1155 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1156 | if( ssl->renegotiation == SSL_RENEGOTIATION ) |
| 1157 | { |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1158 | SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV " |
| 1159 | "during renegotiation" ) ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1160 | |
| 1161 | if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) |
| 1162 | return( ret ); |
| 1163 | |
| 1164 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1165 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1166 | #endif /* POLARSSL_SSL_RENEGOTIATION */ |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1167 | ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION; |
| 1168 | break; |
| 1169 | } |
| 1170 | } |
| 1171 | |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1172 | #if defined(POLARSSL_SSL_FALLBACK_SCSV) |
| 1173 | for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 ) |
| 1174 | { |
| 1175 | if( p[0] == 0 && |
| 1176 | p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) && |
| 1177 | p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) ) |
| 1178 | { |
| 1179 | SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) ); |
| 1180 | |
| 1181 | if( ssl->minor_ver < ssl->max_minor_ver ) |
| 1182 | { |
| 1183 | SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) ); |
| 1184 | |
| 1185 | ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL, |
| 1186 | SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); |
| 1187 | |
| 1188 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1189 | } |
| 1190 | |
| 1191 | break; |
| 1192 | } |
| 1193 | } |
| 1194 | #endif /* POLARSSL_SSL_FALLBACK_SCSV */ |
| 1195 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1196 | got_common_suite = 0; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1197 | ciphersuites = ssl->ciphersuite_list[ssl->minor_ver]; |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1198 | ciphersuite_info = NULL; |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1199 | #if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
| 1200 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) |
| 1201 | { |
| 1202 | for( i = 0; ciphersuites[i] != 0; i++ ) |
| 1203 | #else |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1204 | for( i = 0; ciphersuites[i] != 0; i++ ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1205 | { |
| 1206 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1207 | #endif |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1208 | { |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1209 | if( p[0] != 0 || |
| 1210 | p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || |
| 1211 | p[2] != ( ( ciphersuites[i] ) & 0xFF ) ) |
| 1212 | continue; |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1213 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1214 | got_common_suite = 1; |
| 1215 | |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1216 | if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], |
| 1217 | &ciphersuite_info ) ) != 0 ) |
| 1218 | return( ret ); |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1219 | |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1220 | if( ciphersuite_info != NULL ) |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1221 | goto have_ciphersuite_v2; |
| 1222 | } |
| 1223 | } |
| 1224 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1225 | if( got_common_suite ) |
| 1226 | { |
| 1227 | SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " |
| 1228 | "but none of them usable" ) ); |
| 1229 | return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE ); |
| 1230 | } |
| 1231 | else |
| 1232 | { |
| 1233 | SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); |
| 1234 | return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN ); |
| 1235 | } |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1236 | |
| 1237 | have_ciphersuite_v2: |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1238 | SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) ); |
| 1239 | |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1240 | ssl->session_negotiate->ciphersuite = ciphersuites[i]; |
Paul Bakker | 59c28a2 | 2013-06-29 15:33:42 +0200 | [diff] [blame] | 1241 | ssl->transform_negotiate->ciphersuite_info = ciphersuite_info; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1242 | ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info ); |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1243 | |
| 1244 | /* |
| 1245 | * SSLv2 Client Hello relevant renegotiation security checks |
| 1246 | */ |
| 1247 | if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION && |
| 1248 | ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE ) |
| 1249 | { |
| 1250 | SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
| 1251 | |
| 1252 | if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) |
| 1253 | return( ret ); |
| 1254 | |
| 1255 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1256 | } |
| 1257 | |
| 1258 | ssl->in_left = 0; |
| 1259 | ssl->state++; |
| 1260 | |
| 1261 | SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) ); |
| 1262 | |
| 1263 | return( 0 ); |
| 1264 | } |
| 1265 | #endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */ |
| 1266 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1267 | static int ssl_parse_client_hello( ssl_context *ssl ) |
| 1268 | { |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1269 | int ret, got_common_suite; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1270 | unsigned int i, j; |
| 1271 | size_t n; |
| 1272 | unsigned int ciph_len, sess_len; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1273 | unsigned int comp_len; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1274 | unsigned int ext_len = 0; |
| 1275 | unsigned char *buf, *p, *ext; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1276 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1277 | int renegotiation_info_seen = 0; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1278 | #endif |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1279 | int handshake_failure = 0; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1280 | const int *ciphersuites; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1281 | const ssl_ciphersuite_t *ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1282 | |
| 1283 | SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) ); |
| 1284 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1285 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
| 1286 | if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE ) |
| 1287 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1288 | { |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1289 | if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 ) |
| 1290 | { |
| 1291 | SSL_DEBUG_RET( 1, "ssl_fetch_input", ret ); |
| 1292 | return( ret ); |
| 1293 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | buf = ssl->in_hdr; |
| 1297 | |
Paul Bakker | 78a8c71 | 2013-03-06 17:01:52 +0100 | [diff] [blame] | 1298 | #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) |
| 1299 | if( ( buf[0] & 0x80 ) != 0 ) |
| 1300 | return ssl_parse_client_hello_v2( ssl ); |
| 1301 | #endif |
| 1302 | |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1303 | SSL_DEBUG_BUF( 4, "record header", buf, 5 ); |
| 1304 | |
| 1305 | SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d", |
| 1306 | buf[0] ) ); |
| 1307 | SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d", |
| 1308 | ( buf[3] << 8 ) | buf[4] ) ); |
| 1309 | SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]", |
| 1310 | buf[1], buf[2] ) ); |
| 1311 | |
| 1312 | /* |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1313 | * SSLv3/TLS Client Hello |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1314 | * |
| 1315 | * Record layer: |
| 1316 | * 0 . 0 message type |
| 1317 | * 1 . 2 protocol version |
| 1318 | * 3 . 4 message length |
| 1319 | */ |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1320 | |
| 1321 | /* According to RFC 5246 Appendix E.1, the version here is typically |
| 1322 | * "{03,00}, the lowest version number supported by the client, [or] the |
| 1323 | * value of ClientHello.client_version", so the only meaningful check here |
| 1324 | * is the major version shouldn't be less than 3 */ |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1325 | if( buf[0] != SSL_MSG_HANDSHAKE || |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1326 | buf[1] < SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1327 | { |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1328 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1329 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1330 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1331 | |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1332 | n = ( buf[3] << 8 ) | buf[4]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1333 | |
Paul Bakker | 4f42c11 | 2014-04-17 14:48:23 +0200 | [diff] [blame] | 1334 | if( n < 45 || n > SSL_MAX_CONTENT_LEN ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1335 | { |
| 1336 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1337 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1338 | } |
| 1339 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1340 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
| 1341 | if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE ) |
| 1342 | #endif |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1343 | { |
Manuel Pégourié-Gonnard | 59c6f2e | 2015-01-22 11:06:40 +0000 | [diff] [blame] | 1344 | if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 ) |
| 1345 | { |
| 1346 | SSL_DEBUG_RET( 1, "ssl_fetch_input", ret ); |
| 1347 | return( ret ); |
| 1348 | } |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | buf = ssl->in_msg; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1352 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
| 1353 | if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1354 | n = ssl->in_msglen; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1355 | else |
| 1356 | #endif |
| 1357 | n = ssl->in_left - 5; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1358 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1359 | ssl->handshake->update_checksum( ssl, buf, n ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1360 | |
| 1361 | /* |
| 1362 | * SSL layer: |
| 1363 | * 0 . 0 handshake type |
| 1364 | * 1 . 3 handshake length |
| 1365 | * 4 . 5 protocol version |
| 1366 | * 6 . 9 UNIX time() |
| 1367 | * 10 . 37 random bytes |
| 1368 | * 38 . 38 session id length |
| 1369 | * 39 . 38+x session id |
| 1370 | * 39+x . 40+x ciphersuitelist length |
Paul Bakker | 0f651c7 | 2014-05-22 15:12:19 +0200 | [diff] [blame] | 1371 | * 41+x . 40+y ciphersuitelist |
| 1372 | * 41+y . 41+y compression alg length |
| 1373 | * 42+y . 41+z compression algs |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1374 | * .. . .. extensions |
| 1375 | */ |
| 1376 | SSL_DEBUG_BUF( 4, "record contents", buf, n ); |
| 1377 | |
| 1378 | SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", |
| 1379 | buf[0] ) ); |
| 1380 | SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d", |
| 1381 | ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) ); |
| 1382 | SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]", |
| 1383 | buf[4], buf[5] ) ); |
| 1384 | |
| 1385 | /* |
| 1386 | * Check the handshake type and protocol version |
| 1387 | */ |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1388 | if( buf[0] != SSL_HS_CLIENT_HELLO ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1389 | { |
| 1390 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1391 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1392 | } |
| 1393 | |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1394 | ssl->major_ver = buf[4]; |
| 1395 | ssl->minor_ver = buf[5]; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1396 | |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1397 | ssl->handshake->max_major_ver = ssl->major_ver; |
| 1398 | ssl->handshake->max_minor_ver = ssl->minor_ver; |
| 1399 | |
| 1400 | if( ssl->major_ver < ssl->min_major_ver || |
| 1401 | ssl->minor_ver < ssl->min_minor_ver ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1402 | { |
| 1403 | SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum" |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1404 | " [%d:%d] < [%d:%d]", |
| 1405 | ssl->major_ver, ssl->minor_ver, |
Paul Bakker | 81420ab | 2012-10-23 10:31:15 +0000 | [diff] [blame] | 1406 | ssl->min_major_ver, ssl->min_minor_ver ) ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1407 | |
| 1408 | ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL, |
| 1409 | SSL_ALERT_MSG_PROTOCOL_VERSION ); |
| 1410 | |
| 1411 | return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION ); |
| 1412 | } |
| 1413 | |
Manuel Pégourié-Gonnard | 6b1e207 | 2014-02-12 10:14:54 +0100 | [diff] [blame] | 1414 | if( ssl->major_ver > ssl->max_major_ver ) |
| 1415 | { |
| 1416 | ssl->major_ver = ssl->max_major_ver; |
| 1417 | ssl->minor_ver = ssl->max_minor_ver; |
| 1418 | } |
| 1419 | else if( ssl->minor_ver > ssl->max_minor_ver ) |
| 1420 | ssl->minor_ver = ssl->max_minor_ver; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1421 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1422 | memcpy( ssl->handshake->randbytes, buf + 6, 32 ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1423 | |
| 1424 | /* |
| 1425 | * Check the handshake message length |
| 1426 | */ |
| 1427 | if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) ) |
| 1428 | { |
| 1429 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1430 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * Check the session length |
| 1435 | */ |
| 1436 | sess_len = buf[38]; |
| 1437 | |
Paul Bakker | 0f651c7 | 2014-05-22 15:12:19 +0200 | [diff] [blame] | 1438 | if( sess_len > 32 || sess_len > n - 42 ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1439 | { |
| 1440 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1441 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1442 | } |
| 1443 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1444 | ssl->session_negotiate->length = sess_len; |
| 1445 | memset( ssl->session_negotiate->id, 0, |
| 1446 | sizeof( ssl->session_negotiate->id ) ); |
| 1447 | memcpy( ssl->session_negotiate->id, buf + 39, |
| 1448 | ssl->session_negotiate->length ); |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1449 | |
| 1450 | /* |
| 1451 | * Check the ciphersuitelist length |
| 1452 | */ |
| 1453 | ciph_len = ( buf[39 + sess_len] << 8 ) |
| 1454 | | ( buf[40 + sess_len] ); |
| 1455 | |
Paul Bakker | 0f651c7 | 2014-05-22 15:12:19 +0200 | [diff] [blame] | 1456 | if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1457 | { |
| 1458 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1459 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1460 | } |
| 1461 | |
| 1462 | /* |
| 1463 | * Check the compression algorithms length |
| 1464 | */ |
| 1465 | comp_len = buf[41 + sess_len + ciph_len]; |
| 1466 | |
Paul Bakker | 0f651c7 | 2014-05-22 15:12:19 +0200 | [diff] [blame] | 1467 | if( comp_len < 1 || comp_len > 16 || |
| 1468 | comp_len > n - 42 - sess_len - ciph_len ) |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1469 | { |
| 1470 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1471 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1472 | } |
| 1473 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1474 | /* |
| 1475 | * Check the extension length |
| 1476 | */ |
| 1477 | if( n > 42 + sess_len + ciph_len + comp_len ) |
| 1478 | { |
| 1479 | ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 ) |
| 1480 | | ( buf[43 + sess_len + ciph_len + comp_len] ); |
| 1481 | |
| 1482 | if( ( ext_len > 0 && ext_len < 4 ) || |
| 1483 | n != 44 + sess_len + ciph_len + comp_len + ext_len ) |
| 1484 | { |
| 1485 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1486 | SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len); |
| 1487 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | ssl->session_negotiate->compression = SSL_COMPRESS_NULL; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1492 | #if defined(POLARSSL_ZLIB_SUPPORT) |
| 1493 | for( i = 0; i < comp_len; ++i ) |
| 1494 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1495 | if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1496 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1497 | ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE; |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1498 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1499 | } |
| 1500 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1501 | #endif |
| 1502 | |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1503 | SSL_DEBUG_BUF( 3, "client hello, random bytes", |
| 1504 | buf + 6, 32 ); |
| 1505 | SSL_DEBUG_BUF( 3, "client hello, session id", |
| 1506 | buf + 38, sess_len ); |
| 1507 | SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist", |
| 1508 | buf + 41 + sess_len, ciph_len ); |
| 1509 | SSL_DEBUG_BUF( 3, "client hello, compression", |
| 1510 | buf + 42 + sess_len + ciph_len, comp_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1511 | |
Paul Bakker | ec636f3 | 2012-09-09 19:17:02 +0000 | [diff] [blame] | 1512 | /* |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1513 | * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV |
| 1514 | */ |
| 1515 | for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 ) |
| 1516 | { |
| 1517 | if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO ) |
| 1518 | { |
| 1519 | SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1520 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1521 | if( ssl->renegotiation == SSL_RENEGOTIATION ) |
| 1522 | { |
| 1523 | SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1524 | |
| 1525 | if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) |
| 1526 | return( ret ); |
| 1527 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1528 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1529 | } |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1530 | renegotiation_info_seen = 1; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1531 | #endif /* POLARSSL_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1532 | ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION; |
| 1533 | break; |
| 1534 | } |
| 1535 | } |
| 1536 | |
Manuel Pégourié-Gonnard | 01b2699 | 2014-10-20 14:05:28 +0200 | [diff] [blame] | 1537 | #if defined(POLARSSL_SSL_FALLBACK_SCSV) |
| 1538 | for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 ) |
| 1539 | { |
| 1540 | if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) && |
| 1541 | p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) ) |
| 1542 | { |
| 1543 | SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) ); |
| 1544 | |
| 1545 | if( ssl->minor_ver < ssl->max_minor_ver ) |
| 1546 | { |
| 1547 | SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) ); |
| 1548 | |
| 1549 | ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL, |
| 1550 | SSL_ALERT_MSG_INAPROPRIATE_FALLBACK ); |
| 1551 | |
| 1552 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1553 | } |
| 1554 | |
| 1555 | break; |
| 1556 | } |
| 1557 | } |
| 1558 | #endif /* POLARSSL_SSL_FALLBACK_SCSV */ |
| 1559 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1560 | ext = buf + 44 + sess_len + ciph_len + comp_len; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1561 | |
| 1562 | while( ext_len ) |
| 1563 | { |
| 1564 | unsigned int ext_id = ( ( ext[0] << 8 ) |
| 1565 | | ( ext[1] ) ); |
| 1566 | unsigned int ext_size = ( ( ext[2] << 8 ) |
| 1567 | | ( ext[3] ) ); |
| 1568 | |
| 1569 | if( ext_size + 4 > ext_len ) |
| 1570 | { |
| 1571 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1572 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1573 | } |
| 1574 | switch( ext_id ) |
| 1575 | { |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 1576 | #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1577 | case TLS_EXT_SERVERNAME: |
| 1578 | SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) ); |
| 1579 | if( ssl->f_sni == NULL ) |
| 1580 | break; |
| 1581 | |
| 1582 | ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size ); |
| 1583 | if( ret != 0 ) |
| 1584 | return( ret ); |
| 1585 | break; |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 1586 | #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1587 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1588 | case TLS_EXT_RENEGOTIATION_INFO: |
| 1589 | SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) ); |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1590 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1591 | renegotiation_info_seen = 1; |
Manuel Pégourié-Gonnard | eaecbd3 | 2014-11-06 02:38:02 +0100 | [diff] [blame] | 1592 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1593 | |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 1594 | ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ); |
| 1595 | if( ret != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1596 | return( ret ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 1597 | break; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1598 | |
Manuel Pégourié-Gonnard | d942323 | 2014-12-02 11:57:29 +0100 | [diff] [blame] | 1599 | #if defined(POLARSSL_SSL_PROTO_TLS1_2) && \ |
| 1600 | defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 1601 | case TLS_EXT_SIG_ALG: |
| 1602 | SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1603 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 1604 | if( ssl->renegotiation == SSL_RENEGOTIATION ) |
| 1605 | break; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1606 | #endif |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 1607 | |
| 1608 | ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size ); |
| 1609 | if( ret != 0 ) |
| 1610 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1611 | break; |
Manuel Pégourié-Gonnard | d942323 | 2014-12-02 11:57:29 +0100 | [diff] [blame] | 1612 | #endif /* POLARSSL_SSL_PROTO_TLS1_2 && |
| 1613 | POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1614 | |
Manuel Pégourié-Gonnard | 0b27267 | 2013-08-15 19:38:07 +0200 | [diff] [blame] | 1615 | #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1616 | case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES: |
| 1617 | SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) ); |
| 1618 | |
| 1619 | ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size ); |
| 1620 | if( ret != 0 ) |
| 1621 | return( ret ); |
| 1622 | break; |
| 1623 | |
| 1624 | case TLS_EXT_SUPPORTED_POINT_FORMATS: |
| 1625 | SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) ); |
Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 1626 | ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1627 | |
| 1628 | ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size ); |
| 1629 | if( ret != 0 ) |
| 1630 | return( ret ); |
| 1631 | break; |
Manuel Pégourié-Gonnard | 0b27267 | 2013-08-15 19:38:07 +0200 | [diff] [blame] | 1632 | #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1633 | |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1634 | #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 1635 | case TLS_EXT_MAX_FRAGMENT_LENGTH: |
| 1636 | SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) ); |
| 1637 | |
| 1638 | ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size ); |
| 1639 | if( ret != 0 ) |
| 1640 | return( ret ); |
| 1641 | break; |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1642 | #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 48f8d0d | 2013-07-17 10:25:37 +0200 | [diff] [blame] | 1643 | |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 1644 | #if defined(POLARSSL_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1645 | case TLS_EXT_TRUNCATED_HMAC: |
| 1646 | SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) ); |
| 1647 | |
| 1648 | ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size ); |
| 1649 | if( ret != 0 ) |
| 1650 | return( ret ); |
| 1651 | break; |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 1652 | #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1653 | |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1654 | #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) |
| 1655 | case TLS_EXT_ENCRYPT_THEN_MAC: |
| 1656 | SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) ); |
| 1657 | |
| 1658 | ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size ); |
| 1659 | if( ret != 0 ) |
| 1660 | return( ret ); |
| 1661 | break; |
| 1662 | #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ |
| 1663 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1664 | #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) |
| 1665 | case TLS_EXT_EXTENDED_MASTER_SECRET: |
| 1666 | SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) ); |
| 1667 | |
| 1668 | ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size ); |
| 1669 | if( ret != 0 ) |
| 1670 | return( ret ); |
| 1671 | break; |
| 1672 | #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ |
| 1673 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1674 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 1675 | case TLS_EXT_SESSION_TICKET: |
| 1676 | SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) ); |
| 1677 | |
| 1678 | ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size ); |
| 1679 | if( ret != 0 ) |
| 1680 | return( ret ); |
| 1681 | break; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1682 | #endif /* POLARSSL_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 1683 | |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 1684 | #if defined(POLARSSL_SSL_ALPN) |
| 1685 | case TLS_EXT_ALPN: |
Manuel Pégourié-Gonnard | f6521de | 2014-04-07 12:42:04 +0200 | [diff] [blame] | 1686 | SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 1687 | |
| 1688 | ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ); |
| 1689 | if( ret != 0 ) |
| 1690 | return( ret ); |
| 1691 | break; |
| 1692 | #endif /* POLARSSL_SSL_SESSION_TICKETS */ |
| 1693 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1694 | default: |
| 1695 | SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)", |
| 1696 | ext_id ) ); |
| 1697 | } |
| 1698 | |
| 1699 | ext_len -= 4 + ext_size; |
| 1700 | ext += 4 + ext_size; |
| 1701 | |
| 1702 | if( ext_len > 0 && ext_len < 4 ) |
| 1703 | { |
| 1704 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
| 1705 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | /* |
| 1710 | * Renegotiation security checks |
| 1711 | */ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1712 | if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION && |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1713 | ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE ) |
| 1714 | { |
| 1715 | SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) ); |
| 1716 | handshake_failure = 1; |
| 1717 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1718 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1719 | else if( ssl->renegotiation == SSL_RENEGOTIATION && |
| 1720 | ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION && |
| 1721 | renegotiation_info_seen == 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1722 | { |
| 1723 | SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1724 | handshake_failure = 1; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1725 | } |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1726 | else if( ssl->renegotiation == SSL_RENEGOTIATION && |
| 1727 | ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION && |
| 1728 | ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1729 | { |
| 1730 | SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1731 | handshake_failure = 1; |
| 1732 | } |
| 1733 | else if( ssl->renegotiation == SSL_RENEGOTIATION && |
| 1734 | ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION && |
| 1735 | renegotiation_info_seen == 1 ) |
| 1736 | { |
| 1737 | SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) ); |
| 1738 | handshake_failure = 1; |
| 1739 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1740 | #endif /* POLARSSL_SSL_RENEGOTIATION */ |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 1741 | |
| 1742 | if( handshake_failure == 1 ) |
| 1743 | { |
| 1744 | if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) |
| 1745 | return( ret ); |
| 1746 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1747 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 1748 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1749 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1750 | /* |
| 1751 | * Search for a matching ciphersuite |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 1752 | * (At the end because we need information from the EC-based extensions |
| 1753 | * and certificate from the SNI callback triggered by the SNI extension.) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1754 | */ |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1755 | got_common_suite = 0; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1756 | ciphersuites = ssl->ciphersuite_list[ssl->minor_ver]; |
Manuel Pégourié-Gonnard | 59b81d7 | 2013-11-30 17:46:04 +0100 | [diff] [blame] | 1757 | ciphersuite_info = NULL; |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1758 | #if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE) |
| 1759 | for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 ) |
| 1760 | { |
| 1761 | for( i = 0; ciphersuites[i] != 0; i++ ) |
| 1762 | #else |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1763 | for( i = 0; ciphersuites[i] != 0; i++ ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1764 | { |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1765 | for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 ) |
Manuel Pégourié-Gonnard | 1a9f2c7 | 2013-11-30 18:30:06 +0100 | [diff] [blame] | 1766 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1767 | { |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1768 | if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || |
| 1769 | p[1] != ( ( ciphersuites[i] ) & 0xFF ) ) |
| 1770 | continue; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1771 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1772 | got_common_suite = 1; |
| 1773 | |
Manuel Pégourié-Gonnard | 011a8db | 2013-11-30 18:11:07 +0100 | [diff] [blame] | 1774 | if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], |
| 1775 | &ciphersuite_info ) ) != 0 ) |
| 1776 | return( ret ); |
| 1777 | |
| 1778 | if( ciphersuite_info != NULL ) |
| 1779 | goto have_ciphersuite; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | |
Manuel Pégourié-Gonnard | f01768c | 2015-01-08 17:06:16 +0100 | [diff] [blame] | 1783 | if( got_common_suite ) |
| 1784 | { |
| 1785 | SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, " |
| 1786 | "but none of them usable" ) ); |
| 1787 | ssl_send_fatal_handshake_failure( ssl ); |
| 1788 | return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE ); |
| 1789 | } |
| 1790 | else |
| 1791 | { |
| 1792 | SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) ); |
| 1793 | ssl_send_fatal_handshake_failure( ssl ); |
| 1794 | return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN ); |
| 1795 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1796 | |
| 1797 | have_ciphersuite: |
Manuel Pégourié-Gonnard | 607d663 | 2015-01-26 11:17:20 +0000 | [diff] [blame] | 1798 | SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) ); |
| 1799 | |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 1800 | ssl->session_negotiate->ciphersuite = ciphersuites[i]; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 1801 | ssl->transform_negotiate->ciphersuite_info = ciphersuite_info; |
| 1802 | ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info ); |
| 1803 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1804 | ssl->in_left = 0; |
| 1805 | ssl->state++; |
| 1806 | |
| 1807 | SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) ); |
| 1808 | |
| 1809 | return( 0 ); |
| 1810 | } |
| 1811 | |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 1812 | #if defined(POLARSSL_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1813 | static void ssl_write_truncated_hmac_ext( ssl_context *ssl, |
| 1814 | unsigned char *buf, |
| 1815 | size_t *olen ) |
| 1816 | { |
| 1817 | unsigned char *p = buf; |
| 1818 | |
| 1819 | if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ) |
| 1820 | { |
| 1821 | *olen = 0; |
| 1822 | return; |
| 1823 | } |
| 1824 | |
| 1825 | SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) ); |
| 1826 | |
| 1827 | *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF ); |
| 1828 | *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF ); |
| 1829 | |
| 1830 | *p++ = 0x00; |
| 1831 | *p++ = 0x00; |
| 1832 | |
| 1833 | *olen = 4; |
| 1834 | } |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 1835 | #endif /* POLARSSL_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 1836 | |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1837 | #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) |
| 1838 | static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl, |
| 1839 | unsigned char *buf, |
| 1840 | size_t *olen ) |
| 1841 | { |
| 1842 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 1843 | const ssl_ciphersuite_t *suite = NULL; |
| 1844 | const cipher_info_t *cipher = NULL; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1845 | |
| 1846 | if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED || |
| 1847 | ssl->minor_ver == SSL_MINOR_VERSION_0 ) |
| 1848 | { |
| 1849 | *olen = 0; |
| 1850 | return; |
| 1851 | } |
| 1852 | |
Manuel Pégourié-Gonnard | 78e745f | 2014-11-04 15:44:06 +0100 | [diff] [blame] | 1853 | /* |
| 1854 | * RFC 7366: "If a server receives an encrypt-then-MAC request extension |
| 1855 | * from a client and then selects a stream or Authenticated Encryption |
| 1856 | * with Associated Data (AEAD) ciphersuite, it MUST NOT send an |
| 1857 | * encrypt-then-MAC response extension back to the client." |
| 1858 | */ |
| 1859 | if( ( suite = ssl_ciphersuite_from_id( |
| 1860 | ssl->session_negotiate->ciphersuite ) ) == NULL || |
| 1861 | ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL || |
| 1862 | cipher->mode != POLARSSL_MODE_CBC ) |
| 1863 | { |
| 1864 | *olen = 0; |
| 1865 | return; |
| 1866 | } |
| 1867 | |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1868 | SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) ); |
| 1869 | |
| 1870 | *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); |
| 1871 | *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF ); |
| 1872 | |
| 1873 | *p++ = 0x00; |
| 1874 | *p++ = 0x00; |
| 1875 | |
| 1876 | *olen = 4; |
| 1877 | } |
| 1878 | #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */ |
| 1879 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1880 | #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) |
| 1881 | static void ssl_write_extended_ms_ext( ssl_context *ssl, |
| 1882 | unsigned char *buf, |
| 1883 | size_t *olen ) |
| 1884 | { |
| 1885 | unsigned char *p = buf; |
| 1886 | |
Manuel Pégourié-Gonnard | b575b54 | 2014-10-24 15:12:31 +0200 | [diff] [blame] | 1887 | if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED || |
| 1888 | ssl->minor_ver == SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1889 | { |
| 1890 | *olen = 0; |
| 1891 | return; |
| 1892 | } |
| 1893 | |
| 1894 | SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret " |
| 1895 | "extension" ) ); |
| 1896 | |
| 1897 | *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF ); |
| 1898 | *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF ); |
| 1899 | |
| 1900 | *p++ = 0x00; |
| 1901 | *p++ = 0x00; |
| 1902 | |
| 1903 | *olen = 4; |
| 1904 | } |
| 1905 | #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */ |
| 1906 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1907 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 1908 | static void ssl_write_session_ticket_ext( ssl_context *ssl, |
| 1909 | unsigned char *buf, |
| 1910 | size_t *olen ) |
| 1911 | { |
| 1912 | unsigned char *p = buf; |
| 1913 | |
| 1914 | if( ssl->handshake->new_session_ticket == 0 ) |
| 1915 | { |
| 1916 | *olen = 0; |
| 1917 | return; |
| 1918 | } |
| 1919 | |
| 1920 | SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) ); |
| 1921 | |
| 1922 | *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF ); |
| 1923 | *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF ); |
| 1924 | |
| 1925 | *p++ = 0x00; |
| 1926 | *p++ = 0x00; |
| 1927 | |
| 1928 | *olen = 4; |
| 1929 | } |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 1930 | #endif /* POLARSSL_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 1931 | |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 1932 | static void ssl_write_renegotiation_ext( ssl_context *ssl, |
| 1933 | unsigned char *buf, |
| 1934 | size_t *olen ) |
| 1935 | { |
| 1936 | unsigned char *p = buf; |
| 1937 | |
| 1938 | if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION ) |
| 1939 | { |
| 1940 | *olen = 0; |
| 1941 | return; |
| 1942 | } |
| 1943 | |
| 1944 | SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) ); |
| 1945 | |
| 1946 | *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF ); |
| 1947 | *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF ); |
| 1948 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1949 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
| 1950 | if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ) |
| 1951 | { |
| 1952 | *p++ = 0x00; |
| 1953 | *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF; |
| 1954 | *p++ = ssl->verify_data_len * 2 & 0xFF; |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 1955 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1956 | memcpy( p, ssl->peer_verify_data, ssl->verify_data_len ); |
| 1957 | p += ssl->verify_data_len; |
| 1958 | memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); |
| 1959 | p += ssl->verify_data_len; |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 1960 | |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1961 | *olen = 5 + ssl->verify_data_len * 2; |
| 1962 | } |
| 1963 | else |
| 1964 | #endif /* POLARSSL_SSL_RENEGOTIATION */ |
| 1965 | { |
| 1966 | *p++ = 0x00; |
| 1967 | *p++ = 0x01; |
| 1968 | *p++ = 0x00; |
| 1969 | |
| 1970 | *olen = 5; |
| 1971 | } |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 1972 | } |
| 1973 | |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1974 | #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 1975 | static void ssl_write_max_fragment_length_ext( ssl_context *ssl, |
| 1976 | unsigned char *buf, |
| 1977 | size_t *olen ) |
| 1978 | { |
| 1979 | unsigned char *p = buf; |
| 1980 | |
Manuel Pégourié-Gonnard | e048b67 | 2013-07-19 12:47:00 +0200 | [diff] [blame] | 1981 | if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE ) |
| 1982 | { |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 1983 | *olen = 0; |
| 1984 | return; |
| 1985 | } |
| 1986 | |
| 1987 | SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) ); |
| 1988 | |
| 1989 | *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF ); |
| 1990 | *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF ); |
| 1991 | |
| 1992 | *p++ = 0x00; |
| 1993 | *p++ = 1; |
| 1994 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 1995 | *p++ = ssl->session_negotiate->mfl_code; |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 1996 | |
| 1997 | *olen = 5; |
| 1998 | } |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1999 | #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2000 | |
Manuel Pégourié-Gonnard | 0b27267 | 2013-08-15 19:38:07 +0200 | [diff] [blame] | 2001 | #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2002 | static void ssl_write_supported_point_formats_ext( ssl_context *ssl, |
| 2003 | unsigned char *buf, |
| 2004 | size_t *olen ) |
| 2005 | { |
| 2006 | unsigned char *p = buf; |
| 2007 | ((void) ssl); |
| 2008 | |
Paul Bakker | 677377f | 2013-10-28 12:54:26 +0100 | [diff] [blame] | 2009 | if( ( ssl->handshake->cli_exts & |
| 2010 | TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 ) |
| 2011 | { |
| 2012 | *olen = 0; |
| 2013 | return; |
| 2014 | } |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2015 | |
| 2016 | SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) ); |
| 2017 | |
| 2018 | *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF ); |
| 2019 | *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF ); |
| 2020 | |
| 2021 | *p++ = 0x00; |
| 2022 | *p++ = 2; |
| 2023 | |
| 2024 | *p++ = 1; |
| 2025 | *p++ = POLARSSL_ECP_PF_UNCOMPRESSED; |
| 2026 | |
| 2027 | *olen = 6; |
| 2028 | } |
Manuel Pégourié-Gonnard | 0b27267 | 2013-08-15 19:38:07 +0200 | [diff] [blame] | 2029 | #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */ |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2030 | |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2031 | #if defined(POLARSSL_SSL_ALPN ) |
| 2032 | static void ssl_write_alpn_ext( ssl_context *ssl, |
| 2033 | unsigned char *buf, size_t *olen ) |
| 2034 | { |
| 2035 | if( ssl->alpn_chosen == NULL ) |
| 2036 | { |
| 2037 | *olen = 0; |
| 2038 | return; |
| 2039 | } |
| 2040 | |
Manuel Pégourié-Gonnard | f6521de | 2014-04-07 12:42:04 +0200 | [diff] [blame] | 2041 | SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) ); |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2042 | |
| 2043 | /* |
| 2044 | * 0 . 1 ext identifier |
| 2045 | * 2 . 3 ext length |
| 2046 | * 4 . 5 protocol list length |
| 2047 | * 6 . 6 protocol name length |
| 2048 | * 7 . 7+n protocol name |
| 2049 | */ |
| 2050 | buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF ); |
| 2051 | buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF ); |
| 2052 | |
| 2053 | *olen = 7 + strlen( ssl->alpn_chosen ); |
| 2054 | |
| 2055 | buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF ); |
| 2056 | buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF ); |
| 2057 | |
| 2058 | buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF ); |
| 2059 | buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF ); |
| 2060 | |
| 2061 | buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF ); |
| 2062 | |
| 2063 | memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 ); |
| 2064 | } |
| 2065 | #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */ |
| 2066 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2067 | static int ssl_write_server_hello( ssl_context *ssl ) |
| 2068 | { |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2069 | #if defined(POLARSSL_HAVE_TIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2070 | time_t t; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2071 | #endif |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2072 | int ret; |
| 2073 | size_t olen, ext_len = 0, n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2074 | unsigned char *buf, *p; |
| 2075 | |
| 2076 | SSL_DEBUG_MSG( 2, ( "=> write server hello" ) ); |
| 2077 | |
Paul Bakker | a9a028e | 2013-11-21 17:31:06 +0100 | [diff] [blame] | 2078 | if( ssl->f_rng == NULL ) |
| 2079 | { |
| 2080 | SSL_DEBUG_MSG( 1, ( "no RNG provided") ); |
| 2081 | return( POLARSSL_ERR_SSL_NO_RNG ); |
| 2082 | } |
| 2083 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2084 | /* |
| 2085 | * 0 . 0 handshake type |
| 2086 | * 1 . 3 handshake length |
| 2087 | * 4 . 5 protocol version |
| 2088 | * 6 . 9 UNIX time() |
| 2089 | * 10 . 37 random bytes |
| 2090 | */ |
| 2091 | buf = ssl->out_msg; |
| 2092 | p = buf + 4; |
| 2093 | |
| 2094 | *p++ = (unsigned char) ssl->major_ver; |
| 2095 | *p++ = (unsigned char) ssl->minor_ver; |
| 2096 | |
| 2097 | SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]", |
| 2098 | buf[4], buf[5] ) ); |
| 2099 | |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2100 | #if defined(POLARSSL_HAVE_TIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2101 | t = time( NULL ); |
| 2102 | *p++ = (unsigned char)( t >> 24 ); |
| 2103 | *p++ = (unsigned char)( t >> 16 ); |
| 2104 | *p++ = (unsigned char)( t >> 8 ); |
| 2105 | *p++ = (unsigned char)( t ); |
| 2106 | |
| 2107 | SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 2108 | #else |
| 2109 | if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 ) |
| 2110 | return( ret ); |
| 2111 | |
| 2112 | p += 4; |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 2113 | #endif /* POLARSSL_HAVE_TIME */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2114 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2115 | if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 ) |
| 2116 | return( ret ); |
| 2117 | |
| 2118 | p += 28; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2119 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2120 | memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2121 | |
| 2122 | SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 ); |
| 2123 | |
| 2124 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2125 | * Resume is 0 by default, see ssl_handshake_init(). |
| 2126 | * It may be already set to 1 by ssl_parse_session_ticket_ext(). |
| 2127 | * If not, try looking up session ID in our cache. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2128 | */ |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2129 | if( ssl->handshake->resume == 0 && |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2130 | #if defined(POLARSSL_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2131 | ssl->renegotiation == SSL_INITIAL_HANDSHAKE && |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2132 | #endif |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 2133 | ssl->session_negotiate->length != 0 && |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2134 | ssl->f_get_cache != NULL && |
| 2135 | ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 ) |
| 2136 | { |
Manuel Pégourié-Gonnard | f7c5201 | 2014-02-20 11:43:46 +0100 | [diff] [blame] | 2137 | SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) ); |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2138 | ssl->handshake->resume = 1; |
| 2139 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2140 | |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2141 | if( ssl->handshake->resume == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2142 | { |
| 2143 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2144 | * New session, create a new session id, |
| 2145 | * unless we're about to issue a session ticket |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2146 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2147 | ssl->state++; |
| 2148 | |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2149 | #if defined(POLARSSL_HAVE_TIME) |
| 2150 | ssl->session_negotiate->start = time( NULL ); |
| 2151 | #endif |
| 2152 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2153 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 2154 | if( ssl->handshake->new_session_ticket != 0 ) |
| 2155 | { |
| 2156 | ssl->session_negotiate->length = n = 0; |
| 2157 | memset( ssl->session_negotiate->id, 0, 32 ); |
| 2158 | } |
| 2159 | else |
| 2160 | #endif /* POLARSSL_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2161 | { |
| 2162 | ssl->session_negotiate->length = n = 32; |
| 2163 | if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2164 | n ) ) != 0 ) |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2165 | return( ret ); |
| 2166 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2167 | } |
| 2168 | else |
| 2169 | { |
| 2170 | /* |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2171 | * Resuming a session |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2172 | */ |
Paul Bakker | f0e39ac | 2013-08-15 11:40:48 +0200 | [diff] [blame] | 2173 | n = ssl->session_negotiate->length; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2174 | ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC; |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2175 | |
| 2176 | if( ( ret = ssl_derive_keys( ssl ) ) != 0 ) |
| 2177 | { |
| 2178 | SSL_DEBUG_RET( 1, "ssl_derive_keys", ret ); |
| 2179 | return( ret ); |
| 2180 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Manuel Pégourié-Gonnard | 3ffa3db | 2013-08-02 11:59:05 +0200 | [diff] [blame] | 2183 | /* |
| 2184 | * 38 . 38 session id length |
| 2185 | * 39 . 38+n session id |
| 2186 | * 39+n . 40+n chosen ciphersuite |
| 2187 | * 41+n . 41+n chosen compression alg. |
| 2188 | * 42+n . 43+n extensions length |
| 2189 | * 44+n . 43+n+m extensions |
| 2190 | */ |
| 2191 | *p++ = (unsigned char) ssl->session_negotiate->length; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2192 | memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length ); |
| 2193 | p += ssl->session_negotiate->length; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2194 | |
| 2195 | SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); |
| 2196 | SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n ); |
| 2197 | SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2198 | ssl->handshake->resume ? "a" : "no" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2199 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2200 | *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 ); |
| 2201 | *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite ); |
| 2202 | *p++ = (unsigned char)( ssl->session_negotiate->compression ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2203 | |
Manuel Pégourié-Gonnard | 51451f8 | 2013-09-17 12:06:25 +0200 | [diff] [blame] | 2204 | SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", |
| 2205 | ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 32ea60a | 2013-08-17 17:39:04 +0200 | [diff] [blame] | 2206 | SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X", |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2207 | ssl->session_negotiate->compression ) ); |
| 2208 | |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2209 | /* |
| 2210 | * First write extensions, then the total length |
| 2211 | */ |
| 2212 | ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen ); |
| 2213 | ext_len += olen; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2214 | |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 2215 | #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2216 | ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen ); |
| 2217 | ext_len += olen; |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 2218 | #endif |
Manuel Pégourié-Gonnard | 7bb7899 | 2013-07-17 13:50:08 +0200 | [diff] [blame] | 2219 | |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 2220 | #if defined(POLARSSL_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2221 | ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen ); |
| 2222 | ext_len += olen; |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 2223 | #endif |
Manuel Pégourié-Gonnard | 57c2852 | 2013-07-19 11:41:43 +0200 | [diff] [blame] | 2224 | |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2225 | #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) |
| 2226 | ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen ); |
| 2227 | ext_len += olen; |
| 2228 | #endif |
| 2229 | |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2230 | #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) |
| 2231 | ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen ); |
| 2232 | ext_len += olen; |
| 2233 | #endif |
| 2234 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2235 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2236 | ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen ); |
| 2237 | ext_len += olen; |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2238 | #endif |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 2239 | |
Manuel Pégourié-Gonnard | 0b27267 | 2013-08-15 19:38:07 +0200 | [diff] [blame] | 2240 | #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | 7b19c16 | 2013-08-15 18:01:11 +0200 | [diff] [blame] | 2241 | ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen ); |
| 2242 | ext_len += olen; |
| 2243 | #endif |
| 2244 | |
Manuel Pégourié-Gonnard | 89e3579 | 2014-04-07 12:10:30 +0200 | [diff] [blame] | 2245 | #if defined(POLARSSL_SSL_ALPN) |
| 2246 | ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen ); |
| 2247 | ext_len += olen; |
| 2248 | #endif |
| 2249 | |
Manuel Pégourié-Gonnard | f11a6d7 | 2013-07-17 11:17:14 +0200 | [diff] [blame] | 2250 | SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2251 | |
Paul Bakker | a703663 | 2014-04-30 10:15:38 +0200 | [diff] [blame] | 2252 | if( ext_len > 0 ) |
| 2253 | { |
| 2254 | *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); |
| 2255 | *p++ = (unsigned char)( ( ext_len ) & 0xFF ); |
| 2256 | p += ext_len; |
| 2257 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2258 | |
| 2259 | ssl->out_msglen = p - buf; |
| 2260 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 2261 | ssl->out_msg[0] = SSL_HS_SERVER_HELLO; |
| 2262 | |
| 2263 | ret = ssl_write_record( ssl ); |
| 2264 | |
| 2265 | SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); |
| 2266 | |
| 2267 | return( ret ); |
| 2268 | } |
| 2269 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2270 | #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \ |
| 2271 | !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \ |
Manuel Pégourié-Gonnard | a310459 | 2013-09-17 21:17:44 +0200 | [diff] [blame] | 2272 | !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \ |
| 2273 | !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2274 | static int ssl_write_certificate_request( ssl_context *ssl ) |
| 2275 | { |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 2276 | const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2277 | |
| 2278 | SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
| 2279 | |
| 2280 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK || |
Manuel Pégourié-Gonnard | dc953e8 | 2013-11-25 17:27:39 +0100 | [diff] [blame] | 2281 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK || |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 2282 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK || |
| 2283 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2284 | { |
| 2285 | SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
| 2286 | ssl->state++; |
| 2287 | return( 0 ); |
| 2288 | } |
| 2289 | |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2290 | SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2291 | return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2292 | } |
| 2293 | #else |
| 2294 | static int ssl_write_certificate_request( ssl_context *ssl ) |
| 2295 | { |
| 2296 | int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2297 | const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2298 | size_t dn_size, total_dn_size; /* excluding length bytes */ |
| 2299 | size_t ct_len, sa_len; /* including length bytes */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2300 | unsigned char *buf, *p; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 2301 | const x509_crt *crt; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2302 | |
| 2303 | SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
| 2304 | |
| 2305 | ssl->state++; |
| 2306 | |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 2307 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK || |
Manuel Pégourié-Gonnard | dc953e8 | 2013-11-25 17:27:39 +0100 | [diff] [blame] | 2308 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK || |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2309 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK || |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 2310 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK || |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 2311 | ssl->authmode == SSL_VERIFY_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2312 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2313 | SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2314 | return( 0 ); |
| 2315 | } |
| 2316 | |
| 2317 | /* |
| 2318 | * 0 . 0 handshake type |
| 2319 | * 1 . 3 handshake length |
| 2320 | * 4 . 4 cert type count |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2321 | * 5 .. m-1 cert types |
| 2322 | * m .. m+1 sig alg length (TLS 1.2 only) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 2323 | * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2324 | * n .. n+1 length of all DNs |
| 2325 | * n+2 .. n+3 length of DN 1 |
| 2326 | * n+4 .. ... Distinguished Name #1 |
| 2327 | * ... .. ... length of DN 2, etc. |
| 2328 | */ |
| 2329 | buf = ssl->out_msg; |
| 2330 | p = buf + 4; |
| 2331 | |
| 2332 | /* |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2333 | * Supported certificate types |
| 2334 | * |
| 2335 | * ClientCertificateType certificate_types<1..2^8-1>; |
| 2336 | * enum { (255) } ClientCertificateType; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2337 | */ |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2338 | ct_len = 0; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2339 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2340 | #if defined(POLARSSL_RSA_C) |
| 2341 | p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN; |
| 2342 | #endif |
| 2343 | #if defined(POLARSSL_ECDSA_C) |
| 2344 | p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN; |
| 2345 | #endif |
| 2346 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2347 | p[0] = (unsigned char) ct_len++; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2348 | p += ct_len; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2349 | |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2350 | sa_len = 0; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2351 | #if defined(POLARSSL_SSL_PROTO_TLS1_2) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2352 | /* |
| 2353 | * Add signature_algorithms for verify (TLS 1.2) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2354 | * |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2355 | * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>; |
| 2356 | * |
| 2357 | * struct { |
| 2358 | * HashAlgorithm hash; |
| 2359 | * SignatureAlgorithm signature; |
| 2360 | * } SignatureAndHashAlgorithm; |
| 2361 | * |
| 2362 | * enum { (255) } HashAlgorithm; |
| 2363 | * enum { (255) } SignatureAlgorithm; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2364 | */ |
Paul Bakker | 21dca69 | 2013-01-03 11:41:08 +0100 | [diff] [blame] | 2365 | if( ssl->minor_ver == SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2366 | { |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2367 | /* |
| 2368 | * Only use current running hash algorithm that is already required |
| 2369 | * for requested ciphersuite. |
| 2370 | */ |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2371 | ssl->handshake->verify_sig_alg = SSL_HASH_SHA256; |
| 2372 | |
Paul Bakker | b7149bc | 2013-03-20 15:30:09 +0100 | [diff] [blame] | 2373 | if( ssl->transform_negotiate->ciphersuite_info->mac == |
| 2374 | POLARSSL_MD_SHA384 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2375 | { |
| 2376 | ssl->handshake->verify_sig_alg = SSL_HASH_SHA384; |
| 2377 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2378 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2379 | /* |
| 2380 | * Supported signature algorithms |
| 2381 | */ |
| 2382 | #if defined(POLARSSL_RSA_C) |
| 2383 | p[2 + sa_len++] = ssl->handshake->verify_sig_alg; |
| 2384 | p[2 + sa_len++] = SSL_SIG_RSA; |
| 2385 | #endif |
| 2386 | #if defined(POLARSSL_ECDSA_C) |
| 2387 | p[2 + sa_len++] = ssl->handshake->verify_sig_alg; |
| 2388 | p[2 + sa_len++] = SSL_SIG_ECDSA; |
| 2389 | #endif |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2390 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2391 | p[0] = (unsigned char)( sa_len >> 8 ); |
| 2392 | p[1] = (unsigned char)( sa_len ); |
| 2393 | sa_len += 2; |
| 2394 | p += sa_len; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2395 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2396 | #endif /* POLARSSL_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2397 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2398 | /* |
| 2399 | * DistinguishedName certificate_authorities<0..2^16-1>; |
| 2400 | * opaque DistinguishedName<1..2^16-1>; |
| 2401 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2402 | p += 2; |
| 2403 | crt = ssl->ca_chain; |
| 2404 | |
Paul Bakker | bc3d984 | 2012-11-26 16:12:02 +0100 | [diff] [blame] | 2405 | total_dn_size = 0; |
Paul Bakker | c70e425 | 2014-04-18 13:47:38 +0200 | [diff] [blame] | 2406 | while( crt != NULL && crt->version != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2407 | { |
| 2408 | if( p - buf > 4096 ) |
| 2409 | break; |
| 2410 | |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2411 | dn_size = crt->subject_raw.len; |
| 2412 | *p++ = (unsigned char)( dn_size >> 8 ); |
| 2413 | *p++ = (unsigned char)( dn_size ); |
| 2414 | memcpy( p, crt->subject_raw.p, dn_size ); |
| 2415 | p += dn_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2416 | |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2417 | SSL_DEBUG_BUF( 3, "requested DN", p, dn_size ); |
| 2418 | |
Paul Bakker | bc3d984 | 2012-11-26 16:12:02 +0100 | [diff] [blame] | 2419 | total_dn_size += 2 + dn_size; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2420 | crt = crt->next; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 2423 | ssl->out_msglen = p - buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2424 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 2425 | ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 2426 | ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 ); |
| 2427 | ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2428 | |
| 2429 | ret = ssl_write_record( ssl ); |
| 2430 | |
| 2431 | SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) ); |
| 2432 | |
| 2433 | return( ret ); |
| 2434 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2435 | #endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED && |
| 2436 | !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED && |
Manuel Pégourié-Gonnard | da1ff38 | 2013-11-25 17:38:36 +0100 | [diff] [blame] | 2437 | !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED && |
| 2438 | !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2439 | |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2440 | #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2441 | defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2442 | static int ssl_get_ecdh_params_from_cert( ssl_context *ssl ) |
| 2443 | { |
| 2444 | int ret; |
| 2445 | |
| 2446 | if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) ) |
| 2447 | { |
| 2448 | SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) ); |
| 2449 | return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH ); |
| 2450 | } |
| 2451 | |
| 2452 | if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, |
| 2453 | pk_ec( *ssl_own_key( ssl ) ), |
| 2454 | POLARSSL_ECDH_OURS ) ) != 0 ) |
| 2455 | { |
| 2456 | SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret ); |
| 2457 | return( ret ); |
| 2458 | } |
| 2459 | |
| 2460 | return( 0 ); |
| 2461 | } |
| 2462 | #endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || |
| 2463 | POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
| 2464 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2465 | static int ssl_write_server_key_exchange( ssl_context *ssl ) |
| 2466 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2467 | int ret; |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 2468 | size_t n = 0; |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2469 | const ssl_ciphersuite_t *ciphersuite_info = |
| 2470 | ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 2471 | |
| 2472 | #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2473 | defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ |
| 2474 | defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 2475 | defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 2476 | defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 2477 | unsigned char *p = ssl->out_msg + 4; |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2478 | unsigned char *dig_signed = p; |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 2479 | size_t dig_signed_len = 0, len; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 2480 | ((void) dig_signed); |
| 2481 | ((void) dig_signed_len); |
| 2482 | #endif |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2483 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2484 | SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) ); |
| 2485 | |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2486 | #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 2487 | defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \ |
| 2488 | defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 09258b9 | 2013-10-15 10:43:36 +0200 | [diff] [blame] | 2489 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA || |
| 2490 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK || |
| 2491 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2492 | { |
| 2493 | SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) ); |
| 2494 | ssl->state++; |
| 2495 | return( 0 ); |
| 2496 | } |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2497 | #endif |
| 2498 | |
| 2499 | #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 2500 | defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 2501 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA || |
| 2502 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA ) |
| 2503 | { |
| 2504 | ssl_get_ecdh_params_from_cert( ssl ); |
| 2505 | |
Manuel Pégourié-Gonnard | 8520dac | 2014-02-21 12:12:23 +0100 | [diff] [blame] | 2506 | SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) ); |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 2507 | ssl->state++; |
| 2508 | return( 0 ); |
| 2509 | } |
| 2510 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2511 | |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 2512 | #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ |
| 2513 | defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 2514 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK || |
| 2515 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2516 | { |
| 2517 | /* TODO: Support identity hints */ |
| 2518 | *(p++) = 0x00; |
| 2519 | *(p++) = 0x00; |
| 2520 | |
| 2521 | n += 2; |
| 2522 | } |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 2523 | #endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED || |
| 2524 | POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2525 | |
| 2526 | #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2527 | defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 2528 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA || |
| 2529 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2530 | { |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2531 | /* |
| 2532 | * Ephemeral DH parameters: |
| 2533 | * |
| 2534 | * struct { |
| 2535 | * opaque dh_p<1..2^16-1>; |
| 2536 | * opaque dh_g<1..2^16-1>; |
| 2537 | * opaque dh_Ys<1..2^16-1>; |
| 2538 | * } ServerDHParams; |
| 2539 | */ |
| 2540 | if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 || |
| 2541 | ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 ) |
| 2542 | { |
| 2543 | SSL_DEBUG_RET( 1, "mpi_copy", ret ); |
| 2544 | return( ret ); |
| 2545 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2546 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2547 | if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2548 | (int) mpi_size( &ssl->handshake->dhm_ctx.P ), |
| 2549 | p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2550 | { |
| 2551 | SSL_DEBUG_RET( 1, "dhm_make_params", ret ); |
| 2552 | return( ret ); |
| 2553 | } |
| 2554 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2555 | dig_signed = p; |
| 2556 | dig_signed_len = len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2557 | |
| 2558 | p += len; |
| 2559 | n += len; |
| 2560 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2561 | SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X ); |
| 2562 | SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P ); |
| 2563 | SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G ); |
| 2564 | SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX ); |
| 2565 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2566 | #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 2567 | POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2568 | |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 2569 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED) |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2570 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA || |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 2571 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA || |
| 2572 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2573 | { |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2574 | /* |
| 2575 | * Ephemeral ECDH parameters: |
| 2576 | * |
| 2577 | * struct { |
| 2578 | * ECParameters curve_params; |
| 2579 | * ECPoint public; |
| 2580 | * } ServerECDHParams; |
| 2581 | */ |
Paul Bakker | d893aef | 2014-04-17 14:45:17 +0200 | [diff] [blame] | 2582 | const ecp_curve_info **curve = NULL; |
Manuel Pégourié-Gonnard | de05390 | 2014-02-04 13:58:39 +0100 | [diff] [blame] | 2583 | #if defined(POLARSSL_SSL_SET_CURVES) |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2584 | const ecp_group_id *gid; |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 2585 | |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2586 | /* Match our preference list against the offered curves */ |
| 2587 | for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ ) |
| 2588 | for( curve = ssl->handshake->curves; *curve != NULL; curve++ ) |
| 2589 | if( (*curve)->grp_id == *gid ) |
| 2590 | goto curve_matching_done; |
| 2591 | |
| 2592 | curve_matching_done: |
| 2593 | #else |
| 2594 | curve = ssl->handshake->curves; |
| 2595 | #endif |
| 2596 | |
| 2597 | if( *curve == NULL ) |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 2598 | { |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2599 | SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) ); |
| 2600 | return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN ); |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 2601 | } |
Manuel Pégourié-Gonnard | de05390 | 2014-02-04 13:58:39 +0100 | [diff] [blame] | 2602 | |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2603 | SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) ); |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 2604 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2605 | if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp, |
Manuel Pégourié-Gonnard | c3f6b62c | 2014-02-06 10:13:09 +0100 | [diff] [blame] | 2606 | (*curve)->grp_id ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2607 | { |
| 2608 | SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret ); |
| 2609 | return( ret ); |
| 2610 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2611 | |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2612 | if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len, |
| 2613 | p, SSL_MAX_CONTENT_LEN - n, |
| 2614 | ssl->f_rng, ssl->p_rng ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2615 | { |
| 2616 | SSL_DEBUG_RET( 1, "ecdh_make_params", ret ); |
| 2617 | return( ret ); |
| 2618 | } |
| 2619 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2620 | dig_signed = p; |
| 2621 | dig_signed_len = len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2622 | |
| 2623 | p += len; |
| 2624 | n += len; |
| 2625 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 2626 | SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q ); |
| 2627 | } |
Gergely Budai | 987bfb5 | 2014-01-19 21:48:42 +0100 | [diff] [blame] | 2628 | #endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2629 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2630 | #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2631 | defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 2632 | defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2633 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA || |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2634 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA || |
| 2635 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2636 | { |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2637 | size_t signature_len = 0; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 2638 | unsigned int hashlen = 0; |
| 2639 | unsigned char hash[64]; |
| 2640 | md_type_t md_alg = POLARSSL_MD_NONE; |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 2641 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2642 | /* |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2643 | * Choose hash algorithm. NONE means MD5 + SHA1 here. |
| 2644 | */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2645 | #if defined(POLARSSL_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2646 | if( ssl->minor_ver == SSL_MINOR_VERSION_3 ) |
| 2647 | { |
| 2648 | md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg ); |
| 2649 | |
| 2650 | if( md_alg == POLARSSL_MD_NONE ) |
| 2651 | { |
| 2652 | SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2653 | return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2654 | } |
| 2655 | } |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2656 | else |
Paul Bakker | db20c10 | 2014-06-17 14:34:44 +0200 | [diff] [blame] | 2657 | #endif /* POLARSSL_SSL_PROTO_TLS1_2 */ |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2658 | #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \ |
| 2659 | defined(POLARSSL_SSL_PROTO_TLS1_1) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2660 | if( ciphersuite_info->key_exchange == |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2661 | POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ) |
| 2662 | { |
| 2663 | md_alg = POLARSSL_MD_SHA1; |
| 2664 | } |
| 2665 | else |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2666 | #endif |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2667 | { |
| 2668 | md_alg = POLARSSL_MD_NONE; |
| 2669 | } |
| 2670 | |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2671 | /* |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2672 | * Compute the hash to be signed |
| 2673 | */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2674 | #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \ |
| 2675 | defined(POLARSSL_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 2676 | if( md_alg == POLARSSL_MD_NONE ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 2677 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2678 | md5_context md5; |
| 2679 | sha1_context sha1; |
| 2680 | |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 2681 | md5_init( &md5 ); |
| 2682 | sha1_init( &sha1 ); |
| 2683 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2684 | /* |
| 2685 | * digitally-signed struct { |
| 2686 | * opaque md5_hash[16]; |
| 2687 | * opaque sha_hash[20]; |
| 2688 | * }; |
| 2689 | * |
| 2690 | * md5_hash |
| 2691 | * MD5(ClientHello.random + ServerHello.random |
| 2692 | * + ServerParams); |
| 2693 | * sha_hash |
| 2694 | * SHA(ClientHello.random + ServerHello.random |
| 2695 | * + ServerParams); |
| 2696 | */ |
| 2697 | md5_starts( &md5 ); |
| 2698 | md5_update( &md5, ssl->handshake->randbytes, 64 ); |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2699 | md5_update( &md5, dig_signed, dig_signed_len ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2700 | md5_finish( &md5, hash ); |
| 2701 | |
| 2702 | sha1_starts( &sha1 ); |
| 2703 | sha1_update( &sha1, ssl->handshake->randbytes, 64 ); |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2704 | sha1_update( &sha1, dig_signed, dig_signed_len ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2705 | sha1_finish( &sha1, hash + 16 ); |
| 2706 | |
| 2707 | hashlen = 36; |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 2708 | |
| 2709 | md5_free( &md5 ); |
| 2710 | sha1_free( &sha1 ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2711 | } |
| 2712 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2713 | #endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \ |
| 2714 | POLARSSL_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2715 | #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \ |
| 2716 | defined(POLARSSL_SSL_PROTO_TLS1_2) |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2717 | if( md_alg != POLARSSL_MD_NONE ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2718 | { |
| 2719 | md_context_t ctx; |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2720 | const md_info_t *md_info = md_info_from_type( md_alg ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2721 | |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 2722 | md_init( &ctx ); |
| 2723 | |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 2724 | /* Info from md_alg will be used instead */ |
| 2725 | hashlen = 0; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2726 | |
| 2727 | /* |
| 2728 | * digitally-signed struct { |
| 2729 | * opaque client_random[32]; |
| 2730 | * opaque server_random[32]; |
| 2731 | * ServerDHParams params; |
| 2732 | * }; |
| 2733 | */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2734 | if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2735 | { |
| 2736 | SSL_DEBUG_RET( 1, "md_init_ctx", ret ); |
| 2737 | return( ret ); |
| 2738 | } |
| 2739 | |
| 2740 | md_starts( &ctx ); |
| 2741 | md_update( &ctx, ssl->handshake->randbytes, 64 ); |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2742 | md_update( &ctx, dig_signed, dig_signed_len ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2743 | md_finish( &ctx, hash ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 2744 | md_free( &ctx ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 2745 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2746 | else |
Paul Bakker | 9659dae | 2013-08-28 16:21:34 +0200 | [diff] [blame] | 2747 | #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \ |
| 2748 | POLARSSL_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2749 | { |
| 2750 | SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2751 | return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2752 | } |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2753 | |
Manuel Pégourié-Gonnard | 9cc6f5c | 2013-08-27 14:29:44 +0200 | [diff] [blame] | 2754 | SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen : |
| 2755 | (unsigned int) ( md_info_from_type( md_alg ) )->size ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2756 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2757 | /* |
| 2758 | * Make the signature |
| 2759 | */ |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 2760 | if( ssl_own_key( ssl ) == NULL ) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 2761 | { |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2762 | SSL_DEBUG_MSG( 1, ( "got no private key" ) ); |
| 2763 | return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2764 | } |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 2765 | |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2766 | #if defined(POLARSSL_SSL_PROTO_TLS1_2) |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 2767 | if( ssl->minor_ver == SSL_MINOR_VERSION_3 ) |
| 2768 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2769 | *(p++) = ssl->handshake->sig_alg; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 2770 | *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2771 | |
| 2772 | n += 2; |
| 2773 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2774 | #endif /* POLARSSL_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2775 | |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 2776 | if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen, |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 2777 | p + 2 , &signature_len, |
| 2778 | ssl->f_rng, ssl->p_rng ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2779 | { |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 2780 | SSL_DEBUG_RET( 1, "pk_sign", ret ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2781 | return( ret ); |
Paul Bakker | 23f3680 | 2012-09-28 14:15:14 +0000 | [diff] [blame] | 2782 | } |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2783 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2784 | *(p++) = (unsigned char)( signature_len >> 8 ); |
| 2785 | *(p++) = (unsigned char)( signature_len ); |
Paul Bakker | bf63b36 | 2012-04-12 20:44:34 +0000 | [diff] [blame] | 2786 | n += 2; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2787 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2788 | SSL_DEBUG_BUF( 3, "my signature", p, signature_len ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2789 | |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2790 | p += signature_len; |
| 2791 | n += signature_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2792 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2793 | #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 2794 | POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
| 2795 | POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2796 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2797 | ssl->out_msglen = 4 + n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2798 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 2799 | ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE; |
| 2800 | |
| 2801 | ssl->state++; |
| 2802 | |
| 2803 | if( ( ret = ssl_write_record( ssl ) ) != 0 ) |
| 2804 | { |
| 2805 | SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 2806 | return( ret ); |
| 2807 | } |
| 2808 | |
| 2809 | SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) ); |
| 2810 | |
| 2811 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | static int ssl_write_server_hello_done( ssl_context *ssl ) |
| 2815 | { |
| 2816 | int ret; |
| 2817 | |
| 2818 | SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) ); |
| 2819 | |
| 2820 | ssl->out_msglen = 4; |
| 2821 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 2822 | ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE; |
| 2823 | |
| 2824 | ssl->state++; |
| 2825 | |
| 2826 | if( ( ret = ssl_write_record( ssl ) ) != 0 ) |
| 2827 | { |
| 2828 | SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 2829 | return( ret ); |
| 2830 | } |
| 2831 | |
| 2832 | SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) ); |
| 2833 | |
| 2834 | return( 0 ); |
| 2835 | } |
| 2836 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2837 | #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 2838 | defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 2839 | static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p, |
| 2840 | const unsigned char *end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2841 | { |
| 2842 | int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2843 | size_t n; |
| 2844 | |
| 2845 | /* |
| 2846 | * Receive G^Y mod P, premaster = (G^Y)^X mod P |
| 2847 | */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2848 | if( *p + 2 > end ) |
| 2849 | { |
| 2850 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 2851 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 2852 | } |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2853 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2854 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 2855 | *p += 2; |
| 2856 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 2857 | if( *p + n > end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2858 | { |
| 2859 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 2860 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 2861 | } |
| 2862 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 2863 | if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2864 | { |
| 2865 | SSL_DEBUG_RET( 1, "dhm_read_public", ret ); |
| 2866 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
| 2867 | } |
| 2868 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 2869 | *p += n; |
| 2870 | |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2871 | SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY ); |
| 2872 | |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2873 | return( ret ); |
| 2874 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2875 | #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED || |
| 2876 | POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2877 | |
Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 2878 | #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \ |
| 2879 | defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2880 | static int ssl_parse_encrypted_pms( ssl_context *ssl, |
| 2881 | const unsigned char *p, |
| 2882 | const unsigned char *end, |
| 2883 | size_t pms_offset ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2884 | { |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2885 | int ret; |
| 2886 | size_t len = pk_get_len( ssl_own_key( ssl ) ); |
| 2887 | unsigned char *pms = ssl->handshake->premaster + pms_offset; |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 2888 | unsigned char fake_pms[48], peer_pms[48]; |
| 2889 | unsigned char mask; |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 2890 | size_t i; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2891 | |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 2892 | if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2893 | { |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 2894 | SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2895 | return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 2896 | } |
| 2897 | |
| 2898 | /* |
| 2899 | * Decrypt the premaster using own private RSA key |
| 2900 | */ |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2901 | #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \ |
| 2902 | defined(POLARSSL_SSL_PROTO_TLS1_2) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2903 | if( ssl->minor_ver != SSL_MINOR_VERSION_0 ) |
| 2904 | { |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2905 | if( *p++ != ( ( len >> 8 ) & 0xFF ) || |
| 2906 | *p++ != ( ( len ) & 0xFF ) ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2907 | { |
| 2908 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 2909 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 2910 | } |
| 2911 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2912 | #endif |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2913 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2914 | if( p + len != end ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2915 | { |
| 2916 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 2917 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 2918 | } |
| 2919 | |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 2920 | /* |
| 2921 | * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding |
| 2922 | * must not cause the connection to end immediately; instead, send a |
| 2923 | * bad_record_mac later in the handshake. |
| 2924 | * Also, avoid data-dependant branches here to protect against |
| 2925 | * timing-based variants. |
| 2926 | */ |
| 2927 | ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) ); |
| 2928 | if( ret != 0 ) |
| 2929 | return( ret ); |
| 2930 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2931 | ret = pk_decrypt( ssl_own_key( ssl ), p, len, |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 2932 | peer_pms, &ssl->handshake->pmslen, |
| 2933 | sizeof( peer_pms ), |
Manuel Pégourié-Gonnard | 070cc7f | 2013-08-21 15:09:31 +0200 | [diff] [blame] | 2934 | ssl->f_rng, ssl->p_rng ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2935 | |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 2936 | ret |= ssl->handshake->pmslen - 48; |
| 2937 | ret |= peer_pms[0] - ssl->handshake->max_major_ver; |
| 2938 | ret |= peer_pms[1] - ssl->handshake->max_minor_ver; |
| 2939 | |
| 2940 | #if defined(POLARSSL_SSL_DEBUG_ALL) |
| 2941 | if( ret != 0 ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2942 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 2943 | #endif |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2944 | |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 2945 | if( sizeof( ssl->handshake->premaster ) < pms_offset || |
| 2946 | sizeof( ssl->handshake->premaster ) - pms_offset < 48 ) |
| 2947 | { |
| 2948 | SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2949 | return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2950 | } |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 2951 | ssl->handshake->pmslen = 48; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2952 | |
Manuel Pégourié-Gonnard | 2ee8d24 | 2015-02-11 15:29:15 +0000 | [diff] [blame] | 2953 | mask = (unsigned char)( - ( ret != 0 ) ); /* ret ? 0xff : 0x00 */ |
Manuel Pégourié-Gonnard | 6674cce | 2015-02-06 10:30:58 +0000 | [diff] [blame] | 2954 | for( i = 0; i < ssl->handshake->pmslen; i++ ) |
| 2955 | pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] ); |
| 2956 | |
| 2957 | return( 0 ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2958 | } |
Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 2959 | #endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED || |
| 2960 | POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 2961 | |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 2962 | #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2963 | static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p, |
| 2964 | const unsigned char *end ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 2965 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2966 | int ret = 0; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 2967 | size_t n; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 2968 | |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2969 | if( ssl->f_psk == NULL && |
| 2970 | ( ssl->psk == NULL || ssl->psk_identity == NULL || |
| 2971 | ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 2972 | { |
| 2973 | SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) ); |
| 2974 | return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
| 2975 | } |
| 2976 | |
| 2977 | /* |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2978 | * Receive client pre-shared key identity name |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 2979 | */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2980 | if( *p + 2 > end ) |
| 2981 | { |
| 2982 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 2983 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 2984 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 2985 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 2986 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 2987 | *p += 2; |
| 2988 | |
| 2989 | if( n < 1 || n > 65535 || *p + n > end ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 2990 | { |
| 2991 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 2992 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 2993 | } |
| 2994 | |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2995 | if( ssl->f_psk != NULL ) |
| 2996 | { |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 2997 | if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2998 | ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY; |
| 2999 | } |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 3000 | else |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3001 | { |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 3002 | /* Identity is not a big secret since clients send it in the clear, |
| 3003 | * but treat it carefully anyway, just in case */ |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3004 | if( n != ssl->psk_identity_len || |
Manuel Pégourié-Gonnard | 31ff1d2 | 2013-10-28 13:46:11 +0100 | [diff] [blame] | 3005 | safer_memcmp( ssl->psk_identity, *p, n ) != 0 ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3006 | { |
| 3007 | ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY; |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3012 | { |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3013 | SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3014 | if( ( ret = ssl_send_alert_message( ssl, |
| 3015 | SSL_ALERT_LEVEL_FATAL, |
| 3016 | SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 ) |
| 3017 | { |
| 3018 | return( ret ); |
| 3019 | } |
| 3020 | |
| 3021 | return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3022 | } |
| 3023 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3024 | *p += n; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3025 | |
Manuel Pégourié-Gonnard | d27680b | 2014-07-08 14:15:55 +0200 | [diff] [blame] | 3026 | return( 0 ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3027 | } |
Manuel Pégourié-Gonnard | 8a3c64d | 2013-10-14 19:54:10 +0200 | [diff] [blame] | 3028 | #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3029 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3030 | static int ssl_parse_client_key_exchange( ssl_context *ssl ) |
| 3031 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3032 | int ret; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3033 | const ssl_ciphersuite_t *ciphersuite_info; |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3034 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3035 | ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3036 | |
| 3037 | SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) ); |
| 3038 | |
| 3039 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 3040 | { |
| 3041 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 3042 | return( ret ); |
| 3043 | } |
| 3044 | |
| 3045 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 3046 | { |
| 3047 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 3048 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE ) |
| 3052 | { |
| 3053 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 3054 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3057 | #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3058 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3059 | { |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 3060 | unsigned char *p = ssl->in_msg + 4; |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3061 | unsigned char *end = ssl->in_msg + ssl->in_hslen; |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 3062 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3063 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3064 | { |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3065 | SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); |
| 3066 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3067 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3068 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3069 | if( p != end ) |
| 3070 | { |
| 3071 | SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 3072 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 3073 | } |
| 3074 | |
Manuel Pégourié-Gonnard | dd0c0f3 | 2014-06-23 18:07:11 +0200 | [diff] [blame] | 3075 | ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3076 | |
| 3077 | if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 3078 | ssl->handshake->premaster, |
Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 3079 | &ssl->handshake->pmslen, |
Manuel Pégourié-Gonnard | 15d5de1 | 2013-09-17 11:34:11 +0200 | [diff] [blame] | 3080 | ssl->f_rng, ssl->p_rng ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3081 | { |
| 3082 | SSL_DEBUG_RET( 1, "dhm_calc_secret", ret ); |
| 3083 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS ); |
| 3084 | } |
| 3085 | |
| 3086 | SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3087 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3088 | else |
| 3089 | #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3090 | #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3091 | defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ |
| 3092 | defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ |
| 3093 | defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3094 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA || |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3095 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA || |
| 3096 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA || |
| 3097 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA ) |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3098 | { |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3099 | if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 3100 | ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3101 | { |
| 3102 | SSL_DEBUG_RET( 1, "ecdh_read_public", ret ); |
| 3103 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
| 3104 | } |
| 3105 | |
| 3106 | SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp ); |
| 3107 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3108 | if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, |
| 3109 | &ssl->handshake->pmslen, |
| 3110 | ssl->handshake->premaster, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 3111 | POLARSSL_MPI_MAX_SIZE, |
| 3112 | ssl->f_rng, ssl->p_rng ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3113 | { |
| 3114 | SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret ); |
| 3115 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS ); |
| 3116 | } |
| 3117 | |
| 3118 | SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3119 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3120 | else |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3121 | #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED || |
Manuel Pégourié-Gonnard | 5538970 | 2013-12-12 11:14:16 +0100 | [diff] [blame] | 3122 | POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || |
| 3123 | POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED || |
| 3124 | POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3125 | #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) |
| 3126 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3127 | { |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 3128 | unsigned char *p = ssl->in_msg + 4; |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3129 | unsigned char *end = ssl->in_msg + ssl->in_hslen; |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 3130 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3131 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3132 | { |
| 3133 | SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
| 3134 | return( ret ); |
| 3135 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3136 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3137 | if( p != end ) |
| 3138 | { |
| 3139 | SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 3140 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 3141 | } |
| 3142 | |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3143 | if( ( ret = ssl_psk_derive_premaster( ssl, |
| 3144 | ciphersuite_info->key_exchange ) ) != 0 ) |
| 3145 | { |
| 3146 | SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret ); |
| 3147 | return( ret ); |
| 3148 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3149 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3150 | else |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3151 | #endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3152 | #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 3153 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) |
| 3154 | { |
| 3155 | unsigned char *p = ssl->in_msg + 4; |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3156 | unsigned char *end = ssl->in_msg + ssl->in_hslen; |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3157 | |
| 3158 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 3159 | { |
| 3160 | SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
| 3161 | return( ret ); |
| 3162 | } |
| 3163 | |
| 3164 | if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 ) |
| 3165 | { |
| 3166 | SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret ); |
| 3167 | return( ret ); |
| 3168 | } |
| 3169 | |
| 3170 | if( ( ret = ssl_psk_derive_premaster( ssl, |
| 3171 | ciphersuite_info->key_exchange ) ) != 0 ) |
| 3172 | { |
| 3173 | SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret ); |
| 3174 | return( ret ); |
| 3175 | } |
| 3176 | } |
| 3177 | else |
| 3178 | #endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3179 | #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 3180 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ) |
| 3181 | { |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 3182 | unsigned char *p = ssl->in_msg + 4; |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3183 | unsigned char *end = ssl->in_msg + ssl->in_hslen; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3184 | |
| 3185 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 3186 | { |
| 3187 | SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
| 3188 | return( ret ); |
| 3189 | } |
| 3190 | if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) |
| 3191 | { |
| 3192 | SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret ); |
| 3193 | return( ret ); |
| 3194 | } |
| 3195 | |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3196 | if( p != end ) |
| 3197 | { |
| 3198 | SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); |
| 3199 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
| 3200 | } |
| 3201 | |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3202 | if( ( ret = ssl_psk_derive_premaster( ssl, |
| 3203 | ciphersuite_info->key_exchange ) ) != 0 ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3204 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3205 | SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret ); |
| 3206 | return( ret ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3207 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3208 | } |
| 3209 | else |
| 3210 | #endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3211 | #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 3212 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ) |
| 3213 | { |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3214 | unsigned char *p = ssl->in_msg + 4; |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3215 | unsigned char *end = ssl->in_msg + ssl->in_hslen; |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3216 | |
| 3217 | if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) |
| 3218 | { |
| 3219 | SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); |
| 3220 | return( ret ); |
| 3221 | } |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3222 | |
| 3223 | if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx, |
| 3224 | p, end - p ) ) != 0 ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3225 | { |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3226 | SSL_DEBUG_RET( 1, "ecdh_read_public", ret ); |
| 3227 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP ); |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3228 | } |
| 3229 | |
Manuel Pégourié-Gonnard | b59d699 | 2013-10-14 12:00:45 +0200 | [diff] [blame] | 3230 | SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp ); |
| 3231 | |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3232 | if( ( ret = ssl_psk_derive_premaster( ssl, |
| 3233 | ciphersuite_info->key_exchange ) ) != 0 ) |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3234 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 3235 | SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret ); |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3236 | return( ret ); |
| 3237 | } |
Manuel Pégourié-Gonnard | 3ce3bbd | 2013-10-11 16:53:50 +0200 | [diff] [blame] | 3238 | } |
| 3239 | else |
| 3240 | #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3241 | #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) |
| 3242 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3243 | { |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3244 | if( ( ret = ssl_parse_encrypted_pms( ssl, |
| 3245 | ssl->in_msg + 4, |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3246 | ssl->in_msg + ssl->in_hslen, |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 3247 | 0 ) ) != 0 ) |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 3248 | { |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 3249 | SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret ); |
Paul Bakker | 70df2fb | 2013-04-17 17:19:09 +0200 | [diff] [blame] | 3250 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3251 | } |
| 3252 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3253 | else |
| 3254 | #endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */ |
| 3255 | { |
Manuel Pégourié-Gonnard | abae74c | 2013-08-20 13:53:44 +0200 | [diff] [blame] | 3256 | SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 3257 | return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3258 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3259 | |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 3260 | if( ( ret = ssl_derive_keys( ssl ) ) != 0 ) |
| 3261 | { |
| 3262 | SSL_DEBUG_RET( 1, "ssl_derive_keys", ret ); |
| 3263 | return( ret ); |
| 3264 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3265 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3266 | ssl->state++; |
| 3267 | |
| 3268 | SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) ); |
| 3269 | |
| 3270 | return( 0 ); |
| 3271 | } |
| 3272 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3273 | #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \ |
| 3274 | !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \ |
Manuel Pégourié-Gonnard | a310459 | 2013-09-17 21:17:44 +0200 | [diff] [blame] | 3275 | !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \ |
| 3276 | !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3277 | static int ssl_parse_certificate_verify( ssl_context *ssl ) |
| 3278 | { |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 3279 | const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3280 | |
| 3281 | SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
| 3282 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3283 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK || |
Manuel Pégourié-Gonnard | dc953e8 | 2013-11-25 17:27:39 +0100 | [diff] [blame] | 3284 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK || |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 3285 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK || |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3286 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3287 | { |
| 3288 | SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
| 3289 | ssl->state++; |
| 3290 | return( 0 ); |
| 3291 | } |
| 3292 | |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 3293 | SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3294 | return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3295 | } |
| 3296 | #else |
| 3297 | static int ssl_parse_certificate_verify( ssl_context *ssl ) |
| 3298 | { |
| 3299 | int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3300 | size_t sa_len, sig_len; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3301 | unsigned char hash[48]; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3302 | unsigned char *hash_start = hash; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3303 | size_t hashlen; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3304 | #if defined(POLARSSL_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3305 | pk_type_t pk_alg; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3306 | #endif |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3307 | md_type_t md_alg; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3308 | const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
| 3309 | |
| 3310 | SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
| 3311 | |
| 3312 | if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK || |
Manuel Pégourié-Gonnard | dc953e8 | 2013-11-25 17:27:39 +0100 | [diff] [blame] | 3313 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK || |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 3314 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK || |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3315 | ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ) |
| 3316 | { |
| 3317 | SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
| 3318 | ssl->state++; |
| 3319 | return( 0 ); |
| 3320 | } |
| 3321 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3322 | if( ssl->session_negotiate->peer_cert == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3323 | { |
| 3324 | SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
| 3325 | ssl->state++; |
| 3326 | return( 0 ); |
| 3327 | } |
| 3328 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3329 | ssl->handshake->calc_verify( ssl, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3330 | |
| 3331 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 3332 | { |
| 3333 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 3334 | return( ret ); |
| 3335 | } |
| 3336 | |
| 3337 | ssl->state++; |
| 3338 | |
| 3339 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 3340 | { |
| 3341 | SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 3342 | return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY ) |
| 3346 | { |
| 3347 | SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 3348 | return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3351 | /* |
| 3352 | * 0 . 0 handshake type |
| 3353 | * 1 . 3 handshake length |
| 3354 | * 4 . 5 sig alg (TLS 1.2 only) |
| 3355 | * 4+n . 5+n signature length (n = sa_len) |
| 3356 | * 6+n . 6+n+m signature (m = sig_len) |
| 3357 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3358 | |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3359 | #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \ |
| 3360 | defined(POLARSSL_SSL_PROTO_TLS1_1) |
| 3361 | if( ssl->minor_ver != SSL_MINOR_VERSION_3 ) |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3362 | { |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3363 | sa_len = 0; |
| 3364 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 3365 | md_alg = POLARSSL_MD_NONE; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3366 | hashlen = 36; |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3367 | |
| 3368 | /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */ |
| 3369 | if( pk_can_do( &ssl->session_negotiate->peer_cert->pk, |
| 3370 | POLARSSL_PK_ECDSA ) ) |
| 3371 | { |
| 3372 | hash_start += 16; |
| 3373 | hashlen -= 16; |
| 3374 | md_alg = POLARSSL_MD_SHA1; |
| 3375 | } |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3376 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3377 | else |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3378 | #endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || |
| 3379 | POLARSSL_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3380 | #if defined(POLARSSL_SSL_PROTO_TLS1_2) |
| 3381 | if( ssl->minor_ver == SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 3382 | { |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3383 | sa_len = 2; |
| 3384 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3385 | /* |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 3386 | * Hash |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3387 | */ |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3388 | if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3389 | { |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3390 | SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg" |
| 3391 | " for verify message" ) ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3392 | return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
| 3393 | } |
| 3394 | |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 3395 | md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg ); |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3396 | |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 3397 | /* Info from md_alg will be used instead */ |
| 3398 | hashlen = 0; |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3399 | |
| 3400 | /* |
| 3401 | * Signature |
| 3402 | */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 3403 | if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) ) |
| 3404 | == POLARSSL_PK_NONE ) |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3405 | { |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 3406 | SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg" |
| 3407 | " for verify message" ) ); |
| 3408 | return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3409 | } |
| 3410 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3411 | /* |
| 3412 | * Check the certificate's key type matches the signature alg |
| 3413 | */ |
| 3414 | if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) ) |
| 3415 | { |
| 3416 | SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) ); |
| 3417 | return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
| 3418 | } |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3419 | } |
| 3420 | else |
| 3421 | #endif /* POLARSSL_SSL_PROTO_TLS1_2 */ |
| 3422 | { |
| 3423 | SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 3424 | return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 3425 | } |
Manuel Pégourié-Gonnard | ff56da3 | 2013-07-11 10:46:21 +0200 | [diff] [blame] | 3426 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3427 | sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len]; |
Paul Bakker | 926af75 | 2012-11-23 13:38:07 +0100 | [diff] [blame] | 3428 | |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3429 | if( sa_len + sig_len + 6 != ssl->in_hslen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3430 | { |
| 3431 | SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 3432 | return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 3435 | if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk, |
Manuel Pégourié-Gonnard | 4bd1284 | 2013-08-27 13:31:28 +0200 | [diff] [blame] | 3436 | md_alg, hash_start, hashlen, |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 3437 | ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3438 | { |
Manuel Pégourié-Gonnard | 0b03200 | 2013-08-17 13:01:41 +0200 | [diff] [blame] | 3439 | SSL_DEBUG_RET( 1, "pk_verify", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3440 | return( ret ); |
| 3441 | } |
| 3442 | |
| 3443 | SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) ); |
| 3444 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3445 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3446 | } |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3447 | #endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED && |
| 3448 | !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED && |
| 3449 | !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3450 | |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3451 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3452 | static int ssl_write_new_session_ticket( ssl_context *ssl ) |
| 3453 | { |
| 3454 | int ret; |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 3455 | size_t tlen; |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 3456 | uint32_t lifetime = (uint32_t) ssl->ticket_lifetime; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3457 | |
| 3458 | SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) ); |
| 3459 | |
| 3460 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 3461 | ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET; |
| 3462 | |
| 3463 | /* |
| 3464 | * struct { |
| 3465 | * uint32 ticket_lifetime_hint; |
| 3466 | * opaque ticket<0..2^16-1>; |
| 3467 | * } NewSessionTicket; |
| 3468 | * |
| 3469 | * 4 . 7 ticket_lifetime_hint (0 = unspecified) |
| 3470 | * 8 . 9 ticket_len (n) |
| 3471 | * 10 . 9+n ticket content |
| 3472 | */ |
Manuel Pégourié-Gonnard | 164d894 | 2013-09-23 22:01:39 +0200 | [diff] [blame] | 3473 | |
| 3474 | ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF; |
| 3475 | ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF; |
| 3476 | ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF; |
| 3477 | ssl->out_msg[7] = ( lifetime ) & 0xFF; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3478 | |
Manuel Pégourié-Gonnard | 990c51a | 2013-08-03 15:37:58 +0200 | [diff] [blame] | 3479 | if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 ) |
| 3480 | { |
| 3481 | SSL_DEBUG_RET( 1, "ssl_write_ticket", ret ); |
| 3482 | tlen = 0; |
| 3483 | } |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3484 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 3485 | ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF ); |
| 3486 | ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF ); |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3487 | |
Manuel Pégourié-Gonnard | 609bc81 | 2013-08-01 15:08:40 +0200 | [diff] [blame] | 3488 | ssl->out_msglen = 10 + tlen; |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3489 | |
Manuel Pégourié-Gonnard | 145dfcb | 2014-02-26 14:23:33 +0100 | [diff] [blame] | 3490 | /* |
| 3491 | * Morally equivalent to updating ssl->state, but NewSessionTicket and |
| 3492 | * ChangeCipherSpec share the same state. |
| 3493 | */ |
| 3494 | ssl->handshake->new_session_ticket = 0; |
| 3495 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3496 | if( ( ret = ssl_write_record( ssl ) ) != 0 ) |
| 3497 | { |
| 3498 | SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 3499 | return( ret ); |
| 3500 | } |
| 3501 | |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3502 | SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) ); |
| 3503 | |
| 3504 | return( 0 ); |
| 3505 | } |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3506 | #endif /* POLARSSL_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3507 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3508 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3509 | * SSL handshake -- server side -- single step |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3510 | */ |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3511 | int ssl_handshake_server_step( ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3512 | { |
| 3513 | int ret = 0; |
| 3514 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3515 | if( ssl->state == SSL_HANDSHAKE_OVER ) |
| 3516 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3517 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3518 | SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) ); |
| 3519 | |
| 3520 | if( ( ret = ssl_flush_output( ssl ) ) != 0 ) |
| 3521 | return( ret ); |
| 3522 | |
| 3523 | switch( ssl->state ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3524 | { |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3525 | case SSL_HELLO_REQUEST: |
| 3526 | ssl->state = SSL_CLIENT_HELLO; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3527 | break; |
| 3528 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3529 | /* |
| 3530 | * <== ClientHello |
| 3531 | */ |
| 3532 | case SSL_CLIENT_HELLO: |
| 3533 | ret = ssl_parse_client_hello( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3534 | break; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3535 | |
| 3536 | /* |
| 3537 | * ==> ServerHello |
| 3538 | * Certificate |
| 3539 | * ( ServerKeyExchange ) |
| 3540 | * ( CertificateRequest ) |
| 3541 | * ServerHelloDone |
| 3542 | */ |
| 3543 | case SSL_SERVER_HELLO: |
| 3544 | ret = ssl_write_server_hello( ssl ); |
| 3545 | break; |
| 3546 | |
| 3547 | case SSL_SERVER_CERTIFICATE: |
| 3548 | ret = ssl_write_certificate( ssl ); |
| 3549 | break; |
| 3550 | |
| 3551 | case SSL_SERVER_KEY_EXCHANGE: |
| 3552 | ret = ssl_write_server_key_exchange( ssl ); |
| 3553 | break; |
| 3554 | |
| 3555 | case SSL_CERTIFICATE_REQUEST: |
| 3556 | ret = ssl_write_certificate_request( ssl ); |
| 3557 | break; |
| 3558 | |
| 3559 | case SSL_SERVER_HELLO_DONE: |
| 3560 | ret = ssl_write_server_hello_done( ssl ); |
| 3561 | break; |
| 3562 | |
| 3563 | /* |
| 3564 | * <== ( Certificate/Alert ) |
| 3565 | * ClientKeyExchange |
| 3566 | * ( CertificateVerify ) |
| 3567 | * ChangeCipherSpec |
| 3568 | * Finished |
| 3569 | */ |
| 3570 | case SSL_CLIENT_CERTIFICATE: |
| 3571 | ret = ssl_parse_certificate( ssl ); |
| 3572 | break; |
| 3573 | |
| 3574 | case SSL_CLIENT_KEY_EXCHANGE: |
| 3575 | ret = ssl_parse_client_key_exchange( ssl ); |
| 3576 | break; |
| 3577 | |
| 3578 | case SSL_CERTIFICATE_VERIFY: |
| 3579 | ret = ssl_parse_certificate_verify( ssl ); |
| 3580 | break; |
| 3581 | |
| 3582 | case SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 3583 | ret = ssl_parse_change_cipher_spec( ssl ); |
| 3584 | break; |
| 3585 | |
| 3586 | case SSL_CLIENT_FINISHED: |
| 3587 | ret = ssl_parse_finished( ssl ); |
| 3588 | break; |
| 3589 | |
| 3590 | /* |
Manuel Pégourié-Gonnard | 7a358b8 | 2013-08-01 11:47:56 +0200 | [diff] [blame] | 3591 | * ==> ( NewSessionTicket ) |
| 3592 | * ChangeCipherSpec |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3593 | * Finished |
| 3594 | */ |
| 3595 | case SSL_SERVER_CHANGE_CIPHER_SPEC: |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3596 | #if defined(POLARSSL_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 3597 | if( ssl->handshake->new_session_ticket != 0 ) |
| 3598 | ret = ssl_write_new_session_ticket( ssl ); |
| 3599 | else |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3600 | #endif |
Manuel Pégourié-Gonnard | 7cd5924 | 2013-08-02 13:24:41 +0200 | [diff] [blame] | 3601 | ret = ssl_write_change_cipher_spec( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3602 | break; |
| 3603 | |
| 3604 | case SSL_SERVER_FINISHED: |
| 3605 | ret = ssl_write_finished( ssl ); |
| 3606 | break; |
| 3607 | |
| 3608 | case SSL_FLUSH_BUFFERS: |
| 3609 | SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 3610 | ssl->state = SSL_HANDSHAKE_WRAPUP; |
| 3611 | break; |
| 3612 | |
| 3613 | case SSL_HANDSHAKE_WRAPUP: |
| 3614 | ssl_handshake_wrapup( ssl ); |
| 3615 | break; |
| 3616 | |
| 3617 | default: |
| 3618 | SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
| 3619 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3622 | return( ret ); |
| 3623 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3624 | #endif /* POLARSSL_SSL_SRV_C */ |