2
0
mirror of https://github.com/openssl/openssl.git synced 2025-03-01 19:28:10 +08:00

Start using WPACKET in the dtls write records code

Previously this was writing to the buffers directly. We use the safer
WPACKET instead

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19424)
This commit is contained in:
Matt Caswell 2022-10-13 11:25:56 +01:00
parent 602ee1f672
commit 248a9bf21a
3 changed files with 24 additions and 9 deletions

View File

@ -701,6 +701,9 @@ int dtls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
SSL3_BUFFER *wb;
SSL_SESSION *sess;
SSL *s = SSL_CONNECTION_GET_SSL(sc);
WPACKET pkt, *thispkt = &pkt;
size_t wpinited = 0;
int ret = 0;
sess = sc->session;
@ -731,6 +734,14 @@ int dtls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
return 0;
}
if (!rl->funcs->initialise_write_packets(rl, templates, numtempl,
NULL, thispkt, rl->wbuf,
&wpinited)) {
/* RLAYERfatal() already called */
return 0;
}
wb = rl->wbuf;
p = SSL3_BUFFER_get_buf(wb);
@ -752,7 +763,7 @@ int dtls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
eivlen = EVP_CIPHER_CTX_get_iv_length(sc->enc_write_ctx);
if (eivlen < 0) {
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
return 0;
goto err;
}
if (eivlen <= 1)
eivlen = 0;
@ -780,7 +791,7 @@ int dtls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
if (sc->compress != NULL) {
if (!ssl3_do_compress(sc, &wr)) {
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE);
return 0;
goto err;
}
} else {
memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),
@ -799,7 +810,7 @@ int dtls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
&(p[SSL3_RECORD_get_length(&wr) + eivlen]),
1)) {
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
return 0;
goto err;
}
SSL3_RECORD_add_length(&wr, mac_size);
}
@ -815,14 +826,14 @@ int dtls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
if (!ossl_statem_in_error(sc)) {
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
}
return 0;
goto err;
}
if (SSL_WRITE_ETM(sc) && mac_size != 0) {
if (!s->method->ssl3_enc->mac(sc, &wr,
&(p[SSL3_RECORD_get_length(&wr)]), 1)) {
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
return 0;
goto err;
}
SSL3_RECORD_add_length(&wr, mac_size);
}
@ -863,8 +874,11 @@ int dtls_write_records(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
sc->rlayer.wpend_type = templates->type;
sc->rlayer.wpend_ret = templates->buflen;
return 1;
ret = 1;
err:
if (wpinited > 0)
WPACKET_cleanup(thispkt);
return ret;
}
const OSSL_RECORD_METHOD ossl_dtls_record_method = {

View File

@ -680,7 +680,8 @@ struct record_functions_st dtls_1_funcs = {
* instead.
*/
tls_allocate_write_buffers_default,
NULL,
/* Don't use tls1_initialise_write_packets for same reason as above */
tls_initialise_write_packets_default,
NULL,
NULL,
NULL,

View File

@ -185,7 +185,7 @@ struct record_functions_st dtls_any_funcs = {
NULL,
dtls_write_records,
tls_allocate_write_buffers_default,
NULL,
tls_initialise_write_packets_default,
NULL,
NULL,
NULL,