diff options
Diffstat (limited to 'src/lib/libssl/s23_srvr.c')
-rw-r--r-- | src/lib/libssl/s23_srvr.c | 635 |
1 files changed, 0 insertions, 635 deletions
diff --git a/src/lib/libssl/s23_srvr.c b/src/lib/libssl/s23_srvr.c deleted file mode 100644 index b524124681..0000000000 --- a/src/lib/libssl/s23_srvr.c +++ /dev/null | |||
@@ -1,635 +0,0 @@ | |||
1 | /* $OpenBSD: s23_srvr.c,v 1.41 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_server_method(int ver); | ||
121 | int ssl23_get_client_hello(SSL *s); | ||
122 | static const SSL_METHOD *tls_any_get_server_method(int ver); | ||
123 | |||
124 | const SSL_METHOD SSLv23_server_method_data = { | ||
125 | .version = TLS1_2_VERSION, | ||
126 | .ssl_new = tls1_new, | ||
127 | .ssl_clear = tls1_clear, | ||
128 | .ssl_free = tls1_free, | ||
129 | .ssl_accept = ssl23_accept, | ||
130 | .ssl_connect = ssl_undefined_function, | ||
131 | .ssl_read = ssl23_read, | ||
132 | .ssl_peek = ssl23_peek, | ||
133 | .ssl_write = ssl23_write, | ||
134 | .ssl_shutdown = ssl_undefined_function, | ||
135 | .ssl_renegotiate = ssl_undefined_function, | ||
136 | .ssl_renegotiate_check = ssl_ok, | ||
137 | .ssl_get_message = ssl3_get_message, | ||
138 | .ssl_read_bytes = ssl3_read_bytes, | ||
139 | .ssl_write_bytes = ssl3_write_bytes, | ||
140 | .ssl_dispatch_alert = ssl3_dispatch_alert, | ||
141 | .ssl_ctrl = ssl3_ctrl, | ||
142 | .ssl_ctx_ctrl = ssl3_ctx_ctrl, | ||
143 | .get_cipher_by_char = ssl3_get_cipher_by_char, | ||
144 | .put_cipher_by_char = ssl3_put_cipher_by_char, | ||
145 | .ssl_pending = ssl_undefined_const_function, | ||
146 | .num_ciphers = ssl3_num_ciphers, | ||
147 | .get_cipher = ssl3_get_cipher, | ||
148 | .get_ssl_method = ssl23_get_server_method, | ||
149 | .get_timeout = ssl23_default_timeout, | ||
150 | .ssl3_enc = &ssl3_undef_enc_method, | ||
151 | .ssl_version = ssl_undefined_void_function, | ||
152 | .ssl_callback_ctrl = ssl3_callback_ctrl, | ||
153 | .ssl_ctx_callback_ctrl = ssl3_ctx_callback_ctrl, | ||
154 | }; | ||
155 | |||
156 | const SSL_METHOD TLS_server_method_data = { | ||
157 | .version = TLS1_2_VERSION, | ||
158 | .ssl_new = tls1_new, | ||
159 | .ssl_clear = tls1_clear, | ||
160 | .ssl_free = tls1_free, | ||
161 | .ssl_accept = tls_any_accept, | ||
162 | .ssl_connect = ssl_undefined_function, | ||
163 | .ssl_read = ssl23_read, | ||
164 | .ssl_peek = ssl23_peek, | ||
165 | .ssl_write = ssl23_write, | ||
166 | .ssl_shutdown = ssl_undefined_function, | ||
167 | .ssl_renegotiate = ssl_undefined_function, | ||
168 | .ssl_renegotiate_check = ssl_ok, | ||
169 | .ssl_get_message = ssl3_get_message, | ||
170 | .ssl_read_bytes = ssl3_read_bytes, | ||
171 | .ssl_write_bytes = ssl3_write_bytes, | ||
172 | .ssl_dispatch_alert = ssl3_dispatch_alert, | ||
173 | .ssl_ctrl = ssl3_ctrl, | ||
174 | .ssl_ctx_ctrl = ssl3_ctx_ctrl, | ||
175 | .get_cipher_by_char = ssl3_get_cipher_by_char, | ||
176 | .put_cipher_by_char = ssl3_put_cipher_by_char, | ||
177 | .ssl_pending = ssl_undefined_const_function, | ||
178 | .num_ciphers = ssl3_num_ciphers, | ||
179 | .get_cipher = ssl3_get_cipher, | ||
180 | .get_ssl_method = tls_any_get_server_method, | ||
181 | .get_timeout = ssl23_default_timeout, | ||
182 | .ssl3_enc = &ssl3_undef_enc_method, | ||
183 | .ssl_version = ssl_undefined_void_function, | ||
184 | .ssl_callback_ctrl = ssl3_callback_ctrl, | ||
185 | .ssl_ctx_callback_ctrl = ssl3_ctx_callback_ctrl, | ||
186 | }; | ||
187 | |||
188 | const SSL_METHOD * | ||
189 | SSLv23_server_method(void) | ||
190 | { | ||
191 | return &SSLv23_server_method_data; | ||
192 | } | ||
193 | |||
194 | static const SSL_METHOD * | ||
195 | ssl23_get_server_method(int ver) | ||
196 | { | ||
197 | if (ver == SSL3_VERSION) | ||
198 | return (SSLv3_server_method()); | ||
199 | if (ver == TLS1_VERSION) | ||
200 | return (TLSv1_server_method()); | ||
201 | if (ver == TLS1_1_VERSION) | ||
202 | return (TLSv1_1_server_method()); | ||
203 | if (ver == TLS1_2_VERSION) | ||
204 | return (TLSv1_2_server_method()); | ||
205 | return (NULL); | ||
206 | } | ||
207 | |||
208 | int | ||
209 | ssl23_accept(SSL *s) | ||
210 | { | ||
211 | void (*cb)(const SSL *ssl, int type, int val) = NULL; | ||
212 | int ret = -1; | ||
213 | int new_state, state; | ||
214 | |||
215 | ERR_clear_error(); | ||
216 | errno = 0; | ||
217 | |||
218 | if (s->info_callback != NULL) | ||
219 | cb = s->info_callback; | ||
220 | else if (s->ctx->info_callback != NULL) | ||
221 | cb = s->ctx->info_callback; | ||
222 | |||
223 | s->in_handshake++; | ||
224 | if (!SSL_in_init(s) || SSL_in_before(s)) | ||
225 | SSL_clear(s); | ||
226 | |||
227 | for (;;) { | ||
228 | state = s->state; | ||
229 | |||
230 | switch (s->state) { | ||
231 | case SSL_ST_BEFORE: | ||
232 | case SSL_ST_ACCEPT: | ||
233 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: | ||
234 | case SSL_ST_OK|SSL_ST_ACCEPT: | ||
235 | |||
236 | s->server = 1; | ||
237 | if (cb != NULL) | ||
238 | cb(s, SSL_CB_HANDSHAKE_START, 1); | ||
239 | |||
240 | /* s->version=SSL3_VERSION; */ | ||
241 | s->type = SSL_ST_ACCEPT; | ||
242 | |||
243 | if (!ssl3_setup_init_buffer(s)) { | ||
244 | ret = -1; | ||
245 | goto end; | ||
246 | } | ||
247 | if (!ssl3_init_finished_mac(s)) { | ||
248 | ret = -1; | ||
249 | goto end; | ||
250 | } | ||
251 | |||
252 | s->state = SSL23_ST_SR_CLNT_HELLO_A; | ||
253 | s->ctx->stats.sess_accept++; | ||
254 | s->init_num = 0; | ||
255 | break; | ||
256 | |||
257 | case SSL23_ST_SR_CLNT_HELLO_A: | ||
258 | case SSL23_ST_SR_CLNT_HELLO_B: | ||
259 | |||
260 | s->shutdown = 0; | ||
261 | ret = ssl23_get_client_hello(s); | ||
262 | if (ret >= 0) | ||
263 | cb = NULL; | ||
264 | goto end; | ||
265 | /* break; */ | ||
266 | |||
267 | default: | ||
268 | SSLerr(SSL_F_SSL23_ACCEPT, SSL_R_UNKNOWN_STATE); | ||
269 | ret = -1; | ||
270 | goto end; | ||
271 | /* break; */ | ||
272 | } | ||
273 | |||
274 | if ((cb != NULL) && (s->state != state)) { | ||
275 | new_state = s->state; | ||
276 | s->state = state; | ||
277 | cb(s, SSL_CB_ACCEPT_LOOP, 1); | ||
278 | s->state = new_state; | ||
279 | } | ||
280 | } | ||
281 | |||
282 | end: | ||
283 | s->in_handshake--; | ||
284 | if (cb != NULL) | ||
285 | cb(s, SSL_CB_ACCEPT_EXIT, ret); | ||
286 | |||
287 | return (ret); | ||
288 | } | ||
289 | |||
290 | |||
291 | int | ||
292 | ssl23_get_client_hello(SSL *s) | ||
293 | { | ||
294 | char buf[11]; | ||
295 | /* | ||
296 | * sizeof(buf) == 11, because we'll need to request this many bytes in | ||
297 | * the initial read. | ||
298 | * We can detect SSL 3.0/TLS 1.0 Client Hellos ('type == 3') correctly | ||
299 | * only when the following is in a single record, which is not | ||
300 | * guaranteed by the protocol specification: | ||
301 | * Byte Content | ||
302 | * 0 type \ | ||
303 | * 1/2 version > record header | ||
304 | * 3/4 length / | ||
305 | * 5 msg_type \ | ||
306 | * 6-8 length > Client Hello message | ||
307 | * 9/10 client_version / | ||
308 | */ | ||
309 | unsigned char *p, *d, *d_len, *dd; | ||
310 | unsigned int i; | ||
311 | unsigned int csl, sil, cl; | ||
312 | int n = 0, j; | ||
313 | int type = 0; | ||
314 | int v[2]; | ||
315 | |||
316 | if (s->state == SSL23_ST_SR_CLNT_HELLO_A) { | ||
317 | /* read the initial header */ | ||
318 | v[0] = v[1] = 0; | ||
319 | |||
320 | if (!ssl3_setup_buffers(s)) | ||
321 | return -1; | ||
322 | |||
323 | n = ssl23_read_bytes(s, sizeof buf); | ||
324 | if (n != sizeof buf) | ||
325 | return(n); | ||
326 | |||
327 | p = s->packet; | ||
328 | |||
329 | memcpy(buf, p, n); | ||
330 | |||
331 | if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) { | ||
332 | /* | ||
333 | * SSLv2 header | ||
334 | */ | ||
335 | if ((p[3] == 0x00) && (p[4] == 0x02)) { | ||
336 | v[0] = p[3]; | ||
337 | v[1] = p[4]; | ||
338 | /* SSLv2 */ | ||
339 | if (!(s->options & SSL_OP_NO_SSLv2)) | ||
340 | type = 1; | ||
341 | } else if (p[3] == SSL3_VERSION_MAJOR) { | ||
342 | v[0] = p[3]; | ||
343 | v[1] = p[4]; | ||
344 | /* SSLv3/TLSv1 */ | ||
345 | if (p[4] >= TLS1_VERSION_MINOR) { | ||
346 | if (p[4] >= TLS1_2_VERSION_MINOR && | ||
347 | !(s->options & SSL_OP_NO_TLSv1_2)) { | ||
348 | s->version = TLS1_2_VERSION; | ||
349 | s->state = SSL23_ST_SR_CLNT_HELLO_B; | ||
350 | } else if (p[4] >= TLS1_1_VERSION_MINOR && | ||
351 | !(s->options & SSL_OP_NO_TLSv1_1)) { | ||
352 | s->version = TLS1_1_VERSION; | ||
353 | /* type=2; */ /* done later to survive restarts */ | ||
354 | s->state = SSL23_ST_SR_CLNT_HELLO_B; | ||
355 | } else if (!(s->options & SSL_OP_NO_TLSv1)) { | ||
356 | s->version = TLS1_VERSION; | ||
357 | /* type=2; */ /* done later to survive restarts */ | ||
358 | s->state = SSL23_ST_SR_CLNT_HELLO_B; | ||
359 | } else if (!(s->options & SSL_OP_NO_SSLv3)) { | ||
360 | s->version = SSL3_VERSION; | ||
361 | /* type=2; */ | ||
362 | s->state = SSL23_ST_SR_CLNT_HELLO_B; | ||
363 | } else if (!(s->options & SSL_OP_NO_SSLv2)) { | ||
364 | type = 1; | ||
365 | } | ||
366 | } else if (!(s->options & SSL_OP_NO_SSLv3)) { | ||
367 | s->version = SSL3_VERSION; | ||
368 | /* type=2; */ | ||
369 | s->state = SSL23_ST_SR_CLNT_HELLO_B; | ||
370 | } else if (!(s->options & SSL_OP_NO_SSLv2)) | ||
371 | type = 1; | ||
372 | |||
373 | } | ||
374 | } else if ((p[0] == SSL3_RT_HANDSHAKE) && | ||
375 | (p[1] == SSL3_VERSION_MAJOR) && | ||
376 | (p[5] == SSL3_MT_CLIENT_HELLO) && | ||
377 | ((p[3] == 0 && p[4] < 5 /* silly record length? */) || | ||
378 | (p[9] >= p[1]))) { | ||
379 | /* | ||
380 | * SSLv3 or tls1 header | ||
381 | */ | ||
382 | |||
383 | v[0] = p[1]; /* major version (= SSL3_VERSION_MAJOR) */ | ||
384 | /* We must look at client_version inside the Client Hello message | ||
385 | * to get the correct minor version. | ||
386 | * However if we have only a pathologically small fragment of the | ||
387 | * Client Hello message, this would be difficult, and we'd have | ||
388 | * to read more records to find out. | ||
389 | * No known SSL 3.0 client fragments ClientHello like this, | ||
390 | * so we simply reject such connections to avoid | ||
391 | * protocol version downgrade attacks. */ | ||
392 | if (p[3] == 0 && p[4] < 6) { | ||
393 | SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, | ||
394 | SSL_R_RECORD_TOO_SMALL); | ||
395 | return -1; | ||
396 | } | ||
397 | /* if major version number > 3 set minor to a value | ||
398 | * which will use the highest version 3 we support. | ||
399 | * If TLS 2.0 ever appears we will need to revise | ||
400 | * this.... | ||
401 | */ | ||
402 | if (p[9] > SSL3_VERSION_MAJOR) | ||
403 | v[1] = 0xff; | ||
404 | else | ||
405 | v[1] = p[10]; /* minor version according to client_version */ | ||
406 | if (v[1] >= TLS1_VERSION_MINOR) { | ||
407 | if (v[1] >= TLS1_2_VERSION_MINOR && | ||
408 | !(s->options & SSL_OP_NO_TLSv1_2)) { | ||
409 | s->version = TLS1_2_VERSION; | ||
410 | type = 3; | ||
411 | } else if (v[1] >= TLS1_1_VERSION_MINOR && | ||
412 | !(s->options & SSL_OP_NO_TLSv1_1)) { | ||
413 | s->version = TLS1_1_VERSION; | ||
414 | type = 3; | ||
415 | } else if (!(s->options & SSL_OP_NO_TLSv1)) { | ||
416 | s->version = TLS1_VERSION; | ||
417 | type = 3; | ||
418 | } else if (!(s->options & SSL_OP_NO_SSLv3)) { | ||
419 | s->version = SSL3_VERSION; | ||
420 | type = 3; | ||
421 | } | ||
422 | } else { | ||
423 | /* client requests SSL 3.0 */ | ||
424 | if (!(s->options & SSL_OP_NO_SSLv3)) { | ||
425 | s->version = SSL3_VERSION; | ||
426 | type = 3; | ||
427 | } else if (!(s->options & SSL_OP_NO_TLSv1)) { | ||
428 | /* we won't be able to use TLS of course, | ||
429 | * but this will send an appropriate alert */ | ||
430 | s->version = TLS1_VERSION; | ||
431 | type = 3; | ||
432 | } | ||
433 | } | ||
434 | } | ||
435 | else if ((strncmp("GET ", (char *)p, 4) == 0) || | ||
436 | (strncmp("POST ",(char *)p, 5) == 0) || | ||
437 | (strncmp("HEAD ",(char *)p, 5) == 0) || | ||
438 | (strncmp("PUT ", (char *)p, 4) == 0)) { | ||
439 | SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_HTTP_REQUEST); | ||
440 | return -1; | ||
441 | } else if (strncmp("CONNECT", (char *)p, 7) == 0) { | ||
442 | SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_HTTPS_PROXY_REQUEST); | ||
443 | return -1; | ||
444 | } | ||
445 | } | ||
446 | |||
447 | if (s->state == SSL23_ST_SR_CLNT_HELLO_B) { | ||
448 | /* we have SSLv3/TLSv1 in an SSLv2 header | ||
449 | * (other cases skip this state) */ | ||
450 | |||
451 | type = 2; | ||
452 | p = s->packet; | ||
453 | v[0] = p[3]; /* == SSL3_VERSION_MAJOR */ | ||
454 | v[1] = p[4]; | ||
455 | |||
456 | /* An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2 | ||
457 | * header is sent directly on the wire, not wrapped as a TLS | ||
458 | * record. It's format is: | ||
459 | * Byte Content | ||
460 | * 0-1 msg_length | ||
461 | * 2 msg_type | ||
462 | * 3-4 version | ||
463 | * 5-6 cipher_spec_length | ||
464 | * 7-8 session_id_length | ||
465 | * 9-10 challenge_length | ||
466 | * ... ... | ||
467 | */ | ||
468 | n = ((p[0] & 0x7f) << 8) | p[1]; | ||
469 | if (n > (1024 * 4)) { | ||
470 | SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_RECORD_TOO_LARGE); | ||
471 | return -1; | ||
472 | } | ||
473 | if (n < 9) { | ||
474 | SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, | ||
475 | SSL_R_RECORD_LENGTH_MISMATCH); | ||
476 | return -1; | ||
477 | } | ||
478 | |||
479 | j = ssl23_read_bytes(s, n + 2); | ||
480 | if (j != n + 2) | ||
481 | return -1; | ||
482 | |||
483 | ssl3_finish_mac(s, s->packet + 2, s->packet_length - 2); | ||
484 | if (s->msg_callback) | ||
485 | s->msg_callback(0, SSL2_VERSION, 0, s->packet + 2, | ||
486 | s->packet_length - 2, s, s->msg_callback_arg); | ||
487 | |||
488 | p = s->packet; | ||
489 | p += 5; | ||
490 | n2s(p, csl); | ||
491 | n2s(p, sil); | ||
492 | n2s(p, cl); | ||
493 | d = (unsigned char *)s->init_buf->data; | ||
494 | if ((csl + sil + cl + 11) != s->packet_length) { | ||
495 | /* | ||
496 | * We can't have TLS extensions in SSL 2.0 format | ||
497 | * Client Hello, can we ? Error condition should be | ||
498 | * '>' otherwise | ||
499 | */ | ||
500 | SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, | ||
501 | SSL_R_RECORD_LENGTH_MISMATCH); | ||
502 | return -1; | ||
503 | } | ||
504 | |||
505 | /* record header: msg_type ... */ | ||
506 | *(d++) = SSL3_MT_CLIENT_HELLO; | ||
507 | /* ... and length (actual value will be written later) */ | ||
508 | d_len = d; | ||
509 | d += 3; | ||
510 | |||
511 | /* client_version */ | ||
512 | *(d++) = SSL3_VERSION_MAJOR; /* == v[0] */ | ||
513 | *(d++) = v[1]; | ||
514 | |||
515 | /* lets populate the random area */ | ||
516 | /* get the challenge_length */ | ||
517 | i = (cl > SSL3_RANDOM_SIZE) ? SSL3_RANDOM_SIZE : cl; | ||
518 | memset(d, 0, SSL3_RANDOM_SIZE); | ||
519 | memcpy(&(d[SSL3_RANDOM_SIZE - i]), &(p[csl + sil]), i); | ||
520 | d += SSL3_RANDOM_SIZE; | ||
521 | |||
522 | /* no session-id reuse */ | ||
523 | *(d++) = 0; | ||
524 | |||
525 | /* ciphers */ | ||
526 | j = 0; | ||
527 | dd = d; | ||
528 | d += 2; | ||
529 | for (i = 0; i < csl; i += 3) { | ||
530 | if (p[i] != 0) | ||
531 | continue; | ||
532 | *(d++) = p[i + 1]; | ||
533 | *(d++) = p[i + 2]; | ||
534 | j += 2; | ||
535 | } | ||
536 | s2n(j, dd); | ||
537 | |||
538 | /* add in (no) COMPRESSION */ | ||
539 | *(d++) = 1; | ||
540 | *(d++) = 0; | ||
541 | |||
542 | i = (d - (unsigned char *)s->init_buf->data) - 4; | ||
543 | l2n3((long)i, d_len); | ||
544 | |||
545 | /* get the data reused from the init_buf */ | ||
546 | s->s3->tmp.reuse_message = 1; | ||
547 | s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO; | ||
548 | s->s3->tmp.message_size = i; | ||
549 | } | ||
550 | |||
551 | /* imaginary new state (for program structure): */ | ||
552 | /* s->state = SSL23_SR_CLNT_HELLO_C */ | ||
553 | |||
554 | if (type == 1) { | ||
555 | SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_UNSUPPORTED_PROTOCOL); | ||
556 | return -1; | ||
557 | } | ||
558 | |||
559 | if ((type == 2) || (type == 3)) { | ||
560 | /* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */ | ||
561 | |||
562 | if (!ssl_init_wbio_buffer(s, 1)) | ||
563 | return -1; | ||
564 | |||
565 | /* we are in this state */ | ||
566 | s->state = SSL3_ST_SR_CLNT_HELLO_A; | ||
567 | |||
568 | if (type == 3) { | ||
569 | /* put the 'n' bytes we have read into the input buffer | ||
570 | * for SSLv3 */ | ||
571 | s->rstate = SSL_ST_READ_HEADER; | ||
572 | s->packet_length = n; | ||
573 | if (s->s3->rbuf.buf == NULL) | ||
574 | if (!ssl3_setup_read_buffer(s)) | ||
575 | return -1; | ||
576 | |||
577 | s->packet = &(s->s3->rbuf.buf[0]); | ||
578 | memcpy(s->packet, buf, n); | ||
579 | s->s3->rbuf.left = n; | ||
580 | s->s3->rbuf.offset = 0; | ||
581 | } else { | ||
582 | s->packet_length = 0; | ||
583 | s->s3->rbuf.left = 0; | ||
584 | s->s3->rbuf.offset = 0; | ||
585 | } | ||
586 | if (s->version == TLS1_2_VERSION) | ||
587 | s->method = TLSv1_2_server_method(); | ||
588 | else if (s->version == TLS1_1_VERSION) | ||
589 | s->method = TLSv1_1_server_method(); | ||
590 | else if (s->version == TLS1_VERSION) | ||
591 | s->method = TLSv1_server_method(); | ||
592 | else | ||
593 | s->method = SSLv3_server_method(); | ||
594 | s->handshake_func = s->method->ssl_accept; | ||
595 | } | ||
596 | |||
597 | if ((type < 1) || (type > 3)) { | ||
598 | /* bad, very bad */ | ||
599 | SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL); | ||
600 | return -1; | ||
601 | } | ||
602 | s->init_num = 0; | ||
603 | |||
604 | return (SSL_accept(s)); | ||
605 | } | ||
606 | |||
607 | const SSL_METHOD * | ||
608 | TLS_server_method(void) | ||
609 | { | ||
610 | return &TLS_server_method_data; | ||
611 | } | ||
612 | |||
613 | static const SSL_METHOD * | ||
614 | tls_any_get_server_method(int ver) | ||
615 | { | ||
616 | if (ver == SSL3_VERSION) | ||
617 | return (NULL); | ||
618 | else | ||
619 | return ssl23_get_server_method(ver); | ||
620 | } | ||
621 | |||
622 | int | ||
623 | tls_any_accept(SSL *s) | ||
624 | { | ||
625 | int ret; | ||
626 | unsigned long old_options; | ||
627 | |||
628 | old_options = s->options; | ||
629 | |||
630 | s->options |= SSL_OP_NO_SSLv3; | ||
631 | ret = ssl23_accept(s); | ||
632 | s->options = old_options; | ||
633 | |||
634 | return ret; | ||
635 | } | ||