| 
									
										
										
										
											2016-05-18 02:18:30 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-05-03 18:52:38 +08:00
										 |  |  |  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2000-01-11 09:07:26 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:08:51 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 02:18:30 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							| 
									
										
										
										
											2000-01-11 09:07:26 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2014-05-27 07:26:55 +08:00
										 |  |  | #include <limits.h>
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:40 +08:00
										 |  |  | #include "../ssl_local.h"
 | 
					
						
							| 
									
										
										
										
											1999-04-24 06:13:45 +08:00
										 |  |  | #include <openssl/evp.h>
 | 
					
						
							|  |  |  | #include <openssl/buffer.h>
 | 
					
						
							| 
									
										
										
										
											2009-12-07 21:31:02 +08:00
										 |  |  | #include <openssl/rand.h>
 | 
					
						
							| 
									
										
										
										
											2022-05-06 22:10:00 +08:00
										 |  |  | #include <openssl/core_names.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:40 +08:00
										 |  |  | #include "record_local.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-07 23:32:49 +08:00
										 |  |  | #include "internal/packet.h"
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  | void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s) | 
					
						
							| 
									
										
										
										
											2015-02-02 21:57:12 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     rl->s = s; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-02 19:53:20 +08:00
										 |  |  | void RECORD_LAYER_clear(RECORD_LAYER *rl) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-21 21:06:52 +08:00
										 |  |  |     rl->wnum = 0; | 
					
						
							|  |  |  |     memset(rl->handshake_fragment, 0, sizeof(rl->handshake_fragment)); | 
					
						
							|  |  |  |     rl->handshake_fragment_len = 0; | 
					
						
							|  |  |  |     rl->wpend_tot = 0; | 
					
						
							|  |  |  |     rl->wpend_type = 0; | 
					
						
							|  |  |  |     rl->wpend_ret = 0; | 
					
						
							|  |  |  |     rl->wpend_buf = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  |     if (rl->rrlmethod != NULL) | 
					
						
							|  |  |  |         rl->rrlmethod->free(rl->rrl); /* Ignore return value */ | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     if (rl->wrlmethod != NULL) | 
					
						
							|  |  |  |         rl->wrlmethod->free(rl->wrl); /* Ignore return value */ | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  |     BIO_free(rl->rrlnext); | 
					
						
							|  |  |  |     rl->rrlmethod = NULL; | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     rl->wrlmethod = NULL; | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  |     rl->rrlnext = NULL; | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     rl->rrl = NULL; | 
					
						
							|  |  |  |     rl->wrl = NULL; | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-21 21:06:52 +08:00
										 |  |  |     if (rl->d) | 
					
						
							| 
									
										
										
										
											2015-02-04 00:11:49 +08:00
										 |  |  |         DTLS_RECORD_LAYER_clear(rl); | 
					
						
							| 
									
										
										
										
											2015-02-02 19:53:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 20:41:39 +08:00
										 |  |  | /* Checks if we have unprocessed read ahead data pending */ | 
					
						
							| 
									
										
										
										
											2016-02-12 20:03:58 +08:00
										 |  |  | int RECORD_LAYER_read_pending(const RECORD_LAYER *rl) | 
					
						
							| 
									
										
										
										
											2015-02-02 20:18:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  |     return rl->rrlmethod->unprocessed_read_pending(rl->rrl); | 
					
						
							| 
									
										
										
										
											2015-02-02 20:18:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 20:41:39 +08:00
										 |  |  | /* Checks if we have decrypted unread record data pending */ | 
					
						
							|  |  |  | int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     return (rl->curr_rec < rl->num_recs) | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  |            || rl->rrlmethod->processed_read_pending(rl->rrl); | 
					
						
							| 
									
										
										
										
											2017-03-03 20:41:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-12 20:03:58 +08:00
										 |  |  | int RECORD_LAYER_write_pending(const RECORD_LAYER *rl) | 
					
						
							| 
									
										
										
										
											2015-02-02 20:18:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-25 22:05:13 +08:00
										 |  |  |     return rl->wpend_tot > 0; | 
					
						
							| 
									
										
										
										
											2015-02-02 20:18:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 22:56:16 +08:00
										 |  |  | static uint32_t ossl_get_max_early_data(SSL_CONNECTION *s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint32_t max_early_data; | 
					
						
							|  |  |  |     SSL_SESSION *sess = s->session; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * If we are a client then we always use the max_early_data from the | 
					
						
							|  |  |  |      * session/psksession. Otherwise we go with the lowest out of the max early | 
					
						
							|  |  |  |      * data set in the session and the configured max_early_data. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (!s->server && sess->ext.max_early_data == 0) { | 
					
						
							|  |  |  |         if (!ossl_assert(s->psksession != NULL | 
					
						
							|  |  |  |                          && s->psksession->ext.max_early_data > 0)) { | 
					
						
							|  |  |  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         sess = s->psksession; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!s->server) | 
					
						
							|  |  |  |         max_early_data = sess->ext.max_early_data; | 
					
						
							|  |  |  |     else if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED) | 
					
						
							|  |  |  |         max_early_data = s->recv_max_early_data; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         max_early_data = s->recv_max_early_data < sess->ext.max_early_data | 
					
						
							|  |  |  |                          ? s->recv_max_early_data : sess->ext.max_early_data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return max_early_data; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int ossl_early_data_count_ok(SSL_CONNECTION *s, size_t length, | 
					
						
							|  |  |  |                                     size_t overhead, int send) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint32_t max_early_data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     max_early_data = ossl_get_max_early_data(s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (max_early_data == 0) { | 
					
						
							|  |  |  |         SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE, | 
					
						
							|  |  |  |                  SSL_R_TOO_MUCH_EARLY_DATA); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* If we are dealing with ciphertext we need to allow for the overhead */ | 
					
						
							|  |  |  |     max_early_data += overhead; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (s->early_data_count + length > max_early_data) { | 
					
						
							|  |  |  |         SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE, | 
					
						
							|  |  |  |                  SSL_R_TOO_MUCH_EARLY_DATA); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     s->early_data_count += length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-07 02:17:54 +08:00
										 |  |  | size_t ssl3_pending(const SSL *s) | 
					
						
							| 
									
										
										
										
											2015-02-02 22:03:50 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-10-07 02:17:54 +08:00
										 |  |  |     size_t i, num = 0; | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); | 
					
						
							| 
									
										
										
										
											2016-01-12 22:52:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     if (sc == NULL) | 
					
						
							| 
									
										
										
										
											2015-02-02 22:03:50 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-25 22:59:38 +08:00
										 |  |  |     if (SSL_CONNECTION_IS_DTLS(sc)) { | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |         TLS_RECORD *rdata; | 
					
						
							| 
									
										
										
										
											2022-07-25 22:59:38 +08:00
										 |  |  |         pitem *item, *iter; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         iter = pqueue_iterator(sc->rlayer.d->buffered_app_data.q); | 
					
						
							|  |  |  |         while ((item = pqueue_next(&iter)) != NULL) { | 
					
						
							|  |  |  |             rdata = item->data; | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |             num += rdata->length; | 
					
						
							| 
									
										
										
										
											2022-07-25 22:59:38 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-25 22:59:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |     for (i = 0; i < sc->rlayer.num_recs; i++) { | 
					
						
							|  |  |  |         if (sc->rlayer.tlsrecs[i].type != SSL3_RT_APPLICATION_DATA) | 
					
						
							|  |  |  |             return num; | 
					
						
							|  |  |  |         num += sc->rlayer.tlsrecs[i].length; | 
					
						
							| 
									
										
										
										
											2016-01-12 22:52:35 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  |     num += sc->rlayer.rrlmethod->app_data_pending(sc->rlayer.rrl); | 
					
						
							| 
									
										
										
										
											2022-07-19 22:49:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 22:52:35 +08:00
										 |  |  |     return num; | 
					
						
							| 
									
										
										
										
											2015-02-02 22:03:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-13 22:20:25 +08:00
										 |  |  | void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ctx->default_read_buf_len = len; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SSL_set_default_read_buffer_len(SSL *s, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (sc == NULL) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  |     sc->rlayer.default_read_buf_len = len; | 
					
						
							| 
									
										
										
										
											2016-01-13 22:20:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-03 18:48:28 +08:00
										 |  |  | const char *SSL_rstate_string_long(const SSL *s) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); | 
					
						
							| 
									
										
										
										
											2022-07-22 22:38:26 +08:00
										 |  |  |     const char *lng; | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (sc == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 22:38:26 +08:00
										 |  |  |     if (sc->rlayer.rrlmethod == NULL || sc->rlayer.rrl == NULL) | 
					
						
							| 
									
										
										
										
											2016-03-22 22:52:13 +08:00
										 |  |  |         return "unknown"; | 
					
						
							| 
									
										
										
										
											2022-07-22 22:38:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sc->rlayer.rrlmethod->get_state(sc->rlayer.rrl, NULL, &lng); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return lng; | 
					
						
							| 
									
										
										
										
											2015-02-03 18:48:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const char *SSL_rstate_string(const SSL *s) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); | 
					
						
							| 
									
										
										
										
											2022-07-22 22:38:26 +08:00
										 |  |  |     const char *shrt; | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (sc == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 22:38:26 +08:00
										 |  |  |     if (sc->rlayer.rrlmethod == NULL || sc->rlayer.rrl == NULL) | 
					
						
							| 
									
										
										
										
											2016-03-22 22:52:13 +08:00
										 |  |  |         return "unknown"; | 
					
						
							| 
									
										
										
										
											2015-02-03 18:48:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 22:38:26 +08:00
										 |  |  |     sc->rlayer.rrlmethod->get_state(sc->rlayer.rrl, &shrt, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return shrt; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  | static int tls_write_check_pending(SSL_CONNECTION *s, int type, | 
					
						
							|  |  |  |                                    const unsigned char *buf, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (s->rlayer.wpend_tot == 0) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* We have pending data, so do some sanity checks */ | 
					
						
							|  |  |  |     if ((s->rlayer.wpend_tot > len) | 
					
						
							|  |  |  |         || (!(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) | 
					
						
							|  |  |  |             && (s->rlayer.wpend_buf != buf)) | 
					
						
							|  |  |  |         || (s->rlayer.wpend_type != type)) { | 
					
						
							|  |  |  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_WRITE_RETRY); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Call this to write data in records of type 'type' It will return <= 0 if | 
					
						
							|  |  |  |  * not all data has been sent or non-blocking IO. | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  | int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, size_t len, | 
					
						
							| 
									
										
										
										
											2016-09-07 18:34:39 +08:00
										 |  |  |                      size_t *written) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     const unsigned char *buf = buf_; | 
					
						
							| 
									
										
										
										
											2016-09-07 18:34:39 +08:00
										 |  |  |     size_t tot; | 
					
						
							| 
									
										
										
										
											2017-11-06 00:46:48 +08:00
										 |  |  |     size_t n, max_send_fragment, split_send_fragment, maxpipes; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     int i; | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |     OSSL_RECORD_TEMPLATE tmpls[SSL_MAX_PIPELINES]; | 
					
						
							| 
									
										
										
										
											2022-08-27 00:34:40 +08:00
										 |  |  |     unsigned int recversion; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     if (s == NULL) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     s->rwstate = SSL_NOTHING; | 
					
						
							| 
									
										
										
										
											2015-02-03 19:16:30 +08:00
										 |  |  |     tot = s->rlayer.wnum; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * ensure that if we end up with a smaller value of data to write out | 
					
						
							| 
									
										
										
										
											2017-07-31 06:43:19 +08:00
										 |  |  |      * than the original len from a write which didn't complete for | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |      * non-blocking I/O and also somehow ended up avoiding the check for | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |      * this in tls_write_check_pending/SSL_R_BAD_WRITE_RETRY as it must never be | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |      * possible to end up with (len-tot) as a large number that will then | 
					
						
							|  |  |  |      * promptly send beyond the end of the users buffer ... so we trap and | 
					
						
							|  |  |  |      * report the error in a way the user will notice | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-06-06 23:35:43 +08:00
										 |  |  |     if ((len < s->rlayer.wnum) | 
					
						
							| 
									
										
										
										
											2022-08-25 22:05:13 +08:00
										 |  |  |         || ((s->rlayer.wpend_tot != 0) | 
					
						
							|  |  |  |             && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) { | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH); | 
					
						
							| 
									
										
										
										
											2015-10-31 01:01:01 +08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 21:51:04 +08:00
										 |  |  |     if (s->early_data_state == SSL_EARLY_DATA_WRITING | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |             && !ossl_early_data_count_ok(s, len, 0, 1)) { | 
					
						
							| 
									
										
										
										
											2017-12-04 22:28:35 +08:00
										 |  |  |         /* SSLfatal() already called */ | 
					
						
							| 
									
										
										
										
											2017-02-24 21:51:04 +08:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2017-12-04 22:28:35 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-24 21:51:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 01:01:01 +08:00
										 |  |  |     s->rlayer.wnum = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 18:09:05 +08:00
										 |  |  |     /*
 | 
					
						
							| 
									
										
										
										
											2020-03-17 02:25:58 +08:00
										 |  |  |      * If we are supposed to be sending a KeyUpdate or NewSessionTicket then go | 
					
						
							|  |  |  |      * into init unless we have writes pending - in which case we should finish | 
					
						
							|  |  |  |      * doing that first. | 
					
						
							| 
									
										
										
										
											2019-04-17 18:09:05 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-08-25 22:05:13 +08:00
										 |  |  |     if (s->rlayer.wpend_tot == 0 && (s->key_update != SSL_KEY_UPDATE_NONE | 
					
						
							|  |  |  |                                      || s->ext.extra_tickets_expected > 0)) | 
					
						
							| 
									
										
										
										
											2019-04-17 18:09:05 +08:00
										 |  |  |         ossl_statem_set_in_init(s, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 00:35:55 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * When writing early data on the server side we could be "in_init" in | 
					
						
							|  |  |  |      * between receiving the EoED and the CF - but we don't want to handle those | 
					
						
							|  |  |  |      * messages yet. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     if (SSL_in_init(ssl) && !ossl_statem_get_in_handshake(s) | 
					
						
							| 
									
										
										
										
											2017-03-31 00:35:55 +08:00
										 |  |  |             && s->early_data_state != SSL_EARLY_DATA_UNAUTH_WRITING) { | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |         i = s->handshake_func(ssl); | 
					
						
							| 
									
										
										
										
											2017-12-05 00:16:37 +08:00
										 |  |  |         /* SSLfatal() already called */ | 
					
						
							| 
									
										
										
										
											2015-10-31 01:01:01 +08:00
										 |  |  |         if (i < 0) | 
					
						
							| 
									
										
										
										
											2016-09-07 18:34:39 +08:00
										 |  |  |             return i; | 
					
						
							| 
									
										
										
										
											2015-10-31 01:01:01 +08:00
										 |  |  |         if (i == 0) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |     i = tls_write_check_pending(s, type, buf, len); | 
					
						
							|  |  |  |     if (i < 0) { | 
					
						
							|  |  |  |         /* SSLfatal() already called */ | 
					
						
							|  |  |  |         return i; | 
					
						
							|  |  |  |     } else if (i > 0) { | 
					
						
							|  |  |  |         /* Retry needed */ | 
					
						
							| 
									
										
										
										
											2022-09-01 00:37:48 +08:00
										 |  |  |         i = HANDLE_RLAYER_WRITE_RETURN(s, | 
					
						
							|  |  |  |                 s->rlayer.wrlmethod->retry_write_records(s->rlayer.wrl)); | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |         if (i <= 0) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             return i; | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |         tot += s->rlayer.wpend_tot; | 
					
						
							|  |  |  |         s->rlayer.wpend_tot = 0; | 
					
						
							|  |  |  |     } /* else no retry required */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (tot == 0) { | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * We've not previously sent any data for this write so memorize | 
					
						
							|  |  |  |          * arguments so that we can detect bad write retries later | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         s->rlayer.wpend_tot = 0; | 
					
						
							|  |  |  |         s->rlayer.wpend_type = type; | 
					
						
							|  |  |  |         s->rlayer.wpend_buf = buf; | 
					
						
							|  |  |  |         s->rlayer.wpend_ret = len; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (tot == len) {           /* done? */ | 
					
						
							| 
									
										
										
										
											2016-09-07 18:34:39 +08:00
										 |  |  |         *written = tot; | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-22 22:49:53 +08:00
										 |  |  |     /* If we have an alert to send, lets send it */ | 
					
						
							| 
									
										
										
										
											2022-10-27 22:38:32 +08:00
										 |  |  |     if (s->s3.alert_dispatch > 0) { | 
					
						
							| 
									
										
										
										
											2022-08-22 22:49:53 +08:00
										 |  |  |         i = ssl->method->ssl_dispatch_alert(ssl); | 
					
						
							|  |  |  |         if (i <= 0) { | 
					
						
							|  |  |  |             /* SSLfatal() already called if appropriate */ | 
					
						
							|  |  |  |             return i; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /* if it went, fall through and send more stuff */ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     n = (len - tot); | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-06 00:46:48 +08:00
										 |  |  |     max_send_fragment = ssl_get_max_send_fragment(s); | 
					
						
							|  |  |  |     split_send_fragment = ssl_get_split_send_fragment(s); | 
					
						
							| 
									
										
										
										
											2022-09-09 22:34:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Add "origin" field to EVP_CIPHER, EVP_MD
Add a "where did this EVP_{CIPHER,MD} come from" flag: global, via fetch,
or via EVP_{CIPHER,MD}_meth_new.  Update EVP_{CIPHER,MD}_free to handle all
three origins. The flag is deliberately right before some function pointers,
so that compile-time failures (int/pointer) will occur, as opposed to
taking a bit in the existing "flags" field.  The "global variable" flag
is non-zero, so the default case of using OPENSSL_zalloc (for provider
ciphers), will do the right thing. Ref-counting is a no-op for
Make up_ref no-op for global MD and CIPHER objects
Deprecate EVP_MD_CTX_md().  Added EVP_MD_CTX_get0_md() (same semantics as
the deprecated function) and EVP_MD_CTX_get1_md().  Likewise, deprecate
EVP_CIPHER_CTX_cipher() in favor of EVP_CIPHER_CTX_get0_cipher(), and add
EVP_CIPHER_CTX_get1_CIPHER().
Refactor EVP_MD_free() and EVP_MD_meth_free() to call new common
evp_md_free_int() function.
Refactor EVP_CIPHER_free() and EVP_CIPHER_meth_free() to call new common
evp_cipher_free_int() function.
Also change some flags tests to explicit test == or != zero. E.g.,
        if (flags & x) --> if ((flags & x) != 0)
        if (!(flags & x)) --> if ((flags & x) == 0)
Only done for those lines where "get0_cipher" calls were made.
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14193)
											
										 
											2021-02-17 06:51:56 +08:00
										 |  |  |     if (max_send_fragment == 0 | 
					
						
							|  |  |  |             || split_send_fragment == 0 | 
					
						
							|  |  |  |             || split_send_fragment > max_send_fragment) { | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  |         /*
 | 
					
						
							| 
									
										
										
										
											2017-11-06 00:46:48 +08:00
										 |  |  |          * We should have prevented this when we set/get the split and max send | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  |          * fragments so we shouldn't get here | 
					
						
							| 
									
										
										
										
											2016-08-06 01:03:17 +08:00
										 |  |  |          */ | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 00:34:40 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Some servers hang if initial client hello is larger than 256 bytes | 
					
						
							|  |  |  |      * and record version number > TLS 1.0 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     recversion = (s->version == TLS1_3_VERSION) ? TLS1_2_VERSION : s->version; | 
					
						
							|  |  |  |     if (SSL_get_state(ssl) == TLS_ST_CW_CLNT_HELLO | 
					
						
							|  |  |  |             && !s->renegotiate | 
					
						
							|  |  |  |             && TLS1_get_version(ssl) > TLS1_VERSION | 
					
						
							|  |  |  |             && s->hello_retry_request == SSL_HRR_NONE) | 
					
						
							|  |  |  |         recversion = TLS1_VERSION; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     for (;;) { | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |         size_t tmppipelen, remain; | 
					
						
							| 
									
										
										
										
											2022-09-09 22:53:40 +08:00
										 |  |  |         size_t j, lensofar = 0; | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-09 22:53:40 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |         * Ask the record layer how it would like to split the amount of data | 
					
						
							|  |  |  |         * that we have, and how many of those records it would like in one go. | 
					
						
							|  |  |  |         */ | 
					
						
							|  |  |  |         maxpipes = s->rlayer.wrlmethod->get_max_records(s->rlayer.wrl, type, n, | 
					
						
							|  |  |  |                                                         max_send_fragment, | 
					
						
							|  |  |  |                                                         &split_send_fragment); | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |         * If max_pipelines is 0 then this means "undefined" and we default to | 
					
						
							|  |  |  |         * whatever the record layer wants to do. Otherwise we use the smallest | 
					
						
							|  |  |  |         * value from the number requested by the record layer, and max number | 
					
						
							|  |  |  |         * configured by the user. | 
					
						
							|  |  |  |         */ | 
					
						
							|  |  |  |         if (s->max_pipelines > 0 && maxpipes > s->max_pipelines) | 
					
						
							|  |  |  |             maxpipes = s->max_pipelines; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (maxpipes > SSL_MAX_PIPELINES) | 
					
						
							|  |  |  |             maxpipes = SSL_MAX_PIPELINES; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (split_send_fragment > max_send_fragment) { | 
					
						
							|  |  |  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-09 22:53:40 +08:00
										 |  |  |         if (n / maxpipes >= split_send_fragment) { | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  |             /*
 | 
					
						
							|  |  |  |              * We have enough data to completely fill all available | 
					
						
							|  |  |  |              * pipelines | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2022-09-09 22:53:40 +08:00
										 |  |  |             for (j = 0; j < maxpipes; j++) { | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |                 tmpls[j].type = type; | 
					
						
							| 
									
										
										
										
											2022-08-27 00:34:40 +08:00
										 |  |  |                 tmpls[j].version = recversion; | 
					
						
							| 
									
										
										
										
											2022-09-01 04:03:22 +08:00
										 |  |  |                 tmpls[j].buf = &(buf[tot]) + (j * split_send_fragment); | 
					
						
							|  |  |  |                 tmpls[j].buflen = split_send_fragment; | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |             /* Remember how much data we are going to be sending */ | 
					
						
							| 
									
										
										
										
											2022-09-09 22:53:40 +08:00
										 |  |  |             s->rlayer.wpend_tot = maxpipes * split_send_fragment; | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             /* We can partially fill all available pipelines */ | 
					
						
							| 
									
										
										
										
											2022-09-09 22:53:40 +08:00
										 |  |  |             tmppipelen = n / maxpipes; | 
					
						
							|  |  |  |             remain = n % maxpipes; | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |             /*
 | 
					
						
							|  |  |  |              * If there is a remainder we add an extra byte to the first few | 
					
						
							|  |  |  |              * pipelines | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             if (remain > 0) | 
					
						
							|  |  |  |                 tmppipelen++; | 
					
						
							| 
									
										
										
										
											2022-09-09 22:53:40 +08:00
										 |  |  |             for (j = 0; j < maxpipes; j++) { | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |                 tmpls[j].type = type; | 
					
						
							| 
									
										
										
										
											2022-08-27 00:34:40 +08:00
										 |  |  |                 tmpls[j].version = recversion; | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |                 tmpls[j].buf = &(buf[tot]) + lensofar; | 
					
						
							|  |  |  |                 tmpls[j].buflen = tmppipelen; | 
					
						
							|  |  |  |                 lensofar += tmppipelen; | 
					
						
							|  |  |  |                 if (j + 1 == remain) | 
					
						
							|  |  |  |                     tmppipelen--; | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |             /* Remember how much data we are going to be sending */ | 
					
						
							|  |  |  |             s->rlayer.wpend_tot = n; | 
					
						
							| 
									
										
										
										
											2015-09-22 18:12:50 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-01 00:37:48 +08:00
										 |  |  |         i = HANDLE_RLAYER_WRITE_RETURN(s, | 
					
						
							| 
									
										
										
										
											2022-09-09 22:53:40 +08:00
										 |  |  |             s->rlayer.wrlmethod->write_records(s->rlayer.wrl, tmpls, maxpipes)); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         if (i <= 0) { | 
					
						
							| 
									
										
										
										
											2017-12-05 00:16:37 +08:00
										 |  |  |             /* SSLfatal() already called if appropriate */ | 
					
						
							| 
									
										
										
										
											2015-02-03 19:16:30 +08:00
										 |  |  |             s->rlayer.wnum = tot; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             return i; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-25 22:05:13 +08:00
										 |  |  |         if (s->rlayer.wpend_tot == n | 
					
						
							|  |  |  |                 || (type == SSL3_RT_APPLICATION_DATA | 
					
						
							|  |  |  |                     && (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0)) { | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |             *written = tot + s->rlayer.wpend_tot; | 
					
						
							|  |  |  |             s->rlayer.wpend_tot = 0; | 
					
						
							| 
									
										
										
										
											2016-09-07 18:34:39 +08:00
										 |  |  |             return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 16:51:51 +08:00
										 |  |  |         n -= s->rlayer.wpend_tot; | 
					
						
							|  |  |  |         tot += s->rlayer.wpend_tot; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-01 00:37:48 +08:00
										 |  |  | int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int writing, int ret, | 
					
						
							|  |  |  |                                   char *file, int line) | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ret == OSSL_RECORD_RETURN_RETRY) { | 
					
						
							| 
									
										
										
										
											2022-09-01 00:37:48 +08:00
										 |  |  |         s->rwstate = writing ? SSL_WRITING : SSL_READING; | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         ret = -1; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         s->rwstate = SSL_NOTHING; | 
					
						
							|  |  |  |         if (ret == OSSL_RECORD_RETURN_EOF) { | 
					
						
							| 
									
										
										
										
											2022-09-01 00:37:48 +08:00
										 |  |  |             if (writing) { | 
					
						
							|  |  |  |                 /*
 | 
					
						
							|  |  |  |                  * This shouldn't happen with a writing operation. We treat it | 
					
						
							|  |  |  |                  * as fatal. | 
					
						
							|  |  |  |                  */ | 
					
						
							|  |  |  |                 ERR_new(); | 
					
						
							|  |  |  |                 ERR_set_debug(file, line, 0); | 
					
						
							|  |  |  |                 ossl_statem_fatal(s, SSL_AD_INTERNAL_ERROR, | 
					
						
							|  |  |  |                                   ERR_R_INTERNAL_ERROR, NULL); | 
					
						
							|  |  |  |                 ret = OSSL_RECORD_RETURN_FATAL; | 
					
						
							|  |  |  |             } else if ((s->options & SSL_OP_IGNORE_UNEXPECTED_EOF) != 0) { | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |                 SSL_set_shutdown(ssl, SSL_RECEIVED_SHUTDOWN); | 
					
						
							|  |  |  |                 s->s3.warn_alert = SSL_AD_CLOSE_NOTIFY; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 ERR_new(); | 
					
						
							|  |  |  |                 ERR_set_debug(file, line, 0); | 
					
						
							|  |  |  |                 ossl_statem_fatal(s, SSL_AD_DECODE_ERROR, | 
					
						
							|  |  |  |                                   SSL_R_UNEXPECTED_EOF_WHILE_READING, NULL); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else if (ret == OSSL_RECORD_RETURN_FATAL) { | 
					
						
							| 
									
										
										
										
											2022-07-26 22:25:03 +08:00
										 |  |  |             int al = s->rlayer.rrlmethod->get_alert_code(s->rlayer.rrl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (al != SSL_AD_NO_ALERT) { | 
					
						
							|  |  |  |                 ERR_new(); | 
					
						
							|  |  |  |                 ERR_set_debug(file, line, 0); | 
					
						
							|  |  |  |                 ossl_statem_fatal(s, al, SSL_R_RECORD_LAYER_FAILURE, NULL); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * else some failure but there is no alert code. We don't log an | 
					
						
							|  |  |  |              * error for this. The record layer should have logged an error | 
					
						
							|  |  |  |              * already or, if not, its due to some sys call error which will be | 
					
						
							|  |  |  |              * reported via SSL_ERROR_SYSCALL and errno. | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * The record layer distinguishes the cases of EOF, non-fatal | 
					
						
							|  |  |  |          * err and retry. Upper layers do not. | 
					
						
							|  |  |  |          * If we got a retry or success then *ret is already correct, | 
					
						
							|  |  |  |          * otherwise we need to convert the return value. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         if (ret == OSSL_RECORD_RETURN_NON_FATAL_ERR || ret == OSSL_RECORD_RETURN_EOF) | 
					
						
							|  |  |  |             ret = 0; | 
					
						
							|  |  |  |         else if (ret < OSSL_RECORD_RETURN_NON_FATAL_ERR) | 
					
						
							|  |  |  |             ret = -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  | void ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (rr->rechandle != NULL) { | 
					
						
							|  |  |  |         /* The record layer allocated the buffers for this record */ | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  |         s->rlayer.rrlmethod->release_record(s->rlayer.rrl, rr->rechandle); | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         /* We allocated the buffers for this record (only happens with DTLS) */ | 
					
						
							|  |  |  |         OPENSSL_free(rr->data); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     s->rlayer.curr_rec++; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 10:48:40 +08:00
										 |  |  | /*-
 | 
					
						
							|  |  |  |  * Return up to 'len' payload bytes received in 'type' records. | 
					
						
							| 
									
										
										
										
											2000-02-21 07:04:06 +08:00
										 |  |  |  * 'type' is one of the following: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *   -  SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) | 
					
						
							|  |  |  |  *   -  SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) | 
					
						
							|  |  |  |  *   -  0 (during a shutdown, no data has to be returned) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * If we don't have stored data to work from, read a SSL/TLS record first | 
					
						
							|  |  |  |  * (possibly multiple records if we still don't have anything to return). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This function must handle any surprises the peer may have for us, such as | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:41 +08:00
										 |  |  |  * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec | 
					
						
							| 
									
										
										
										
											2021-09-25 18:06:15 +08:00
										 |  |  |  * messages are treated as if they were handshake messages *if* the |recvd_type| | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:41 +08:00
										 |  |  |  * argument is non NULL. | 
					
						
							| 
									
										
										
										
											2000-02-21 07:04:06 +08:00
										 |  |  |  * Also if record payloads contain fragments too small to process, we store | 
					
						
							|  |  |  |  * them until there is enough for the respective protocol (the record protocol | 
					
						
							|  |  |  |  * may use arbitrary fragmentation and even interleaving): | 
					
						
							|  |  |  |  *     Change cipher spec protocol | 
					
						
							|  |  |  |  *             just 1 byte needed, no need for keeping anything stored | 
					
						
							|  |  |  |  *     Alert protocol | 
					
						
							|  |  |  |  *             2 bytes needed (AlertLevel, AlertDescription) | 
					
						
							|  |  |  |  *     Handshake protocol | 
					
						
							|  |  |  |  *             4 bytes needed (HandshakeType, uint24 length) -- we just have | 
					
						
							|  |  |  |  *             to detect unexpected Client Hello and Hello Request messages | 
					
						
							|  |  |  |  *             here, anything else is handled by higher layers | 
					
						
							|  |  |  |  *     Application data protocol | 
					
						
							|  |  |  |  *             none of our business | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  | int ssl3_read_bytes(SSL *ssl, int type, int *recvd_type, unsigned char *buf, | 
					
						
							| 
									
										
										
										
											2016-10-26 17:43:34 +08:00
										 |  |  |                     size_t len, int peek, size_t *readbytes) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |     int i, j, ret; | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     size_t n, curr_rec, totalbytes; | 
					
						
							|  |  |  |     TLS_RECORD *rr; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     void (*cb) (const SSL *ssl, int type2, int val) = NULL; | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     int is_tls13; | 
					
						
							|  |  |  |     SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     is_tls13 = SSL_CONNECTION_IS_TLS13(s); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     if ((type != 0 | 
					
						
							|  |  |  |             && (type != SSL3_RT_APPLICATION_DATA) | 
					
						
							|  |  |  |             && (type != SSL3_RT_HANDSHAKE)) | 
					
						
							|  |  |  |         || (peek && (type != SSL3_RT_APPLICATION_DATA))) { | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-03 21:12:22 +08:00
										 |  |  |     if ((type == SSL3_RT_HANDSHAKE) && (s->rlayer.handshake_fragment_len > 0)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         /* (partially) satisfy request from storage */ | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-02-03 21:12:22 +08:00
										 |  |  |         unsigned char *src = s->rlayer.handshake_fragment; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         unsigned char *dst = buf; | 
					
						
							|  |  |  |         unsigned int k; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* peek == 0 */ | 
					
						
							|  |  |  |         n = 0; | 
					
						
							| 
									
										
										
										
											2015-02-03 21:12:22 +08:00
										 |  |  |         while ((len > 0) && (s->rlayer.handshake_fragment_len > 0)) { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             *dst++ = *src++; | 
					
						
							|  |  |  |             len--; | 
					
						
							| 
									
										
										
										
											2015-02-03 21:12:22 +08:00
										 |  |  |             s->rlayer.handshake_fragment_len--; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             n++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /* move any remaining fragment bytes: */ | 
					
						
							| 
									
										
										
										
											2015-02-03 21:12:22 +08:00
										 |  |  |         for (k = 0; k < s->rlayer.handshake_fragment_len; k++) | 
					
						
							|  |  |  |             s->rlayer.handshake_fragment[k] = *src++; | 
					
						
							| 
									
										
										
										
											2015-06-30 18:30:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (recvd_type != NULL) | 
					
						
							|  |  |  |             *recvd_type = SSL3_RT_HANDSHAKE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 17:43:34 +08:00
										 |  |  |         *readbytes = n; | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							| 
									
										
										
										
											2015-02-03 21:12:22 +08:00
										 |  |  |      * Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     if (!ossl_statem_get_in_handshake(s) && SSL_in_init(ssl)) { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         /* type == SSL3_RT_APPLICATION_DATA */ | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |         i = s->handshake_func(ssl); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |         /* SSLfatal() already called */ | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         if (i < 0) | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |             return i; | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |         if (i == 0) | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |  start: | 
					
						
							|  |  |  |     s->rwstate = SSL_NOTHING; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-05 19:30:03 +08:00
										 |  |  |     /*-
 | 
					
						
							| 
									
										
										
										
											2016-01-12 22:52:35 +08:00
										 |  |  |      * For each record 'i' up to |num_recs] | 
					
						
							|  |  |  |      * rr[i].type     - is the type of record | 
					
						
							|  |  |  |      * rr[i].data,    - data | 
					
						
							|  |  |  |      * rr[i].off,     - offset into 'data' for next read | 
					
						
							|  |  |  |      * rr[i].length,  - number of bytes. | 
					
						
							| 
									
										
										
										
											2015-01-05 19:30:03 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     /* get new records if necessary */ | 
					
						
							|  |  |  |     if (s->rlayer.curr_rec >= s->rlayer.num_recs) { | 
					
						
							|  |  |  |         s->rlayer.curr_rec = s->rlayer.num_recs = 0; | 
					
						
							|  |  |  |         do { | 
					
						
							|  |  |  |             rr = &s->rlayer.tlsrecs[s->rlayer.num_recs]; | 
					
						
							| 
									
										
										
										
											2016-01-12 22:52:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-01 00:37:48 +08:00
										 |  |  |             ret = HANDLE_RLAYER_READ_RETURN(s, | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  |                     s->rlayer.rrlmethod->read_record(s->rlayer.rrl, | 
					
						
							|  |  |  |                                                      &rr->rechandle, | 
					
						
							|  |  |  |                                                      &rr->version, &rr->type, | 
					
						
							|  |  |  |                                                      &rr->data, &rr->length, | 
					
						
							|  |  |  |                                                      NULL, NULL)); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |             if (ret <= 0) { | 
					
						
							|  |  |  |                 /* SSLfatal() already called if appropriate */ | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |                 return ret; | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |             rr->off = 0; | 
					
						
							|  |  |  |             s->rlayer.num_recs++; | 
					
						
							| 
									
										
										
										
											2022-07-22 19:39:24 +08:00
										 |  |  |         } while (s->rlayer.rrlmethod->processed_read_pending(s->rlayer.rrl) | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |                  && s->rlayer.num_recs < SSL_MAX_PIPELINES); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     rr = &s->rlayer.tlsrecs[s->rlayer.curr_rec]; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 00:36:32 +08:00
										 |  |  |     if (s->rlayer.handshake_fragment_len > 0 | 
					
						
							| 
									
										
										
										
											2022-07-22 00:01:54 +08:00
										 |  |  |             && rr->type != SSL3_RT_HANDSHAKE | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |             && SSL_CONNECTION_IS_TLS13(s)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, | 
					
						
							| 
									
										
										
										
											2019-02-09 00:36:32 +08:00
										 |  |  |                  SSL_R_MIXED_HANDSHAKE_AND_NON_HANDSHAKE_DATA); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 21:07:31 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Reset the count of consecutive warning alerts if we've got a non-empty | 
					
						
							|  |  |  |      * record that isn't an alert. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     if (rr->type != SSL3_RT_ALERT && rr->length != 0) | 
					
						
							| 
									
										
										
										
											2016-09-21 21:07:31 +08:00
										 |  |  |         s->rlayer.alert_count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* we now have a packet which can be read and processed */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 02:09:50 +08:00
										 |  |  |     if (s->s3.change_cipher_spec /* set when we receive ChangeCipherSpec,
 | 
					
						
							|  |  |  |                                   * reset by ssl3_get_finished */ | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         && (rr->type != SSL3_RT_HANDSHAKE)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |                  SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * If the other end has shut down, throw anything we read away (even in | 
					
						
							|  |  |  |      * 'peek' mode) | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         s->rlayer.curr_rec++; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         s->rwstate = SSL_NOTHING; | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     if (type == rr->type | 
					
						
							|  |  |  |         || (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC | 
					
						
							| 
									
										
										
										
											2017-01-07 00:22:23 +08:00
										 |  |  |             && type == SSL3_RT_HANDSHAKE && recvd_type != NULL | 
					
						
							| 
									
										
										
										
											2018-06-09 00:18:03 +08:00
										 |  |  |             && !is_tls13)) { | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:41 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * SSL3_RT_APPLICATION_DATA or | 
					
						
							|  |  |  |          * SSL3_RT_HANDSHAKE or | 
					
						
							|  |  |  |          * SSL3_RT_CHANGE_CIPHER_SPEC | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * make sure that we are not getting application data when we are | 
					
						
							|  |  |  |          * doing a handshake for the first time | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |         if (SSL_in_init(ssl) && type == SSL3_RT_APPLICATION_DATA | 
					
						
							| 
									
										
										
										
											2022-11-01 00:04:08 +08:00
										 |  |  |                 && SSL_IS_FIRST_HANDSHAKE(s)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_APP_DATA_IN_HANDSHAKE); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:41 +08:00
										 |  |  |         if (type == SSL3_RT_HANDSHAKE | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |             && rr->type == SSL3_RT_CHANGE_CIPHER_SPEC | 
					
						
							| 
									
										
										
										
											2016-08-06 01:03:17 +08:00
										 |  |  |             && s->rlayer.handshake_fragment_len > 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:41 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (recvd_type != NULL) | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |             *recvd_type = rr->type; | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 00:52:06 +08:00
										 |  |  |         if (len == 0) { | 
					
						
							|  |  |  |             /*
 | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |              * Skip a zero length record. This ensures multiple calls to | 
					
						
							| 
									
										
										
										
											2017-11-07 00:52:06 +08:00
										 |  |  |              * SSL_read() with a zero length buffer will eventually cause | 
					
						
							|  |  |  |              * SSL_pending() to report data as being available. | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |             if (rr->length == 0) | 
					
						
							|  |  |  |                 ssl_release_record(s, rr); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2017-11-07 00:52:06 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 17:43:34 +08:00
										 |  |  |         totalbytes = 0; | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         curr_rec = s->rlayer.curr_rec; | 
					
						
							| 
									
										
										
										
											2016-01-12 22:52:35 +08:00
										 |  |  |         do { | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |             if (len - totalbytes > rr->length) | 
					
						
							|  |  |  |                 n = rr->length; | 
					
						
							| 
									
										
										
										
											2016-01-12 22:52:35 +08:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2016-10-26 17:43:34 +08:00
										 |  |  |                 n = len - totalbytes; | 
					
						
							| 
									
										
										
										
											2016-01-12 22:52:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             memcpy(buf, &(rr->data[rr->off]), n); | 
					
						
							|  |  |  |             buf += n; | 
					
						
							| 
									
										
										
										
											2016-09-11 04:24:40 +08:00
										 |  |  |             if (peek) { | 
					
						
							|  |  |  |                 /* Mark any zero length record as consumed CVE-2016-6305 */ | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |                 if (rr->length == 0) | 
					
						
							|  |  |  |                     ssl_release_record(s, rr); | 
					
						
							| 
									
										
										
										
											2016-09-11 04:24:40 +08:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2020-06-23 18:14:41 +08:00
										 |  |  |                 if (s->options & SSL_OP_CLEANSE_PLAINTEXT) | 
					
						
							|  |  |  |                     OPENSSL_cleanse(&(rr->data[rr->off]), n); | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |                 rr->length -= n; | 
					
						
							|  |  |  |                 rr->off += n; | 
					
						
							| 
									
										
										
										
											2022-07-21 22:39:39 +08:00
										 |  |  |                 if (rr->length == 0) | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |                     ssl_release_record(s, rr); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |             if (rr->length == 0 | 
					
						
							|  |  |  |                 || (peek && n == rr->length)) { | 
					
						
							| 
									
										
										
										
											2016-01-12 22:52:35 +08:00
										 |  |  |                 rr++; | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |                 curr_rec++; | 
					
						
							| 
									
										
										
										
											2016-01-12 22:52:35 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-10-26 17:43:34 +08:00
										 |  |  |             totalbytes += n; | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         } while (type == SSL3_RT_APPLICATION_DATA | 
					
						
							|  |  |  |                     && curr_rec < s->rlayer.num_recs | 
					
						
							|  |  |  |                     && totalbytes < len); | 
					
						
							| 
									
										
										
										
											2016-10-26 17:43:34 +08:00
										 |  |  |         if (totalbytes == 0) { | 
					
						
							| 
									
										
										
										
											2016-06-01 23:31:11 +08:00
										 |  |  |             /* We must have read empty records. Get more data */ | 
					
						
							|  |  |  |             goto start; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-26 17:43:34 +08:00
										 |  |  |         *readbytes = totalbytes; | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * If we get here, then type != rr->type; if we have a handshake message, | 
					
						
							| 
									
										
										
										
											2015-05-11 16:35:41 +08:00
										 |  |  |      * then it was unexpected (Hello Request or Client Hello) or invalid (we | 
					
						
							|  |  |  |      * were actually expecting a CCS). | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-28 07:01:51 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Lets just double check that we've not got an SSLv2 record | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     if (rr->version == SSL2_VERSION) { | 
					
						
							| 
									
										
										
										
											2015-03-28 07:01:51 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * Should never happen. ssl3_get_record() should only give us an SSLv2 | 
					
						
							|  |  |  |          * record back if this is the first packet and we are looking for an | 
					
						
							|  |  |  |          * initial ClientHello. Therefore |type| should always be equal to | 
					
						
							|  |  |  |          * |rr->type|. If not then something has gone horribly wrong | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2015-03-28 07:01:51 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |     if (ssl->method->version == TLS_ANY_VERSION | 
					
						
							| 
									
										
										
										
											2016-08-06 01:03:17 +08:00
										 |  |  |         && (s->server || rr->type != SSL3_RT_ALERT)) { | 
					
						
							| 
									
										
										
										
											2015-03-31 07:18:31 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * If we've got this far and still haven't decided on what version | 
					
						
							| 
									
										
										
										
											2016-11-15 21:55:40 +08:00
										 |  |  |          * we're using then this must be a client side alert we're dealing | 
					
						
							|  |  |  |          * with. We shouldn't be receiving anything other than a ClientHello | 
					
						
							|  |  |  |          * if we are a server. | 
					
						
							| 
									
										
										
										
											2015-03-31 07:18:31 +08:00
										 |  |  |          */ | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         s->version = rr->version; | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2015-03-31 07:18:31 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-05 19:30:03 +08:00
										 |  |  |     /*-
 | 
					
						
							| 
									
										
										
										
											2015-02-03 21:12:22 +08:00
										 |  |  |      * s->rlayer.handshake_fragment_len == 4  iff  rr->type == SSL3_RT_HANDSHAKE; | 
					
						
							| 
									
										
										
										
											2015-01-05 19:30:03 +08:00
										 |  |  |      * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     if (rr->type == SSL3_RT_ALERT) { | 
					
						
							| 
									
										
										
										
											2017-05-15 18:24:24 +08:00
										 |  |  |         unsigned int alert_level, alert_descr; | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         unsigned char *alert_bytes = rr->data | 
					
						
							|  |  |  |                                      + rr->off; | 
					
						
							| 
									
										
										
										
											2017-05-15 18:24:24 +08:00
										 |  |  |         PACKET alert; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         if (!PACKET_buf_init(&alert, alert_bytes, rr->length) | 
					
						
							| 
									
										
										
										
											2017-05-15 18:24:24 +08:00
										 |  |  |                 || !PACKET_get_1(&alert, &alert_level) | 
					
						
							|  |  |  |                 || !PACKET_get_1(&alert, &alert_descr) | 
					
						
							|  |  |  |                 || PACKET_remaining(&alert) != 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2017-05-15 18:24:24 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (s->msg_callback) | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |             s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, ssl, | 
					
						
							| 
									
										
										
										
											2015-02-03 21:12:22 +08:00
										 |  |  |                             s->msg_callback_arg); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (s->info_callback != NULL) | 
					
						
							|  |  |  |             cb = s->info_callback; | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |         else if (ssl->ctx->info_callback != NULL) | 
					
						
							|  |  |  |             cb = ssl->ctx->info_callback; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (cb != NULL) { | 
					
						
							|  |  |  |             j = (alert_level << 8) | alert_descr; | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |             cb(ssl, SSL_CB_READ_ALERT, j); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         if ((!is_tls13 && alert_level == SSL3_AL_WARNING) | 
					
						
							| 
									
										
										
										
											2018-06-09 00:18:03 +08:00
										 |  |  |                 || (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED)) { | 
					
						
							| 
									
										
										
										
											2018-12-13 02:09:50 +08:00
										 |  |  |             s->s3.warn_alert = alert_descr; | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |             ssl_release_record(s, rr); | 
					
						
							| 
									
										
										
										
											2016-09-21 21:07:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             s->rlayer.alert_count++; | 
					
						
							|  |  |  |             if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) { | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |                 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |                          SSL_R_TOO_MANY_WARN_ALERTS); | 
					
						
							|  |  |  |                 return -1; | 
					
						
							| 
									
										
										
										
											2016-09-21 21:07:31 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-05-18 16:07:42 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /*
 | 
					
						
							| 
									
										
										
										
											2018-06-09 00:18:03 +08:00
										 |  |  |          * Apart from close_notify the only other warning alert in TLSv1.3 | 
					
						
							|  |  |  |          * is user_cancelled - which we just ignore. | 
					
						
							| 
									
										
										
										
											2018-05-18 16:07:42 +08:00
										 |  |  |          */ | 
					
						
							| 
									
										
										
										
											2018-06-09 00:18:03 +08:00
										 |  |  |         if (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED) { | 
					
						
							|  |  |  |             goto start; | 
					
						
							|  |  |  |         } else if (alert_descr == SSL_AD_CLOSE_NOTIFY | 
					
						
							|  |  |  |                 && (is_tls13 || alert_level == SSL3_AL_WARNING)) { | 
					
						
							|  |  |  |             s->shutdown |= SSL_RECEIVED_SHUTDOWN; | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } else if (alert_level == SSL3_AL_FATAL || is_tls13) { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             s->rwstate = SSL_NOTHING; | 
					
						
							| 
									
										
										
										
											2018-12-13 02:09:50 +08:00
										 |  |  |             s->s3.fatal_alert = alert_descr; | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |             SSLfatal_data(s, SSL_AD_NO_ALERT, | 
					
						
							|  |  |  |                           SSL_AD_REASON_OFFSET + alert_descr, | 
					
						
							|  |  |  |                           "SSL alert number %d", alert_descr); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             s->shutdown |= SSL_RECEIVED_SHUTDOWN; | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |             ssl_release_record(s, rr); | 
					
						
							| 
									
										
										
										
											2016-05-27 01:49:36 +08:00
										 |  |  |             SSL_CTX_remove_session(s->session_ctx, s->session); | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2018-06-09 00:18:03 +08:00
										 |  |  |         } else if (alert_descr == SSL_AD_NO_RENEGOTIATION) { | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * This is a warning but we receive it if we requested | 
					
						
							|  |  |  |              * renegotiation and the peer denied it. Terminate with a fatal | 
					
						
							|  |  |  |              * alert because if application tried to renegotiate it | 
					
						
							|  |  |  |              * presumably had a good reason and expects it to succeed. In | 
					
						
							|  |  |  |              * future we might have a renegotiation where we don't care if | 
					
						
							|  |  |  |              * the peer refused it where we carry on. | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_RENEGOTIATION); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2018-06-18 19:06:52 +08:00
										 |  |  |         } else if (alert_level == SSL3_AL_WARNING) { | 
					
						
							|  |  |  |             /* We ignore any other warning alert in TLSv1.2 and below */ | 
					
						
							|  |  |  |             goto start; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-09 00:18:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE); | 
					
						
							| 
									
										
										
										
											2018-06-09 00:18:03 +08:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 20:30:38 +08:00
										 |  |  |     if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) { | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         if (rr->type == SSL3_RT_HANDSHAKE) { | 
					
						
							| 
									
										
										
										
											2018-06-21 20:30:38 +08:00
										 |  |  |             BIO *rbio; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 20:36:55 +08:00
										 |  |  |             /*
 | 
					
						
							|  |  |  |              * We ignore any handshake messages sent to us unless they are | 
					
						
							|  |  |  |              * TLSv1.3 in which case we want to process them. For all other | 
					
						
							|  |  |  |              * handshake messages we can't do anything reasonable with them | 
					
						
							|  |  |  |              * because we are unable to write any response due to having already | 
					
						
							|  |  |  |              * sent close_notify. | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |             if (!SSL_CONNECTION_IS_TLS13(s)) { | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |                 ssl_release_record(s, rr); | 
					
						
							| 
									
										
										
										
											2018-09-04 20:36:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if ((s->mode & SSL_MODE_AUTO_RETRY) != 0) | 
					
						
							|  |  |  |                     goto start; | 
					
						
							| 
									
										
										
										
											2018-06-21 20:30:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 20:36:55 +08:00
										 |  |  |                 s->rwstate = SSL_READING; | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |                 rbio = SSL_get_rbio(ssl); | 
					
						
							| 
									
										
										
										
											2018-09-04 20:36:55 +08:00
										 |  |  |                 BIO_clear_retry_flags(rbio); | 
					
						
							|  |  |  |                 BIO_set_retry_read(rbio); | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-06-25 21:51:11 +08:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * The peer is continuing to send application data, but we have | 
					
						
							|  |  |  |              * already sent close_notify. If this was expected we should have | 
					
						
							|  |  |  |              * been called via SSL_read() and this would have been handled | 
					
						
							|  |  |  |              * above. | 
					
						
							|  |  |  |              * No alert sent because we already sent close_notify | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |             ssl_release_record(s, rr); | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |             SSLfatal(s, SSL_AD_NO_ALERT, | 
					
						
							| 
									
										
										
										
											2018-06-25 21:51:11 +08:00
										 |  |  |                      SSL_R_APPLICATION_DATA_AFTER_CLOSE_NOTIFY); | 
					
						
							| 
									
										
										
										
											2018-09-04 20:36:55 +08:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2018-06-21 20:30:38 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-23 19:00:10 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * For handshake data we have 'fragment' storage, so fill that so that we | 
					
						
							|  |  |  |      * can process the header at a fixed place. This is done after the | 
					
						
							|  |  |  |      * "SHUTDOWN" code above to avoid filling the fragment storage with data | 
					
						
							|  |  |  |      * that we're just going to discard. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     if (rr->type == SSL3_RT_HANDSHAKE) { | 
					
						
							| 
									
										
										
										
											2018-05-23 19:00:10 +08:00
										 |  |  |         size_t dest_maxlen = sizeof(s->rlayer.handshake_fragment); | 
					
						
							|  |  |  |         unsigned char *dest = s->rlayer.handshake_fragment; | 
					
						
							|  |  |  |         size_t *dest_len = &s->rlayer.handshake_fragment_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         n = dest_maxlen - *dest_len; /* available space in 'dest' */ | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         if (rr->length < n) | 
					
						
							|  |  |  |             n = rr->length; /* available bytes */ | 
					
						
							| 
									
										
										
										
											2018-05-23 19:00:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /* now move 'n' bytes: */ | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |         memcpy(dest + *dest_len, rr->data + rr->off, n); | 
					
						
							|  |  |  |         rr->off += n; | 
					
						
							|  |  |  |         rr->length -= n; | 
					
						
							| 
									
										
										
										
											2018-05-23 19:00:10 +08:00
										 |  |  |         *dest_len += n; | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |         if (rr->length == 0) | 
					
						
							|  |  |  |             ssl_release_record(s, rr); | 
					
						
							| 
									
										
										
										
											2018-05-23 19:00:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (*dest_len < dest_maxlen) | 
					
						
							|  |  |  |             goto start;     /* fragment was too small */ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) { | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							| 
									
										
										
										
											2017-01-11 07:02:28 +08:00
										 |  |  |      * Unexpected handshake message (ClientHello, NewSessionTicket (TLS1.3) or | 
					
						
							| 
									
										
										
										
											2017-01-10 22:58:17 +08:00
										 |  |  |      * protocol violation) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-10-22 20:57:18 +08:00
										 |  |  |     if ((s->rlayer.handshake_fragment_len >= 4) | 
					
						
							| 
									
										
										
										
											2017-01-11 07:02:28 +08:00
										 |  |  |             && !ossl_statem_get_in_handshake(s)) { | 
					
						
							| 
									
										
										
										
											2017-03-30 00:00:55 +08:00
										 |  |  |         int ined = (s->early_data_state == SSL_EARLY_DATA_READING); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 07:02:28 +08:00
										 |  |  |         /* We found handshake data, so we're going back into init */ | 
					
						
							|  |  |  |         ossl_statem_set_in_init(s, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |         i = s->handshake_func(ssl); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |         /* SSLfatal() already called if appropriate */ | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         if (i < 0) | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |             return i; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         if (i == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-30 00:00:55 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * If we were actually trying to read early data and we found a | 
					
						
							|  |  |  |          * handshake message, then we don't want to continue to try and read | 
					
						
							|  |  |  |          * the application data any more. It won't be "early" now. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         if (ined) | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         if (!(s->mode & SSL_MODE_AUTO_RETRY)) { | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |             if (!RECORD_LAYER_read_pending(&s->rlayer)) { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                 BIO *bio; | 
					
						
							|  |  |  |                 /*
 | 
					
						
							|  |  |  |                  * In the case where we try to read application data, but we | 
					
						
							|  |  |  |                  * trigger an SSL handshake, we return -1 with the retry | 
					
						
							|  |  |  |                  * option set.  Otherwise renegotiation may cause nasty | 
					
						
							|  |  |  |                  * problems in the blocking world | 
					
						
							|  |  |  |                  */ | 
					
						
							|  |  |  |                 s->rwstate = SSL_READING; | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |                 bio = SSL_get_rbio(ssl); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                 BIO_clear_retry_flags(bio); | 
					
						
							|  |  |  |                 BIO_set_retry_read(bio); | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |                 return -1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         goto start; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     switch (rr->type) { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     default: | 
					
						
							|  |  |  |         /*
 | 
					
						
							| 
									
										
										
										
											2016-11-02 17:14:51 +08:00
										 |  |  |          * TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but | 
					
						
							|  |  |  |          * TLS 1.2 says you MUST send an unexpected message alert. We use the | 
					
						
							|  |  |  |          * TLS 1.2 behaviour for all protocol versions to prevent issues where | 
					
						
							|  |  |  |          * no progress is being made and the peer continually sends unrecognised | 
					
						
							|  |  |  |          * record types, using up resources processing them. | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |          */ | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     case SSL3_RT_CHANGE_CIPHER_SPEC: | 
					
						
							|  |  |  |     case SSL3_RT_ALERT: | 
					
						
							|  |  |  |     case SSL3_RT_HANDSHAKE: | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * we already handled all of these, with the possible exception of | 
					
						
							| 
									
										
										
										
											2015-10-22 20:57:18 +08:00
										 |  |  |          * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but | 
					
						
							|  |  |  |          * that should not happen when type != rr->type | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |          */ | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     case SSL3_RT_APPLICATION_DATA: | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * At this point, we were expecting handshake data, but have | 
					
						
							|  |  |  |          * application data.  If the library was running inside ssl3_read() | 
					
						
							|  |  |  |          * (i.e. in_read_app_data is set) and it makes sense to read | 
					
						
							|  |  |  |          * application data at this point (session renegotiation not yet | 
					
						
							|  |  |  |          * started), we will indulge it. | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2015-10-05 17:39:54 +08:00
										 |  |  |         if (ossl_statem_app_data_allowed(s)) { | 
					
						
							| 
									
										
										
										
											2018-12-13 02:09:50 +08:00
										 |  |  |             s->s3.in_read_app_data = 2; | 
					
						
							| 
									
										
										
										
											2016-09-06 19:05:25 +08:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2017-02-24 19:40:49 +08:00
										 |  |  |         } else if (ossl_statem_skip_early_data(s)) { | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * This can happen after a client sends a CH followed by early_data, | 
					
						
							|  |  |  |              * but the server responds with a HelloRetryRequest. The server | 
					
						
							|  |  |  |              * reads the next record from the client expecting to find a | 
					
						
							|  |  |  |              * plaintext ClientHello but gets a record which appears to be | 
					
						
							|  |  |  |              * application data. The trial decrypt "works" because null | 
					
						
							|  |  |  |              * decryption was applied. We just skip it and move on to the next | 
					
						
							|  |  |  |              * record. | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2022-06-20 23:11:28 +08:00
										 |  |  |             if (!ossl_early_data_count_ok(s, rr->length, | 
					
						
							|  |  |  |                                           EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) { | 
					
						
							| 
									
										
										
										
											2017-12-04 22:28:35 +08:00
										 |  |  |                 /* SSLfatal() already called */ | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |                 return -1; | 
					
						
							| 
									
										
										
										
											2017-12-04 22:28:35 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-02 23:29:04 +08:00
										 |  |  |             ssl_release_record(s, rr); | 
					
						
							| 
									
										
										
										
											2017-02-24 19:40:49 +08:00
										 |  |  |             goto start; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2020-11-04 21:39:57 +08:00
										 |  |  |             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD); | 
					
						
							| 
									
										
										
										
											2017-12-04 22:47:04 +08:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-24 22:05:27 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Returns true if the current rrec was sent in SSLv2 backwards compatible | 
					
						
							|  |  |  |  * format and false otherwise. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-03-28 07:01:51 +08:00
										 |  |  | int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-12 21:50:28 +08:00
										 |  |  |     if (SSL_CONNECTION_IS_DTLS(rl->s)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     return rl->tlsrecs[0].version == SSL2_VERSION; | 
					
						
							| 
									
										
										
										
											2015-03-28 07:01:51 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-26 00:10:38 +08:00
										 |  |  | static OSSL_FUNC_rlayer_msg_callback_fn rlayer_msg_callback_wrapper; | 
					
						
							| 
									
										
										
										
											2022-05-25 23:41:30 +08:00
										 |  |  | static void rlayer_msg_callback_wrapper(int write_p, int version, | 
					
						
							|  |  |  |                                         int content_type, const void *buf, | 
					
						
							|  |  |  |                                         size_t len, void *cbarg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SSL_CONNECTION *s = cbarg; | 
					
						
							|  |  |  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (s->msg_callback != NULL) | 
					
						
							|  |  |  |         s->msg_callback(write_p, version, content_type, buf, len, ssl, | 
					
						
							|  |  |  |                         s->msg_callback_arg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-26 00:10:38 +08:00
										 |  |  | static OSSL_FUNC_rlayer_security_fn rlayer_security_wrapper; | 
					
						
							|  |  |  | static int rlayer_security_wrapper(void *cbarg, int op, int bits, int nid, | 
					
						
							|  |  |  |                                    void *other) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SSL_CONNECTION *s = cbarg; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ssl_security(s, op, bits, nid, other); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-31 22:41:16 +08:00
										 |  |  | static OSSL_FUNC_rlayer_padding_fn rlayer_padding_wrapper; | 
					
						
							|  |  |  | static size_t rlayer_padding_wrapper(void *cbarg, int type, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SSL_CONNECTION *s = cbarg; | 
					
						
							|  |  |  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-31 23:39:36 +08:00
										 |  |  |     return s->rlayer.record_padding_cb(ssl, type, len, | 
					
						
							|  |  |  |                                        s->rlayer.record_padding_arg); | 
					
						
							| 
									
										
										
										
											2022-08-31 22:41:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 22:16:48 +08:00
										 |  |  | static const OSSL_DISPATCH rlayer_dispatch[] = { | 
					
						
							|  |  |  |     { OSSL_FUNC_RLAYER_SKIP_EARLY_DATA, (void (*)(void))ossl_statem_skip_early_data }, | 
					
						
							| 
									
										
										
										
											2022-05-25 23:41:30 +08:00
										 |  |  |     { OSSL_FUNC_RLAYER_MSG_CALLBACK, (void (*)(void))rlayer_msg_callback_wrapper }, | 
					
						
							| 
									
										
										
										
											2022-05-26 00:10:38 +08:00
										 |  |  |     { OSSL_FUNC_RLAYER_SECURITY, (void (*)(void))rlayer_security_wrapper }, | 
					
						
							| 
									
										
										
										
											2022-08-31 22:41:16 +08:00
										 |  |  |     { OSSL_FUNC_RLAYER_PADDING, (void (*)(void))rlayer_padding_wrapper }, | 
					
						
							| 
									
										
										
										
											2022-05-25 22:16:48 +08:00
										 |  |  |     { 0, NULL } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  | static const OSSL_RECORD_METHOD *ssl_select_next_record_layer(SSL_CONNECTION *s, | 
					
						
							| 
									
										
										
										
											2022-09-17 00:34:40 +08:00
										 |  |  |                                                               int direction, | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  |                                                               int level) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE) { | 
					
						
							|  |  |  |         if (SSL_CONNECTION_IS_DTLS(s)) | 
					
						
							|  |  |  |             return &ossl_dtls_record_method; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return &ossl_tls_record_method; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef OPENSSL_NO_KTLS
 | 
					
						
							|  |  |  |     /* KTLS does not support renegotiation */ | 
					
						
							|  |  |  |     if (level == OSSL_RECORD_PROTECTION_LEVEL_APPLICATION | 
					
						
							|  |  |  |             && (s->options & SSL_OP_ENABLE_KTLS) != 0 | 
					
						
							|  |  |  |             && (SSL_CONNECTION_IS_TLS13(s) || SSL_IS_FIRST_HANDSHAKE(s))) | 
					
						
							|  |  |  |         return &ossl_ktls_record_method; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Default to the current OSSL_RECORD_METHOD */ | 
					
						
							| 
									
										
										
										
											2022-09-17 00:34:40 +08:00
										 |  |  |     return direction == OSSL_RECORD_DIRECTION_READ ? s->rlayer.rrlmethod | 
					
						
							|  |  |  |                                                    : s->rlayer.wrlmethod; | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  | static int ssl_post_record_layer_select(SSL_CONNECTION *s, int direction) | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     const OSSL_RECORD_METHOD *thismethod; | 
					
						
							|  |  |  |     OSSL_RECORD_LAYER *thisrl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (direction == OSSL_RECORD_DIRECTION_READ) { | 
					
						
							|  |  |  |         thismethod = s->rlayer.rrlmethod; | 
					
						
							|  |  |  |         thisrl = s->rlayer.rrl; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         thismethod = s->rlayer.wrlmethod; | 
					
						
							|  |  |  |         thisrl = s->rlayer.wrl; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  | #ifndef OPENSSL_NO_KTLS
 | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         SSL *ssl = SSL_CONNECTION_GET_SSL(s); | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |         if (s->rlayer.rrlmethod == &ossl_ktls_record_method) { | 
					
						
							|  |  |  |             /* KTLS does not support renegotiation so disallow it */ | 
					
						
							|  |  |  |             SSL_set_options(ssl, SSL_OP_NO_RENEGOTIATION); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     if (SSL_IS_FIRST_HANDSHAKE(s) && thismethod->set_first_handshake != NULL) | 
					
						
							|  |  |  |         thismethod->set_first_handshake(thisrl, 1); | 
					
						
							| 
									
										
										
										
											2022-05-26 00:30:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     if (s->max_pipelines != 0 && thismethod->set_max_pipelines != NULL) | 
					
						
							|  |  |  |         thismethod->set_max_pipelines(thisrl, s->max_pipelines); | 
					
						
							| 
									
										
										
										
											2022-05-26 00:30:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ssl_set_new_record_layer(SSL_CONNECTION *s, int version, | 
					
						
							|  |  |  |                              int direction, int level, | 
					
						
							| 
									
										
										
										
											2022-05-06 22:10:00 +08:00
										 |  |  |                              unsigned char *key, size_t keylen, | 
					
						
							|  |  |  |                              unsigned char *iv,  size_t ivlen, | 
					
						
							|  |  |  |                              unsigned char *mackey, size_t mackeylen, | 
					
						
							|  |  |  |                              const EVP_CIPHER *ciph, size_t taglen, | 
					
						
							|  |  |  |                              int mactype, const EVP_MD *md, | 
					
						
							|  |  |  |                              const SSL_COMP *comp) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-19 22:49:51 +08:00
										 |  |  |     OSSL_PARAM options[5], *opts = options; | 
					
						
							| 
									
										
										
										
											2022-05-26 00:30:33 +08:00
										 |  |  |     OSSL_PARAM settings[6], *set =  settings; | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     const OSSL_RECORD_METHOD **thismethod; | 
					
						
							| 
									
										
										
										
											2022-08-22 23:09:23 +08:00
										 |  |  |     OSSL_RECORD_LAYER **thisrl, *newrl = NULL; | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     BIO *thisbio; | 
					
						
							| 
									
										
										
										
											2022-05-06 22:10:00 +08:00
										 |  |  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  |     const OSSL_RECORD_METHOD *meth; | 
					
						
							| 
									
										
										
										
											2022-05-26 00:30:33 +08:00
										 |  |  |     int use_etm, stream_mac = 0, tlstree = 0; | 
					
						
							| 
									
										
										
										
											2022-09-23 23:53:23 +08:00
										 |  |  |     unsigned int maxfrag = (direction == OSSL_RECORD_DIRECTION_WRITE) | 
					
						
							|  |  |  |                            ? ssl_get_max_send_fragment(s) | 
					
						
							|  |  |  |                            : SSL3_RT_MAX_PLAIN_LENGTH; | 
					
						
							| 
									
										
										
										
											2022-05-25 22:16:48 +08:00
										 |  |  |     int use_early_data = 0; | 
					
						
							|  |  |  |     uint32_t max_early_data; | 
					
						
							| 
									
										
										
										
											2022-09-15 23:03:02 +08:00
										 |  |  |     COMP_METHOD *compm = (comp == NULL) ? NULL : comp->method; | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-17 00:34:40 +08:00
										 |  |  |     meth = ssl_select_next_record_layer(s, direction, level); | 
					
						
							| 
									
										
										
										
											2022-05-06 22:10:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     if (direction == OSSL_RECORD_DIRECTION_READ) { | 
					
						
							|  |  |  |         thismethod = &s->rlayer.rrlmethod; | 
					
						
							|  |  |  |         thisrl = &s->rlayer.rrl; | 
					
						
							|  |  |  |         thisbio = s->rbio; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         thismethod = &s->rlayer.wrlmethod; | 
					
						
							|  |  |  |         thisrl = &s->rlayer.wrl; | 
					
						
							|  |  |  |         thisbio = s->wbio; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-22 23:09:23 +08:00
										 |  |  |     if (meth == NULL) | 
					
						
							|  |  |  |         meth = *thismethod; | 
					
						
							| 
									
										
										
										
											2022-05-06 22:10:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-22 23:09:23 +08:00
										 |  |  |     if (!ossl_assert(meth != NULL)) { | 
					
						
							| 
									
										
										
										
											2022-05-06 22:10:00 +08:00
										 |  |  |         ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-20 23:54:12 +08:00
										 |  |  |     /* Parameters that *may* be supported by a record layer if passed */ | 
					
						
							|  |  |  |     *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS, | 
					
						
							|  |  |  |                                           &s->options); | 
					
						
							|  |  |  |     *opts++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE, | 
					
						
							|  |  |  |                                           &s->mode); | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     if (direction == OSSL_RECORD_DIRECTION_READ) { | 
					
						
							|  |  |  |         *opts++ = OSSL_PARAM_construct_size_t(OSSL_LIBSSL_RECORD_LAYER_READ_BUFFER_LEN, | 
					
						
							|  |  |  |                                               &s->rlayer.default_read_buf_len); | 
					
						
							|  |  |  |         *opts++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD, | 
					
						
							|  |  |  |                                            &s->rlayer.read_ahead); | 
					
						
							| 
									
										
										
										
											2022-08-31 23:39:36 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         *opts++ = OSSL_PARAM_construct_size_t(OSSL_LIBSSL_RECORD_LAYER_PARAM_BLOCK_PADDING, | 
					
						
							|  |  |  |                                               &s->rlayer.block_padding); | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-05-20 23:54:12 +08:00
										 |  |  |     *opts = OSSL_PARAM_construct_end(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Parameters that *must* be supported by a record layer if passed */ | 
					
						
							| 
									
										
										
										
											2022-05-26 00:30:33 +08:00
										 |  |  |     if (direction == OSSL_RECORD_DIRECTION_READ) { | 
					
						
							| 
									
										
										
										
											2022-05-20 23:54:12 +08:00
										 |  |  |         use_etm = SSL_READ_ETM(s) ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2022-05-26 00:30:33 +08:00
										 |  |  |         if ((s->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM) != 0) | 
					
						
							|  |  |  |             stream_mac = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ((s->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE) != 0) | 
					
						
							|  |  |  |             tlstree = 1; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2022-05-20 23:54:12 +08:00
										 |  |  |         use_etm = SSL_WRITE_ETM(s) ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2022-05-26 00:30:33 +08:00
										 |  |  |         if ((s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM) != 0) | 
					
						
							|  |  |  |             stream_mac = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ((s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE) != 0) | 
					
						
							|  |  |  |             tlstree = 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-05-20 23:54:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (use_etm) | 
					
						
							|  |  |  |         *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_USE_ETM, | 
					
						
							|  |  |  |                                           &use_etm); | 
					
						
							| 
									
										
										
										
											2022-05-23 18:31:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-26 00:30:33 +08:00
										 |  |  |     if (stream_mac) | 
					
						
							|  |  |  |         *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_STREAM_MAC, | 
					
						
							|  |  |  |                                           &stream_mac); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (tlstree) | 
					
						
							|  |  |  |         *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_TLSTREE, | 
					
						
							|  |  |  |                                           &tlstree); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-23 23:53:23 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * We only need to do this for the read side. The write side should already | 
					
						
							|  |  |  |      * have the correct value due to the ssl_get_max_send_fragment() call above | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (direction == OSSL_RECORD_DIRECTION_READ | 
					
						
							|  |  |  |             && s->session != NULL | 
					
						
							|  |  |  |             && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)) | 
					
						
							| 
									
										
										
										
											2022-05-23 18:31:53 +08:00
										 |  |  |         maxfrag = GET_MAX_FRAGMENT_LENGTH(s->session); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-23 23:53:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-23 18:31:53 +08:00
										 |  |  |     if (maxfrag != SSL3_RT_MAX_PLAIN_LENGTH) | 
					
						
							|  |  |  |         *set++ = OSSL_PARAM_construct_uint(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_FRAG_LEN, | 
					
						
							|  |  |  |                                            &maxfrag); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 22:16:48 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * The record layer must check the amount of early data sent or received | 
					
						
							|  |  |  |      * using the early keys. A server also needs to worry about rejected early | 
					
						
							|  |  |  |      * data that might arrive when the handshake keys are in force. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (s->server && direction == OSSL_RECORD_DIRECTION_READ) { | 
					
						
							|  |  |  |         use_early_data = (level == OSSL_RECORD_PROTECTION_LEVEL_EARLY | 
					
						
							|  |  |  |                           || level == OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE); | 
					
						
							|  |  |  |     } else if (!s->server && direction == OSSL_RECORD_DIRECTION_WRITE) { | 
					
						
							|  |  |  |         use_early_data = (level == OSSL_RECORD_PROTECTION_LEVEL_EARLY); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (use_early_data) { | 
					
						
							|  |  |  |         max_early_data = ossl_get_max_early_data(s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (max_early_data != 0) | 
					
						
							| 
									
										
										
										
											2022-11-11 00:46:40 +08:00
										 |  |  |             *set++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA, | 
					
						
							|  |  |  |                                                  &max_early_data); | 
					
						
							| 
									
										
										
										
											2022-05-25 22:16:48 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-20 23:54:12 +08:00
										 |  |  |     *set = OSSL_PARAM_construct_end(); | 
					
						
							| 
									
										
										
										
											2022-05-06 22:10:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  |     for (;;) { | 
					
						
							| 
									
										
										
										
											2022-05-12 23:35:52 +08:00
										 |  |  |         int rlret; | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |         BIO *prev = NULL; | 
					
						
							|  |  |  |         BIO *next = NULL; | 
					
						
							| 
									
										
										
										
											2022-08-31 22:41:16 +08:00
										 |  |  |         unsigned int epoch = 0; | 
					
						
							|  |  |  |         OSSL_DISPATCH rlayer_dispatch_tmp[OSSL_NELEM(rlayer_dispatch)]; | 
					
						
							|  |  |  |         size_t i, j; | 
					
						
							| 
									
										
										
										
											2022-06-08 21:52:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |         if (direction == OSSL_RECORD_DIRECTION_READ) { | 
					
						
							|  |  |  |             prev = s->rlayer.rrlnext; | 
					
						
							|  |  |  |             if (SSL_CONNECTION_IS_DTLS(s) | 
					
						
							|  |  |  |                     && level != OSSL_RECORD_PROTECTION_LEVEL_NONE) | 
					
						
							|  |  |  |                 epoch =  DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer) + 1; /* new epoch */ | 
					
						
							| 
									
										
										
										
											2022-05-17 23:16:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |             if (SSL_CONNECTION_IS_DTLS(s)) | 
					
						
							|  |  |  |                 next = BIO_new(BIO_s_dgram_mem()); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 next = BIO_new(BIO_s_mem()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (next == NULL) { | 
					
						
							|  |  |  |                 BIO_free(prev); | 
					
						
							|  |  |  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             s->rlayer.rrlnext = next; | 
					
						
							| 
									
										
										
										
											2022-10-13 23:44:22 +08:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             if (SSL_CONNECTION_IS_DTLS(s) | 
					
						
							|  |  |  |                     && level != OSSL_RECORD_PROTECTION_LEVEL_NONE) | 
					
						
							|  |  |  |                 epoch =  DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) + 1; /* new epoch */ | 
					
						
							| 
									
										
										
										
											2022-05-17 23:16:40 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-05-12 23:35:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-31 22:41:16 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * Create a copy of the dispatch array, missing out wrappers for | 
					
						
							|  |  |  |          * callbacks that we don't need. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         for (i = 0, j = 0; i < OSSL_NELEM(rlayer_dispatch); i++) { | 
					
						
							|  |  |  |             switch (rlayer_dispatch[i].function_id) { | 
					
						
							|  |  |  |             case OSSL_FUNC_RLAYER_MSG_CALLBACK: | 
					
						
							|  |  |  |                 if (s->msg_callback == NULL) | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case OSSL_FUNC_RLAYER_PADDING: | 
					
						
							| 
									
										
										
										
											2022-08-31 23:39:36 +08:00
										 |  |  |                 if (s->rlayer.record_padding_cb == NULL) | 
					
						
							| 
									
										
										
										
											2022-08-31 22:41:16 +08:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             rlayer_dispatch_tmp[j++] = rlayer_dispatch[i]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-22 23:09:23 +08:00
										 |  |  |         rlret = meth->new_record_layer(sctx->libctx, sctx->propq, version, | 
					
						
							|  |  |  |                                        s->server, direction, level, epoch, | 
					
						
							|  |  |  |                                        key, keylen, iv, ivlen, mackey, | 
					
						
							|  |  |  |                                        mackeylen, ciph, taglen, mactype, md, | 
					
						
							| 
									
										
										
										
											2022-09-15 23:03:02 +08:00
										 |  |  |                                        compm, prev, thisbio, next, NULL, NULL, | 
					
						
							| 
									
										
										
										
											2022-08-31 22:41:16 +08:00
										 |  |  |                                        settings, options, rlayer_dispatch_tmp, | 
					
						
							|  |  |  |                                        s, &newrl); | 
					
						
							| 
									
										
										
										
											2022-05-17 23:16:40 +08:00
										 |  |  |         BIO_free(prev); | 
					
						
							| 
									
										
										
										
											2022-05-12 23:35:52 +08:00
										 |  |  |         switch (rlret) { | 
					
						
							|  |  |  |         case OSSL_RECORD_RETURN_FATAL: | 
					
						
							|  |  |  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_RECORD_LAYER_FAILURE); | 
					
						
							| 
									
										
										
										
											2022-05-20 23:54:12 +08:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2022-05-12 23:35:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         case OSSL_RECORD_RETURN_NON_FATAL_ERR: | 
					
						
							| 
									
										
										
										
											2022-08-22 23:09:23 +08:00
										 |  |  |             if (*thismethod != meth && *thismethod != NULL) { | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  |                 /*
 | 
					
						
							|  |  |  |                  * We tried a new record layer method, but it didn't work out, | 
					
						
							|  |  |  |                  * so we fallback to the original method and try again | 
					
						
							|  |  |  |                  */ | 
					
						
							| 
									
										
										
										
											2022-08-22 23:09:23 +08:00
										 |  |  |                 meth = *thismethod; | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-05-12 23:35:52 +08:00
										 |  |  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_RECORD_LAYER); | 
					
						
							| 
									
										
										
										
											2022-05-20 23:54:12 +08:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2022-05-12 23:35:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         case OSSL_RECORD_RETURN_SUCCESS: | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             /* Should not happen */ | 
					
						
							|  |  |  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | 
					
						
							| 
									
										
										
										
											2022-05-20 23:54:12 +08:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2022-05-11 01:50:00 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-05-06 22:10:00 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-13 23:44:22 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Free the old record layer if we have one except in the case of DTLS when | 
					
						
							| 
									
										
										
										
											2022-11-07 23:13:35 +08:00
										 |  |  |      * writing and there are still buffered sent messages in our queue. In that | 
					
						
							|  |  |  |      * case the record layer is still referenced by those buffered messages for | 
					
						
							|  |  |  |      * potential retransmit. Only when those buffered messages get freed do we | 
					
						
							|  |  |  |      * free the record layer object (see dtls1_hm_fragment_free) | 
					
						
							| 
									
										
										
										
											2022-10-13 23:44:22 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-11-07 23:13:35 +08:00
										 |  |  |     if (!SSL_CONNECTION_IS_DTLS(s) | 
					
						
							|  |  |  |             || direction == OSSL_RECORD_DIRECTION_READ | 
					
						
							|  |  |  |             || pqueue_peek(s->d1->sent_messages) == NULL) { | 
					
						
							| 
									
										
										
										
											2022-10-13 23:44:22 +08:00
										 |  |  |         if (*thismethod != NULL && !(*thismethod)->free(*thisrl)) { | 
					
						
							|  |  |  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-22 23:09:23 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *thisrl = newrl; | 
					
						
							|  |  |  |     *thismethod = meth; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-19 23:54:09 +08:00
										 |  |  |     return ssl_post_record_layer_select(s, direction); | 
					
						
							| 
									
										
										
										
											2022-05-06 22:10:00 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-08-27 00:34:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!ossl_assert(s->rlayer.rrlmethod != NULL) | 
					
						
							|  |  |  |             || !ossl_assert(s->rlayer.wrlmethod != NULL)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     s->rlayer.rrlmethod->set_protocol_version(s->rlayer.rrl, s->version); | 
					
						
							|  |  |  |     s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, s->version); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } |