Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 client-side functions |
| 3 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame^] | 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 5 | * |
| 6 | * Copyright (C) 2009 Paul Bakker |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 23 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 25 | #if defined(POLARSSL_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #include "polarssl/debug.h" |
| 28 | #include "polarssl/ssl.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
| 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <stdio.h> |
| 33 | #include <time.h> |
| 34 | |
| 35 | static int ssl_write_client_hello( ssl_context *ssl ) |
| 36 | { |
| 37 | int ret, i, n; |
| 38 | unsigned char *buf; |
| 39 | unsigned char *p; |
| 40 | time_t t; |
| 41 | |
| 42 | SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); |
| 43 | |
| 44 | ssl->major_ver = SSL_MAJOR_VERSION_3; |
| 45 | ssl->minor_ver = SSL_MINOR_VERSION_0; |
| 46 | |
| 47 | ssl->max_major_ver = SSL_MAJOR_VERSION_3; |
| 48 | ssl->max_minor_ver = SSL_MINOR_VERSION_1; |
| 49 | |
| 50 | /* |
| 51 | * 0 . 0 handshake type |
| 52 | * 1 . 3 handshake length |
| 53 | * 4 . 5 highest version supported |
| 54 | * 6 . 9 current UNIX time |
| 55 | * 10 . 37 random bytes |
| 56 | */ |
| 57 | buf = ssl->out_msg; |
| 58 | p = buf + 4; |
| 59 | |
| 60 | *p++ = (unsigned char) ssl->max_major_ver; |
| 61 | *p++ = (unsigned char) ssl->max_minor_ver; |
| 62 | |
| 63 | SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]", |
| 64 | buf[4], buf[5] ) ); |
| 65 | |
| 66 | t = time( NULL ); |
| 67 | *p++ = (unsigned char)( t >> 24 ); |
| 68 | *p++ = (unsigned char)( t >> 16 ); |
| 69 | *p++ = (unsigned char)( t >> 8 ); |
| 70 | *p++ = (unsigned char)( t ); |
| 71 | |
| 72 | SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) ); |
| 73 | |
| 74 | for( i = 28; i > 0; i-- ) |
| 75 | *p++ = (unsigned char) ssl->f_rng( ssl->p_rng ); |
| 76 | |
| 77 | memcpy( ssl->randbytes, buf + 6, 32 ); |
| 78 | |
| 79 | SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 ); |
| 80 | |
| 81 | /* |
| 82 | * 38 . 38 session id length |
| 83 | * 39 . 39+n session id |
| 84 | * 40+n . 41+n cipherlist length |
| 85 | * 42+n . .. cipherlist |
| 86 | * .. . .. compression alg. (0) |
| 87 | * .. . .. extensions (unused) |
| 88 | */ |
| 89 | n = ssl->session->length; |
| 90 | |
| 91 | if( n < 16 || n > 32 || ssl->resume == 0 || |
| 92 | t - ssl->session->start > ssl->timeout ) |
| 93 | n = 0; |
| 94 | |
| 95 | *p++ = (unsigned char) n; |
| 96 | |
| 97 | for( i = 0; i < n; i++ ) |
| 98 | *p++ = ssl->session->id[i]; |
| 99 | |
| 100 | SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) ); |
| 101 | SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n ); |
| 102 | |
| 103 | for( n = 0; ssl->ciphers[n] != 0; n++ ); |
| 104 | *p++ = (unsigned char)( n >> 7 ); |
| 105 | *p++ = (unsigned char)( n << 1 ); |
| 106 | |
| 107 | SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphers", n ) ); |
| 108 | |
| 109 | for( i = 0; i < n; i++ ) |
| 110 | { |
| 111 | SSL_DEBUG_MSG( 3, ( "client hello, add cipher: %2d", |
| 112 | ssl->ciphers[i] ) ); |
| 113 | |
| 114 | *p++ = (unsigned char)( ssl->ciphers[i] >> 8 ); |
| 115 | *p++ = (unsigned char)( ssl->ciphers[i] ); |
| 116 | } |
| 117 | |
| 118 | SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) ); |
| 119 | SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", 0 ) ); |
| 120 | |
| 121 | *p++ = 1; |
| 122 | *p++ = SSL_COMPRESS_NULL; |
| 123 | |
| 124 | if ( ssl->hostname != NULL ) |
| 125 | { |
| 126 | SSL_DEBUG_MSG( 3, ( "client hello, server name extension: %s", |
| 127 | ssl->hostname ) ); |
| 128 | |
| 129 | *p++ = (unsigned char)( ( (ssl->hostname_len + 9) >> 8 ) & 0xFF ); |
| 130 | *p++ = (unsigned char)( ( (ssl->hostname_len + 9) ) & 0xFF ); |
| 131 | |
| 132 | *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF ); |
| 133 | *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF ); |
| 134 | |
| 135 | *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF ); |
| 136 | *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF ); |
| 137 | |
| 138 | *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF ); |
| 139 | *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF ); |
| 140 | |
| 141 | *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF ); |
| 142 | *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF ); |
| 143 | *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF ); |
| 144 | |
| 145 | memcpy( p, ssl->hostname, ssl->hostname_len ); |
| 146 | |
| 147 | p += ssl->hostname_len; |
| 148 | } |
| 149 | |
| 150 | ssl->out_msglen = p - buf; |
| 151 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 152 | ssl->out_msg[0] = SSL_HS_CLIENT_HELLO; |
| 153 | |
| 154 | ssl->state++; |
| 155 | |
| 156 | if( ( ret = ssl_write_record( ssl ) ) != 0 ) |
| 157 | { |
| 158 | SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 159 | return( ret ); |
| 160 | } |
| 161 | |
| 162 | SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); |
| 163 | |
| 164 | return( 0 ); |
| 165 | } |
| 166 | |
| 167 | static int ssl_parse_server_hello( ssl_context *ssl ) |
| 168 | { |
| 169 | time_t t; |
| 170 | int ret, i, n; |
| 171 | int ext_len; |
| 172 | unsigned char *buf; |
| 173 | |
| 174 | SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) ); |
| 175 | |
| 176 | /* |
| 177 | * 0 . 0 handshake type |
| 178 | * 1 . 3 handshake length |
| 179 | * 4 . 5 protocol version |
| 180 | * 6 . 9 UNIX time() |
| 181 | * 10 . 37 random bytes |
| 182 | */ |
| 183 | buf = ssl->in_msg; |
| 184 | |
| 185 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 186 | { |
| 187 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 188 | return( ret ); |
| 189 | } |
| 190 | |
| 191 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 192 | { |
| 193 | SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 194 | return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]", |
| 198 | buf[4], buf[5] ) ); |
| 199 | |
| 200 | if( ssl->in_hslen < 42 || |
| 201 | buf[0] != SSL_HS_SERVER_HELLO || |
| 202 | buf[4] != SSL_MAJOR_VERSION_3 ) |
| 203 | { |
| 204 | SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 205 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | if( buf[5] != SSL_MINOR_VERSION_0 && |
| 209 | buf[5] != SSL_MINOR_VERSION_1 ) |
| 210 | { |
| 211 | SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 212 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | ssl->minor_ver = buf[5]; |
| 216 | |
| 217 | t = ( (time_t) buf[6] << 24 ) |
| 218 | | ( (time_t) buf[7] << 16 ) |
| 219 | | ( (time_t) buf[8] << 8 ) |
| 220 | | ( (time_t) buf[9] ); |
| 221 | |
| 222 | memcpy( ssl->randbytes + 32, buf + 6, 32 ); |
| 223 | |
| 224 | n = buf[38]; |
| 225 | |
| 226 | SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) ); |
| 227 | SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 ); |
| 228 | |
| 229 | /* |
| 230 | * 38 . 38 session id length |
| 231 | * 39 . 38+n session id |
| 232 | * 39+n . 40+n chosen cipher |
| 233 | * 41+n . 41+n chosen compression alg. |
| 234 | * 42+n . 43+n extensions length |
| 235 | * 44+n . 44+n+m extensions |
| 236 | */ |
| 237 | if( n < 0 || n > 32 || ssl->in_hslen > 42 + n ) |
| 238 | { |
| 239 | ext_len = ( ( buf[42 + n] << 8 ) |
| 240 | | ( buf[43 + n] ) ) + 2; |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | ext_len = 0; |
| 245 | } |
| 246 | |
| 247 | if( n < 0 || n > 32 || ssl->in_hslen != 42 + n + ext_len ) |
| 248 | { |
| 249 | SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 250 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | i = ( buf[39 + n] << 8 ) | buf[40 + n]; |
| 254 | |
| 255 | SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); |
| 256 | SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n ); |
| 257 | |
| 258 | /* |
| 259 | * Check if the session can be resumed |
| 260 | */ |
| 261 | if( ssl->resume == 0 || n == 0 || |
| 262 | ssl->session->cipher != i || |
| 263 | ssl->session->length != n || |
| 264 | memcmp( ssl->session->id, buf + 39, n ) != 0 ) |
| 265 | { |
| 266 | ssl->state++; |
| 267 | ssl->resume = 0; |
| 268 | ssl->session->start = time( NULL ); |
| 269 | ssl->session->cipher = i; |
| 270 | ssl->session->length = n; |
| 271 | memcpy( ssl->session->id, buf + 39, n ); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC; |
| 276 | ssl_derive_keys( ssl ); |
| 277 | } |
| 278 | |
| 279 | SSL_DEBUG_MSG( 3, ( "%s session has been resumed", |
| 280 | ssl->resume ? "a" : "no" ) ); |
| 281 | |
| 282 | SSL_DEBUG_MSG( 3, ( "server hello, chosen cipher: %d", i ) ); |
| 283 | SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) ); |
| 284 | |
| 285 | i = 0; |
| 286 | while( 1 ) |
| 287 | { |
| 288 | if( ssl->ciphers[i] == 0 ) |
| 289 | { |
| 290 | SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 291 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | if( ssl->ciphers[i++] == ssl->session->cipher ) |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | if( buf[41 + n] != SSL_COMPRESS_NULL ) |
| 299 | { |
| 300 | SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 301 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* TODO: Process extensions */ |
| 305 | |
| 306 | SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) ); |
| 307 | |
| 308 | return( 0 ); |
| 309 | } |
| 310 | |
| 311 | static int ssl_parse_server_key_exchange( ssl_context *ssl ) |
| 312 | { |
| 313 | int ret, n; |
| 314 | unsigned char *p, *end; |
| 315 | unsigned char hash[36]; |
| 316 | md5_context md5; |
| 317 | sha1_context sha1; |
| 318 | |
| 319 | SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) ); |
| 320 | |
| 321 | if( ssl->session->cipher != SSL_EDH_RSA_DES_168_SHA && |
| 322 | ssl->session->cipher != SSL_EDH_RSA_AES_256_SHA ) |
| 323 | { |
| 324 | SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); |
| 325 | ssl->state++; |
| 326 | return( 0 ); |
| 327 | } |
| 328 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 329 | #if !defined(POLARSSL_DHM_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | SSL_DEBUG_MSG( 1, ( "support for dhm in not available" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 331 | return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 332 | #else |
| 333 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 334 | { |
| 335 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 336 | return( ret ); |
| 337 | } |
| 338 | |
| 339 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 340 | { |
| 341 | SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 342 | return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE ) |
| 346 | { |
| 347 | SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 348 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Ephemeral DH parameters: |
| 353 | * |
| 354 | * struct { |
| 355 | * opaque dh_p<1..2^16-1>; |
| 356 | * opaque dh_g<1..2^16-1>; |
| 357 | * opaque dh_Ys<1..2^16-1>; |
| 358 | * } ServerDHParams; |
| 359 | */ |
| 360 | p = ssl->in_msg + 4; |
| 361 | end = ssl->in_msg + ssl->in_hslen; |
| 362 | |
| 363 | if( ( ret = dhm_read_params( &ssl->dhm_ctx, &p, end ) ) != 0 ) |
| 364 | { |
| 365 | SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 366 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | if( (int)( end - p ) != ssl->peer_cert->rsa.len ) |
| 370 | { |
| 371 | SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 372 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | if( ssl->dhm_ctx.len < 64 || ssl->dhm_ctx.len > 256 ) |
| 376 | { |
| 377 | SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 378 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->dhm_ctx.P ); |
| 382 | SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->dhm_ctx.G ); |
| 383 | SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->dhm_ctx.GY ); |
| 384 | |
| 385 | /* |
| 386 | * digitally-signed struct { |
| 387 | * opaque md5_hash[16]; |
| 388 | * opaque sha_hash[20]; |
| 389 | * }; |
| 390 | * |
| 391 | * md5_hash |
| 392 | * MD5(ClientHello.random + ServerHello.random |
| 393 | * + ServerParams); |
| 394 | * sha_hash |
| 395 | * SHA(ClientHello.random + ServerHello.random |
| 396 | * + ServerParams); |
| 397 | */ |
| 398 | n = ssl->in_hslen - ( end - p ) - 6; |
| 399 | |
| 400 | md5_starts( &md5 ); |
| 401 | md5_update( &md5, ssl->randbytes, 64 ); |
| 402 | md5_update( &md5, ssl->in_msg + 4, n ); |
| 403 | md5_finish( &md5, hash ); |
| 404 | |
| 405 | sha1_starts( &sha1 ); |
| 406 | sha1_update( &sha1, ssl->randbytes, 64 ); |
| 407 | sha1_update( &sha1, ssl->in_msg + 4, n ); |
| 408 | sha1_finish( &sha1, hash + 16 ); |
| 409 | |
| 410 | SSL_DEBUG_BUF( 3, "parameters hash", hash, 36 ); |
| 411 | |
| 412 | if( ( ret = rsa_pkcs1_verify( &ssl->peer_cert->rsa, RSA_PUBLIC, |
| 413 | RSA_RAW, 36, hash, p ) ) != 0 ) |
| 414 | { |
| 415 | SSL_DEBUG_RET( 1, "rsa_pkcs1_verify", ret ); |
| 416 | return( ret ); |
| 417 | } |
| 418 | |
| 419 | ssl->state++; |
| 420 | |
| 421 | SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) ); |
| 422 | |
| 423 | return( 0 ); |
| 424 | #endif |
| 425 | } |
| 426 | |
| 427 | static int ssl_parse_certificate_request( ssl_context *ssl ) |
| 428 | { |
| 429 | int ret; |
| 430 | |
| 431 | SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); |
| 432 | |
| 433 | /* |
| 434 | * 0 . 0 handshake type |
| 435 | * 1 . 3 handshake length |
| 436 | * 4 . 5 SSL version |
| 437 | * 6 . 6 cert type count |
| 438 | * 7 .. n-1 cert types |
| 439 | * n .. n+1 length of all DNs |
| 440 | * n+2 .. n+3 length of DN 1 |
| 441 | * n+4 .. ... Distinguished Name #1 |
| 442 | * ... .. ... length of DN 2, etc. |
| 443 | */ |
| 444 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 445 | { |
| 446 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 447 | return( ret ); |
| 448 | } |
| 449 | |
| 450 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 451 | { |
| 452 | SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 453 | return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | ssl->client_auth = 0; |
| 457 | ssl->state++; |
| 458 | |
| 459 | if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST ) |
| 460 | ssl->client_auth++; |
| 461 | |
| 462 | SSL_DEBUG_MSG( 3, ( "got %s certificate request", |
| 463 | ssl->client_auth ? "a" : "no" ) ); |
| 464 | |
| 465 | SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) ); |
| 466 | |
| 467 | return( 0 ); |
| 468 | } |
| 469 | |
| 470 | static int ssl_parse_server_hello_done( ssl_context *ssl ) |
| 471 | { |
| 472 | int ret; |
| 473 | |
| 474 | SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) ); |
| 475 | |
| 476 | if( ssl->client_auth != 0 ) |
| 477 | { |
| 478 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 479 | { |
| 480 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 481 | return( ret ); |
| 482 | } |
| 483 | |
| 484 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 485 | { |
| 486 | SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 487 | return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | if( ssl->in_hslen != 4 || |
| 492 | ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE ) |
| 493 | { |
| 494 | SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 495 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | ssl->state++; |
| 499 | |
| 500 | SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) ); |
| 501 | |
| 502 | return( 0 ); |
| 503 | } |
| 504 | |
| 505 | static int ssl_write_client_key_exchange( ssl_context *ssl ) |
| 506 | { |
| 507 | int ret, i, n; |
| 508 | |
| 509 | SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) ); |
| 510 | |
| 511 | if( ssl->session->cipher == SSL_EDH_RSA_DES_168_SHA || |
| 512 | ssl->session->cipher == SSL_EDH_RSA_AES_256_SHA ) |
| 513 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 514 | #if !defined(POLARSSL_DHM_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 515 | SSL_DEBUG_MSG( 1, ( "support for dhm in not available" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 516 | return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 517 | #else |
| 518 | /* |
| 519 | * DHM key exchange -- send G^X mod P |
| 520 | */ |
| 521 | n = ssl->dhm_ctx.len; |
| 522 | |
| 523 | ssl->out_msg[4] = (unsigned char)( n >> 8 ); |
| 524 | ssl->out_msg[5] = (unsigned char)( n ); |
| 525 | i = 6; |
| 526 | |
| 527 | ret = dhm_make_public( &ssl->dhm_ctx, 256, |
| 528 | &ssl->out_msg[i], n, |
| 529 | ssl->f_rng, ssl->p_rng ); |
| 530 | if( ret != 0 ) |
| 531 | { |
| 532 | SSL_DEBUG_RET( 1, "dhm_make_public", ret ); |
| 533 | return( ret ); |
| 534 | } |
| 535 | |
| 536 | SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->dhm_ctx.X ); |
| 537 | SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->dhm_ctx.GX ); |
| 538 | |
| 539 | ssl->pmslen = ssl->dhm_ctx.len; |
| 540 | |
| 541 | if( ( ret = dhm_calc_secret( &ssl->dhm_ctx, |
| 542 | ssl->premaster, |
| 543 | &ssl->pmslen ) ) != 0 ) |
| 544 | { |
| 545 | SSL_DEBUG_RET( 1, "dhm_calc_secret", ret ); |
| 546 | return( ret ); |
| 547 | } |
| 548 | |
| 549 | SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->dhm_ctx.K ); |
| 550 | #endif |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | /* |
| 555 | * RSA key exchange -- send rsa_public(pkcs1 v1.5(premaster)) |
| 556 | */ |
| 557 | ssl->premaster[0] = (unsigned char) ssl->max_major_ver; |
| 558 | ssl->premaster[1] = (unsigned char) ssl->max_minor_ver; |
| 559 | ssl->pmslen = 48; |
| 560 | |
| 561 | for( i = 2; i < ssl->pmslen; i++ ) |
| 562 | ssl->premaster[i] = (unsigned char) ssl->f_rng( ssl->p_rng ); |
| 563 | |
| 564 | i = 4; |
| 565 | n = ssl->peer_cert->rsa.len; |
| 566 | |
| 567 | if( ssl->minor_ver != SSL_MINOR_VERSION_0 ) |
| 568 | { |
| 569 | i += 2; |
| 570 | ssl->out_msg[4] = (unsigned char)( n >> 8 ); |
| 571 | ssl->out_msg[5] = (unsigned char)( n ); |
| 572 | } |
| 573 | |
| 574 | ret = rsa_pkcs1_encrypt( &ssl->peer_cert->rsa, RSA_PUBLIC, |
| 575 | ssl->pmslen, ssl->premaster, |
| 576 | ssl->out_msg + i ); |
| 577 | if( ret != 0 ) |
| 578 | { |
| 579 | SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret ); |
| 580 | return( ret ); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | ssl_derive_keys( ssl ); |
| 585 | |
| 586 | ssl->out_msglen = i + n; |
| 587 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 588 | ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE; |
| 589 | |
| 590 | ssl->state++; |
| 591 | |
| 592 | if( ( ret = ssl_write_record( ssl ) ) != 0 ) |
| 593 | { |
| 594 | SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 595 | return( ret ); |
| 596 | } |
| 597 | |
| 598 | SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) ); |
| 599 | |
| 600 | return( 0 ); |
| 601 | } |
| 602 | |
| 603 | static int ssl_write_certificate_verify( ssl_context *ssl ) |
| 604 | { |
| 605 | int ret, n; |
| 606 | unsigned char hash[36]; |
| 607 | |
| 608 | SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); |
| 609 | |
| 610 | if( ssl->client_auth == 0 ) |
| 611 | { |
| 612 | SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
| 613 | ssl->state++; |
| 614 | return( 0 ); |
| 615 | } |
| 616 | |
| 617 | if( ssl->rsa_key == NULL ) |
| 618 | { |
| 619 | SSL_DEBUG_MSG( 1, ( "got no private key" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 620 | return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Make an RSA signature of the handshake digests |
| 625 | */ |
| 626 | ssl_calc_verify( ssl, hash ); |
| 627 | |
| 628 | n = ssl->rsa_key->len; |
| 629 | ssl->out_msg[4] = (unsigned char)( n >> 8 ); |
| 630 | ssl->out_msg[5] = (unsigned char)( n ); |
| 631 | |
| 632 | if( ( ret = rsa_pkcs1_sign( ssl->rsa_key, RSA_PRIVATE, RSA_RAW, |
| 633 | 36, hash, ssl->out_msg + 6 ) ) != 0 ) |
| 634 | { |
| 635 | SSL_DEBUG_RET( 1, "rsa_pkcs1_sign", ret ); |
| 636 | return( ret ); |
| 637 | } |
| 638 | |
| 639 | ssl->out_msglen = 6 + n; |
| 640 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 641 | ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY; |
| 642 | |
| 643 | ssl->state++; |
| 644 | |
| 645 | if( ( ret = ssl_write_record( ssl ) ) != 0 ) |
| 646 | { |
| 647 | SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 648 | return( ret ); |
| 649 | } |
| 650 | |
| 651 | SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) ); |
| 652 | |
| 653 | return( 0 ); |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * SSL handshake -- client side |
| 658 | */ |
| 659 | int ssl_handshake_client( ssl_context *ssl ) |
| 660 | { |
| 661 | int ret = 0; |
| 662 | |
| 663 | SSL_DEBUG_MSG( 2, ( "=> handshake client" ) ); |
| 664 | |
| 665 | while( ssl->state != SSL_HANDSHAKE_OVER ) |
| 666 | { |
| 667 | SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); |
| 668 | |
| 669 | if( ( ret = ssl_flush_output( ssl ) ) != 0 ) |
| 670 | break; |
| 671 | |
| 672 | switch( ssl->state ) |
| 673 | { |
| 674 | case SSL_HELLO_REQUEST: |
| 675 | ssl->state = SSL_CLIENT_HELLO; |
| 676 | break; |
| 677 | |
| 678 | /* |
| 679 | * ==> ClientHello |
| 680 | */ |
| 681 | case SSL_CLIENT_HELLO: |
| 682 | ret = ssl_write_client_hello( ssl ); |
| 683 | break; |
| 684 | |
| 685 | /* |
| 686 | * <== ServerHello |
| 687 | * Certificate |
| 688 | * ( ServerKeyExchange ) |
| 689 | * ( CertificateRequest ) |
| 690 | * ServerHelloDone |
| 691 | */ |
| 692 | case SSL_SERVER_HELLO: |
| 693 | ret = ssl_parse_server_hello( ssl ); |
| 694 | break; |
| 695 | |
| 696 | case SSL_SERVER_CERTIFICATE: |
| 697 | ret = ssl_parse_certificate( ssl ); |
| 698 | break; |
| 699 | |
| 700 | case SSL_SERVER_KEY_EXCHANGE: |
| 701 | ret = ssl_parse_server_key_exchange( ssl ); |
| 702 | break; |
| 703 | |
| 704 | case SSL_CERTIFICATE_REQUEST: |
| 705 | ret = ssl_parse_certificate_request( ssl ); |
| 706 | break; |
| 707 | |
| 708 | case SSL_SERVER_HELLO_DONE: |
| 709 | ret = ssl_parse_server_hello_done( ssl ); |
| 710 | break; |
| 711 | |
| 712 | /* |
| 713 | * ==> ( Certificate/Alert ) |
| 714 | * ClientKeyExchange |
| 715 | * ( CertificateVerify ) |
| 716 | * ChangeCipherSpec |
| 717 | * Finished |
| 718 | */ |
| 719 | case SSL_CLIENT_CERTIFICATE: |
| 720 | ret = ssl_write_certificate( ssl ); |
| 721 | break; |
| 722 | |
| 723 | case SSL_CLIENT_KEY_EXCHANGE: |
| 724 | ret = ssl_write_client_key_exchange( ssl ); |
| 725 | break; |
| 726 | |
| 727 | case SSL_CERTIFICATE_VERIFY: |
| 728 | ret = ssl_write_certificate_verify( ssl ); |
| 729 | break; |
| 730 | |
| 731 | case SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 732 | ret = ssl_write_change_cipher_spec( ssl ); |
| 733 | break; |
| 734 | |
| 735 | case SSL_CLIENT_FINISHED: |
| 736 | ret = ssl_write_finished( ssl ); |
| 737 | break; |
| 738 | |
| 739 | /* |
| 740 | * <== ChangeCipherSpec |
| 741 | * Finished |
| 742 | */ |
| 743 | case SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 744 | ret = ssl_parse_change_cipher_spec( ssl ); |
| 745 | break; |
| 746 | |
| 747 | case SSL_SERVER_FINISHED: |
| 748 | ret = ssl_parse_finished( ssl ); |
| 749 | break; |
| 750 | |
| 751 | case SSL_FLUSH_BUFFERS: |
| 752 | SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 753 | ssl->state = SSL_HANDSHAKE_OVER; |
| 754 | break; |
| 755 | |
| 756 | default: |
| 757 | SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 758 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | if( ret != 0 ) |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | SSL_DEBUG_MSG( 2, ( "<= handshake client" ) ); |
| 766 | |
| 767 | return( ret ); |
| 768 | } |
| 769 | |
| 770 | #endif |