Skip to content

Commit 0521d64

Browse files
committed
Improve ABI compat trick
In principle, pointers to char and pointers to word-aligned structs might have different sizes. However, it's guaranteed that any pointer can be converted to a char pointer and back, so it's OK to store the pointer to struct as a pointer to char. Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
1 parent 8d0111b commit 0521d64

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

include/mbedtls/ssl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ struct mbedtls_ssl_context {
18051805
* (the end is marked by in_len). */
18061806
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
18071807
unsigned char *MBEDTLS_PRIVATE(in_len); /*!< two-bytes message length field */
1808-
mbedtls_ssl_context_in_ext *MBEDTLS_PRIVATE(in_ext); /*!< extension structure */
1808+
unsigned char *MBEDTLS_PRIVATE(in_ext); /*!< extension structure (ABI compat) */
18091809
unsigned char *MBEDTLS_PRIVATE(in_msg); /*!< message contents (in_iv+ivlen) */
18101810
unsigned char *MBEDTLS_PRIVATE(in_offt); /*!< read offset in application data */
18111811

library/ssl_misc.h

+5
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type);
409409
#define MBEDTLS_CLIENT_HELLO_RANDOM_LEN 32
410410
#define MBEDTLS_SERVER_HELLO_RANDOM_LEN 32
411411

412+
static inline mbedtls_ssl_context_in_ext *mbedtls_ssl_get_in_ext(const mbedtls_ssl_context *ssl)
413+
{
414+
return (mbedtls_ssl_context_in_ext *) ssl->in_ext;
415+
}
416+
412417
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
413418
/**
414419
* \brief Return the maximum fragment length (payload, in bytes) for

library/ssl_msg.c

+22-22
Original file line numberDiff line numberDiff line change
@@ -3229,8 +3229,8 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
32293229

32303230
if (ssl->in_hslen == 0) {
32313231
ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
3232-
ssl->in_ext->in_hsfraglen = 0;
3233-
ssl->in_ext->in_hshdr = ssl->in_hdr;
3232+
mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen = 0;
3233+
mbedtls_ssl_get_in_ext(ssl)->in_hshdr = ssl->in_hdr;
32343234
}
32353235

32363236
MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
@@ -3298,35 +3298,35 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
32983298
} else
32993299
#endif /* MBEDTLS_SSL_PROTO_DTLS */
33003300
{
3301-
if (ssl->in_ext->in_hsfraglen > ssl->in_hslen) {
3301+
if (mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen > ssl->in_hslen) {
33023302
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
33033303
}
33043304
int ret;
3305-
const size_t hs_remain = ssl->in_hslen - ssl->in_ext->in_hsfraglen;
3305+
const size_t hs_remain = ssl->in_hslen - mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen;
33063306
MBEDTLS_SSL_DEBUG_MSG(3,
33073307
("handshake fragment: %" MBEDTLS_PRINTF_SIZET " .. %"
33083308
MBEDTLS_PRINTF_SIZET " of %"
33093309
MBEDTLS_PRINTF_SIZET " msglen %" MBEDTLS_PRINTF_SIZET,
3310-
ssl->in_ext->in_hsfraglen,
3311-
ssl->in_ext->in_hsfraglen +
3310+
mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen,
3311+
mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen +
33123312
(hs_remain <= ssl->in_msglen ? hs_remain : ssl->in_msglen),
33133313
ssl->in_hslen, ssl->in_msglen));
33143314
if (ssl->in_msglen < hs_remain) {
3315-
ssl->in_ext->in_hsfraglen += ssl->in_msglen;
3315+
mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen += ssl->in_msglen;
33163316
ssl->in_hdr = ssl->in_msg + ssl->in_msglen;
33173317
ssl->in_msglen = 0;
33183318
mbedtls_ssl_update_in_pointers(ssl);
33193319
return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
33203320
}
3321-
if (ssl->in_ext->in_hshdr != ssl->in_hdr) {
3321+
if (mbedtls_ssl_get_in_ext(ssl)->in_hshdr != ssl->in_hdr) {
33223322
/*
3323-
* At ssl->in_ext->in_hshdr we have a sequence of records that cover the next handshake
3323+
* At mbedtls_ssl_get_in_ext(ssl)->in_hshdr we have a sequence of records that cover the next handshake
33243324
* record, each with its own record header that we need to remove.
33253325
* Note that the reassembled record size may not equal the size of the message,
33263326
* there maybe bytes from the next message following it.
33273327
*/
33283328
size_t merged_rec_len = 0;
3329-
unsigned char *p = ssl->in_ext->in_hshdr, *q = NULL;
3329+
unsigned char *p = mbedtls_ssl_get_in_ext(ssl)->in_hshdr, *q = NULL;
33303330
do {
33313331
mbedtls_record rec;
33323332
ret = ssl_parse_record_header(ssl, p, mbedtls_ssl_in_hdr_len(ssl), &rec);
@@ -3342,13 +3342,13 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
33423342
q = p;
33433343
}
33443344
} while (merged_rec_len < ssl->in_hslen);
3345-
ssl->in_hdr = ssl->in_ext->in_hshdr;
3345+
ssl->in_hdr = mbedtls_ssl_get_in_ext(ssl)->in_hshdr;
33463346
mbedtls_ssl_update_in_pointers(ssl);
33473347
ssl->in_msglen = merged_rec_len;
33483348
/* Adjust message length. */
33493349
MBEDTLS_PUT_UINT16_BE(merged_rec_len, ssl->in_len, 0);
3350-
ssl->in_ext->in_hsfraglen = 0;
3351-
ssl->in_ext->in_hshdr = NULL;
3350+
mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen = 0;
3351+
mbedtls_ssl_get_in_ext(ssl)->in_hshdr = NULL;
33523352
MBEDTLS_SSL_DEBUG_BUF(4, "reassembled record",
33533353
ssl->in_hdr, mbedtls_ssl_in_hdr_len(ssl) + merged_rec_len);
33543354
}
@@ -4696,13 +4696,13 @@ static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
46964696
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
46974697
}
46984698

4699-
if (ssl->in_ext->in_hsfraglen != 0) {
4699+
if (mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen != 0) {
47004700
/* Not all handshake fragments have arrived, do not consume. */
47014701
MBEDTLS_SSL_DEBUG_MSG(3,
47024702
("waiting for more fragments (%" MBEDTLS_PRINTF_SIZET " of %"
47034703
MBEDTLS_PRINTF_SIZET ", %" MBEDTLS_PRINTF_SIZET " left)",
4704-
ssl->in_ext->in_hsfraglen, ssl->in_hslen,
4705-
ssl->in_hslen - ssl->in_ext->in_hsfraglen));
4704+
mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen, ssl->in_hslen,
4705+
ssl->in_hslen - mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen));
47064706
return 0;
47074707
}
47084708

@@ -4955,7 +4955,7 @@ static int ssl_get_next_record(mbedtls_ssl_context *ssl)
49554955
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
49564956
ssl->in_len = ssl->in_cid + rec.cid_len;
49574957
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4958-
ssl->in_ext->in_iv = ssl->in_msg = ssl->in_len + 2;
4958+
mbedtls_ssl_get_in_ext(ssl)->in_iv = ssl->in_msg = ssl->in_len + 2;
49594959
ssl->in_msglen = rec.data_len;
49604960

49614961
ret = ssl_check_client_reconnect(ssl);
@@ -5074,7 +5074,7 @@ static int ssl_get_next_record(mbedtls_ssl_context *ssl)
50745074
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
50755075
ssl->in_len = ssl->in_cid + rec.cid_len;
50765076
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5077-
ssl->in_ext->in_iv = ssl->in_len + 2;
5077+
mbedtls_ssl_get_in_ext(ssl)->in_iv = ssl->in_len + 2;
50785078

50795079
/* The record content type may change during decryption,
50805080
* so re-read it. */
@@ -5380,7 +5380,7 @@ void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
53805380
void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
53815381
{
53825382
/* This function sets the pointers to match the case
5383-
* of unprotected TLS/DTLS records, with both ssl->in_ext->in_iv
5383+
* of unprotected TLS/DTLS records, with both mbedtls_ssl_get_in_ext(ssl)->in_iv
53845384
* and ssl->in_msg pointing to the beginning of the record
53855385
* content.
53865386
*
@@ -5402,7 +5402,7 @@ void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
54025402
#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
54035403
ssl->in_len = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
54045404
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5405-
ssl->in_ext->in_iv = ssl->in_len + 2;
5405+
mbedtls_ssl_get_in_ext(ssl)->in_iv = ssl->in_len + 2;
54065406
} else
54075407
#endif
54085408
{
@@ -5411,11 +5411,11 @@ void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
54115411
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
54125412
ssl->in_cid = ssl->in_len;
54135413
#endif
5414-
ssl->in_ext->in_iv = ssl->in_hdr + 5;
5414+
mbedtls_ssl_get_in_ext(ssl)->in_iv = ssl->in_hdr + 5;
54155415
}
54165416

54175417
/* This will be adjusted at record decryption time. */
5418-
ssl->in_msg = ssl->in_ext->in_iv;
5418+
ssl->in_msg = mbedtls_ssl_get_in_ext(ssl)->in_iv;
54195419
}
54205420

54215421
/*

library/ssl_tls.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,11 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
349349
size_t hshdr_in = 0;
350350
if (ssl->in_buf != NULL) {
351351
written_in = ssl->in_msg - ssl->in_buf;
352-
iv_offset_in = ssl->in_ext->in_iv - ssl->in_buf;
352+
iv_offset_in = mbedtls_ssl_get_in_ext(ssl)->in_iv - ssl->in_buf;
353353
len_offset_in = ssl->in_len - ssl->in_buf;
354354
hdr_in = ssl->in_hdr - ssl->in_buf;
355-
if (ssl->in_ext->in_hshdr != NULL) {
356-
hshdr_in = ssl->in_ext->in_hshdr - ssl->in_buf;
355+
if (mbedtls_ssl_get_in_ext(ssl)->in_hshdr != NULL) {
356+
hshdr_in = mbedtls_ssl_get_in_ext(ssl)->in_hshdr - ssl->in_buf;
357357
}
358358
if (downsizing ?
359359
ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
@@ -398,9 +398,9 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
398398

399399
ssl->in_msg = ssl->in_buf + written_in;
400400
ssl->in_len = ssl->in_buf + len_offset_in;
401-
ssl->in_ext->in_iv = ssl->in_buf + iv_offset_in;
402-
if (ssl->in_ext->in_hshdr != NULL) {
403-
ssl->in_ext->in_hshdr = ssl->in_buf + hshdr_in;
401+
mbedtls_ssl_get_in_ext(ssl)->in_iv = ssl->in_buf + iv_offset_in;
402+
if (mbedtls_ssl_get_in_ext(ssl)->in_hshdr != NULL) {
403+
mbedtls_ssl_get_in_ext(ssl)->in_hshdr = ssl->in_buf + hshdr_in;
404404
}
405405
}
406406
}
@@ -1459,7 +1459,7 @@ int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
14591459
ssl->in_hdr = NULL;
14601460
ssl->in_ctr = NULL;
14611461
ssl->in_len = NULL;
1462-
ssl->in_ext->in_iv = NULL;
1462+
mbedtls_ssl_get_in_ext(ssl)->in_iv = NULL;
14631463
ssl->in_msg = NULL;
14641464

14651465
ssl->out_hdr = NULL;
@@ -1507,8 +1507,8 @@ void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
15071507
ssl->in_hslen = 0;
15081508
ssl->keep_current_message = 0;
15091509
ssl->transform_in = NULL;
1510-
ssl->in_ext->in_hshdr = NULL;
1511-
ssl->in_ext->in_hsfraglen = 0;
1510+
mbedtls_ssl_get_in_ext(ssl)->in_hshdr = NULL;
1511+
mbedtls_ssl_get_in_ext(ssl)->in_hsfraglen = 0;
15121512

15131513
#if defined(MBEDTLS_SSL_PROTO_DTLS)
15141514
ssl->next_record_offset = 0;

0 commit comments

Comments
 (0)