Skip to content

Commit aca70ca

Browse files
committed
Defer record header preparation to the protocol methods
We introduce a new function to prepare the record header. KTLS has its own version since this is done by the kernel. Reviewed-by: Hugo Landau <hlandau@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from openssl#19343)
1 parent 7ca61d6 commit aca70ca

7 files changed

Lines changed: 76 additions & 38 deletions

File tree

ssl/record/methods/ktls_meth.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static int ktls_initialise_write_packets(OSSL_RECORD_LAYER *rl,
461461
SSL3_BUFFER *wb;
462462

463463
/*
464-
* We just use the application buffer directly, and don't use any WPACKET
464+
* We just use the application buffer directly and don't use any WPACKET
465465
* structures
466466
*/
467467
wb = &bufs[0];
@@ -470,8 +470,8 @@ static int ktls_initialise_write_packets(OSSL_RECORD_LAYER *rl,
470470
/*
471471
* ktls doesn't modify the buffer, but to avoid a warning we need
472472
* to discard the const qualifier.
473-
* This doesn't leak memory because the buffers have been
474-
* released when switching to ktls.
473+
* This doesn't leak memory because the buffers have never been allocated
474+
* with KTLS
475475
*/
476476
SSL3_BUFFER_set_buf(wb, (unsigned char *)templates[0].buf);
477477
SSL3_BUFFER_set_offset(wb, 0);
@@ -480,6 +480,18 @@ static int ktls_initialise_write_packets(OSSL_RECORD_LAYER *rl,
480480
return 1;
481481
}
482482

483+
static int ktls_prepare_record_header(OSSL_RECORD_LAYER *rl,
484+
WPACKET *thispkt,
485+
OSSL_RECORD_TEMPLATE *templ,
486+
unsigned int rectype,
487+
unsigned char **recdata)
488+
{
489+
/* The kernel writes the record header, so nothing to do */
490+
*recdata = NULL;
491+
492+
return 1;
493+
}
494+
483495
static struct record_functions_st ossl_ktls_funcs = {
484496
ktls_set_crypto_state,
485497
ktls_cipher,
@@ -493,7 +505,8 @@ static struct record_functions_st ossl_ktls_funcs = {
493505
tls_write_records_default,
494506
ktls_allocate_write_buffers,
495507
ktls_initialise_write_packets,
496-
NULL
508+
NULL,
509+
ktls_prepare_record_header
497510
};
498511

499512
const OSSL_RECORD_METHOD ossl_ktls_record_method = {

ssl/record/methods/recmethod_local.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ struct record_functions_st
9999
/* Get the actual record type to be used for a given template */
100100
unsigned int (*get_record_type)(OSSL_RECORD_LAYER *rl,
101101
OSSL_RECORD_TEMPLATE *template);
102+
103+
/* Write the record header data to the WPACKET */
104+
int (*prepare_record_header)(OSSL_RECORD_LAYER *rl, WPACKET *thispkt,
105+
OSSL_RECORD_TEMPLATE *templ,
106+
unsigned int rectype,
107+
unsigned char **recdata);
102108
};
103109

104110
struct ossl_record_layer_st
@@ -368,6 +374,9 @@ int tls_write_records_multiblock(OSSL_RECORD_LAYER *rl,
368374

369375
size_t tls_get_max_records_default(OSSL_RECORD_LAYER *rl, int type, size_t len,
370376
size_t maxfrag, size_t *preffrag);
377+
size_t tls_get_max_records_multiblock(OSSL_RECORD_LAYER *rl, int type,
378+
size_t len, size_t maxfrag,
379+
size_t *preffrag);
371380
int tls_allocate_write_buffers_default(OSSL_RECORD_LAYER *rl,
372381
OSSL_RECORD_TEMPLATE *templates,
373382
size_t numtempl, size_t *prefix);
@@ -388,9 +397,11 @@ int tls1_initialise_write_packets(OSSL_RECORD_LAYER *rl,
388397
WPACKET *pkt,
389398
SSL3_BUFFER *bufs,
390399
size_t *wpinited);
391-
size_t tls_get_max_records_multiblock(OSSL_RECORD_LAYER *rl, int type,
392-
size_t len, size_t maxfrag,
393-
size_t *preffrag);
400+
int tls_prepare_record_header_default(OSSL_RECORD_LAYER *rl,
401+
WPACKET *thispkt,
402+
OSSL_RECORD_TEMPLATE *templ,
403+
unsigned int rectype,
404+
unsigned char **recdata);
394405
int tls_write_records_default(OSSL_RECORD_LAYER *rl,
395406
OSSL_RECORD_TEMPLATE *templates,
396407
size_t numtempl);

ssl/record/methods/ssl3_meth.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,5 +314,6 @@ struct record_functions_st ssl_3_0_funcs = {
314314
/* These 2 functions are defined in tls1_meth.c */
315315
tls1_allocate_write_buffers,
316316
tls1_initialise_write_packets,
317-
NULL
317+
NULL,
318+
tls_prepare_record_header_default
318319
};

ssl/record/methods/tls13_meth.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,5 +266,6 @@ struct record_functions_st tls_1_3_funcs = {
266266
tls_write_records_default,
267267
tls_allocate_write_buffers_default,
268268
tls_initialise_write_packets_default,
269-
tls13_get_record_type
269+
tls13_get_record_type,
270+
tls_prepare_record_header_default
270271
};

ssl/record/methods/tls1_meth.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,8 @@ struct record_functions_st tls_1_funcs = {
655655
tls_write_records_multiblock, /* Defined in tls_multib.c */
656656
tls1_allocate_write_buffers,
657657
tls1_initialise_write_packets,
658-
NULL
658+
NULL,
659+
tls_prepare_record_header_default
659660
};
660661

661662
struct record_functions_st dtls_1_funcs = {

ssl/record/methods/tls_common.c

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,35 @@ int tls_initialise_write_packets_default(OSSL_RECORD_LAYER *rl,
15331533
return 1;
15341534
}
15351535

1536+
int tls_prepare_record_header_default(OSSL_RECORD_LAYER *rl,
1537+
WPACKET *thispkt,
1538+
OSSL_RECORD_TEMPLATE *templ,
1539+
unsigned int rectype,
1540+
unsigned char **recdata)
1541+
{
1542+
size_t maxcomplen;
1543+
1544+
*recdata = NULL;
1545+
1546+
maxcomplen = templ->buflen;
1547+
if (rl->compctx != NULL)
1548+
maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
1549+
1550+
if (!WPACKET_put_bytes_u8(thispkt, rectype)
1551+
|| !WPACKET_put_bytes_u16(thispkt, templ->version)
1552+
|| !WPACKET_start_sub_packet_u16(thispkt)
1553+
|| (rl->eivlen > 0
1554+
&& !WPACKET_allocate_bytes(thispkt, rl->eivlen, NULL))
1555+
|| (maxcomplen > 0
1556+
&& !WPACKET_reserve_bytes(thispkt, maxcomplen,
1557+
recdata))) {
1558+
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1559+
return 0;
1560+
}
1561+
1562+
return 1;
1563+
}
1564+
15361565
int tls_write_records_default(OSSL_RECORD_LAYER *rl,
15371566
OSSL_RECORD_TEMPLATE *templates,
15381567
size_t numtempl)
@@ -1579,7 +1608,6 @@ int tls_write_records_default(OSSL_RECORD_LAYER *rl,
15791608
memset(wr, 0, sizeof(wr));
15801609
for (j = 0; j < numtempl + prefix; j++) {
15811610
unsigned char *compressdata = NULL;
1582-
size_t maxcomplen;
15831611
unsigned int rectype;
15841612

15851613
thispkt = &pkt[j];
@@ -1598,23 +1626,8 @@ int tls_write_records_default(OSSL_RECORD_LAYER *rl,
15981626
SSL3_RECORD_set_type(thiswr, rectype);
15991627
SSL3_RECORD_set_rec_version(thiswr, thistempl->version);
16001628

1601-
maxcomplen = thistempl->buflen;
1602-
if (rl->compctx != NULL)
1603-
maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
1604-
1605-
/*
1606-
* When using offload kernel will write the header.
1607-
* Otherwise write the header now
1608-
*/
1609-
if (!using_ktls
1610-
&& (!WPACKET_put_bytes_u8(thispkt, rectype)
1611-
|| !WPACKET_put_bytes_u16(thispkt, thistempl->version)
1612-
|| !WPACKET_start_sub_packet_u16(thispkt)
1613-
|| (rl->eivlen > 0
1614-
&& !WPACKET_allocate_bytes(thispkt, rl->eivlen, NULL))
1615-
|| (maxcomplen > 0
1616-
&& !WPACKET_reserve_bytes(thispkt, maxcomplen,
1617-
&compressdata)))) {
1629+
if (!rl->funcs->prepare_record_header(rl, thispkt, thistempl, rectype,
1630+
&compressdata)) {
16181631
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
16191632
goto err;
16201633
}
@@ -1637,16 +1650,12 @@ int tls_write_records_default(OSSL_RECORD_LAYER *rl,
16371650
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE);
16381651
goto err;
16391652
}
1640-
} else {
1641-
if (using_ktls) {
1642-
SSL3_RECORD_reset_data(&wr[j]);
1643-
} else {
1644-
if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {
1645-
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1646-
goto err;
1647-
}
1648-
SSL3_RECORD_reset_input(&wr[j]);
1653+
} else if (compressdata != NULL) {
1654+
if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {
1655+
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1656+
goto err;
16491657
}
1658+
SSL3_RECORD_reset_input(&wr[j]);
16501659
}
16511660

16521661
if (rl->version == TLS1_3_VERSION

ssl/record/methods/tlsany_meth.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ struct record_functions_st tls_any_funcs = {
147147
tls_write_records_default,
148148
tls_allocate_write_buffers_default,
149149
tls_initialise_write_packets_default,
150-
NULL
150+
NULL,
151+
tls_prepare_record_header_default
151152
};
152153

153154
static int dtls_any_set_protocol_version(OSSL_RECORD_LAYER *rl, int vers)
@@ -172,5 +173,6 @@ struct record_functions_st dtls_any_funcs = {
172173
NULL,
173174
NULL,
174175
NULL,
176+
NULL,
175177
NULL
176178
};

0 commit comments

Comments
 (0)