Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 server-side functions |
| 3 | * |
Paul Bakker | fc8c436 | 2010-03-21 17:37:16 +0000 | [diff] [blame^] | 4 | * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 22 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 24 | #if defined(POLARSSL_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 26 | #include "polarssl/debug.h" |
| 27 | #include "polarssl/ssl.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdio.h> |
| 32 | #include <time.h> |
| 33 | |
| 34 | static int ssl_parse_client_hello( ssl_context *ssl ) |
| 35 | { |
| 36 | int ret, i, j, n; |
| 37 | int ciph_len, sess_len; |
| 38 | int chal_len, comp_len; |
| 39 | unsigned char *buf, *p; |
| 40 | |
| 41 | SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) ); |
| 42 | |
| 43 | if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 ) |
| 44 | { |
| 45 | SSL_DEBUG_RET( 1, "ssl_fetch_input", ret ); |
| 46 | return( ret ); |
| 47 | } |
| 48 | |
| 49 | buf = ssl->in_hdr; |
| 50 | |
| 51 | if( ( buf[0] & 0x80 ) != 0 ) |
| 52 | { |
| 53 | SSL_DEBUG_BUF( 4, "record header", buf, 5 ); |
| 54 | |
| 55 | SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d", |
| 56 | buf[2] ) ); |
| 57 | SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d", |
| 58 | ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) ); |
| 59 | SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]", |
| 60 | buf[3], buf[4] ) ); |
| 61 | |
| 62 | /* |
| 63 | * SSLv2 Client Hello |
| 64 | * |
| 65 | * Record layer: |
| 66 | * 0 . 1 message length |
| 67 | * |
| 68 | * SSL layer: |
| 69 | * 2 . 2 message type |
| 70 | * 3 . 4 protocol version |
| 71 | */ |
| 72 | if( buf[2] != SSL_HS_CLIENT_HELLO || |
| 73 | buf[3] != SSL_MAJOR_VERSION_3 ) |
| 74 | { |
| 75 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 76 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF; |
| 80 | |
| 81 | if( n < 17 || n > 512 ) |
| 82 | { |
| 83 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 84 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | ssl->max_major_ver = buf[3]; |
| 88 | ssl->max_minor_ver = buf[4]; |
| 89 | |
| 90 | ssl->major_ver = SSL_MAJOR_VERSION_3; |
| 91 | ssl->minor_ver = ( buf[4] <= SSL_MINOR_VERSION_1 ) |
| 92 | ? buf[4] : SSL_MINOR_VERSION_1; |
| 93 | |
| 94 | if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 ) |
| 95 | { |
| 96 | SSL_DEBUG_RET( 1, "ssl_fetch_input", ret ); |
| 97 | return( ret ); |
| 98 | } |
| 99 | |
| 100 | md5_update( &ssl->fin_md5 , buf + 2, n ); |
| 101 | sha1_update( &ssl->fin_sha1, buf + 2, n ); |
| 102 | |
| 103 | buf = ssl->in_msg; |
| 104 | n = ssl->in_left - 5; |
| 105 | |
| 106 | /* |
| 107 | * 0 . 1 cipherlist length |
| 108 | * 2 . 3 session id length |
| 109 | * 4 . 5 challenge length |
| 110 | * 6 . .. cipherlist |
| 111 | * .. . .. session id |
| 112 | * .. . .. challenge |
| 113 | */ |
| 114 | SSL_DEBUG_BUF( 4, "record contents", buf, n ); |
| 115 | |
| 116 | ciph_len = ( buf[0] << 8 ) | buf[1]; |
| 117 | sess_len = ( buf[2] << 8 ) | buf[3]; |
| 118 | chal_len = ( buf[4] << 8 ) | buf[5]; |
| 119 | |
| 120 | SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d", |
| 121 | ciph_len, sess_len, chal_len ) ); |
| 122 | |
| 123 | /* |
| 124 | * Make sure each parameter length is valid |
| 125 | */ |
| 126 | if( ciph_len < 3 || ( ciph_len % 3 ) != 0 ) |
| 127 | { |
| 128 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 129 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if( sess_len < 0 || sess_len > 32 ) |
| 133 | { |
| 134 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 135 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if( chal_len < 8 || chal_len > 32 ) |
| 139 | { |
| 140 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 141 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if( n != 6 + ciph_len + sess_len + chal_len ) |
| 145 | { |
| 146 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 147 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | SSL_DEBUG_BUF( 3, "client hello, cipherlist", |
| 151 | buf + 6, ciph_len ); |
| 152 | SSL_DEBUG_BUF( 3, "client hello, session id", |
| 153 | buf + 6 + ciph_len, sess_len ); |
| 154 | SSL_DEBUG_BUF( 3, "client hello, challenge", |
| 155 | buf + 6 + ciph_len + sess_len, chal_len ); |
| 156 | |
| 157 | p = buf + 6 + ciph_len; |
| 158 | ssl->session->length = sess_len; |
| 159 | memset( ssl->session->id, 0, sizeof( ssl->session->id ) ); |
| 160 | memcpy( ssl->session->id, p, ssl->session->length ); |
| 161 | |
| 162 | p += sess_len; |
| 163 | memset( ssl->randbytes, 0, 64 ); |
| 164 | memcpy( ssl->randbytes + 32 - chal_len, p, chal_len ); |
| 165 | |
| 166 | for( i = 0; ssl->ciphers[i] != 0; i++ ) |
| 167 | { |
| 168 | for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) |
| 169 | { |
| 170 | if( p[0] == 0 && |
| 171 | p[1] == 0 && |
| 172 | p[2] == ssl->ciphers[i] ) |
| 173 | goto have_cipher; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | SSL_DEBUG_BUF( 4, "record header", buf, 5 ); |
| 180 | |
| 181 | SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d", |
| 182 | buf[0] ) ); |
| 183 | SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d", |
| 184 | ( buf[3] << 8 ) | buf[4] ) ); |
| 185 | SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]", |
| 186 | buf[1], buf[2] ) ); |
| 187 | |
| 188 | /* |
| 189 | * SSLv3 Client Hello |
| 190 | * |
| 191 | * Record layer: |
| 192 | * 0 . 0 message type |
| 193 | * 1 . 2 protocol version |
| 194 | * 3 . 4 message length |
| 195 | */ |
| 196 | if( buf[0] != SSL_MSG_HANDSHAKE || |
| 197 | buf[1] != SSL_MAJOR_VERSION_3 ) |
| 198 | { |
| 199 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 200 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | n = ( buf[3] << 8 ) | buf[4]; |
| 204 | |
| 205 | if( n < 45 || n > 512 ) |
| 206 | { |
| 207 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 208 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 ) |
| 212 | { |
| 213 | SSL_DEBUG_RET( 1, "ssl_fetch_input", ret ); |
| 214 | return( ret ); |
| 215 | } |
| 216 | |
| 217 | buf = ssl->in_msg; |
| 218 | n = ssl->in_left - 5; |
| 219 | |
| 220 | md5_update( &ssl->fin_md5 , buf, n ); |
| 221 | sha1_update( &ssl->fin_sha1, buf, n ); |
| 222 | |
| 223 | /* |
| 224 | * SSL layer: |
| 225 | * 0 . 0 handshake type |
| 226 | * 1 . 3 handshake length |
| 227 | * 4 . 5 protocol version |
| 228 | * 6 . 9 UNIX time() |
| 229 | * 10 . 37 random bytes |
| 230 | * 38 . 38 session id length |
| 231 | * 39 . 38+x session id |
| 232 | * 39+x . 40+x cipherlist length |
| 233 | * 41+x . .. cipherlist |
| 234 | * .. . .. compression alg. |
| 235 | * .. . .. extensions |
| 236 | */ |
| 237 | SSL_DEBUG_BUF( 4, "record contents", buf, n ); |
| 238 | |
| 239 | SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", |
| 240 | buf[0] ) ); |
| 241 | SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d", |
| 242 | ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) ); |
| 243 | SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]", |
| 244 | buf[4], buf[5] ) ); |
| 245 | |
| 246 | /* |
| 247 | * Check the handshake type and protocol version |
| 248 | */ |
| 249 | if( buf[0] != SSL_HS_CLIENT_HELLO || |
| 250 | buf[4] != SSL_MAJOR_VERSION_3 ) |
| 251 | { |
| 252 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 253 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | ssl->major_ver = SSL_MAJOR_VERSION_3; |
| 257 | ssl->minor_ver = ( buf[5] <= SSL_MINOR_VERSION_1 ) |
| 258 | ? buf[5] : SSL_MINOR_VERSION_1; |
| 259 | |
| 260 | ssl->max_major_ver = buf[4]; |
| 261 | ssl->max_minor_ver = buf[5]; |
| 262 | |
| 263 | memcpy( ssl->randbytes, buf + 6, 32 ); |
| 264 | |
| 265 | /* |
| 266 | * Check the handshake message length |
| 267 | */ |
| 268 | if( buf[1] != 0 || n != 4 + ( ( buf[2] << 8 ) | buf[3] ) ) |
| 269 | { |
| 270 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 271 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Check the session length |
| 276 | */ |
| 277 | sess_len = buf[38]; |
| 278 | |
| 279 | if( sess_len < 0 || sess_len > 32 ) |
| 280 | { |
| 281 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 282 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | ssl->session->length = sess_len; |
| 286 | memset( ssl->session->id, 0, sizeof( ssl->session->id ) ); |
| 287 | memcpy( ssl->session->id, buf + 39 , ssl->session->length ); |
| 288 | |
| 289 | /* |
| 290 | * Check the cipherlist length |
| 291 | */ |
| 292 | ciph_len = ( buf[39 + sess_len] << 8 ) |
| 293 | | ( buf[40 + sess_len] ); |
| 294 | |
| 295 | if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 ) |
| 296 | { |
| 297 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 298 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Check the compression algorithms length |
| 303 | */ |
| 304 | comp_len = buf[41 + sess_len + ciph_len]; |
| 305 | |
| 306 | if( comp_len < 1 || comp_len > 16 ) |
| 307 | { |
| 308 | SSL_DEBUG_MSG( 1, ( "bad client hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 309 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | SSL_DEBUG_BUF( 3, "client hello, random bytes", |
| 313 | buf + 6, 32 ); |
| 314 | SSL_DEBUG_BUF( 3, "client hello, session id", |
| 315 | buf + 38, sess_len ); |
| 316 | SSL_DEBUG_BUF( 3, "client hello, cipherlist", |
| 317 | buf + 41 + sess_len, ciph_len ); |
| 318 | SSL_DEBUG_BUF( 3, "client hello, compression", |
| 319 | buf + 42 + sess_len + ciph_len, comp_len ); |
| 320 | |
| 321 | /* |
| 322 | * Search for a matching cipher |
| 323 | */ |
| 324 | for( i = 0; ssl->ciphers[i] != 0; i++ ) |
| 325 | { |
| 326 | for( j = 0, p = buf + 41 + sess_len; j < ciph_len; |
| 327 | j += 2, p += 2 ) |
| 328 | { |
| 329 | if( p[0] == 0 && p[1] == ssl->ciphers[i] ) |
| 330 | goto have_cipher; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | SSL_DEBUG_MSG( 1, ( "got no ciphers in common" ) ); |
| 336 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 337 | return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 | |
| 339 | have_cipher: |
| 340 | |
| 341 | ssl->session->cipher = ssl->ciphers[i]; |
| 342 | ssl->in_left = 0; |
| 343 | ssl->state++; |
| 344 | |
| 345 | SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) ); |
| 346 | |
| 347 | return( 0 ); |
| 348 | } |
| 349 | |
| 350 | static int ssl_write_server_hello( ssl_context *ssl ) |
| 351 | { |
| 352 | time_t t; |
| 353 | int ret, i, n; |
| 354 | unsigned char *buf, *p; |
| 355 | |
| 356 | SSL_DEBUG_MSG( 2, ( "=> write server hello" ) ); |
| 357 | |
| 358 | /* |
| 359 | * 0 . 0 handshake type |
| 360 | * 1 . 3 handshake length |
| 361 | * 4 . 5 protocol version |
| 362 | * 6 . 9 UNIX time() |
| 363 | * 10 . 37 random bytes |
| 364 | */ |
| 365 | buf = ssl->out_msg; |
| 366 | p = buf + 4; |
| 367 | |
| 368 | *p++ = (unsigned char) ssl->major_ver; |
| 369 | *p++ = (unsigned char) ssl->minor_ver; |
| 370 | |
| 371 | SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]", |
| 372 | buf[4], buf[5] ) ); |
| 373 | |
| 374 | t = time( NULL ); |
| 375 | *p++ = (unsigned char)( t >> 24 ); |
| 376 | *p++ = (unsigned char)( t >> 16 ); |
| 377 | *p++ = (unsigned char)( t >> 8 ); |
| 378 | *p++ = (unsigned char)( t ); |
| 379 | |
| 380 | SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) ); |
| 381 | |
| 382 | for( i = 28; i > 0; i-- ) |
| 383 | *p++ = (unsigned char) ssl->f_rng( ssl->p_rng ); |
| 384 | |
| 385 | memcpy( ssl->randbytes + 32, buf + 6, 32 ); |
| 386 | |
| 387 | SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 ); |
| 388 | |
| 389 | /* |
| 390 | * 38 . 38 session id length |
| 391 | * 39 . 38+n session id |
| 392 | * 39+n . 40+n chosen cipher |
| 393 | * 41+n . 41+n chosen compression alg. |
| 394 | */ |
| 395 | ssl->session->length = n = 32; |
| 396 | *p++ = (unsigned char) ssl->session->length; |
| 397 | |
| 398 | if( ssl->s_get == NULL || |
| 399 | ssl->s_get( ssl ) != 0 ) |
| 400 | { |
| 401 | /* |
| 402 | * Not found, create a new session id |
| 403 | */ |
| 404 | ssl->resume = 0; |
| 405 | ssl->state++; |
| 406 | |
| 407 | for( i = 0; i < n; i++ ) |
| 408 | ssl->session->id[i] = |
| 409 | (unsigned char) ssl->f_rng( ssl->p_rng ); |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | /* |
| 414 | * Found a matching session, resume it |
| 415 | */ |
| 416 | ssl->resume = 1; |
| 417 | ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC; |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 418 | |
| 419 | if( ( ret = ssl_derive_keys( ssl ) ) != 0 ) |
| 420 | { |
| 421 | SSL_DEBUG_RET( 1, "ssl_derive_keys", ret ); |
| 422 | return( ret ); |
| 423 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | memcpy( p, ssl->session->id, ssl->session->length ); |
| 427 | p += ssl->session->length; |
| 428 | |
| 429 | SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); |
| 430 | SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n ); |
| 431 | SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
| 432 | ssl->resume ? "a" : "no" ) ); |
| 433 | |
| 434 | *p++ = (unsigned char)( ssl->session->cipher >> 8 ); |
| 435 | *p++ = (unsigned char)( ssl->session->cipher ); |
| 436 | *p++ = SSL_COMPRESS_NULL; |
| 437 | |
| 438 | SSL_DEBUG_MSG( 3, ( "server hello, chosen cipher: %d", |
| 439 | ssl->session->cipher ) ); |
| 440 | SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", 0 ) ); |
| 441 | |
| 442 | ssl->out_msglen = p - buf; |
| 443 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 444 | ssl->out_msg[0] = SSL_HS_SERVER_HELLO; |
| 445 | |
| 446 | ret = ssl_write_record( ssl ); |
| 447 | |
| 448 | SSL_DEBUG_MSG( 2, ( "<= write server hello" ) ); |
| 449 | |
| 450 | return( ret ); |
| 451 | } |
| 452 | |
| 453 | static int ssl_write_certificate_request( ssl_context *ssl ) |
| 454 | { |
| 455 | int ret, n; |
| 456 | unsigned char *buf, *p; |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 457 | const x509_cert *crt; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 458 | |
| 459 | SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); |
| 460 | |
| 461 | ssl->state++; |
| 462 | |
| 463 | if( ssl->authmode == SSL_VERIFY_NONE ) |
| 464 | { |
| 465 | SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) ); |
| 466 | return( 0 ); |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * 0 . 0 handshake type |
| 471 | * 1 . 3 handshake length |
| 472 | * 4 . 4 cert type count |
| 473 | * 5 .. n-1 cert types |
| 474 | * n .. n+1 length of all DNs |
| 475 | * n+2 .. n+3 length of DN 1 |
| 476 | * n+4 .. ... Distinguished Name #1 |
| 477 | * ... .. ... length of DN 2, etc. |
| 478 | */ |
| 479 | buf = ssl->out_msg; |
| 480 | p = buf + 4; |
| 481 | |
| 482 | /* |
| 483 | * At the moment, only RSA certificates are supported |
| 484 | */ |
| 485 | *p++ = 1; |
| 486 | *p++ = 1; |
| 487 | |
| 488 | p += 2; |
| 489 | crt = ssl->ca_chain; |
| 490 | |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 491 | while( crt != NULL && crt->version != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 492 | { |
| 493 | if( p - buf > 4096 ) |
| 494 | break; |
| 495 | |
| 496 | n = crt->subject_raw.len; |
| 497 | *p++ = (unsigned char)( n >> 8 ); |
| 498 | *p++ = (unsigned char)( n ); |
| 499 | memcpy( p, crt->subject_raw.p, n ); |
| 500 | |
| 501 | SSL_DEBUG_BUF( 3, "requested DN", p, n ); |
| 502 | p += n; crt = crt->next; |
| 503 | } |
| 504 | |
| 505 | ssl->out_msglen = n = p - buf; |
| 506 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 507 | ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST; |
| 508 | ssl->out_msg[6] = (unsigned char)( ( n - 8 ) >> 8 ); |
| 509 | ssl->out_msg[7] = (unsigned char)( ( n - 8 ) ); |
| 510 | |
| 511 | ret = ssl_write_record( ssl ); |
| 512 | |
| 513 | SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) ); |
| 514 | |
| 515 | return( ret ); |
| 516 | } |
| 517 | |
| 518 | static int ssl_write_server_key_exchange( ssl_context *ssl ) |
| 519 | { |
| 520 | int ret, n; |
| 521 | unsigned char hash[36]; |
| 522 | md5_context md5; |
| 523 | sha1_context sha1; |
| 524 | |
| 525 | SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) ); |
| 526 | |
| 527 | if( ssl->session->cipher != SSL_EDH_RSA_DES_168_SHA && |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 528 | ssl->session->cipher != SSL_EDH_RSA_AES_256_SHA && |
| 529 | ssl->session->cipher != SSL_EDH_RSA_CAMELLIA_256_SHA) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 530 | { |
| 531 | SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) ); |
| 532 | ssl->state++; |
| 533 | return( 0 ); |
| 534 | } |
| 535 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 536 | #if !defined(POLARSSL_DHM_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 537 | SSL_DEBUG_MSG( 1, ( "support for dhm is not available" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 538 | return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 539 | #else |
| 540 | /* |
| 541 | * Ephemeral DH parameters: |
| 542 | * |
| 543 | * struct { |
| 544 | * opaque dh_p<1..2^16-1>; |
| 545 | * opaque dh_g<1..2^16-1>; |
| 546 | * opaque dh_Ys<1..2^16-1>; |
| 547 | * } ServerDHParams; |
| 548 | */ |
| 549 | if( ( ret = dhm_make_params( &ssl->dhm_ctx, 256, ssl->out_msg + 4, |
| 550 | &n, ssl->f_rng, ssl->p_rng ) ) != 0 ) |
| 551 | { |
| 552 | SSL_DEBUG_RET( 1, "dhm_make_params", ret ); |
| 553 | return( ret ); |
| 554 | } |
| 555 | |
| 556 | SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->dhm_ctx.X ); |
| 557 | SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->dhm_ctx.P ); |
| 558 | SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->dhm_ctx.G ); |
| 559 | SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->dhm_ctx.GX ); |
| 560 | |
| 561 | /* |
| 562 | * digitally-signed struct { |
| 563 | * opaque md5_hash[16]; |
| 564 | * opaque sha_hash[20]; |
| 565 | * }; |
| 566 | * |
| 567 | * md5_hash |
| 568 | * MD5(ClientHello.random + ServerHello.random |
| 569 | * + ServerParams); |
| 570 | * sha_hash |
| 571 | * SHA(ClientHello.random + ServerHello.random |
| 572 | * + ServerParams); |
| 573 | */ |
| 574 | md5_starts( &md5 ); |
| 575 | md5_update( &md5, ssl->randbytes, 64 ); |
| 576 | md5_update( &md5, ssl->out_msg + 4, n ); |
| 577 | md5_finish( &md5, hash ); |
| 578 | |
| 579 | sha1_starts( &sha1 ); |
| 580 | sha1_update( &sha1, ssl->randbytes, 64 ); |
| 581 | sha1_update( &sha1, ssl->out_msg + 4, n ); |
| 582 | sha1_finish( &sha1, hash + 16 ); |
| 583 | |
| 584 | SSL_DEBUG_BUF( 3, "parameters hash", hash, 36 ); |
| 585 | |
| 586 | ssl->out_msg[4 + n] = (unsigned char)( ssl->rsa_key->len >> 8 ); |
| 587 | ssl->out_msg[5 + n] = (unsigned char)( ssl->rsa_key->len ); |
| 588 | |
| 589 | ret = rsa_pkcs1_sign( ssl->rsa_key, RSA_PRIVATE, |
Paul Bakker | fc22c44 | 2009-07-19 20:36:27 +0000 | [diff] [blame] | 590 | SIG_RSA_RAW, 36, hash, ssl->out_msg + 6 + n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 591 | if( ret != 0 ) |
| 592 | { |
| 593 | SSL_DEBUG_RET( 1, "rsa_pkcs1_sign", ret ); |
| 594 | return( ret ); |
| 595 | } |
| 596 | |
| 597 | SSL_DEBUG_BUF( 3, "my RSA sig", ssl->out_msg + 6 + n, |
| 598 | ssl->rsa_key->len ); |
| 599 | |
| 600 | ssl->out_msglen = 6 + n + ssl->rsa_key->len; |
| 601 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 602 | ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE; |
| 603 | |
| 604 | ssl->state++; |
| 605 | |
| 606 | if( ( ret = ssl_write_record( ssl ) ) != 0 ) |
| 607 | { |
| 608 | SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 609 | return( ret ); |
| 610 | } |
| 611 | |
| 612 | SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) ); |
| 613 | |
| 614 | return( 0 ); |
| 615 | #endif |
| 616 | } |
| 617 | |
| 618 | static int ssl_write_server_hello_done( ssl_context *ssl ) |
| 619 | { |
| 620 | int ret; |
| 621 | |
| 622 | SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) ); |
| 623 | |
| 624 | ssl->out_msglen = 4; |
| 625 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 626 | ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE; |
| 627 | |
| 628 | ssl->state++; |
| 629 | |
| 630 | if( ( ret = ssl_write_record( ssl ) ) != 0 ) |
| 631 | { |
| 632 | SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 633 | return( ret ); |
| 634 | } |
| 635 | |
| 636 | SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) ); |
| 637 | |
| 638 | return( 0 ); |
| 639 | } |
| 640 | |
| 641 | static int ssl_parse_client_key_exchange( ssl_context *ssl ) |
| 642 | { |
| 643 | int ret, i, n; |
| 644 | |
| 645 | SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) ); |
| 646 | |
| 647 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 648 | { |
| 649 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 650 | return( ret ); |
| 651 | } |
| 652 | |
| 653 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 654 | { |
| 655 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 656 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE ) |
| 660 | { |
| 661 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 662 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | if( ssl->session->cipher == SSL_EDH_RSA_DES_168_SHA || |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 666 | ssl->session->cipher == SSL_EDH_RSA_AES_256_SHA || |
| 667 | ssl->session->cipher == SSL_EDH_RSA_CAMELLIA_256_SHA) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 668 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 669 | #if !defined(POLARSSL_DHM_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 670 | SSL_DEBUG_MSG( 1, ( "support for dhm is not available" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 671 | return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 672 | #else |
| 673 | /* |
| 674 | * Receive G^Y mod P, premaster = (G^Y)^X mod P |
| 675 | */ |
| 676 | n = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 677 | |
| 678 | if( n < 1 || n > ssl->dhm_ctx.len || |
| 679 | n + 6 != ssl->in_hslen ) |
| 680 | { |
| 681 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 682 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | if( ( ret = dhm_read_public( &ssl->dhm_ctx, |
| 686 | ssl->in_msg + 6, n ) ) != 0 ) |
| 687 | { |
| 688 | SSL_DEBUG_RET( 1, "dhm_read_public", ret ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 689 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE | ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->dhm_ctx.GY ); |
| 693 | |
| 694 | ssl->pmslen = ssl->dhm_ctx.len; |
| 695 | |
| 696 | if( ( ret = dhm_calc_secret( &ssl->dhm_ctx, |
| 697 | ssl->premaster, &ssl->pmslen ) ) != 0 ) |
| 698 | { |
| 699 | SSL_DEBUG_RET( 1, "dhm_calc_secret", ret ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 700 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE | ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->dhm_ctx.K ); |
| 704 | #endif |
| 705 | } |
| 706 | else |
| 707 | { |
| 708 | /* |
| 709 | * Decrypt the premaster using own private RSA key |
| 710 | */ |
| 711 | i = 4; |
| 712 | n = ssl->rsa_key->len; |
| 713 | ssl->pmslen = 48; |
| 714 | |
| 715 | if( ssl->minor_ver != SSL_MINOR_VERSION_0 ) |
| 716 | { |
| 717 | i += 2; |
| 718 | if( ssl->in_msg[4] != ( ( n >> 8 ) & 0xFF ) || |
| 719 | ssl->in_msg[5] != ( ( n ) & 0xFF ) ) |
| 720 | { |
| 721 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 722 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | if( ssl->in_hslen != i + n ) |
| 727 | { |
| 728 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 729 | return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | ret = rsa_pkcs1_decrypt( ssl->rsa_key, RSA_PRIVATE, &ssl->pmslen, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 733 | ssl->in_msg + i, ssl->premaster, |
| 734 | sizeof(ssl->premaster) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 735 | |
| 736 | if( ret != 0 || ssl->pmslen != 48 || |
| 737 | ssl->premaster[0] != ssl->max_major_ver || |
| 738 | ssl->premaster[1] != ssl->max_minor_ver ) |
| 739 | { |
| 740 | SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); |
| 741 | |
| 742 | /* |
| 743 | * Protection against Bleichenbacher's attack: |
| 744 | * invalid PKCS#1 v1.5 padding must not cause |
| 745 | * the connection to end immediately; instead, |
| 746 | * send a bad_record_mac later in the handshake. |
| 747 | */ |
| 748 | ssl->pmslen = 48; |
| 749 | |
| 750 | for( i = 0; i < ssl->pmslen; i++ ) |
| 751 | ssl->premaster[i] = (unsigned char) ssl->f_rng( ssl->p_rng ); |
| 752 | } |
| 753 | } |
| 754 | |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 755 | if( ( ret = ssl_derive_keys( ssl ) ) != 0 ) |
| 756 | { |
| 757 | SSL_DEBUG_RET( 1, "ssl_derive_keys", ret ); |
| 758 | return( ret ); |
| 759 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 760 | |
| 761 | if( ssl->s_set != NULL ) |
| 762 | ssl->s_set( ssl ); |
| 763 | |
| 764 | ssl->state++; |
| 765 | |
| 766 | SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) ); |
| 767 | |
| 768 | return( 0 ); |
| 769 | } |
| 770 | |
| 771 | static int ssl_parse_certificate_verify( ssl_context *ssl ) |
| 772 | { |
| 773 | int n1, n2, ret; |
| 774 | unsigned char hash[36]; |
| 775 | |
| 776 | SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); |
| 777 | |
| 778 | if( ssl->peer_cert == NULL ) |
| 779 | { |
| 780 | SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) ); |
| 781 | ssl->state++; |
| 782 | return( 0 ); |
| 783 | } |
| 784 | |
| 785 | ssl_calc_verify( ssl, hash ); |
| 786 | |
| 787 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 788 | { |
| 789 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 790 | return( ret ); |
| 791 | } |
| 792 | |
| 793 | ssl->state++; |
| 794 | |
| 795 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 796 | { |
| 797 | SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 798 | return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY ) |
| 802 | { |
| 803 | SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 804 | return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | n1 = ssl->peer_cert->rsa.len; |
| 808 | n2 = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 809 | |
| 810 | if( n1 + 6 != ssl->in_hslen || n1 != n2 ) |
| 811 | { |
| 812 | SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 813 | return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | ret = rsa_pkcs1_verify( &ssl->peer_cert->rsa, RSA_PUBLIC, |
Paul Bakker | fc22c44 | 2009-07-19 20:36:27 +0000 | [diff] [blame] | 817 | SIG_RSA_RAW, 36, hash, ssl->in_msg + 6 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 818 | if( ret != 0 ) |
| 819 | { |
| 820 | SSL_DEBUG_RET( 1, "rsa_pkcs1_verify", ret ); |
| 821 | return( ret ); |
| 822 | } |
| 823 | |
| 824 | SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) ); |
| 825 | |
| 826 | return( 0 ); |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * SSL handshake -- server side |
| 831 | */ |
| 832 | int ssl_handshake_server( ssl_context *ssl ) |
| 833 | { |
| 834 | int ret = 0; |
| 835 | |
| 836 | SSL_DEBUG_MSG( 2, ( "=> handshake server" ) ); |
| 837 | |
| 838 | while( ssl->state != SSL_HANDSHAKE_OVER ) |
| 839 | { |
| 840 | SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) ); |
| 841 | |
| 842 | if( ( ret = ssl_flush_output( ssl ) ) != 0 ) |
| 843 | break; |
| 844 | |
| 845 | switch( ssl->state ) |
| 846 | { |
| 847 | case SSL_HELLO_REQUEST: |
| 848 | ssl->state = SSL_CLIENT_HELLO; |
| 849 | break; |
| 850 | |
| 851 | /* |
| 852 | * <== ClientHello |
| 853 | */ |
| 854 | case SSL_CLIENT_HELLO: |
| 855 | ret = ssl_parse_client_hello( ssl ); |
| 856 | break; |
| 857 | |
| 858 | /* |
| 859 | * ==> ServerHello |
| 860 | * Certificate |
| 861 | * ( ServerKeyExchange ) |
| 862 | * ( CertificateRequest ) |
| 863 | * ServerHelloDone |
| 864 | */ |
| 865 | case SSL_SERVER_HELLO: |
| 866 | ret = ssl_write_server_hello( ssl ); |
| 867 | break; |
| 868 | |
| 869 | case SSL_SERVER_CERTIFICATE: |
| 870 | ret = ssl_write_certificate( ssl ); |
| 871 | break; |
| 872 | |
| 873 | case SSL_SERVER_KEY_EXCHANGE: |
| 874 | ret = ssl_write_server_key_exchange( ssl ); |
| 875 | break; |
| 876 | |
| 877 | case SSL_CERTIFICATE_REQUEST: |
| 878 | ret = ssl_write_certificate_request( ssl ); |
| 879 | break; |
| 880 | |
| 881 | case SSL_SERVER_HELLO_DONE: |
| 882 | ret = ssl_write_server_hello_done( ssl ); |
| 883 | break; |
| 884 | |
| 885 | /* |
| 886 | * <== ( Certificate/Alert ) |
| 887 | * ClientKeyExchange |
| 888 | * ( CertificateVerify ) |
| 889 | * ChangeCipherSpec |
| 890 | * Finished |
| 891 | */ |
| 892 | case SSL_CLIENT_CERTIFICATE: |
| 893 | ret = ssl_parse_certificate( ssl ); |
| 894 | break; |
| 895 | |
| 896 | case SSL_CLIENT_KEY_EXCHANGE: |
| 897 | ret = ssl_parse_client_key_exchange( ssl ); |
| 898 | break; |
| 899 | |
| 900 | case SSL_CERTIFICATE_VERIFY: |
| 901 | ret = ssl_parse_certificate_verify( ssl ); |
| 902 | break; |
| 903 | |
| 904 | case SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 905 | ret = ssl_parse_change_cipher_spec( ssl ); |
| 906 | break; |
| 907 | |
| 908 | case SSL_CLIENT_FINISHED: |
| 909 | ret = ssl_parse_finished( ssl ); |
| 910 | break; |
| 911 | |
| 912 | /* |
| 913 | * ==> ChangeCipherSpec |
| 914 | * Finished |
| 915 | */ |
| 916 | case SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 917 | ret = ssl_write_change_cipher_spec( ssl ); |
| 918 | break; |
| 919 | |
| 920 | case SSL_SERVER_FINISHED: |
| 921 | ret = ssl_write_finished( ssl ); |
| 922 | break; |
| 923 | |
| 924 | case SSL_FLUSH_BUFFERS: |
| 925 | SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 926 | ssl->state = SSL_HANDSHAKE_OVER; |
| 927 | break; |
| 928 | |
| 929 | default: |
| 930 | SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 931 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | if( ret != 0 ) |
| 935 | break; |
| 936 | } |
| 937 | |
| 938 | SSL_DEBUG_MSG( 2, ( "<= handshake server" ) ); |
| 939 | |
| 940 | return( ret ); |
| 941 | } |
| 942 | |
| 943 | #endif |