Skip to content

Commit ac788ec

Browse files
authored
Merge pull request #7995 from julek-wolfssl/dtls12-cid
Implement DTLS 1.2 Connection ID (CID)
2 parents b8dff12 + 7e69c20 commit ac788ec

13 files changed

Lines changed: 1113 additions & 700 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
if: github.repository_owner == 'wolfssl'

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: 65 additions & 42 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;
@@ -1231,9 +1215,8 @@ int TLSX_ConnectionID_Use(WOLFSSL* ssl)
12311215
int TLSX_ConnectionID_Parse(WOLFSSL* ssl, const byte* input, word16 length,
12321216
byte isRequest)
12331217
{
1234-
ConnectionID* id;
12351218
CIDInfo* info;
1236-
byte cidSize;
1219+
byte cidSz;
12371220
TLSX* ext;
12381221

12391222
ext = TLSX_Find(ssl->extensions, TLSX_CONNECTION_ID);
@@ -1249,35 +1232,41 @@ int TLSX_ConnectionID_Parse(WOLFSSL* ssl, const byte* input, word16 length,
12491232
}
12501233
}
12511234

1235+
if (length < OPAQUE8_LEN)
1236+
return BUFFER_ERROR;
1237+
1238+
cidSz = *input;
1239+
if (cidSz + OPAQUE8_LEN > length)
1240+
return BUFFER_ERROR;
1241+
12521242
info = DtlsCidGetInfo(ssl);
12531243
if (info == NULL)
12541244
return BAD_STATE_E;
12551245

12561246
/* it may happen if we process two ClientHello because the server sent an
1257-
* HRR request */
1258-
if (info->tx != NULL) {
1247+
* HRR/HVR request */
1248+
if (info->tx != NULL || info->negotiated) {
12591249
if (ssl->options.side != WOLFSSL_SERVER_END &&
1260-
ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE)
1250+
ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE &&
1251+
!IsSCR(ssl))
12611252
return BAD_STATE_E;
12621253

1263-
XFREE(info->tx, ssl->heap, DYNAMIC_TYPE_TLSX);
1264-
info->tx = NULL;
1265-
}
1266-
1267-
if (length < OPAQUE8_LEN)
1268-
return BUFFER_ERROR;
1269-
1270-
cidSize = *input;
1271-
if (cidSize + OPAQUE8_LEN > length)
1272-
return BUFFER_ERROR;
1254+
/* Should not be null if negotiated */
1255+
if (info->tx == NULL)
1256+
return BAD_STATE_E;
12731257

1274-
if (cidSize > 0) {
1275-
id = (ConnectionID*)XMALLOC(sizeof(*id) + cidSize, ssl->heap,
1276-
DYNAMIC_TYPE_TLSX);
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);
12771266
if (id == NULL)
12781267
return MEMORY_ERROR;
1279-
XMEMCPY(id->id, input + OPAQUE8_LEN, cidSize);
1280-
id->length = cidSize;
1268+
XMEMCPY(id->id, input + OPAQUE8_LEN, cidSz);
1269+
id->length = cidSz;
12811270
info->tx = id;
12821271
}
12831272

@@ -1317,10 +1306,6 @@ int wolfSSL_dtls_cid_use(WOLFSSL* ssl)
13171306
{
13181307
int ret;
13191308

1320-
/* CID is supported on DTLSv1.3 only */
1321-
if (!IsAtLeastTLSv1_3(ssl->version))
1322-
return WOLFSSL_FAILURE;
1323-
13241309
ssl->options.useDtlsCID = 1;
13251310
ret = TLSX_ConnectionID_Use(ssl);
13261311
if (ret != 0)
@@ -1345,8 +1330,11 @@ int wolfSSL_dtls_cid_set(WOLFSSL* ssl, unsigned char* cid, unsigned int size)
13451330
if (cidInfo == NULL)
13461331
return WOLFSSL_FAILURE;
13471332

1348-
XFREE(cidInfo->rx, ssl->heap, DYNAMIC_TYPE_TLSX);
1349-
cidInfo->rx = NULL;
1333+
if (cidInfo->rx != NULL) {
1334+
WOLFSSL_MSG("wolfSSL doesn't support changing the CID during a "
1335+
"connection");
1336+
return WOLFSSL_FAILURE;
1337+
}
13501338

13511339
/* empty CID */
13521340
if (size == 0)
@@ -1384,7 +1372,42 @@ int wolfSSL_dtls_cid_get_tx(WOLFSSL* ssl, unsigned char* buf,
13841372
return DtlsCidGet(ssl, buf, bufferSz, 0);
13851373
}
13861374

1375+
int wolfSSL_dtls_cid_max_size(void)
1376+
{
1377+
return DTLS_CID_MAX_SIZE;
1378+
}
13871379
#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+
13881411
#endif /* WOLFSSL_DTLS */
13891412

13901413
#endif /* WOLFCRYPT_ONLY */

src/dtls13.c

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,45 +1054,26 @@ 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
{
1079-
byte cidSize;
1060+
byte cidSz;
10801061
int ret;
10811062

10821063
if (!wolfSSL_dtls_cid_is_enabled(ssl))
10831064
return 0;
10841065

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

10871068
/* no cid */
1088-
if (cidSize == 0)
1069+
if (cidSz == 0)
10891070
return 0;
10901071
*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);
1072+
/* we know that we have at least cidSz of space */
1073+
ret = wolfSSL_dtls_cid_get_tx(ssl, out + *idx, cidSz);
10931074
if (ret != WOLFSSL_SUCCESS)
10941075
return ret;
1095-
*idx += cidSize;
1076+
*idx += cidSz;
10961077
return 0;
10971078
}
10981079

@@ -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)