Skip to content

Commit fb2144d

Browse files
Merge pull request #7951 from julek-wolfssl/dtls13-cid-fixes
DTLS 1.3 CID fixes
2 parents c390047 + 84c80b4 commit fb2144d

11 files changed

Lines changed: 491 additions & 271 deletions

File tree

src/dtls13.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,11 @@ int Dtls13HandshakeAddHeader(WOLFSSL* ssl, byte* output,
12091209
return 0;
12101210
}
12111211

1212+
int Dtls13MinimumRecordLength(WOLFSSL* ssl)
1213+
{
1214+
return Dtls13GetRlHeaderLength(ssl, 1) + DTLS13_MIN_CIPHERTEXT;
1215+
}
1216+
12121217
/**
12131218
* Dtls13EncryptRecordNumber() - encrypt record number in the header
12141219
* @ssl: ssl object
@@ -1225,9 +1230,15 @@ int Dtls13EncryptRecordNumber(WOLFSSL* ssl, byte* hdr, word16 recordLength)
12251230
if (ssl == NULL || hdr == NULL)
12261231
return BAD_FUNC_ARG;
12271232

1233+
#ifdef HAVE_NULL_CIPHER
1234+
/* Do not encrypt record numbers with null cipher. See RFC 9150 Sec 9 */
1235+
if (ssl->specs.bulk_cipher_algorithm == wolfssl_cipher_null)
1236+
return 0;
1237+
#endif /*HAVE_NULL_CIPHER */
1238+
12281239
/* we need at least a 16 bytes of ciphertext to encrypt record number see
12291240
4.2.3*/
1230-
if (recordLength < Dtls13GetRlHeaderLength(ssl, 1) + DTLS13_MIN_CIPHERTEXT)
1241+
if (recordLength < Dtls13MinimumRecordLength(ssl))
12311242
return BUFFER_ERROR;
12321243

12331244
seqLength = (*hdr & DTLS13_LEN_BIT) ? DTLS13_SEQ_16_LEN : DTLS13_SEQ_8_LEN;
@@ -1453,17 +1464,22 @@ int Dtls13ParseUnifiedRecordLayer(WOLFSSL* ssl, const byte* input,
14531464
hdrInfo->recordLength = inputSize - idx;
14541465
}
14551466

1456-
/* minimum size for a dtls1.3 packet is 16 bytes (to have enough ciphertext
1457-
to create record number xor mask). (draft 43 - Sec 4.2.3) */
1458-
if (hdrInfo->recordLength < DTLS13_RN_MASK_SIZE)
1459-
return LENGTH_ERROR;
1460-
if (inputSize < idx + DTLS13_RN_MASK_SIZE)
1461-
return BUFFER_ERROR;
1467+
/* Do not encrypt record numbers with null cipher. See RFC 9150 Sec 9 */
1468+
if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null)
1469+
{
1470+
/* minimum size for a dtls1.3 packet is 16 bytes (to have enough
1471+
* ciphertext to create record number xor mask).
1472+
* (draft 43 - Sec 4.2.3) */
1473+
if (hdrInfo->recordLength < DTLS13_RN_MASK_SIZE)
1474+
return LENGTH_ERROR;
1475+
if (inputSize < idx + DTLS13_RN_MASK_SIZE)
1476+
return BUFFER_ERROR;
14621477

1463-
ret = Dtls13EncryptDecryptRecordNumber(ssl, seqNum, seqLen, input + idx,
1464-
DEPROTECT);
1465-
if (ret != 0)
1466-
return ret;
1478+
ret = Dtls13EncryptDecryptRecordNumber(ssl, seqNum, seqLen, input + idx,
1479+
DEPROTECT);
1480+
if (ret != 0)
1481+
return ret;
1482+
}
14671483

14681484
if (seqLen == DTLS13_SEQ_16_LEN) {
14691485
hdrInfo->seqHiPresent = 1;

src/internal.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11255,6 +11255,11 @@ static int GetDtls13RecordHeader(WOLFSSL* ssl, word32* inOutIdx,
1125511255
if (ret != 0)
1125611256
return ret;
1125711257

11258+
if (ssl->dtls13CurRlLength > sizeof(ssl->dtls13CurRL)) {
11259+
WOLFSSL_MSG("Record header too long");
11260+
return SEQUENCE_ERROR;
11261+
}
11262+
1125811263
if (readSize < ssl->dtls13CurRlLength + DTLS13_RN_MASK_SIZE) {
1125911264
/* when using DTLS over a medium that does not guarantee that a full
1126011265
* message is received in a single read, we may end up without the full
@@ -24789,6 +24794,14 @@ int SendData(WOLFSSL* ssl, const void* data, int sz)
2478924794
if (IsEncryptionOn(ssl, 1) || ssl->options.tls1_3)
2479024795
outputSz += cipherExtraData(ssl);
2479124796

24797+
#if defined(WOLFSSL_DTLS) && defined(WOLFSSL_DTLS_CID)
24798+
if (ssl->options.dtls) {
24799+
unsigned int cidSz = 0;
24800+
if (wolfSSL_dtls_cid_get_tx_size(ssl, &cidSz) == WOLFSSL_SUCCESS)
24801+
outputSz += cidSz;
24802+
}
24803+
#endif
24804+
2479224805
/* check for available size */
2479324806
if ((ret = CheckAvailableSize(ssl, outputSz)) != 0)
2479424807
return ssl->error = ret;
@@ -25935,7 +25948,7 @@ void SetErrorString(int error, char* str)
2593525948
*/
2593625949

2593725950
#ifndef NO_ERROR_STRINGS
25938-
#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
25951+
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_QT) || \
2593925952
defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
2594025953
#define SUITE_INFO(x,y,z,w,v,u) {(x),(y),(z),(w),(v),(u),WOLFSSL_CIPHER_SUITE_FLAG_NONE}
2594125954
#define SUITE_ALIAS(x,z,w,v,u) {(x),"",(z),(w),(v),(u),WOLFSSL_CIPHER_SUITE_FLAG_NAMEALIAS},
@@ -25944,7 +25957,7 @@ void SetErrorString(int error, char* str)
2594425957
#define SUITE_ALIAS(x,z,w,v,u) {(x),"",(z),(w),WOLFSSL_CIPHER_SUITE_FLAG_NAMEALIAS},
2594525958
#endif
2594625959
#else
25947-
#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
25960+
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_QT) || \
2594825961
defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
2594925962
#define SUITE_INFO(x,y,z,w,v,u) {(x),(z),(w),(v),(u),WOLFSSL_CIPHER_SUITE_FLAG_NONE}
2595025963
#define SUITE_ALIAS(x,z,w,v,u) {(x),(z),(w),(v),(u),WOLFSSL_CIPHER_SUITE_FLAG_NAMEALIAS},
@@ -26806,13 +26819,16 @@ const char* wolfSSL_get_cipher_name_iana(WOLFSSL* ssl)
2680626819
}
2680726820

2680826821
int GetCipherSuiteFromName(const char* name, byte* cipherSuite0,
26809-
byte* cipherSuite, int* flags)
26822+
byte* cipherSuite, byte* major, byte* minor, int* flags)
2681026823
{
2681126824
int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG);
2681226825
int i;
2681326826
unsigned long len;
2681426827
const char* nameDelim;
2681526828

26829+
(void)major;
26830+
(void)minor;
26831+
2681626832
/* Support trailing : */
2681726833
nameDelim = XSTRSTR(name, ":");
2681826834
if (nameDelim)
@@ -26830,9 +26846,19 @@ int GetCipherSuiteFromName(const char* name, byte* cipherSuite0,
2683026846
#endif
2683126847

2683226848
if (found) {
26833-
*cipherSuite0 = cipher_names[i].cipherSuite0;
26834-
*cipherSuite = cipher_names[i].cipherSuite;
26835-
*flags = cipher_names[i].flags;
26849+
if (cipherSuite0 != NULL)
26850+
*cipherSuite0 = cipher_names[i].cipherSuite0;
26851+
if (cipherSuite != NULL)
26852+
*cipherSuite = cipher_names[i].cipherSuite;
26853+
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_QT) || \
26854+
defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
26855+
if (major != NULL)
26856+
*major = cipher_names[i].major;
26857+
if (minor != NULL)
26858+
*minor = cipher_names[i].minor;
26859+
#endif
26860+
if (flags != NULL)
26861+
*flags = cipher_names[i].flags;
2683626862
ret = 0;
2683726863
break;
2683826864
}

src/ssl.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8412,6 +8412,8 @@ static int CheckcipherList(const char* list)
84128412
char name[MAX_SUITE_NAME + 1];
84138413
word32 length = MAX_SUITE_NAME;
84148414
word32 current_length;
8415+
byte major = INVALID_BYTE;
8416+
byte minor = INVALID_BYTE;
84158417

84168418
next = XSTRSTR(next, ":");
84178419

@@ -8436,10 +8438,10 @@ static int CheckcipherList(const char* list)
84368438
break;
84378439
}
84388440

8439-
ret = wolfSSL_get_cipher_suite_from_name(name, &cipherSuite0,
8440-
&cipherSuite1, &flags);
8441+
ret = GetCipherSuiteFromName(name, &cipherSuite0,
8442+
&cipherSuite1, &major, &minor, &flags);
84418443
if (ret == 0) {
8442-
if (cipherSuite0 == TLS13_BYTE) {
8444+
if (cipherSuite0 == TLS13_BYTE || minor == TLSv1_3_MINOR) {
84438445
/* TLSv13 suite */
84448446
findTLSv13Suites = 1;
84458447
}
@@ -14297,7 +14299,8 @@ int wolfSSL_get_cipher_suite_from_name(const char* name, byte* cipherSuite0,
1429714299
(cipherSuite == NULL) ||
1429814300
(flags == NULL))
1429914301
return BAD_FUNC_ARG;
14300-
return GetCipherSuiteFromName(name, cipherSuite0, cipherSuite, flags);
14302+
return GetCipherSuiteFromName(name, cipherSuite0, cipherSuite, NULL, NULL,
14303+
flags);
1430114304
}
1430214305

1430314306

src/tls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13500,7 +13500,7 @@ int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
1350013500
ssl->arrays->client_identity, MAX_PSK_ID_LEN,
1350113501
ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
1350213502
if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
13503-
&cipherSuite, &cipherSuiteFlags) != 0) {
13503+
&cipherSuite, NULL, NULL, &cipherSuiteFlags) != 0) {
1350413504
return PSK_KEY_ERROR;
1350513505
}
1350613506
}

src/tls13.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3199,6 +3199,7 @@ typedef struct BuildMsg13Args {
31993199
word32 idx;
32003200
word32 headerSz;
32013201
word16 size;
3202+
word32 paddingSz;
32023203
} BuildMsg13Args;
32033204

32043205
static void FreeBuildMsg13Args(WOLFSSL* ssl, void* pArgs)
@@ -3304,7 +3305,14 @@ int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
33043305
args->sz++;
33053306
/* Authentication data at the end. */
33063307
args->sz += ssl->specs.aead_mac_size;
3307-
3308+
#ifdef WOLFSSL_DTLS13
3309+
/* Pad to minimum length */
3310+
if (ssl->options.dtls &&
3311+
args->sz < (word32)Dtls13MinimumRecordLength(ssl)) {
3312+
args->paddingSz = Dtls13MinimumRecordLength(ssl) - args->sz;
3313+
args->sz = Dtls13MinimumRecordLength(ssl);
3314+
}
3315+
#endif
33083316
if (sizeOnly)
33093317
return (int)args->sz;
33103318

@@ -3348,6 +3356,9 @@ int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
33483356

33493357
/* The real record content type goes at the end of the data. */
33503358
output[args->idx++] = (byte)type;
3359+
/* Double check that any necessary padding is zero'd out */
3360+
XMEMSET(output + args->idx, 0, args->paddingSz);
3361+
args->idx += args->paddingSz;
33513362

33523363
ssl->options.buildMsgState = BUILD_MSG_ENCRYPT;
33533364
}
@@ -3393,7 +3404,8 @@ int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
33933404
#ifdef WOLFSSL_DTLS13
33943405
if (ret == 0 && ssl->options.dtls) {
33953406
/* AAD points to the header. Reuse the variable */
3396-
ret = Dtls13EncryptRecordNumber(ssl, (byte*)aad, (word16)args->sz);
3407+
ret = Dtls13EncryptRecordNumber(ssl, (byte*)aad,
3408+
(word16)args->sz);
33973409
}
33983410
#endif /* WOLFSSL_DTLS13 */
33993411
}
@@ -3940,7 +3952,7 @@ static int SetupPskKey(WOLFSSL* ssl, PreSharedKey* psk, int clientHello)
39403952
MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN,
39413953
&cipherName);
39423954
if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
3943-
&cipherSuite, &cipherSuiteFlags) != 0) {
3955+
&cipherSuite, NULL, NULL, &cipherSuiteFlags) != 0) {
39443956
WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
39453957
return PSK_KEY_ERROR;
39463958
}
@@ -5852,7 +5864,7 @@ int FindPskSuite(const WOLFSSL* ssl, PreSharedKey* psk, byte* psk_key,
58525864
if (*psk_keySz != 0) {
58535865
int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
58545866
*found = (GetCipherSuiteFromName(cipherName, &cipherSuite0,
5855-
&cipherSuite, &cipherSuiteFlags) == 0);
5867+
&cipherSuite, NULL, NULL, &cipherSuiteFlags) == 0);
58565868
(void)cipherSuiteFlags;
58575869
}
58585870
}

0 commit comments

Comments
 (0)