diff options
author | jsing <> | 2020-04-28 20:37:22 +0000 |
---|---|---|
committer | jsing <> | 2020-04-28 20:37:22 +0000 |
commit | 8c08e264e3278f882edf60eb3f157388941a16ee (patch) | |
tree | 3340d02c12df4a9485d2eaf80db0ce8efbfa3418 | |
parent | ceb4dab60b6d8724db6522bd50acb817a2ae5f69 (diff) | |
download | openbsd-8c08e264e3278f882edf60eb3f157388941a16ee.tar.gz openbsd-8c08e264e3278f882edf60eb3f157388941a16ee.tar.bz2 openbsd-8c08e264e3278f882edf60eb3f157388941a16ee.zip |
Move legacy stack interfacing functions into tls13_legacy.c.
No functional change.
ok inoguchi@ tb@
-rw-r--r-- | src/lib/libssl/tls13_client.c | 106 | ||||
-rw-r--r-- | src/lib/libssl/tls13_internal.h | 9 | ||||
-rw-r--r-- | src/lib/libssl/tls13_legacy.c | 191 | ||||
-rw-r--r-- | src/lib/libssl/tls13_server.c | 99 |
4 files changed, 206 insertions, 199 deletions
diff --git a/src/lib/libssl/tls13_client.c b/src/lib/libssl/tls13_client.c index 5bd7681f19..79318d9313 100644 --- a/src/lib/libssl/tls13_client.c +++ b/src/lib/libssl/tls13_client.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls13_client.c,v 1.53 2020/04/28 20:30:40 jsing Exp $ */ | 1 | /* $OpenBSD: tls13_client.c,v 1.54 2020/04/28 20:37:22 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -24,7 +24,7 @@ | |||
24 | #include "tls13_handshake.h" | 24 | #include "tls13_handshake.h" |
25 | #include "tls13_internal.h" | 25 | #include "tls13_internal.h" |
26 | 26 | ||
27 | static int | 27 | int |
28 | tls13_client_init(struct tls13_ctx *ctx) | 28 | tls13_client_init(struct tls13_ctx *ctx) |
29 | { | 29 | { |
30 | const uint16_t *groups; | 30 | const uint16_t *groups; |
@@ -71,8 +71,8 @@ tls13_client_init(struct tls13_ctx *ctx) | |||
71 | return 1; | 71 | return 1; |
72 | } | 72 | } |
73 | 73 | ||
74 | static int | 74 | int |
75 | tls13_connect(struct tls13_ctx *ctx) | 75 | tls13_client_connect(struct tls13_ctx *ctx) |
76 | { | 76 | { |
77 | if (ctx->mode != TLS13_HS_CLIENT) | 77 | if (ctx->mode != TLS13_HS_CLIENT) |
78 | return TLS13_IO_FAILURE; | 78 | return TLS13_IO_FAILURE; |
@@ -80,104 +80,6 @@ tls13_connect(struct tls13_ctx *ctx) | |||
80 | return tls13_handshake_perform(ctx); | 80 | return tls13_handshake_perform(ctx); |
81 | } | 81 | } |
82 | 82 | ||
83 | int | ||
84 | tls13_legacy_connect(SSL *ssl) | ||
85 | { | ||
86 | struct tls13_ctx *ctx = ssl->internal->tls13; | ||
87 | int ret; | ||
88 | |||
89 | #ifdef TLS13_USE_LEGACY_CLIENT_AUTH | ||
90 | /* XXX drop back to legacy for client auth for now */ | ||
91 | if (ssl->cert->key->privatekey != NULL) { | ||
92 | ssl->method = tls_legacy_client_method(); | ||
93 | return ssl->method->internal->ssl_connect(ssl); | ||
94 | } | ||
95 | #endif | ||
96 | |||
97 | if (ctx == NULL) { | ||
98 | if ((ctx = tls13_ctx_new(TLS13_HS_CLIENT)) == NULL) { | ||
99 | SSLerror(ssl, ERR_R_INTERNAL_ERROR); /* XXX */ | ||
100 | return -1; | ||
101 | } | ||
102 | ssl->internal->tls13 = ctx; | ||
103 | ctx->ssl = ssl; | ||
104 | ctx->hs = &S3I(ssl)->hs_tls13; | ||
105 | |||
106 | if (!tls13_client_init(ctx)) { | ||
107 | if (ERR_peek_error() == 0) | ||
108 | SSLerror(ssl, ERR_R_INTERNAL_ERROR); /* XXX */ | ||
109 | return -1; | ||
110 | } | ||
111 | } | ||
112 | |||
113 | ERR_clear_error(); | ||
114 | S3I(ssl)->hs.state = SSL_ST_CONNECT; | ||
115 | |||
116 | ret = tls13_connect(ctx); | ||
117 | if (ret == TLS13_IO_USE_LEGACY) | ||
118 | return ssl->method->internal->ssl_connect(ssl); | ||
119 | if (ret == TLS13_IO_SUCCESS) | ||
120 | S3I(ssl)->hs.state = SSL_ST_OK; | ||
121 | |||
122 | return tls13_legacy_return_code(ssl, ret); | ||
123 | } | ||
124 | |||
125 | int | ||
126 | tls13_use_legacy_client(struct tls13_ctx *ctx) | ||
127 | { | ||
128 | SSL *s = ctx->ssl; | ||
129 | CBS cbs; | ||
130 | |||
131 | s->method = tls_legacy_client_method(); | ||
132 | s->internal->handshake_func = s->method->internal->ssl_connect; | ||
133 | s->client_version = s->version = s->method->internal->max_version; | ||
134 | |||
135 | if (!ssl3_setup_init_buffer(s)) | ||
136 | goto err; | ||
137 | if (!ssl3_setup_buffers(s)) | ||
138 | goto err; | ||
139 | if (!ssl_init_wbio_buffer(s, 0)) | ||
140 | goto err; | ||
141 | |||
142 | if (s->bbio != s->wbio) | ||
143 | s->wbio = BIO_push(s->bbio, s->wbio); | ||
144 | |||
145 | /* Stash any unprocessed data from the last record. */ | ||
146 | tls13_record_layer_rbuf(ctx->rl, &cbs); | ||
147 | if (CBS_len(&cbs) > 0) { | ||
148 | if (!CBS_write_bytes(&cbs, | ||
149 | S3I(s)->rbuf.buf + SSL3_RT_HEADER_LENGTH, | ||
150 | S3I(s)->rbuf.len - SSL3_RT_HEADER_LENGTH, NULL)) | ||
151 | goto err; | ||
152 | |||
153 | S3I(s)->rbuf.offset = SSL3_RT_HEADER_LENGTH; | ||
154 | S3I(s)->rbuf.left = CBS_len(&cbs); | ||
155 | S3I(s)->rrec.type = SSL3_RT_HANDSHAKE; | ||
156 | S3I(s)->rrec.length = CBS_len(&cbs); | ||
157 | s->internal->rstate = SSL_ST_READ_BODY; | ||
158 | s->internal->packet = S3I(s)->rbuf.buf; | ||
159 | s->internal->packet_length = SSL3_RT_HEADER_LENGTH; | ||
160 | s->internal->mac_packet = 1; | ||
161 | } | ||
162 | |||
163 | /* Stash the current handshake message. */ | ||
164 | tls13_handshake_msg_data(ctx->hs_msg, &cbs); | ||
165 | if (!CBS_write_bytes(&cbs, s->internal->init_buf->data, | ||
166 | s->internal->init_buf->length, NULL)) | ||
167 | goto err; | ||
168 | |||
169 | S3I(s)->tmp.reuse_message = 1; | ||
170 | S3I(s)->tmp.message_type = tls13_handshake_msg_type(ctx->hs_msg); | ||
171 | S3I(s)->tmp.message_size = CBS_len(&cbs); | ||
172 | |||
173 | S3I(s)->hs.state = SSL3_ST_CR_SRVR_HELLO_A; | ||
174 | |||
175 | return 1; | ||
176 | |||
177 | err: | ||
178 | return 0; | ||
179 | } | ||
180 | |||
181 | static int | 83 | static int |
182 | tls13_client_hello_build(struct tls13_ctx *ctx, CBB *cbb) | 84 | tls13_client_hello_build(struct tls13_ctx *ctx, CBB *cbb) |
183 | { | 85 | { |
diff --git a/src/lib/libssl/tls13_internal.h b/src/lib/libssl/tls13_internal.h index d711f1a58d..d53672dbfe 100644 --- a/src/lib/libssl/tls13_internal.h +++ b/src/lib/libssl/tls13_internal.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls13_internal.h,v 1.66 2020/04/28 20:30:40 jsing Exp $ */ | 1 | /* $OpenBSD: tls13_internal.h,v 1.67 2020/04/28 20:37:22 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2018 Bob Beck <beck@openbsd.org> | 3 | * Copyright (c) 2018 Bob Beck <beck@openbsd.org> |
4 | * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> |
@@ -250,6 +250,8 @@ const EVP_MD *tls13_cipher_hash(const SSL_CIPHER *cipher); | |||
250 | /* | 250 | /* |
251 | * Legacy interfaces. | 251 | * Legacy interfaces. |
252 | */ | 252 | */ |
253 | int tls13_use_legacy_client(struct tls13_ctx *ctx); | ||
254 | int tls13_use_legacy_server(struct tls13_ctx *ctx); | ||
253 | int tls13_legacy_accept(SSL *ssl); | 255 | int tls13_legacy_accept(SSL *ssl); |
254 | int tls13_legacy_connect(SSL *ssl); | 256 | int tls13_legacy_connect(SSL *ssl); |
255 | int tls13_legacy_return_code(SSL *ssl, ssize_t ret); | 257 | int tls13_legacy_return_code(SSL *ssl, ssize_t ret); |
@@ -292,6 +294,11 @@ int tls13_legacy_shutdown(SSL *ssl); | |||
292 | int tls13_handshake_msg_record(struct tls13_ctx *ctx); | 294 | int tls13_handshake_msg_record(struct tls13_ctx *ctx); |
293 | int tls13_handshake_perform(struct tls13_ctx *ctx); | 295 | int tls13_handshake_perform(struct tls13_ctx *ctx); |
294 | 296 | ||
297 | int tls13_client_init(struct tls13_ctx *ctx); | ||
298 | int tls13_server_init(struct tls13_ctx *ctx); | ||
299 | int tls13_client_connect(struct tls13_ctx *ctx); | ||
300 | int tls13_server_accept(struct tls13_ctx *ctx); | ||
301 | |||
295 | int tls13_client_hello_send(struct tls13_ctx *ctx, CBB *cbb); | 302 | int tls13_client_hello_send(struct tls13_ctx *ctx, CBB *cbb); |
296 | int tls13_client_hello_sent(struct tls13_ctx *ctx); | 303 | int tls13_client_hello_sent(struct tls13_ctx *ctx); |
297 | int tls13_client_hello_recv(struct tls13_ctx *ctx, CBS *cbs); | 304 | int tls13_client_hello_recv(struct tls13_ctx *ctx, CBS *cbs); |
diff --git a/src/lib/libssl/tls13_legacy.c b/src/lib/libssl/tls13_legacy.c index 747bdc2728..1e18a8258c 100644 --- a/src/lib/libssl/tls13_legacy.c +++ b/src/lib/libssl/tls13_legacy.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls13_legacy.c,v 1.2 2020/03/10 17:02:21 jsing Exp $ */ | 1 | /* $OpenBSD: tls13_legacy.c,v 1.3 2020/04/28 20:37:22 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -277,6 +277,195 @@ tls13_legacy_write_bytes(SSL *ssl, int type, const void *vbuf, int len) | |||
277 | } | 277 | } |
278 | 278 | ||
279 | int | 279 | int |
280 | tls13_use_legacy_client(struct tls13_ctx *ctx) | ||
281 | { | ||
282 | SSL *s = ctx->ssl; | ||
283 | CBS cbs; | ||
284 | |||
285 | s->method = tls_legacy_client_method(); | ||
286 | s->internal->handshake_func = s->method->internal->ssl_connect; | ||
287 | s->client_version = s->version = s->method->internal->max_version; | ||
288 | |||
289 | if (!ssl3_setup_init_buffer(s)) | ||
290 | goto err; | ||
291 | if (!ssl3_setup_buffers(s)) | ||
292 | goto err; | ||
293 | if (!ssl_init_wbio_buffer(s, 0)) | ||
294 | goto err; | ||
295 | |||
296 | if (s->bbio != s->wbio) | ||
297 | s->wbio = BIO_push(s->bbio, s->wbio); | ||
298 | |||
299 | /* Stash any unprocessed data from the last record. */ | ||
300 | tls13_record_layer_rbuf(ctx->rl, &cbs); | ||
301 | if (CBS_len(&cbs) > 0) { | ||
302 | if (!CBS_write_bytes(&cbs, | ||
303 | S3I(s)->rbuf.buf + SSL3_RT_HEADER_LENGTH, | ||
304 | S3I(s)->rbuf.len - SSL3_RT_HEADER_LENGTH, NULL)) | ||
305 | goto err; | ||
306 | |||
307 | S3I(s)->rbuf.offset = SSL3_RT_HEADER_LENGTH; | ||
308 | S3I(s)->rbuf.left = CBS_len(&cbs); | ||
309 | S3I(s)->rrec.type = SSL3_RT_HANDSHAKE; | ||
310 | S3I(s)->rrec.length = CBS_len(&cbs); | ||
311 | s->internal->rstate = SSL_ST_READ_BODY; | ||
312 | s->internal->packet = S3I(s)->rbuf.buf; | ||
313 | s->internal->packet_length = SSL3_RT_HEADER_LENGTH; | ||
314 | s->internal->mac_packet = 1; | ||
315 | } | ||
316 | |||
317 | /* Stash the current handshake message. */ | ||
318 | tls13_handshake_msg_data(ctx->hs_msg, &cbs); | ||
319 | if (!CBS_write_bytes(&cbs, s->internal->init_buf->data, | ||
320 | s->internal->init_buf->length, NULL)) | ||
321 | goto err; | ||
322 | |||
323 | S3I(s)->tmp.reuse_message = 1; | ||
324 | S3I(s)->tmp.message_type = tls13_handshake_msg_type(ctx->hs_msg); | ||
325 | S3I(s)->tmp.message_size = CBS_len(&cbs); | ||
326 | |||
327 | S3I(s)->hs.state = SSL3_ST_CR_SRVR_HELLO_A; | ||
328 | |||
329 | return 1; | ||
330 | |||
331 | err: | ||
332 | return 0; | ||
333 | } | ||
334 | |||
335 | int | ||
336 | tls13_use_legacy_server(struct tls13_ctx *ctx) | ||
337 | { | ||
338 | SSL *s = ctx->ssl; | ||
339 | CBS cbs; | ||
340 | |||
341 | s->method = tls_legacy_server_method(); | ||
342 | s->internal->handshake_func = s->method->internal->ssl_accept; | ||
343 | s->client_version = s->version = s->method->internal->max_version; | ||
344 | s->server = 1; | ||
345 | |||
346 | if (!ssl3_setup_init_buffer(s)) | ||
347 | goto err; | ||
348 | if (!ssl3_setup_buffers(s)) | ||
349 | goto err; | ||
350 | if (!ssl_init_wbio_buffer(s, 0)) | ||
351 | goto err; | ||
352 | |||
353 | if (s->bbio != s->wbio) | ||
354 | s->wbio = BIO_push(s->bbio, s->wbio); | ||
355 | |||
356 | /* Stash any unprocessed data from the last record. */ | ||
357 | tls13_record_layer_rbuf(ctx->rl, &cbs); | ||
358 | if (CBS_len(&cbs) > 0) { | ||
359 | if (!CBS_write_bytes(&cbs, | ||
360 | S3I(s)->rbuf.buf + SSL3_RT_HEADER_LENGTH, | ||
361 | S3I(s)->rbuf.len - SSL3_RT_HEADER_LENGTH, NULL)) | ||
362 | goto err; | ||
363 | |||
364 | S3I(s)->rbuf.offset = SSL3_RT_HEADER_LENGTH; | ||
365 | S3I(s)->rbuf.left = CBS_len(&cbs); | ||
366 | S3I(s)->rrec.type = SSL3_RT_HANDSHAKE; | ||
367 | S3I(s)->rrec.length = CBS_len(&cbs); | ||
368 | s->internal->rstate = SSL_ST_READ_BODY; | ||
369 | s->internal->packet = S3I(s)->rbuf.buf; | ||
370 | s->internal->packet_length = SSL3_RT_HEADER_LENGTH; | ||
371 | s->internal->mac_packet = 1; | ||
372 | } | ||
373 | |||
374 | /* Stash the current handshake message. */ | ||
375 | tls13_handshake_msg_data(ctx->hs_msg, &cbs); | ||
376 | if (!CBS_write_bytes(&cbs, s->internal->init_buf->data, | ||
377 | s->internal->init_buf->length, NULL)) | ||
378 | goto err; | ||
379 | |||
380 | S3I(s)->tmp.reuse_message = 1; | ||
381 | S3I(s)->tmp.message_type = tls13_handshake_msg_type(ctx->hs_msg); | ||
382 | S3I(s)->tmp.message_size = CBS_len(&cbs); | ||
383 | |||
384 | S3I(s)->hs.state = SSL3_ST_SR_CLNT_HELLO_A; | ||
385 | |||
386 | return 1; | ||
387 | |||
388 | err: | ||
389 | return 0; | ||
390 | } | ||
391 | |||
392 | int | ||
393 | tls13_legacy_accept(SSL *ssl) | ||
394 | { | ||
395 | struct tls13_ctx *ctx = ssl->internal->tls13; | ||
396 | int ret; | ||
397 | |||
398 | if (ctx == NULL) { | ||
399 | if ((ctx = tls13_ctx_new(TLS13_HS_SERVER)) == NULL) { | ||
400 | SSLerror(ssl, ERR_R_INTERNAL_ERROR); /* XXX */ | ||
401 | return -1; | ||
402 | } | ||
403 | ssl->internal->tls13 = ctx; | ||
404 | ctx->ssl = ssl; | ||
405 | ctx->hs = &S3I(ssl)->hs_tls13; | ||
406 | |||
407 | if (!tls13_server_init(ctx)) { | ||
408 | if (ERR_peek_error() == 0) | ||
409 | SSLerror(ssl, ERR_R_INTERNAL_ERROR); /* XXX */ | ||
410 | return -1; | ||
411 | } | ||
412 | } | ||
413 | |||
414 | ERR_clear_error(); | ||
415 | S3I(ssl)->hs.state = SSL_ST_ACCEPT; | ||
416 | |||
417 | ret = tls13_server_accept(ctx); | ||
418 | if (ret == TLS13_IO_USE_LEGACY) | ||
419 | return ssl->method->internal->ssl_accept(ssl); | ||
420 | if (ret == TLS13_IO_SUCCESS) | ||
421 | S3I(ssl)->hs.state = SSL_ST_OK; | ||
422 | |||
423 | return tls13_legacy_return_code(ssl, ret); | ||
424 | } | ||
425 | |||
426 | int | ||
427 | tls13_legacy_connect(SSL *ssl) | ||
428 | { | ||
429 | struct tls13_ctx *ctx = ssl->internal->tls13; | ||
430 | int ret; | ||
431 | |||
432 | #ifdef TLS13_USE_LEGACY_CLIENT_AUTH | ||
433 | /* XXX drop back to legacy for client auth for now */ | ||
434 | if (ssl->cert->key->privatekey != NULL) { | ||
435 | ssl->method = tls_legacy_client_method(); | ||
436 | return ssl->method->internal->ssl_connect(ssl); | ||
437 | } | ||
438 | #endif | ||
439 | |||
440 | if (ctx == NULL) { | ||
441 | if ((ctx = tls13_ctx_new(TLS13_HS_CLIENT)) == NULL) { | ||
442 | SSLerror(ssl, ERR_R_INTERNAL_ERROR); /* XXX */ | ||
443 | return -1; | ||
444 | } | ||
445 | ssl->internal->tls13 = ctx; | ||
446 | ctx->ssl = ssl; | ||
447 | ctx->hs = &S3I(ssl)->hs_tls13; | ||
448 | |||
449 | if (!tls13_client_init(ctx)) { | ||
450 | if (ERR_peek_error() == 0) | ||
451 | SSLerror(ssl, ERR_R_INTERNAL_ERROR); /* XXX */ | ||
452 | return -1; | ||
453 | } | ||
454 | } | ||
455 | |||
456 | ERR_clear_error(); | ||
457 | S3I(ssl)->hs.state = SSL_ST_CONNECT; | ||
458 | |||
459 | ret = tls13_client_connect(ctx); | ||
460 | if (ret == TLS13_IO_USE_LEGACY) | ||
461 | return ssl->method->internal->ssl_connect(ssl); | ||
462 | if (ret == TLS13_IO_SUCCESS) | ||
463 | S3I(ssl)->hs.state = SSL_ST_OK; | ||
464 | |||
465 | return tls13_legacy_return_code(ssl, ret); | ||
466 | } | ||
467 | |||
468 | int | ||
280 | tls13_legacy_shutdown(SSL *ssl) | 469 | tls13_legacy_shutdown(SSL *ssl) |
281 | { | 470 | { |
282 | struct tls13_ctx *ctx = ssl->internal->tls13; | 471 | struct tls13_ctx *ctx = ssl->internal->tls13; |
diff --git a/src/lib/libssl/tls13_server.c b/src/lib/libssl/tls13_server.c index 864e434fda..4fa1aba31d 100644 --- a/src/lib/libssl/tls13_server.c +++ b/src/lib/libssl/tls13_server.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls13_server.c,v 1.33 2020/04/27 20:15:17 jsing Exp $ */ | 1 | /* $OpenBSD: tls13_server.c,v 1.34 2020/04/28 20:37:22 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2019, 2020 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2019, 2020 Joel Sing <jsing@openbsd.org> |
4 | * Copyright (c) 2020 Bob Beck <beck@openbsd.org> | 4 | * Copyright (c) 2020 Bob Beck <beck@openbsd.org> |
@@ -22,7 +22,7 @@ | |||
22 | #include "tls13_handshake.h" | 22 | #include "tls13_handshake.h" |
23 | #include "tls13_internal.h" | 23 | #include "tls13_internal.h" |
24 | 24 | ||
25 | static int | 25 | int |
26 | tls13_server_init(struct tls13_ctx *ctx) | 26 | tls13_server_init(struct tls13_ctx *ctx) |
27 | { | 27 | { |
28 | SSL *s = ctx->ssl; | 28 | SSL *s = ctx->ssl; |
@@ -45,8 +45,8 @@ tls13_server_init(struct tls13_ctx *ctx) | |||
45 | return 1; | 45 | return 1; |
46 | } | 46 | } |
47 | 47 | ||
48 | static int | 48 | int |
49 | tls13_accept(struct tls13_ctx *ctx) | 49 | tls13_server_accept(struct tls13_ctx *ctx) |
50 | { | 50 | { |
51 | if (ctx->mode != TLS13_HS_SERVER) | 51 | if (ctx->mode != TLS13_HS_SERVER) |
52 | return TLS13_IO_FAILURE; | 52 | return TLS13_IO_FAILURE; |
@@ -54,97 +54,6 @@ tls13_accept(struct tls13_ctx *ctx) | |||
54 | return tls13_handshake_perform(ctx); | 54 | return tls13_handshake_perform(ctx); |
55 | } | 55 | } |
56 | 56 | ||
57 | int | ||
58 | tls13_legacy_accept(SSL *ssl) | ||
59 | { | ||
60 | struct tls13_ctx *ctx = ssl->internal->tls13; | ||
61 | int ret; | ||
62 | |||
63 | if (ctx == NULL) { | ||
64 | if ((ctx = tls13_ctx_new(TLS13_HS_SERVER)) == NULL) { | ||
65 | SSLerror(ssl, ERR_R_INTERNAL_ERROR); /* XXX */ | ||
66 | return -1; | ||
67 | } | ||
68 | ssl->internal->tls13 = ctx; | ||
69 | ctx->ssl = ssl; | ||
70 | ctx->hs = &S3I(ssl)->hs_tls13; | ||
71 | |||
72 | if (!tls13_server_init(ctx)) { | ||
73 | if (ERR_peek_error() == 0) | ||
74 | SSLerror(ssl, ERR_R_INTERNAL_ERROR); /* XXX */ | ||
75 | return -1; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | ERR_clear_error(); | ||
80 | S3I(ssl)->hs.state = SSL_ST_ACCEPT; | ||
81 | |||
82 | ret = tls13_accept(ctx); | ||
83 | if (ret == TLS13_IO_USE_LEGACY) | ||
84 | return ssl->method->internal->ssl_accept(ssl); | ||
85 | if (ret == TLS13_IO_SUCCESS) | ||
86 | S3I(ssl)->hs.state = SSL_ST_OK; | ||
87 | |||
88 | return tls13_legacy_return_code(ssl, ret); | ||
89 | } | ||
90 | |||
91 | int | ||
92 | tls13_use_legacy_server(struct tls13_ctx *ctx) | ||
93 | { | ||
94 | SSL *s = ctx->ssl; | ||
95 | CBS cbs; | ||
96 | |||
97 | s->method = tls_legacy_server_method(); | ||
98 | s->internal->handshake_func = s->method->internal->ssl_accept; | ||
99 | s->client_version = s->version = s->method->internal->max_version; | ||
100 | s->server = 1; | ||
101 | |||
102 | if (!ssl3_setup_init_buffer(s)) | ||
103 | goto err; | ||
104 | if (!ssl3_setup_buffers(s)) | ||
105 | goto err; | ||
106 | if (!ssl_init_wbio_buffer(s, 0)) | ||
107 | goto err; | ||
108 | |||
109 | if (s->bbio != s->wbio) | ||
110 | s->wbio = BIO_push(s->bbio, s->wbio); | ||
111 | |||
112 | /* Stash any unprocessed data from the last record. */ | ||
113 | tls13_record_layer_rbuf(ctx->rl, &cbs); | ||
114 | if (CBS_len(&cbs) > 0) { | ||
115 | if (!CBS_write_bytes(&cbs, | ||
116 | S3I(s)->rbuf.buf + SSL3_RT_HEADER_LENGTH, | ||
117 | S3I(s)->rbuf.len - SSL3_RT_HEADER_LENGTH, NULL)) | ||
118 | goto err; | ||
119 | |||
120 | S3I(s)->rbuf.offset = SSL3_RT_HEADER_LENGTH; | ||
121 | S3I(s)->rbuf.left = CBS_len(&cbs); | ||
122 | S3I(s)->rrec.type = SSL3_RT_HANDSHAKE; | ||
123 | S3I(s)->rrec.length = CBS_len(&cbs); | ||
124 | s->internal->rstate = SSL_ST_READ_BODY; | ||
125 | s->internal->packet = S3I(s)->rbuf.buf; | ||
126 | s->internal->packet_length = SSL3_RT_HEADER_LENGTH; | ||
127 | s->internal->mac_packet = 1; | ||
128 | } | ||
129 | |||
130 | /* Stash the current handshake message. */ | ||
131 | tls13_handshake_msg_data(ctx->hs_msg, &cbs); | ||
132 | if (!CBS_write_bytes(&cbs, s->internal->init_buf->data, | ||
133 | s->internal->init_buf->length, NULL)) | ||
134 | goto err; | ||
135 | |||
136 | S3I(s)->tmp.reuse_message = 1; | ||
137 | S3I(s)->tmp.message_type = tls13_handshake_msg_type(ctx->hs_msg); | ||
138 | S3I(s)->tmp.message_size = CBS_len(&cbs); | ||
139 | |||
140 | S3I(s)->hs.state = SSL3_ST_SR_CLNT_HELLO_A; | ||
141 | |||
142 | return 1; | ||
143 | |||
144 | err: | ||
145 | return 0; | ||
146 | } | ||
147 | |||
148 | static int | 57 | static int |
149 | tls13_client_hello_is_legacy(CBS *cbs) | 58 | tls13_client_hello_is_legacy(CBS *cbs) |
150 | { | 59 | { |