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