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 | * |
Paul Bakker | 785a9ee | 2009-01-25 14:15:10 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
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 && |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 322 | ssl->session->cipher != SSL_EDH_RSA_AES_256_SHA && |
| 323 | ssl->session->cipher != SSL_EDH_RSA_CAMELLIA_256_SHA) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 324 | { |
| 325 | SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); |
| 326 | ssl->state++; |
| 327 | return( 0 ); |
| 328 | } |
| 329 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 330 | #if !defined(POLARSSL_DHM_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 331 | SSL_DEBUG_MSG( 1, ( "support for dhm in not available" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 332 | return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 333 | #else |
| 334 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 335 | { |
| 336 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 337 | return( ret ); |
| 338 | } |
| 339 | |
| 340 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 341 | { |
| 342 | SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 343 | return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE ) |
| 347 | { |
| 348 | SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 349 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Ephemeral DH parameters: |
| 354 | * |
| 355 | * struct { |
| 356 | * opaque dh_p<1..2^16-1>; |
| 357 | * opaque dh_g<1..2^16-1>; |
| 358 | * opaque dh_Ys<1..2^16-1>; |
| 359 | * } ServerDHParams; |
| 360 | */ |
| 361 | p = ssl->in_msg + 4; |
| 362 | end = ssl->in_msg + ssl->in_hslen; |
| 363 | |
| 364 | if( ( ret = dhm_read_params( &ssl->dhm_ctx, &p, end ) ) != 0 ) |
| 365 | { |
| 366 | SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 367 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | if( (int)( end - p ) != ssl->peer_cert->rsa.len ) |
| 371 | { |
| 372 | SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 373 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | if( ssl->dhm_ctx.len < 64 || ssl->dhm_ctx.len > 256 ) |
| 377 | { |
| 378 | SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 379 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->dhm_ctx.P ); |
| 383 | SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->dhm_ctx.G ); |
| 384 | SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->dhm_ctx.GY ); |
| 385 | |
| 386 | /* |
| 387 | * digitally-signed struct { |
| 388 | * opaque md5_hash[16]; |
| 389 | * opaque sha_hash[20]; |
| 390 | * }; |
| 391 | * |
| 392 | * md5_hash |
| 393 | * MD5(ClientHello.random + ServerHello.random |
| 394 | * + ServerParams); |
| 395 | * sha_hash |
| 396 | * SHA(ClientHello.random + ServerHello.random |
| 397 | * + ServerParams); |
| 398 | */ |
| 399 | n = ssl->in_hslen - ( end - p ) - 6; |
| 400 | |
| 401 | md5_starts( &md5 ); |
| 402 | md5_update( &md5, ssl->randbytes, 64 ); |
| 403 | md5_update( &md5, ssl->in_msg + 4, n ); |
| 404 | md5_finish( &md5, hash ); |
| 405 | |
| 406 | sha1_starts( &sha1 ); |
| 407 | sha1_update( &sha1, ssl->randbytes, 64 ); |
| 408 | sha1_update( &sha1, ssl->in_msg + 4, n ); |
| 409 | sha1_finish( &sha1, hash + 16 ); |
| 410 | |
| 411 | SSL_DEBUG_BUF( 3, "parameters hash", hash, 36 ); |
| 412 | |
| 413 | if( ( ret = rsa_pkcs1_verify( &ssl->peer_cert->rsa, RSA_PUBLIC, |
| 414 | RSA_RAW, 36, hash, p ) ) != 0 ) |
| 415 | { |
| 416 | SSL_DEBUG_RET( 1, "rsa_pkcs1_verify", ret ); |
| 417 | return( ret ); |
| 418 | } |
| 419 | |
| 420 | ssl->state++; |
| 421 | |
| 422 | SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) ); |
| 423 | |
| 424 | return( 0 ); |
| 425 | #endif |
| 426 | } |
| 427 | |
| 428 | static int ssl_parse_certificate_request( ssl_context *ssl ) |
| 429 | { |
| 430 | int ret; |
| 431 | |
| 432 | SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); |
| 433 | |
| 434 | /* |
| 435 | * 0 . 0 handshake type |
| 436 | * 1 . 3 handshake length |
| 437 | * 4 . 5 SSL version |
| 438 | * 6 . 6 cert type count |
| 439 | * 7 .. n-1 cert types |
| 440 | * n .. n+1 length of all DNs |
| 441 | * n+2 .. n+3 length of DN 1 |
| 442 | * n+4 .. ... Distinguished Name #1 |
| 443 | * ... .. ... length of DN 2, etc. |
| 444 | */ |
| 445 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 446 | { |
| 447 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 448 | return( ret ); |
| 449 | } |
| 450 | |
| 451 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 452 | { |
| 453 | SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 454 | return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | ssl->client_auth = 0; |
| 458 | ssl->state++; |
| 459 | |
| 460 | if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST ) |
| 461 | ssl->client_auth++; |
| 462 | |
| 463 | SSL_DEBUG_MSG( 3, ( "got %s certificate request", |
| 464 | ssl->client_auth ? "a" : "no" ) ); |
| 465 | |
| 466 | SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) ); |
| 467 | |
| 468 | return( 0 ); |
| 469 | } |
| 470 | |
| 471 | static int ssl_parse_server_hello_done( ssl_context *ssl ) |
| 472 | { |
| 473 | int ret; |
| 474 | |
| 475 | SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) ); |
| 476 | |
| 477 | if( ssl->client_auth != 0 ) |
| 478 | { |
| 479 | if( ( ret = ssl_read_record( ssl ) ) != 0 ) |
| 480 | { |
| 481 | SSL_DEBUG_RET( 1, "ssl_read_record", ret ); |
| 482 | return( ret ); |
| 483 | } |
| 484 | |
| 485 | if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ) |
| 486 | { |
| 487 | SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 488 | return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | if( ssl->in_hslen != 4 || |
| 493 | ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE ) |
| 494 | { |
| 495 | SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 496 | return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | ssl->state++; |
| 500 | |
| 501 | SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) ); |
| 502 | |
| 503 | return( 0 ); |
| 504 | } |
| 505 | |
| 506 | static int ssl_write_client_key_exchange( ssl_context *ssl ) |
| 507 | { |
| 508 | int ret, i, n; |
| 509 | |
| 510 | SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) ); |
| 511 | |
| 512 | if( ssl->session->cipher == SSL_EDH_RSA_DES_168_SHA || |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 513 | ssl->session->cipher == SSL_EDH_RSA_AES_256_SHA || |
| 514 | ssl->session->cipher == SSL_EDH_RSA_CAMELLIA_256_SHA) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 515 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 516 | #if !defined(POLARSSL_DHM_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 517 | SSL_DEBUG_MSG( 1, ( "support for dhm in not available" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 518 | return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 519 | #else |
| 520 | /* |
| 521 | * DHM key exchange -- send G^X mod P |
| 522 | */ |
| 523 | n = ssl->dhm_ctx.len; |
| 524 | |
| 525 | ssl->out_msg[4] = (unsigned char)( n >> 8 ); |
| 526 | ssl->out_msg[5] = (unsigned char)( n ); |
| 527 | i = 6; |
| 528 | |
| 529 | ret = dhm_make_public( &ssl->dhm_ctx, 256, |
| 530 | &ssl->out_msg[i], n, |
| 531 | ssl->f_rng, ssl->p_rng ); |
| 532 | if( ret != 0 ) |
| 533 | { |
| 534 | SSL_DEBUG_RET( 1, "dhm_make_public", ret ); |
| 535 | return( ret ); |
| 536 | } |
| 537 | |
| 538 | SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->dhm_ctx.X ); |
| 539 | SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->dhm_ctx.GX ); |
| 540 | |
| 541 | ssl->pmslen = ssl->dhm_ctx.len; |
| 542 | |
| 543 | if( ( ret = dhm_calc_secret( &ssl->dhm_ctx, |
| 544 | ssl->premaster, |
| 545 | &ssl->pmslen ) ) != 0 ) |
| 546 | { |
| 547 | SSL_DEBUG_RET( 1, "dhm_calc_secret", ret ); |
| 548 | return( ret ); |
| 549 | } |
| 550 | |
| 551 | SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->dhm_ctx.K ); |
| 552 | #endif |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | /* |
| 557 | * RSA key exchange -- send rsa_public(pkcs1 v1.5(premaster)) |
| 558 | */ |
| 559 | ssl->premaster[0] = (unsigned char) ssl->max_major_ver; |
| 560 | ssl->premaster[1] = (unsigned char) ssl->max_minor_ver; |
| 561 | ssl->pmslen = 48; |
| 562 | |
| 563 | for( i = 2; i < ssl->pmslen; i++ ) |
| 564 | ssl->premaster[i] = (unsigned char) ssl->f_rng( ssl->p_rng ); |
| 565 | |
| 566 | i = 4; |
| 567 | n = ssl->peer_cert->rsa.len; |
| 568 | |
| 569 | if( ssl->minor_ver != SSL_MINOR_VERSION_0 ) |
| 570 | { |
| 571 | i += 2; |
| 572 | ssl->out_msg[4] = (unsigned char)( n >> 8 ); |
| 573 | ssl->out_msg[5] = (unsigned char)( n ); |
| 574 | } |
| 575 | |
| 576 | ret = rsa_pkcs1_encrypt( &ssl->peer_cert->rsa, RSA_PUBLIC, |
| 577 | ssl->pmslen, ssl->premaster, |
| 578 | ssl->out_msg + i ); |
| 579 | if( ret != 0 ) |
| 580 | { |
| 581 | SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret ); |
| 582 | return( ret ); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | ssl_derive_keys( ssl ); |
| 587 | |
| 588 | ssl->out_msglen = i + n; |
| 589 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 590 | ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE; |
| 591 | |
| 592 | ssl->state++; |
| 593 | |
| 594 | if( ( ret = ssl_write_record( ssl ) ) != 0 ) |
| 595 | { |
| 596 | SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 597 | return( ret ); |
| 598 | } |
| 599 | |
| 600 | SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) ); |
| 601 | |
| 602 | return( 0 ); |
| 603 | } |
| 604 | |
| 605 | static int ssl_write_certificate_verify( ssl_context *ssl ) |
| 606 | { |
| 607 | int ret, n; |
| 608 | unsigned char hash[36]; |
| 609 | |
| 610 | SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); |
| 611 | |
| 612 | if( ssl->client_auth == 0 ) |
| 613 | { |
| 614 | SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) ); |
| 615 | ssl->state++; |
| 616 | return( 0 ); |
| 617 | } |
| 618 | |
| 619 | if( ssl->rsa_key == NULL ) |
| 620 | { |
| 621 | SSL_DEBUG_MSG( 1, ( "got no private key" ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 622 | return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /* |
| 626 | * Make an RSA signature of the handshake digests |
| 627 | */ |
| 628 | ssl_calc_verify( ssl, hash ); |
| 629 | |
| 630 | n = ssl->rsa_key->len; |
| 631 | ssl->out_msg[4] = (unsigned char)( n >> 8 ); |
| 632 | ssl->out_msg[5] = (unsigned char)( n ); |
| 633 | |
| 634 | if( ( ret = rsa_pkcs1_sign( ssl->rsa_key, RSA_PRIVATE, RSA_RAW, |
| 635 | 36, hash, ssl->out_msg + 6 ) ) != 0 ) |
| 636 | { |
| 637 | SSL_DEBUG_RET( 1, "rsa_pkcs1_sign", ret ); |
| 638 | return( ret ); |
| 639 | } |
| 640 | |
| 641 | ssl->out_msglen = 6 + n; |
| 642 | ssl->out_msgtype = SSL_MSG_HANDSHAKE; |
| 643 | ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY; |
| 644 | |
| 645 | ssl->state++; |
| 646 | |
| 647 | if( ( ret = ssl_write_record( ssl ) ) != 0 ) |
| 648 | { |
| 649 | SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 650 | return( ret ); |
| 651 | } |
| 652 | |
| 653 | SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) ); |
| 654 | |
| 655 | return( 0 ); |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * SSL handshake -- client side |
| 660 | */ |
| 661 | int ssl_handshake_client( ssl_context *ssl ) |
| 662 | { |
| 663 | int ret = 0; |
| 664 | |
| 665 | SSL_DEBUG_MSG( 2, ( "=> handshake client" ) ); |
| 666 | |
| 667 | while( ssl->state != SSL_HANDSHAKE_OVER ) |
| 668 | { |
| 669 | SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); |
| 670 | |
| 671 | if( ( ret = ssl_flush_output( ssl ) ) != 0 ) |
| 672 | break; |
| 673 | |
| 674 | switch( ssl->state ) |
| 675 | { |
| 676 | case SSL_HELLO_REQUEST: |
| 677 | ssl->state = SSL_CLIENT_HELLO; |
| 678 | break; |
| 679 | |
| 680 | /* |
| 681 | * ==> ClientHello |
| 682 | */ |
| 683 | case SSL_CLIENT_HELLO: |
| 684 | ret = ssl_write_client_hello( ssl ); |
| 685 | break; |
| 686 | |
| 687 | /* |
| 688 | * <== ServerHello |
| 689 | * Certificate |
| 690 | * ( ServerKeyExchange ) |
| 691 | * ( CertificateRequest ) |
| 692 | * ServerHelloDone |
| 693 | */ |
| 694 | case SSL_SERVER_HELLO: |
| 695 | ret = ssl_parse_server_hello( ssl ); |
| 696 | break; |
| 697 | |
| 698 | case SSL_SERVER_CERTIFICATE: |
| 699 | ret = ssl_parse_certificate( ssl ); |
| 700 | break; |
| 701 | |
| 702 | case SSL_SERVER_KEY_EXCHANGE: |
| 703 | ret = ssl_parse_server_key_exchange( ssl ); |
| 704 | break; |
| 705 | |
| 706 | case SSL_CERTIFICATE_REQUEST: |
| 707 | ret = ssl_parse_certificate_request( ssl ); |
| 708 | break; |
| 709 | |
| 710 | case SSL_SERVER_HELLO_DONE: |
| 711 | ret = ssl_parse_server_hello_done( ssl ); |
| 712 | break; |
| 713 | |
| 714 | /* |
| 715 | * ==> ( Certificate/Alert ) |
| 716 | * ClientKeyExchange |
| 717 | * ( CertificateVerify ) |
| 718 | * ChangeCipherSpec |
| 719 | * Finished |
| 720 | */ |
| 721 | case SSL_CLIENT_CERTIFICATE: |
| 722 | ret = ssl_write_certificate( ssl ); |
| 723 | break; |
| 724 | |
| 725 | case SSL_CLIENT_KEY_EXCHANGE: |
| 726 | ret = ssl_write_client_key_exchange( ssl ); |
| 727 | break; |
| 728 | |
| 729 | case SSL_CERTIFICATE_VERIFY: |
| 730 | ret = ssl_write_certificate_verify( ssl ); |
| 731 | break; |
| 732 | |
| 733 | case SSL_CLIENT_CHANGE_CIPHER_SPEC: |
| 734 | ret = ssl_write_change_cipher_spec( ssl ); |
| 735 | break; |
| 736 | |
| 737 | case SSL_CLIENT_FINISHED: |
| 738 | ret = ssl_write_finished( ssl ); |
| 739 | break; |
| 740 | |
| 741 | /* |
| 742 | * <== ChangeCipherSpec |
| 743 | * Finished |
| 744 | */ |
| 745 | case SSL_SERVER_CHANGE_CIPHER_SPEC: |
| 746 | ret = ssl_parse_change_cipher_spec( ssl ); |
| 747 | break; |
| 748 | |
| 749 | case SSL_SERVER_FINISHED: |
| 750 | ret = ssl_parse_finished( ssl ); |
| 751 | break; |
| 752 | |
| 753 | case SSL_FLUSH_BUFFERS: |
| 754 | SSL_DEBUG_MSG( 2, ( "handshake: done" ) ); |
| 755 | ssl->state = SSL_HANDSHAKE_OVER; |
| 756 | break; |
| 757 | |
| 758 | default: |
| 759 | SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 760 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | if( ret != 0 ) |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | SSL_DEBUG_MSG( 2, ( "<= handshake client" ) ); |
| 768 | |
| 769 | return( ret ); |
| 770 | } |
| 771 | |
| 772 | #endif |