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