summaryrefslogtreecommitdiff
path: root/src/lib/libssl/s23_clnt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/s23_clnt.c')
-rw-r--r--src/lib/libssl/s23_clnt.c452
1 files changed, 0 insertions, 452 deletions
diff --git a/src/lib/libssl/s23_clnt.c b/src/lib/libssl/s23_clnt.c
deleted file mode 100644
index 4a7641b818..0000000000
--- a/src/lib/libssl/s23_clnt.c
+++ /dev/null
@@ -1,452 +0,0 @@
1/* $OpenBSD: s23_clnt.c,v 1.57 2017/01/25 10:54: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
120static int ssl23_client_hello(SSL *s);
121static int ssl23_get_server_hello(SSL *s);
122
123int
124ssl23_connect(SSL *s)
125{
126 void (*cb)(const SSL *ssl, int type, int val) = NULL;
127 int ret = -1;
128 int new_state, state;
129
130 ERR_clear_error();
131 errno = 0;
132
133 if (s->internal->info_callback != NULL)
134 cb = s->internal->info_callback;
135 else if (s->ctx->internal->info_callback != NULL)
136 cb = s->ctx->internal->info_callback;
137
138 s->internal->in_handshake++;
139 if (!SSL_in_init(s) || SSL_in_before(s))
140 SSL_clear(s);
141
142 for (;;) {
143 state = s->internal->state;
144
145 switch (s->internal->state) {
146 case SSL_ST_BEFORE:
147 case SSL_ST_CONNECT:
148 case SSL_ST_BEFORE|SSL_ST_CONNECT:
149 case SSL_ST_OK|SSL_ST_CONNECT:
150
151 if (s->session != NULL) {
152 SSLerr(SSL_F_SSL23_CONNECT, SSL_R_SSL23_DOING_SESSION_ID_REUSE);
153 ret = -1;
154 goto end;
155 }
156 s->server = 0;
157 if (cb != NULL)
158 cb(s, SSL_CB_HANDSHAKE_START, 1);
159
160 /* s->version=TLS1_VERSION; */
161 s->internal->type = SSL_ST_CONNECT;
162
163 if (!ssl3_setup_init_buffer(s)) {
164 ret = -1;
165 goto end;
166 }
167 if (!ssl3_setup_buffers(s)) {
168 ret = -1;
169 goto end;
170 }
171 if (!tls1_init_finished_mac(s)) {
172 ret = -1;
173 goto end;
174 }
175
176 s->internal->state = SSL23_ST_CW_CLNT_HELLO_A;
177 s->ctx->internal->stats.sess_connect++;
178 s->internal->init_num = 0;
179 break;
180
181 case SSL23_ST_CW_CLNT_HELLO_A:
182 case SSL23_ST_CW_CLNT_HELLO_B:
183
184 s->internal->shutdown = 0;
185 ret = ssl23_client_hello(s);
186 if (ret <= 0)
187 goto end;
188 s->internal->state = SSL23_ST_CR_SRVR_HELLO_A;
189 s->internal->init_num = 0;
190
191 break;
192
193 case SSL23_ST_CR_SRVR_HELLO_A:
194 case SSL23_ST_CR_SRVR_HELLO_B:
195 ret = ssl23_get_server_hello(s);
196 if (ret >= 0)
197 cb = NULL;
198 goto end;
199 /* break; */
200
201 default:
202 SSLerr(SSL_F_SSL23_CONNECT, SSL_R_UNKNOWN_STATE);
203 ret = -1;
204 goto end;
205 /* break; */
206 }
207
208 if (s->internal->debug) {
209 (void)BIO_flush(s->wbio);
210 }
211
212 if ((cb != NULL) && (s->internal->state != state)) {
213 new_state = s->internal->state;
214 s->internal->state = state;
215 cb(s, SSL_CB_CONNECT_LOOP, 1);
216 s->internal->state = new_state;
217 }
218 }
219
220end:
221 s->internal->in_handshake--;
222 if (cb != NULL)
223 cb(s, SSL_CB_CONNECT_EXIT, ret);
224
225 return (ret);
226}
227
228static int
229ssl23_client_hello(SSL *s)
230{
231 unsigned char *buf;
232 unsigned char *p, *d;
233 unsigned long l;
234 uint16_t version;
235 size_t outlen;
236 int ret;
237
238 buf = (unsigned char *)s->internal->init_buf->data;
239 if (s->internal->state == SSL23_ST_CW_CLNT_HELLO_A) {
240 arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE);
241
242 if (ssl_supported_version_range(s, NULL, &version) != 1) {
243 SSLerr(SSL_F_SSL23_CLIENT_HELLO,
244 SSL_R_NO_PROTOCOLS_AVAILABLE);
245 return (-1);
246 }
247 s->client_version = version;
248
249 /* create Client Hello in SSL 3.0/TLS 1.0 format */
250
251 /*
252 * Do the record header (5 bytes) and handshake
253 * message header (4 bytes) last
254 */
255 d = p = &(buf[SSL3_RT_HEADER_LENGTH + SSL3_HM_HEADER_LENGTH]);
256
257 *(p++) = version >> 8;
258 *(p++) = version & 0xff;
259
260 /* Random stuff */
261 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
262 p += SSL3_RANDOM_SIZE;
263
264 /* Session ID (zero since there is no reuse) */
265 *(p++) = 0;
266
267 /* Ciphers supported (using SSL 3.0/TLS 1.0 format) */
268 if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2],
269 buf - &p[2] + SSL3_RT_MAX_PLAIN_LENGTH, &outlen))
270 return -1;
271 if (outlen == 0) {
272 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
273 SSL_R_NO_CIPHERS_AVAILABLE);
274 return -1;
275 }
276 s2n(outlen, p);
277 p += outlen;
278
279 /* add in (no) COMPRESSION */
280 *(p++) = 1;
281 /* Add the NULL method */
282 *(p++) = 0;
283
284 /* TLS extensions*/
285 if ((p = ssl_add_clienthello_tlsext(s, p,
286 buf + SSL3_RT_MAX_PLAIN_LENGTH)) == NULL) {
287 SSLerr(SSL_F_SSL23_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
288 return -1;
289 }
290
291 l = p - d;
292
293 /* fill in 4-byte handshake header */
294 d = &(buf[SSL3_RT_HEADER_LENGTH]);
295 *(d++) = SSL3_MT_CLIENT_HELLO;
296 l2n3(l, d);
297
298 l += 4;
299
300 if (l > SSL3_RT_MAX_PLAIN_LENGTH) {
301 SSLerr(SSL_F_SSL23_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
302 return -1;
303 }
304
305 /* fill in 5-byte record header */
306 d = buf;
307 *(d++) = SSL3_RT_HANDSHAKE;
308 *(d++) = version >> 8;
309
310 /*
311 * Some servers hang if we use long client hellos
312 * and a record number > TLS 1.0.
313 */
314 if (TLS1_get_client_version(s) > TLS1_VERSION)
315 *(d++) = 1;
316 else
317 *(d++) = version & 0xff;
318 s2n((int)l, d);
319
320 /* number of bytes to write */
321 s->internal->init_num = p - buf;
322 s->internal->init_off = 0;
323
324 tls1_finish_mac(s, &(buf[SSL3_RT_HEADER_LENGTH]),
325 s->internal->init_num - SSL3_RT_HEADER_LENGTH);
326
327 s->internal->state = SSL23_ST_CW_CLNT_HELLO_B;
328 s->internal->init_off = 0;
329 }
330
331 /* SSL3_ST_CW_CLNT_HELLO_B */
332 ret = ssl23_write_bytes(s);
333
334 if ((ret >= 2) && s->internal->msg_callback) {
335 /* Client Hello has been sent; tell msg_callback */
336 s->internal->msg_callback(1, s->client_version, SSL3_RT_HANDSHAKE,
337 s->internal->init_buf->data + 5, ret - 5, s, s->internal->msg_callback_arg);
338 }
339
340 return ret;
341}
342
343static int
344ssl23_get_server_hello(SSL *s)
345{
346 char buf[8];
347 unsigned char *p;
348 int i;
349 int n;
350
351 n = ssl23_read_bytes(s, 7);
352
353 if (n != 7)
354 return (n);
355 p = s->internal->packet;
356
357 memcpy(buf, p, n);
358
359 /* Old unsupported sslv2 handshake */
360 if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
361 (p[5] == 0x00) && (p[6] == 0x02)) {
362 SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,
363 SSL_R_UNSUPPORTED_PROTOCOL);
364 goto err;
365 }
366
367 if (p[1] == SSL3_VERSION_MAJOR &&
368 p[2] <= TLS1_2_VERSION_MINOR &&
369 ((p[0] == SSL3_RT_HANDSHAKE && p[5] == SSL3_MT_SERVER_HELLO) ||
370 (p[0] == SSL3_RT_ALERT && p[3] == 0 && p[4] == 2))) {
371 /* we have sslv3 or tls1 (server hello or alert) */
372
373 if ((p[2] == TLS1_VERSION_MINOR) &&
374 !(s->internal->options & SSL_OP_NO_TLSv1)) {
375 s->version = TLS1_VERSION;
376 s->method = TLSv1_client_method();
377 } else if ((p[2] == TLS1_1_VERSION_MINOR) &&
378 !(s->internal->options & SSL_OP_NO_TLSv1_1)) {
379 s->version = TLS1_1_VERSION;
380 s->method = TLSv1_1_client_method();
381 } else if ((p[2] == TLS1_2_VERSION_MINOR) &&
382 !(s->internal->options & SSL_OP_NO_TLSv1_2)) {
383 s->version = TLS1_2_VERSION;
384 s->method = TLSv1_2_client_method();
385 } else {
386 SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,
387 SSL_R_UNSUPPORTED_PROTOCOL);
388 goto err;
389 }
390
391 if (p[0] == SSL3_RT_ALERT && p[5] != SSL3_AL_WARNING) {
392 /* fatal alert */
393 void (*cb)(const SSL *ssl, int type, int val) = NULL;
394 int j;
395
396 if (s->internal->info_callback != NULL)
397 cb = s->internal->info_callback;
398 else if (s->ctx->internal->info_callback != NULL)
399 cb = s->ctx->internal->info_callback;
400
401 i = p[5];
402 if (cb != NULL) {
403 j = (i << 8) | p[6];
404 cb(s, SSL_CB_READ_ALERT, j);
405 }
406
407 if (s->internal->msg_callback)
408 s->internal->msg_callback(0, s->version, SSL3_RT_ALERT,
409 p + 5, 2, s, s->internal->msg_callback_arg);
410
411 s->internal->rwstate = SSL_NOTHING;
412 SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,
413 SSL_AD_REASON_OFFSET + p[6]);
414 goto err;
415 }
416
417 if (!ssl_init_wbio_buffer(s, 1))
418 goto err;
419
420 /* we are in this state */
421 s->internal->state = SSL3_ST_CR_SRVR_HELLO_A;
422
423 /* put the 7 bytes we have read into the input buffer
424 * for SSLv3 */
425 s->internal->rstate = SSL_ST_READ_HEADER;
426 s->internal->packet_length = n;
427 if (s->s3->rbuf.buf == NULL)
428 if (!ssl3_setup_read_buffer(s))
429 goto err;
430 s->internal->packet = &(s->s3->rbuf.buf[0]);
431 memcpy(s->internal->packet, buf, n);
432 s->s3->rbuf.left = n;
433 s->s3->rbuf.offset = 0;
434
435 s->internal->handshake_func = s->method->internal->ssl_connect;
436 } else {
437 SSLerr(SSL_F_SSL23_GET_SERVER_HELLO, SSL_R_UNKNOWN_PROTOCOL);
438 goto err;
439 }
440 s->internal->init_num = 0;
441
442 /*
443 * Since, if we are sending a ssl23 client hello, we are not
444 * reusing a session-id
445 */
446 if (!ssl_get_new_session(s, 0))
447 goto err;
448
449 return (SSL_connect(s));
450err:
451 return (-1);
452}