Skip to content

Commit 99a99e3

Browse files
committed
Implement DTLS 1.2 Connection ID (CID)
1 parent bbbc40d commit 99a99e3

13 files changed

Lines changed: 1079 additions & 674 deletions

File tree

.github/workflows/os-check.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ jobs:
3838
'--enable-all --enable-dtls13 --enable-dtls-frag-ch',
3939
'--enable-dtls --enable-dtls13 --enable-dtls-frag-ch
4040
--enable-dtls-mtu',
41+
'--enable-dtls --enable-dtlscid --enable-dtls13 --enable-secure-renegotiation
42+
--enable-psk --enable-aesccm --enable-nullcipher CPPFLAGS=-DWOLFSSL_STATIC_RSA',
4143
]
4244
name: make check
4345
runs-on: ${{ matrix.os }}

examples/client/client.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4184,10 +4184,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
41844184

41854185
printf("CID extension was negotiated\n");
41864186
ret = wolfSSL_dtls_cid_get_tx_size(ssl, &receivedCIDSz);
4187-
if (ret != WOLFSSL_SUCCESS)
4188-
err_sys("Can't get negotiated DTLS CID size\n");
4189-
4190-
if (receivedCIDSz > 0) {
4187+
if (ret == WOLFSSL_SUCCESS && receivedCIDSz > 0) {
41914188
ret = wolfSSL_dtls_cid_get_tx(ssl, receivedCID,
41924189
DTLS_CID_BUFFER_SIZE - 1);
41934190
if (ret != WOLFSSL_SUCCESS)

examples/server/server.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,10 +3595,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
35953595
unsigned int receivedCIDSz;
35963596
printf("CID extension was negotiated\n");
35973597
ret = wolfSSL_dtls_cid_get_tx_size(ssl, &receivedCIDSz);
3598-
if (ret != WOLFSSL_SUCCESS)
3599-
err_sys("Can't get negotiated DTLS CID size\n");
3600-
3601-
if (receivedCIDSz > 0) {
3598+
if (ret == WOLFSSL_SUCCESS && receivedCIDSz > 0) {
36023599
ret = wolfSSL_dtls_cid_get_tx(ssl, receivedCID,
36033600
DTLS_CID_BUFFER_SIZE - 1);
36043601
if (ret != WOLFSSL_SUCCESS)

src/dtls.c

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,22 +1038,6 @@ int DoClientHelloStateless(WOLFSSL* ssl, const byte* input, word32 helloSz,
10381038

10391039
#if defined(WOLFSSL_DTLS_CID)
10401040

1041-
typedef struct ConnectionID {
1042-
byte length;
1043-
/* Ignore "nonstandard extension used : zero-sized array in struct/union"
1044-
* MSVC warning */
1045-
#ifdef _MSC_VER
1046-
#pragma warning(disable: 4200)
1047-
#endif
1048-
byte id[];
1049-
} ConnectionID;
1050-
1051-
typedef struct CIDInfo {
1052-
ConnectionID* tx;
1053-
ConnectionID* rx;
1054-
byte negotiated : 1;
1055-
} CIDInfo;
1056-
10571041
static ConnectionID* DtlsCidNew(const byte* cid, byte size, void* heap)
10581042
{
10591043
ConnectionID* ret;
@@ -1079,20 +1063,22 @@ static int DtlsCidGetSize(WOLFSSL* ssl, unsigned int* size, int rx)
10791063
ConnectionID* id;
10801064
CIDInfo* info;
10811065

1082-
if (ssl == NULL || size == NULL)
1066+
if (ssl == NULL)
10831067
return BAD_FUNC_ARG;
10841068

10851069
info = DtlsCidGetInfo(ssl);
10861070
if (info == NULL)
10871071
return WOLFSSL_FAILURE;
10881072

10891073
id = rx ? info->rx : info->tx;
1090-
if (id == NULL) {
1091-
*size = 0;
1092-
return WOLFSSL_SUCCESS;
1074+
if (id == NULL || id->length == 0) {
1075+
if (size != NULL)
1076+
*size = 0;
1077+
return WOLFSSL_FAILURE;
10931078
}
10941079

1095-
*size = id->length;
1080+
if (size != NULL)
1081+
*size = id->length;
10961082
return WOLFSSL_SUCCESS;
10971083
}
10981084

@@ -1231,9 +1217,8 @@ int TLSX_ConnectionID_Use(WOLFSSL* ssl)
12311217
int TLSX_ConnectionID_Parse(WOLFSSL* ssl, const byte* input, word16 length,
12321218
byte isRequest)
12331219
{
1234-
ConnectionID* id;
12351220
CIDInfo* info;
1236-
byte cidSize;
1221+
byte cidSz;
12371222
TLSX* ext;
12381223

12391224
ext = TLSX_Find(ssl->extensions, TLSX_CONNECTION_ID);
@@ -1254,31 +1239,41 @@ int TLSX_ConnectionID_Parse(WOLFSSL* ssl, const byte* input, word16 length,
12541239
return BAD_STATE_E;
12551240

12561241
/* it may happen if we process two ClientHello because the server sent an
1257-
* HRR request */
1242+
* HRR/HVR request */
12581243
if (info->tx != NULL) {
12591244
if (ssl->options.side != WOLFSSL_SERVER_END &&
1260-
ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE)
1245+
ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE &&
1246+
!IsSCR(ssl))
12611247
return BAD_STATE_E;
12621248

1263-
XFREE(info->tx, ssl->heap, DYNAMIC_TYPE_TLSX);
1264-
info->tx = NULL;
1249+
if (!info->negotiated) {
1250+
XFREE(info->tx, ssl->heap, DYNAMIC_TYPE_TLSX);
1251+
info->tx = NULL;
1252+
}
12651253
}
12661254

12671255
if (length < OPAQUE8_LEN)
12681256
return BUFFER_ERROR;
12691257

1270-
cidSize = *input;
1271-
if (cidSize + OPAQUE8_LEN > length)
1258+
cidSz = *input;
1259+
if (cidSz + OPAQUE8_LEN > length)
12721260
return BUFFER_ERROR;
12731261

1274-
if (cidSize > 0) {
1275-
id = (ConnectionID*)XMALLOC(sizeof(*id) + cidSize, ssl->heap,
1276-
DYNAMIC_TYPE_TLSX);
1277-
if (id == NULL)
1278-
return MEMORY_ERROR;
1279-
XMEMCPY(id->id, input + OPAQUE8_LEN, cidSize);
1280-
id->length = cidSize;
1281-
info->tx = id;
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+
}
12821277
}
12831278

12841279
info->negotiated = 1;
@@ -1317,10 +1312,6 @@ int wolfSSL_dtls_cid_use(WOLFSSL* ssl)
13171312
{
13181313
int ret;
13191314

1320-
/* CID is supported on DTLSv1.3 only */
1321-
if (!IsAtLeastTLSv1_3(ssl->version))
1322-
return WOLFSSL_FAILURE;
1323-
13241315
ssl->options.useDtlsCID = 1;
13251316
ret = TLSX_ConnectionID_Use(ssl);
13261317
if (ret != 0)
@@ -1345,8 +1336,11 @@ int wolfSSL_dtls_cid_set(WOLFSSL* ssl, unsigned char* cid, unsigned int size)
13451336
if (cidInfo == NULL)
13461337
return WOLFSSL_FAILURE;
13471338

1348-
XFREE(cidInfo->rx, ssl->heap, DYNAMIC_TYPE_TLSX);
1349-
cidInfo->rx = NULL;
1339+
if (cidInfo->rx != NULL) {
1340+
WOLFSSL_MSG("wolfSSL doesn't support changing the CID during a "
1341+
"connection");
1342+
return WOLFSSL_FAILURE;
1343+
}
13501344

13511345
/* empty CID */
13521346
if (size == 0)
@@ -1384,6 +1378,11 @@ int wolfSSL_dtls_cid_get_tx(WOLFSSL* ssl, unsigned char* buf,
13841378
return DtlsCidGet(ssl, buf, bufferSz, 0);
13851379
}
13861380

1381+
int wolfSSL_dtls_cid_max_size(void)
1382+
{
1383+
return DTLS_CID_MAX_SIZE;
1384+
}
1385+
13871386
#endif /* WOLFSSL_DTLS_CID */
13881387
#endif /* WOLFSSL_DTLS */
13891388

src/dtls13.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,23 +1076,23 @@ static byte Dtls13GetCidRxSize(WOLFSSL* ssl)
10761076

10771077
static int Dtls13AddCID(WOLFSSL* ssl, byte* flags, byte* out, word16* idx)
10781078
{
1079-
byte cidSize;
1079+
byte cidSz;
10801080
int ret;
10811081

10821082
if (!wolfSSL_dtls_cid_is_enabled(ssl))
10831083
return 0;
10841084

1085-
cidSize = Dtls13GetCidTxSize(ssl);
1085+
cidSz = Dtls13GetCidTxSize(ssl);
10861086

10871087
/* no cid */
1088-
if (cidSize == 0)
1088+
if (cidSz == 0)
10891089
return 0;
10901090
*flags |= DTLS13_CID_BIT;
1091-
/* we know that we have at least cidSize of space */
1092-
ret = wolfSSL_dtls_cid_get_tx(ssl, out + *idx, cidSize);
1091+
/* we know that we have at least cidSz of space */
1092+
ret = wolfSSL_dtls_cid_get_tx(ssl, out + *idx, cidSz);
10931093
if (ret != WOLFSSL_SUCCESS)
10941094
return ret;
1095-
*idx += cidSize;
1095+
*idx += cidSz;
10961096
return 0;
10971097
}
10981098

0 commit comments

Comments
 (0)