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