Skip to content

Commit cf96ab2

Browse files
committed
Address code review
1 parent 99a99e3 commit cf96ab2

5 files changed

Lines changed: 103 additions & 110 deletions

File tree

src/dtls.c

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,22 +1063,20 @@ static int DtlsCidGetSize(WOLFSSL* ssl, unsigned int* size, int rx)
10631063
ConnectionID* id;
10641064
CIDInfo* info;
10651065

1066-
if (ssl == NULL)
1066+
if (ssl == NULL || size == NULL)
10671067
return BAD_FUNC_ARG;
10681068

10691069
info = DtlsCidGetInfo(ssl);
10701070
if (info == NULL)
10711071
return WOLFSSL_FAILURE;
10721072

10731073
id = rx ? info->rx : info->tx;
1074-
if (id == NULL || id->length == 0) {
1075-
if (size != NULL)
1076-
*size = 0;
1077-
return WOLFSSL_FAILURE;
1074+
if (id == NULL) {
1075+
*size = 0;
1076+
return WOLFSSL_SUCCESS;
10781077
}
10791078

1080-
if (size != NULL)
1081-
*size = id->length;
1079+
*size = id->length;
10821080
return WOLFSSL_SUCCESS;
10831081
}
10841082

@@ -1234,46 +1232,42 @@ int TLSX_ConnectionID_Parse(WOLFSSL* ssl, const byte* input, word16 length,
12341232
}
12351233
}
12361234

1235+
if (length < OPAQUE8_LEN)
1236+
return BUFFER_ERROR;
1237+
1238+
cidSz = *input;
1239+
if (cidSz + OPAQUE8_LEN > length)
1240+
return BUFFER_ERROR;
1241+
12371242
info = DtlsCidGetInfo(ssl);
12381243
if (info == NULL)
12391244
return BAD_STATE_E;
12401245

12411246
/* it may happen if we process two ClientHello because the server sent an
12421247
* HRR/HVR request */
1243-
if (info->tx != NULL) {
1248+
if (info->tx != NULL || info->negotiated) {
12441249
if (ssl->options.side != WOLFSSL_SERVER_END &&
12451250
ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE &&
12461251
!IsSCR(ssl))
12471252
return BAD_STATE_E;
12481253

1249-
if (!info->negotiated) {
1250-
XFREE(info->tx, ssl->heap, DYNAMIC_TYPE_TLSX);
1251-
info->tx = NULL;
1252-
}
1253-
}
1254-
1255-
if (length < OPAQUE8_LEN)
1256-
return BUFFER_ERROR;
1257-
1258-
cidSz = *input;
1259-
if (cidSz + OPAQUE8_LEN > length)
1260-
return BUFFER_ERROR;
1254+
/* Should not be null if negotiated */
1255+
if (info->tx == NULL)
1256+
return BAD_STATE_E;
12611257

1262-
if (cidSz > 0) {
1263-
if (!info->negotiated) {
1264-
ConnectionID* id = (ConnectionID*)XMALLOC(sizeof(*id) + cidSz,
1265-
ssl->heap, DYNAMIC_TYPE_TLSX);
1266-
if (id == NULL)
1267-
return MEMORY_ERROR;
1268-
XMEMCPY(id->id, input + OPAQUE8_LEN, cidSz);
1269-
id->length = cidSz;
1270-
info->tx = id;
1271-
}
1272-
else {
1273-
/* For now we don't support changing the CID on a rehandshake */
1274-
if (XMEMCMP(info->tx->id, input + OPAQUE8_LEN, cidSz) != 0)
1275-
return DTLS_CID_ERROR;
1276-
}
1258+
/* For now we don't support changing the CID on a rehandshake */
1259+
if (cidSz != info->tx->length ||
1260+
XMEMCMP(info->tx->id, input + OPAQUE8_LEN, cidSz) != 0)
1261+
return DTLS_CID_ERROR;
1262+
}
1263+
else if (cidSz > 0) {
1264+
ConnectionID* id = (ConnectionID*)XMALLOC(sizeof(*id) + cidSz,
1265+
ssl->heap, DYNAMIC_TYPE_TLSX);
1266+
if (id == NULL)
1267+
return MEMORY_ERROR;
1268+
XMEMCPY(id->id, input + OPAQUE8_LEN, cidSz);
1269+
id->length = cidSz;
1270+
info->tx = id;
12771271
}
12781272

12791273
info->negotiated = 1;
@@ -1382,8 +1376,38 @@ int wolfSSL_dtls_cid_max_size(void)
13821376
{
13831377
return DTLS_CID_MAX_SIZE;
13841378
}
1385-
13861379
#endif /* WOLFSSL_DTLS_CID */
1380+
1381+
byte DtlsGetCidTxSize(WOLFSSL* ssl)
1382+
{
1383+
#ifdef WOLFSSL_DTLS_CID
1384+
unsigned int cidSz;
1385+
int ret;
1386+
ret = wolfSSL_dtls_cid_get_tx_size(ssl, &cidSz);
1387+
if (ret != WOLFSSL_SUCCESS)
1388+
return 0;
1389+
return (byte)cidSz;
1390+
#else
1391+
(void)ssl;
1392+
return 0;
1393+
#endif
1394+
}
1395+
1396+
byte DtlsGetCidRxSize(WOLFSSL* ssl)
1397+
{
1398+
#ifdef WOLFSSL_DTLS_CID
1399+
unsigned int cidSz;
1400+
int ret;
1401+
ret = wolfSSL_dtls_cid_get_rx_size(ssl, &cidSz);
1402+
if (ret != WOLFSSL_SUCCESS)
1403+
return 0;
1404+
return (byte)cidSz;
1405+
#else
1406+
(void)ssl;
1407+
return 0;
1408+
#endif
1409+
}
1410+
13871411
#endif /* WOLFSSL_DTLS */
13881412

13891413
#endif /* WOLFCRYPT_ONLY */

src/dtls13.c

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,25 +1054,6 @@ static WC_INLINE word8 Dtls13GetEpochBits(w64wrapper epoch)
10541054
}
10551055

10561056
#ifdef WOLFSSL_DTLS_CID
1057-
static byte Dtls13GetCidTxSize(WOLFSSL* ssl)
1058-
{
1059-
unsigned int cidSz;
1060-
int ret;
1061-
ret = wolfSSL_dtls_cid_get_tx_size(ssl, &cidSz);
1062-
if (ret != WOLFSSL_SUCCESS)
1063-
return 0;
1064-
return (byte)cidSz;
1065-
}
1066-
1067-
static byte Dtls13GetCidRxSize(WOLFSSL* ssl)
1068-
{
1069-
unsigned int cidSz;
1070-
int ret;
1071-
ret = wolfSSL_dtls_cid_get_rx_size(ssl, &cidSz);
1072-
if (ret != WOLFSSL_SUCCESS)
1073-
return 0;
1074-
return (byte)cidSz;
1075-
}
10761057

10771058
static int Dtls13AddCID(WOLFSSL* ssl, byte* flags, byte* out, word16* idx)
10781059
{
@@ -1082,7 +1063,7 @@ static int Dtls13AddCID(WOLFSSL* ssl, byte* flags, byte* out, word16* idx)
10821063
if (!wolfSSL_dtls_cid_is_enabled(ssl))
10831064
return 0;
10841065

1085-
cidSz = Dtls13GetCidTxSize(ssl);
1066+
cidSz = DtlsGetCidTxSize(ssl);
10861067

10871068
/* no cid */
10881069
if (cidSz == 0)
@@ -1138,8 +1119,6 @@ static int Dtls13UnifiedHeaderParseCID(WOLFSSL* ssl, byte flags,
11381119

11391120
#else
11401121
#define Dtls13AddCID(a, b, c, d) 0
1141-
#define Dtls13GetCidRxSize(a) 0
1142-
#define Dtls13GetCidTxSize(a) 0
11431122
#define Dtls13UnifiedHeaderParseCID(a, b, c, d, e) 0
11441123
#endif /* WOLFSSL_DTLS_CID */
11451124

@@ -1245,7 +1224,7 @@ int Dtls13EncryptRecordNumber(WOLFSSL* ssl, byte* hdr, word16 recordLength)
12451224

12461225
seqLength = (*hdr & DTLS13_LEN_BIT) ? DTLS13_SEQ_16_LEN : DTLS13_SEQ_8_LEN;
12471226

1248-
cidSz = Dtls13GetCidTxSize(ssl);
1227+
cidSz = DtlsGetCidTxSize(ssl);
12491228
/* header flags + seq number + CID size*/
12501229
hdrLength = OPAQUE8_LEN + seqLength + cidSz;
12511230

@@ -1276,7 +1255,7 @@ word16 Dtls13GetRlHeaderLength(WOLFSSL* ssl, byte isEncrypted)
12761255
if (!isEncrypted)
12771256
return DTLS_RECORD_HEADER_SZ;
12781257

1279-
return DTLS13_UNIFIED_HEADER_SIZE + Dtls13GetCidTxSize(ssl);
1258+
return DTLS13_UNIFIED_HEADER_SIZE + DtlsGetCidTxSize(ssl);
12801259
}
12811260

12821261
/**
@@ -1403,7 +1382,7 @@ int Dtls13GetUnifiedHeaderSize(WOLFSSL* ssl, const byte input, word16* size)
14031382
return BAD_FUNC_ARG;
14041383

14051384
/* flags (1) + CID + seq 8bit (1) */
1406-
*size = OPAQUE8_LEN + Dtls13GetCidRxSize(ssl) + OPAQUE8_LEN;
1385+
*size = OPAQUE8_LEN + DtlsGetCidRxSize(ssl) + OPAQUE8_LEN;
14071386
if (input & DTLS13_SEQ_LEN_BIT)
14081387
*size += OPAQUE8_LEN;
14091388
if (input & DTLS13_LEN_BIT)

0 commit comments

Comments
 (0)