summaryrefslogtreecommitdiff
path: root/src/lib/libssl/s23_srvr.c
diff options
context:
space:
mode:
authorjsing <>2017-01-26 05:31:25 +0000
committerjsing <>2017-01-26 05:31:25 +0000
commit9366f4ef0d67a19fe5eca3feedbc756a4a8966b2 (patch)
tree4e54623d2ff33c8bd1295cf166bf8253188d9d34 /src/lib/libssl/s23_srvr.c
parentc7118cf7a1b4ff8cec6c52fba26ecfbfba0d7919 (diff)
downloadopenbsd-9366f4ef0d67a19fe5eca3feedbc756a4a8966b2.tar.gz
openbsd-9366f4ef0d67a19fe5eca3feedbc756a4a8966b2.tar.bz2
openbsd-9366f4ef0d67a19fe5eca3feedbc756a4a8966b2.zip
Merge the client/server version negotiation into the existing (currently
fixed version) client/server code. ok beck@
Diffstat (limited to 'src/lib/libssl/s23_srvr.c')
-rw-r--r--src/lib/libssl/s23_srvr.c466
1 files changed, 0 insertions, 466 deletions
diff --git a/src/lib/libssl/s23_srvr.c b/src/lib/libssl/s23_srvr.c
deleted file mode 100644
index 1cd6721d1f..0000000000
--- a/src/lib/libssl/s23_srvr.c
+++ /dev/null
@@ -1,466 +0,0 @@
1/* $OpenBSD: s23_srvr.c,v 1.57 2017/01/23 14:35:42 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
120static int ssl23_get_client_hello(SSL *s);
121
122int
123ssl23_accept(SSL *s)
124{
125 void (*cb)(const SSL *ssl, int type, int val) = NULL;
126 int ret = -1;
127 int new_state, state;
128
129 ERR_clear_error();
130 errno = 0;
131
132 if (s->internal->info_callback != NULL)
133 cb = s->internal->info_callback;
134 else if (s->ctx->internal->info_callback != NULL)
135 cb = s->ctx->internal->info_callback;
136
137 s->internal->in_handshake++;
138 if (!SSL_in_init(s) || SSL_in_before(s))
139 SSL_clear(s);
140
141 for (;;) {
142 state = s->internal->state;
143
144 switch (s->internal->state) {
145 case SSL_ST_BEFORE:
146 case SSL_ST_ACCEPT:
147 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
148 case SSL_ST_OK|SSL_ST_ACCEPT:
149
150 s->server = 1;
151 if (cb != NULL)
152 cb(s, SSL_CB_HANDSHAKE_START, 1);
153
154 /* s->version=SSL3_VERSION; */
155 s->internal->type = SSL_ST_ACCEPT;
156
157 if (!ssl3_setup_init_buffer(s)) {
158 ret = -1;
159 goto end;
160 }
161 if (!tls1_init_finished_mac(s)) {
162 ret = -1;
163 goto end;
164 }
165
166 s->internal->state = SSL23_ST_SR_CLNT_HELLO_A;
167 s->ctx->internal->stats.sess_accept++;
168 s->internal->init_num = 0;
169 break;
170
171 case SSL23_ST_SR_CLNT_HELLO_A:
172 case SSL23_ST_SR_CLNT_HELLO_B:
173
174 s->internal->shutdown = 0;
175 ret = ssl23_get_client_hello(s);
176 if (ret >= 0)
177 cb = NULL;
178 goto end;
179 /* break; */
180
181 default:
182 SSLerr(SSL_F_SSL23_ACCEPT, SSL_R_UNKNOWN_STATE);
183 ret = -1;
184 goto end;
185 /* break; */
186 }
187
188 if ((cb != NULL) && (s->internal->state != state)) {
189 new_state = s->internal->state;
190 s->internal->state = state;
191 cb(s, SSL_CB_ACCEPT_LOOP, 1);
192 s->internal->state = new_state;
193 }
194 }
195
196end:
197 s->internal->in_handshake--;
198 if (cb != NULL)
199 cb(s, SSL_CB_ACCEPT_EXIT, ret);
200
201 return (ret);
202}
203
204
205int
206ssl23_get_client_hello(SSL *s)
207{
208 char buf[11];
209 /*
210 * sizeof(buf) == 11, because we'll need to request this many bytes in
211 * the initial read.
212 * We can detect SSL 3.0/TLS 1.0 Client Hellos ('type == 3') correctly
213 * only when the following is in a single record, which is not
214 * guaranteed by the protocol specification:
215 * Byte Content
216 * 0 type \
217 * 1/2 version > record header
218 * 3/4 length /
219 * 5 msg_type \
220 * 6-8 length > Client Hello message
221 * 9/10 client_version /
222 */
223 uint16_t client_version = 0;
224 uint16_t shared_version;
225 unsigned char *p, *d, *d_len, *dd;
226 unsigned int i;
227 unsigned int csl, sil, cl;
228 int n = 0, j;
229 int type = 0;
230
231 if (s->internal->state == SSL23_ST_SR_CLNT_HELLO_A) {
232 /* read the initial header */
233 if (!ssl3_setup_buffers(s))
234 return -1;
235
236 n = ssl23_read_bytes(s, sizeof buf);
237 if (n != sizeof buf)
238 return(n);
239
240 p = s->internal->packet;
241
242 memcpy(buf, p, n);
243
244 if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) {
245 /*
246 * SSLv2 header
247 */
248 client_version = p[3] << 8 | p[4];
249
250 if (!ssl_max_shared_version(s, client_version,
251 &shared_version))
252 goto unsupported;
253
254 s->version = shared_version;
255 s->internal->state = SSL23_ST_SR_CLNT_HELLO_B;
256 } else if ((p[0] == SSL3_RT_HANDSHAKE) &&
257 (p[1] == SSL3_VERSION_MAJOR) &&
258 (p[5] == SSL3_MT_CLIENT_HELLO) &&
259 ((p[3] == 0 && p[4] < 5 /* silly record length? */) ||
260 (p[9] >= p[1]))) {
261 /*
262 * SSLv3 or tls1 header
263 */
264
265 /* We must look at client_version inside the Client Hello message
266 * to get the correct minor version.
267 * However if we have only a pathologically small fragment of the
268 * Client Hello message, this would be difficult, and we'd have
269 * to read more records to find out.
270 * No known SSL 3.0 client fragments ClientHello like this,
271 * so we simply reject such connections to avoid
272 * protocol version downgrade attacks. */
273 if (p[3] == 0 && p[4] < 6) {
274 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,
275 SSL_R_RECORD_TOO_SMALL);
276 return -1;
277 }
278 client_version = p[9] << 8 | p[10];
279
280 if (!ssl_max_shared_version(s, client_version,
281 &shared_version)) {
282 if (s->internal->options & SSL_OP_NO_TLSv1)
283 goto unsupported;
284 /*
285 * We won't be able to use TLS of course,
286 * but this will send an appropriate alert.
287 */
288 shared_version = TLS1_VERSION;
289 }
290 s->version = shared_version;
291 type = 3;
292 } else if ((strncmp("GET ", (char *)p, 4) == 0) ||
293 (strncmp("POST ",(char *)p, 5) == 0) ||
294 (strncmp("HEAD ",(char *)p, 5) == 0) ||
295 (strncmp("PUT ", (char *)p, 4) == 0)) {
296 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_HTTP_REQUEST);
297 return -1;
298 } else if (strncmp("CONNECT", (char *)p, 7) == 0) {
299 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_HTTPS_PROXY_REQUEST);
300 return -1;
301 }
302 }
303
304 if (s->internal->state == SSL23_ST_SR_CLNT_HELLO_B) {
305 /* we have SSLv3/TLSv1 in an SSLv2 header
306 * (other cases skip this state) */
307
308 /*
309 * Limit the support of "backward compatible" headers
310 * only to "backward" versions of TLS. If we have moved
311 * on to modernity, just say no.
312 */
313 if (s->internal->options & SSL_OP_NO_TLSv1)
314 goto unsupported;
315
316 type = 2;
317 p = s->internal->packet;
318 client_version = p[3] << 8 | p[4];
319
320 /* An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
321 * header is sent directly on the wire, not wrapped as a TLS
322 * record. It's format is:
323 * Byte Content
324 * 0-1 msg_length
325 * 2 msg_type
326 * 3-4 version
327 * 5-6 cipher_spec_length
328 * 7-8 session_id_length
329 * 9-10 challenge_length
330 * ... ...
331 */
332 n = ((p[0] & 0x7f) << 8) | p[1];
333 if (n > (1024 * 4)) {
334 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_RECORD_TOO_LARGE);
335 return -1;
336 }
337 if (n < 9) {
338 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,
339 SSL_R_RECORD_LENGTH_MISMATCH);
340 return -1;
341 }
342
343 j = ssl23_read_bytes(s, n + 2);
344 if (j != n + 2)
345 return -1;
346
347 tls1_finish_mac(s, s->internal->packet + 2, s->internal->packet_length - 2);
348 if (s->internal->msg_callback)
349 s->internal->msg_callback(0, SSL2_VERSION, 0, s->internal->packet + 2,
350 s->internal->packet_length - 2, s, s->internal->msg_callback_arg);
351
352 p = s->internal->packet;
353 p += 5;
354 n2s(p, csl);
355 n2s(p, sil);
356 n2s(p, cl);
357 d = (unsigned char *)s->internal->init_buf->data;
358 if ((csl + sil + cl + 11) != s->internal->packet_length) {
359 /*
360 * We can't have TLS extensions in SSL 2.0 format
361 * Client Hello, can we ? Error condition should be
362 * '>' otherwise
363 */
364 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,
365 SSL_R_RECORD_LENGTH_MISMATCH);
366 return -1;
367 }
368
369 /* record header: msg_type ... */
370 *(d++) = SSL3_MT_CLIENT_HELLO;
371 /* ... and length (actual value will be written later) */
372 d_len = d;
373 d += 3;
374
375 /* client_version */
376 *(d++) = client_version >> 8;
377 *(d++) = client_version & 0xff;
378
379 /* lets populate the random area */
380 /* get the challenge_length */
381 i = (cl > SSL3_RANDOM_SIZE) ? SSL3_RANDOM_SIZE : cl;
382 memset(d, 0, SSL3_RANDOM_SIZE);
383 memcpy(&(d[SSL3_RANDOM_SIZE - i]), &(p[csl + sil]), i);
384 d += SSL3_RANDOM_SIZE;
385
386 /* no session-id reuse */
387 *(d++) = 0;
388
389 /* ciphers */
390 j = 0;
391 dd = d;
392 d += 2;
393 for (i = 0; i < csl; i += 3) {
394 if (p[i] != 0)
395 continue;
396 *(d++) = p[i + 1];
397 *(d++) = p[i + 2];
398 j += 2;
399 }
400 s2n(j, dd);
401
402 /* add in (no) COMPRESSION */
403 *(d++) = 1;
404 *(d++) = 0;
405
406 i = (d - (unsigned char *)s->internal->init_buf->data) - 4;
407 l2n3((long)i, d_len);
408
409 /* get the data reused from the init_buf */
410 S3I(s)->tmp.reuse_message = 1;
411 S3I(s)->tmp.message_type = SSL3_MT_CLIENT_HELLO;
412 S3I(s)->tmp.message_size = i;
413 }
414
415 /* imaginary new state (for program structure): */
416 /* s->internal->state = SSL23_SR_CLNT_HELLO_C */
417
418 if (type == 2 || type == 3) {
419 /* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */
420
421 if (!ssl_init_wbio_buffer(s, 1))
422 return -1;
423
424 /* we are in this state */
425 s->internal->state = SSL3_ST_SR_CLNT_HELLO_A;
426
427 if (type == 3) {
428 /* put the 'n' bytes we have read into the input buffer
429 * for SSLv3 */
430 s->internal->rstate = SSL_ST_READ_HEADER;
431 s->internal->packet_length = n;
432 if (s->s3->rbuf.buf == NULL)
433 if (!ssl3_setup_read_buffer(s))
434 return -1;
435
436 s->internal->packet = &(s->s3->rbuf.buf[0]);
437 memcpy(s->internal->packet, buf, n);
438 s->s3->rbuf.left = n;
439 s->s3->rbuf.offset = 0;
440 } else {
441 s->internal->packet_length = 0;
442 s->s3->rbuf.left = 0;
443 s->s3->rbuf.offset = 0;
444 }
445 if (s->version == TLS1_2_VERSION)
446 s->method = TLSv1_2_server_method();
447 else if (s->version == TLS1_1_VERSION)
448 s->method = TLSv1_1_server_method();
449 else if (s->version == TLS1_VERSION)
450 s->method = TLSv1_server_method();
451 else
452 goto unsupported;
453 s->internal->handshake_func = s->method->internal->ssl_accept;
454 } else {
455 /* bad, very bad */
456 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
457 return -1;
458 }
459 s->internal->init_num = 0;
460
461 return (SSL_accept(s));
462
463 unsupported:
464 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
465 return -1;
466}