Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 | |
| 109 | #include <stdio.h> |
| 110 | |
| 111 | #include <openssl/buf.h> |
| 112 | #include <openssl/err.h> |
| 113 | #include <openssl/evp.h> |
| 114 | #include <openssl/obj.h> |
| 115 | #include <openssl/rand.h> |
| 116 | |
| 117 | #include "ssl_locl.h" |
| 118 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | static int ssl23_client_hello(SSL *s); |
| 120 | static int ssl23_get_server_hello(SSL *s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | |
| 122 | IMPLEMENT_ssl23_meth_func(SSLv23_client_method, |
| 123 | ssl_undefined_function, |
David Benjamin | 0f1e64b | 2014-11-22 14:32:53 -0500 | [diff] [blame] | 124 | ssl23_connect) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | |
| 126 | int ssl23_connect(SSL *s) |
| 127 | { |
| 128 | BUF_MEM *buf=NULL; |
| 129 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 130 | int ret= -1; |
| 131 | int new_state,state; |
| 132 | |
| 133 | ERR_clear_error(); |
| 134 | ERR_clear_system_error(); |
| 135 | |
| 136 | if (s->info_callback != NULL) |
| 137 | cb=s->info_callback; |
| 138 | else if (s->ctx->info_callback != NULL) |
| 139 | cb=s->ctx->info_callback; |
| 140 | |
| 141 | s->in_handshake++; |
| 142 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); |
| 143 | |
| 144 | for (;;) |
| 145 | { |
| 146 | state=s->state; |
| 147 | |
| 148 | switch(s->state) |
| 149 | { |
| 150 | case SSL_ST_BEFORE: |
| 151 | case SSL_ST_CONNECT: |
| 152 | case SSL_ST_BEFORE|SSL_ST_CONNECT: |
| 153 | case SSL_ST_OK|SSL_ST_CONNECT: |
| 154 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | s->server=0; |
| 156 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
| 157 | |
| 158 | /* s->version=TLS1_VERSION; */ |
| 159 | s->type=SSL_ST_CONNECT; |
| 160 | |
| 161 | if (s->init_buf == NULL) |
| 162 | { |
| 163 | if ((buf=BUF_MEM_new()) == NULL) |
| 164 | { |
| 165 | ret= -1; |
| 166 | goto end; |
| 167 | } |
| 168 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) |
| 169 | { |
| 170 | ret= -1; |
| 171 | goto end; |
| 172 | } |
| 173 | s->init_buf=buf; |
| 174 | buf=NULL; |
| 175 | } |
| 176 | |
| 177 | if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } |
| 178 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 179 | if (!ssl3_init_finished_mac(s)) |
| 180 | { |
| 181 | OPENSSL_PUT_ERROR(SSL, ssl23_connect, ERR_R_INTERNAL_ERROR); |
| 182 | ret = -1; |
| 183 | goto end; |
| 184 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 185 | |
| 186 | s->state=SSL23_ST_CW_CLNT_HELLO_A; |
| 187 | s->ctx->stats.sess_connect++; |
| 188 | s->init_num=0; |
| 189 | break; |
| 190 | |
| 191 | case SSL23_ST_CW_CLNT_HELLO_A: |
| 192 | case SSL23_ST_CW_CLNT_HELLO_B: |
| 193 | |
| 194 | s->shutdown=0; |
| 195 | ret=ssl23_client_hello(s); |
| 196 | if (ret <= 0) goto end; |
| 197 | s->state=SSL23_ST_CR_SRVR_HELLO_A; |
| 198 | s->init_num=0; |
| 199 | |
| 200 | break; |
| 201 | |
| 202 | case SSL23_ST_CR_SRVR_HELLO_A: |
| 203 | case SSL23_ST_CR_SRVR_HELLO_B: |
| 204 | ret=ssl23_get_server_hello(s); |
| 205 | if (ret >= 0) cb=NULL; |
| 206 | goto end; |
| 207 | /* break; */ |
| 208 | |
| 209 | default: |
| 210 | OPENSSL_PUT_ERROR(SSL, ssl23_connect, SSL_R_UNKNOWN_STATE); |
| 211 | ret= -1; |
| 212 | goto end; |
| 213 | /* break; */ |
| 214 | } |
| 215 | |
| 216 | if (s->debug) { (void)BIO_flush(s->wbio); } |
| 217 | |
| 218 | if ((cb != NULL) && (s->state != state)) |
| 219 | { |
| 220 | new_state=s->state; |
| 221 | s->state=state; |
| 222 | cb(s,SSL_CB_CONNECT_LOOP,1); |
| 223 | s->state=new_state; |
| 224 | } |
| 225 | } |
| 226 | end: |
| 227 | s->in_handshake--; |
| 228 | if (buf != NULL) |
| 229 | BUF_MEM_free(buf); |
| 230 | if (cb != NULL) |
| 231 | cb(s,SSL_CB_CONNECT_EXIT,ret); |
| 232 | return(ret); |
| 233 | } |
| 234 | |
David Benjamin | d1681e6 | 2014-11-20 14:54:14 -0500 | [diff] [blame] | 235 | /* Fill a ClientRandom or ServerRandom field of length len. Returns 0 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | * on failure, 1 on success. */ |
| 237 | int ssl_fill_hello_random(SSL *s, int server, unsigned char *result, int len) |
| 238 | { |
| 239 | int send_time = 0; |
| 240 | if (len < 4) |
| 241 | return 0; |
| 242 | if (server) |
| 243 | send_time = (s->mode & SSL_MODE_SEND_SERVERHELLO_TIME) != 0; |
| 244 | else |
| 245 | send_time = (s->mode & SSL_MODE_SEND_CLIENTHELLO_TIME) != 0; |
| 246 | if (send_time) |
| 247 | { |
| 248 | unsigned long Time = (unsigned long)time(NULL); |
| 249 | unsigned char *p = result; |
| 250 | l2n(Time, p); |
David Benjamin | d1681e6 | 2014-11-20 14:54:14 -0500 | [diff] [blame] | 251 | return RAND_bytes(p, len-4); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | } |
| 253 | else |
David Benjamin | d1681e6 | 2014-11-20 14:54:14 -0500 | [diff] [blame] | 254 | return RAND_bytes(result, len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static int ssl23_client_hello(SSL *s) |
| 258 | { |
| 259 | unsigned char *buf; |
| 260 | unsigned char *p,*d; |
David Benjamin | 0eb1790 | 2014-07-20 12:01:32 -0400 | [diff] [blame] | 261 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | unsigned long l; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | int version = 0, version_major, version_minor; |
| 264 | int ret; |
| 265 | unsigned long mask, options = s->options; |
| 266 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 267 | /* |
| 268 | * SSL_OP_NO_X disables all protocols above X *if* there are |
| 269 | * some protocols below X enabled. This is required in order |
| 270 | * to maintain "version capability" vector contiguous. So |
| 271 | * that if application wants to disable TLS1.0 in favour of |
| 272 | * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the |
| 273 | * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3|SSL_OP_NO_SSLv2. |
| 274 | */ |
David Benjamin | 7f520db | 2014-09-24 13:53:56 -0400 | [diff] [blame] | 275 | mask = SSL_OP_NO_TLSv1_1|SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 276 | version = TLS1_2_VERSION; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 277 | if ((options & SSL_OP_NO_TLSv1_2) && (options & mask) != mask) |
| 278 | version = TLS1_1_VERSION; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 279 | mask &= ~SSL_OP_NO_TLSv1_1; |
| 280 | if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask) |
| 281 | version = TLS1_VERSION; |
| 282 | mask &= ~SSL_OP_NO_TLSv1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 283 | if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask) |
| 284 | version = SSL3_VERSION; |
| 285 | mask &= ~SSL_OP_NO_SSLv3; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 287 | buf=(unsigned char *)s->init_buf->data; |
| 288 | if (s->state == SSL23_ST_CW_CLNT_HELLO_A) |
| 289 | { |
David Benjamin | 0f1e64b | 2014-11-22 14:32:53 -0500 | [diff] [blame] | 290 | /* If the configured session was created at a version |
| 291 | * higher than our maximum version, drop it. */ |
| 292 | if (s->session && |
| 293 | (s->session->ssl_version > version || |
David Benjamin | 30ddb43 | 2014-09-24 17:56:34 -0400 | [diff] [blame] | 294 | s->session->session_id_length == 0 || |
David Benjamin | 0f1e64b | 2014-11-22 14:32:53 -0500 | [diff] [blame] | 295 | s->session->not_resumable)) |
| 296 | { |
| 297 | SSL_set_session(s, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 298 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | |
| 300 | p=s->s3->client_random; |
| 301 | if (ssl_fill_hello_random(s, 0, p, SSL3_RANDOM_SIZE) <= 0) |
| 302 | return -1; |
| 303 | |
| 304 | if (version == TLS1_2_VERSION) |
| 305 | { |
| 306 | version_major = TLS1_2_VERSION_MAJOR; |
| 307 | version_minor = TLS1_2_VERSION_MINOR; |
| 308 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 309 | else if (version == TLS1_1_VERSION) |
| 310 | { |
| 311 | version_major = TLS1_1_VERSION_MAJOR; |
| 312 | version_minor = TLS1_1_VERSION_MINOR; |
| 313 | } |
| 314 | else if (version == TLS1_VERSION) |
| 315 | { |
| 316 | version_major = TLS1_VERSION_MAJOR; |
| 317 | version_minor = TLS1_VERSION_MINOR; |
| 318 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | else if (version == SSL3_VERSION) |
| 320 | { |
| 321 | version_major = SSL3_VERSION_MAJOR; |
| 322 | version_minor = SSL3_VERSION_MINOR; |
| 323 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 324 | else |
| 325 | { |
| 326 | OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, SSL_R_NO_PROTOCOLS_AVAILABLE); |
| 327 | return(-1); |
| 328 | } |
| 329 | |
| 330 | s->client_version = version; |
| 331 | |
David Benjamin | 0eb1790 | 2014-07-20 12:01:32 -0400 | [diff] [blame] | 332 | /* create Client Hello in SSL 3.0/TLS 1.0 format */ |
| 333 | |
| 334 | /* do the record header (5 bytes) and handshake message |
| 335 | * header (4 bytes) last. Note: the final argument to |
| 336 | * ssl_add_clienthello_tlsext below depends on the size |
| 337 | * of this prefix. */ |
| 338 | d = p = &(buf[9]); |
| 339 | |
| 340 | *(p++) = version_major; |
| 341 | *(p++) = version_minor; |
| 342 | |
| 343 | /* Random stuff */ |
| 344 | memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); |
| 345 | p += SSL3_RANDOM_SIZE; |
| 346 | |
David Benjamin | 30ddb43 | 2014-09-24 17:56:34 -0400 | [diff] [blame] | 347 | /* Session ID */ |
| 348 | if (s->new_session || s->session == NULL) |
| 349 | i=0; |
| 350 | else |
| 351 | i=s->session->session_id_length; |
| 352 | *(p++)=i; |
| 353 | if (i != 0) |
| 354 | { |
| 355 | if (i > (int)sizeof(s->session->session_id)) |
| 356 | { |
| 357 | OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, ERR_R_INTERNAL_ERROR); |
| 358 | return -1; |
| 359 | } |
| 360 | memcpy(p,s->session->session_id,i); |
| 361 | p+=i; |
| 362 | } |
David Benjamin | 0eb1790 | 2014-07-20 12:01:32 -0400 | [diff] [blame] | 363 | |
| 364 | /* Ciphers supported (using SSL 3.0/TLS 1.0 format) */ |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 365 | i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]); |
David Benjamin | 0eb1790 | 2014-07-20 12:01:32 -0400 | [diff] [blame] | 366 | if (i == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 367 | { |
David Benjamin | 0eb1790 | 2014-07-20 12:01:32 -0400 | [diff] [blame] | 368 | OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, SSL_R_NO_CIPHERS_AVAILABLE); |
| 369 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 370 | } |
David Benjamin | 0eb1790 | 2014-07-20 12:01:32 -0400 | [diff] [blame] | 371 | s2n(i,p); |
| 372 | p+=i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 373 | |
David Benjamin | 0eb1790 | 2014-07-20 12:01:32 -0400 | [diff] [blame] | 374 | /* COMPRESSION */ |
| 375 | *(p++)=1; |
| 376 | *(p++)=0; /* Add the NULL method */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 377 | |
David Benjamin | 0eb1790 | 2014-07-20 12:01:32 -0400 | [diff] [blame] | 378 | /* TLS extensions*/ |
| 379 | if (ssl_prepare_clienthello_tlsext(s) <= 0) |
| 380 | { |
| 381 | OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, SSL_R_CLIENTHELLO_TLSEXT); |
| 382 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 383 | } |
| 384 | |
David Benjamin | 0eb1790 | 2014-07-20 12:01:32 -0400 | [diff] [blame] | 385 | /* The buffer includes the 5 byte record header, so |
| 386 | * subtract it to compute hlen for |
| 387 | * ssl_add_clienthello_tlsext. */ |
| 388 | if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf-5)) == NULL) |
| 389 | { |
| 390 | OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, ERR_R_INTERNAL_ERROR); |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | l = p-d; |
| 395 | |
| 396 | /* fill in 4-byte handshake header */ |
| 397 | d=&(buf[5]); |
| 398 | *(d++)=SSL3_MT_CLIENT_HELLO; |
| 399 | l2n3(l,d); |
| 400 | |
| 401 | l += 4; |
| 402 | |
| 403 | if (l > SSL3_RT_MAX_PLAIN_LENGTH) |
| 404 | { |
| 405 | OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, ERR_R_INTERNAL_ERROR); |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | /* fill in 5-byte record header */ |
| 410 | d=buf; |
| 411 | *(d++) = SSL3_RT_HANDSHAKE; |
| 412 | *(d++) = version_major; |
| 413 | /* Some servers hang if we use long client hellos |
| 414 | * and a record number > TLS 1.0. |
| 415 | */ |
| 416 | if (TLS1_get_client_version(s) > TLS1_VERSION) |
| 417 | *(d++) = 1; |
| 418 | else |
| 419 | *(d++) = version_minor; |
| 420 | s2n((int)l,d); |
| 421 | |
| 422 | /* number of bytes to write */ |
| 423 | s->init_num=p-buf; |
| 424 | s->init_off=0; |
| 425 | |
| 426 | ssl3_finish_mac(s,&(buf[5]), s->init_num - 5); |
| 427 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 428 | s->state=SSL23_ST_CW_CLNT_HELLO_B; |
| 429 | s->init_off=0; |
| 430 | } |
| 431 | |
| 432 | /* SSL3_ST_CW_CLNT_HELLO_B */ |
| 433 | ret = ssl23_write_bytes(s); |
| 434 | |
| 435 | if ((ret >= 2) && s->msg_callback) |
| 436 | { |
| 437 | /* Client Hello has been sent; tell msg_callback */ |
| 438 | |
David Benjamin | 0eb1790 | 2014-07-20 12:01:32 -0400 | [diff] [blame] | 439 | s->msg_callback(1, version, SSL3_RT_HEADER, s->init_buf->data, 5, s, s->msg_callback_arg); |
| 440 | s->msg_callback(1, version, SSL3_RT_HANDSHAKE, s->init_buf->data+5, ret-5, s, s->msg_callback_arg); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | static int ssl23_get_server_hello(SSL *s) |
| 447 | { |
| 448 | char buf[8]; |
| 449 | unsigned char *p; |
| 450 | int i; |
| 451 | int n; |
| 452 | |
| 453 | n=ssl23_read_bytes(s,7); |
| 454 | |
| 455 | if (n != 7) return(n); |
| 456 | p=s->packet; |
| 457 | |
| 458 | memcpy(buf,p,n); |
| 459 | |
| 460 | if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) && |
| 461 | (p[5] == 0x00) && (p[6] == 0x02)) |
| 462 | { |
| 463 | OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL); |
| 464 | goto err; |
| 465 | } |
| 466 | else if (p[1] == SSL3_VERSION_MAJOR && |
| 467 | p[2] <= TLS1_2_VERSION_MINOR && |
| 468 | ((p[0] == SSL3_RT_HANDSHAKE && p[5] == SSL3_MT_SERVER_HELLO) || |
| 469 | (p[0] == SSL3_RT_ALERT && p[3] == 0 && p[4] == 2))) |
| 470 | { |
| 471 | /* we have sslv3 or tls1 (server hello or alert) */ |
| 472 | |
| 473 | if ((p[2] == SSL3_VERSION_MINOR) && |
| 474 | !(s->options & SSL_OP_NO_SSLv3)) |
| 475 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 476 | s->version=SSL3_VERSION; |
| 477 | s->method=SSLv3_client_method(); |
| 478 | } |
| 479 | else if ((p[2] == TLS1_VERSION_MINOR) && |
| 480 | !(s->options & SSL_OP_NO_TLSv1)) |
| 481 | { |
| 482 | s->version=TLS1_VERSION; |
| 483 | s->method=TLSv1_client_method(); |
| 484 | } |
| 485 | else if ((p[2] == TLS1_1_VERSION_MINOR) && |
| 486 | !(s->options & SSL_OP_NO_TLSv1_1)) |
| 487 | { |
| 488 | s->version=TLS1_1_VERSION; |
| 489 | s->method=TLSv1_1_client_method(); |
| 490 | } |
| 491 | else if ((p[2] == TLS1_2_VERSION_MINOR) && |
| 492 | !(s->options & SSL_OP_NO_TLSv1_2)) |
| 493 | { |
| 494 | s->version=TLS1_2_VERSION; |
| 495 | s->method=TLSv1_2_client_method(); |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL); |
| 500 | goto err; |
| 501 | } |
| 502 | |
| 503 | if (p[0] == SSL3_RT_ALERT && p[5] != SSL3_AL_WARNING) |
| 504 | { |
| 505 | /* fatal alert */ |
| 506 | |
| 507 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 508 | int j; |
| 509 | |
| 510 | if (s->info_callback != NULL) |
| 511 | cb=s->info_callback; |
| 512 | else if (s->ctx->info_callback != NULL) |
| 513 | cb=s->ctx->info_callback; |
| 514 | |
| 515 | i=p[5]; |
| 516 | if (cb != NULL) |
| 517 | { |
| 518 | j=(i<<8)|p[6]; |
| 519 | cb(s,SSL_CB_READ_ALERT,j); |
| 520 | } |
| 521 | |
| 522 | if (s->msg_callback) |
| 523 | { |
| 524 | s->msg_callback(0, s->version, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg); |
| 525 | s->msg_callback(0, s->version, SSL3_RT_ALERT, p+5, 2, s, s->msg_callback_arg); |
| 526 | } |
| 527 | |
| 528 | s->rwstate=SSL_NOTHING; |
| 529 | OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_AD_REASON_OFFSET + p[6]); |
| 530 | goto err; |
| 531 | } |
| 532 | |
| 533 | if (!ssl_init_wbio_buffer(s,1)) goto err; |
| 534 | |
| 535 | /* we are in this state */ |
| 536 | s->state=SSL3_ST_CR_SRVR_HELLO_A; |
| 537 | |
| 538 | /* put the 7 bytes we have read into the input buffer |
| 539 | * for SSLv3 */ |
| 540 | s->rstate=SSL_ST_READ_HEADER; |
| 541 | s->packet_length=n; |
| 542 | if (s->s3->rbuf.buf == NULL) |
| 543 | if (!ssl3_setup_read_buffer(s)) |
| 544 | goto err; |
| 545 | s->packet= &(s->s3->rbuf.buf[0]); |
| 546 | memcpy(s->packet,buf,n); |
| 547 | s->s3->rbuf.left=n; |
| 548 | s->s3->rbuf.offset=0; |
| 549 | |
| 550 | s->handshake_func=s->method->ssl_connect; |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_R_UNKNOWN_PROTOCOL); |
| 555 | goto err; |
| 556 | } |
| 557 | s->init_num=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | return(SSL_connect(s)); |
| 559 | err: |
| 560 | return(-1); |
| 561 | } |