Skip to content

Commit 7b7f9a4

Browse files
committed
dtls: Check PSK ciphersuite against local list
1 parent c14b1a0 commit 7b7f9a4

File tree

4 files changed

+82
-67
lines changed

4 files changed

+82
-67
lines changed

src/dtls.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,13 @@ static int SendStatelessReplyDtls13(const WOLFSSL* ssl, WolfSSL_CH* ch)
732732

733733
/* Ask the user for the ciphersuite matching this identity */
734734
if (TLSX_PreSharedKey_Parse_ClientHello(&parsedExts,
735-
tlsx.elements, (word16)tlsx.size, ssl->heap) == 0)
735+
tlsx.elements, (word16)tlsx.size, ssl->heap) == 0) {
736+
/* suites only needs to be refined when searching for a PSK.
737+
* MatchSuite_ex handles refining internally. */
738+
refineSuites(WOLFSSL_SUITES(ssl), &suites, &suites,
739+
ssl->options.useClientOrder);
736740
FindPskSuiteFromExt(ssl, parsedExts, &pskInfo, &suites);
741+
}
737742
/* Revert to full handshake if PSK parsing failed */
738743

739744
if (pskInfo.isValid) {
@@ -753,8 +758,9 @@ static int SendStatelessReplyDtls13(const WOLFSSL* ssl, WolfSSL_CH* ch)
753758
ERROR_OUT(PSK_KEY_ERROR, dtls13_cleanup);
754759
doKE = 1;
755760
}
756-
else if ((modes & (1 << PSK_KE)) == 0) {
757-
ERROR_OUT(PSK_KEY_ERROR, dtls13_cleanup);
761+
else if ((modes & (1 << PSK_KE)) == 0 ||
762+
ssl->options.onlyPskDheKe) {
763+
ERROR_OUT(PSK_KEY_ERROR, dtls13_cleanup);
758764
}
759765
usePSK = 1;
760766
}

src/internal.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37424,6 +37424,74 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
3742437424
return 1;
3742537425
}
3742637426

37427+
void refineSuites(const Suites* sslSuites, const Suites* peerSuites,
37428+
Suites* outSuites, byte useClientOrder)
37429+
{
37430+
byte suites[WOLFSSL_MAX_SUITE_SZ];
37431+
word16 suiteSz = 0;
37432+
word16 i;
37433+
word16 j;
37434+
37435+
XMEMSET(suites, 0, sizeof(suites));
37436+
37437+
if (!useClientOrder) {
37438+
/* Server order refining. */
37439+
for (i = 0; i < sslSuites->suiteSz; i += 2) {
37440+
for (j = 0; j < peerSuites->suiteSz; j += 2) {
37441+
if ((sslSuites->suites[i+0] == peerSuites->suites[j+0]) &&
37442+
(sslSuites->suites[i+1] == peerSuites->suites[j+1])) {
37443+
suites[suiteSz++] = peerSuites->suites[j+0];
37444+
suites[suiteSz++] = peerSuites->suites[j+1];
37445+
break;
37446+
}
37447+
}
37448+
if (suiteSz == WOLFSSL_MAX_SUITE_SZ)
37449+
break;
37450+
}
37451+
}
37452+
else {
37453+
/* Client order refining. */
37454+
for (j = 0; j < peerSuites->suiteSz; j += 2) {
37455+
for (i = 0; i < sslSuites->suiteSz; i += 2) {
37456+
if ((sslSuites->suites[i+0] == peerSuites->suites[j+0]) &&
37457+
(sslSuites->suites[i+1] == peerSuites->suites[j+1])) {
37458+
suites[suiteSz++] = peerSuites->suites[j+0];
37459+
suites[suiteSz++] = peerSuites->suites[j+1];
37460+
break;
37461+
}
37462+
}
37463+
if (suiteSz == WOLFSSL_MAX_SUITE_SZ)
37464+
break;
37465+
}
37466+
}
37467+
37468+
outSuites->suiteSz = suiteSz;
37469+
XMEMCPY(outSuites->suites, &suites, sizeof(suites));
37470+
#ifdef WOLFSSL_DEBUG_TLS
37471+
{
37472+
int ii;
37473+
WOLFSSL_MSG("Refined Ciphers:");
37474+
for (ii = 0 ; ii < suites->suiteSz; ii += 2) {
37475+
WOLFSSL_MSG(GetCipherNameInternal(suites->suites[ii+0],
37476+
suites->suites[ii+1]));
37477+
}
37478+
}
37479+
#endif
37480+
}
37481+
37482+
/* Refine list of supported cipher suites to those common to server and client.
37483+
*
37484+
* ssl SSL/TLS object.
37485+
* peerSuites The peer's advertised list of supported cipher suites.
37486+
*/
37487+
void sslRefineSuites(WOLFSSL* ssl, Suites* peerSuites)
37488+
{
37489+
if (AllocateSuites(ssl) != 0)
37490+
return;
37491+
refineSuites(ssl->suites, peerSuites, ssl->suites,
37492+
(byte)ssl->options.useClientOrder);
37493+
}
37494+
3742737495
static int CompareSuites(const WOLFSSL* ssl, const Suites* suites,
3742837496
Suites* peerSuites, word16 i, word16 j,
3742937497
CipherSuite* cs, TLSX* extensions)

src/tls13.c

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5887,69 +5887,6 @@ static int DoTls13CertificateRequest(WOLFSSL* ssl, const byte* input,
58875887

58885888
#ifndef NO_WOLFSSL_SERVER
58895889
#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
5890-
/* Refine list of supported cipher suites to those common to server and client.
5891-
*
5892-
* ssl SSL/TLS object.
5893-
* peerSuites The peer's advertised list of supported cipher suites.
5894-
*/
5895-
static void RefineSuites(WOLFSSL* ssl, Suites* peerSuites)
5896-
{
5897-
byte suites[WOLFSSL_MAX_SUITE_SZ];
5898-
word16 suiteSz = 0;
5899-
word16 i;
5900-
word16 j;
5901-
5902-
if (AllocateSuites(ssl) != 0)
5903-
return;
5904-
5905-
XMEMSET(suites, 0, sizeof(suites));
5906-
5907-
if (!ssl->options.useClientOrder) {
5908-
/* Server order refining. */
5909-
for (i = 0; i < ssl->suites->suiteSz; i += 2) {
5910-
for (j = 0; j < peerSuites->suiteSz; j += 2) {
5911-
if ((ssl->suites->suites[i+0] == peerSuites->suites[j+0]) &&
5912-
(ssl->suites->suites[i+1] == peerSuites->suites[j+1])) {
5913-
suites[suiteSz++] = peerSuites->suites[j+0];
5914-
suites[suiteSz++] = peerSuites->suites[j+1];
5915-
break;
5916-
}
5917-
}
5918-
if (suiteSz == WOLFSSL_MAX_SUITE_SZ)
5919-
break;
5920-
}
5921-
}
5922-
else {
5923-
/* Client order refining. */
5924-
for (j = 0; j < peerSuites->suiteSz; j += 2) {
5925-
for (i = 0; i < ssl->suites->suiteSz; i += 2) {
5926-
if ((ssl->suites->suites[i+0] == peerSuites->suites[j+0]) &&
5927-
(ssl->suites->suites[i+1] == peerSuites->suites[j+1])) {
5928-
suites[suiteSz++] = peerSuites->suites[j+0];
5929-
suites[suiteSz++] = peerSuites->suites[j+1];
5930-
break;
5931-
}
5932-
}
5933-
if (suiteSz == WOLFSSL_MAX_SUITE_SZ)
5934-
break;
5935-
}
5936-
}
5937-
5938-
ssl->suites->suiteSz = suiteSz;
5939-
XMEMCPY(ssl->suites->suites, &suites, sizeof(suites));
5940-
#ifdef WOLFSSL_DEBUG_TLS
5941-
{
5942-
int ii;
5943-
WOLFSSL_MSG("Refined Ciphers:");
5944-
for (ii = 0 ; ii < ssl->suites->suiteSz; ii += 2) {
5945-
WOLFSSL_MSG(GetCipherNameInternal(ssl->suites->suites[ii+0],
5946-
ssl->suites->suites[ii+1]));
5947-
}
5948-
}
5949-
#endif
5950-
}
5951-
5952-
59535890
#ifndef NO_PSK
59545891
int FindPskSuite(const WOLFSSL* ssl, PreSharedKey* psk, byte* psk_key,
59555892
word32* psk_keySz, const byte* suite, int* found, byte* foundSuite)
@@ -6322,7 +6259,7 @@ static int CheckPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz,
63226259
return ret;
63236260

63246261
/* Refine list for PSK processing. */
6325-
RefineSuites(ssl, clSuites);
6262+
sslRefineSuites(ssl, clSuites);
63266263
#ifndef WOLFSSL_PSK_ONE_ID
63276264
if (usingPSK == NULL)
63286265
return BAD_FUNC_ARG;

wolfssl/internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,6 +2383,10 @@ WOLFSSL_LOCAL void InitSuites(Suites* suites, ProtocolVersion pv, int keySz,
23832383
word16 haveAES128, word16 haveSHA1,
23842384
word16 haveRC4, int side);
23852385

2386+
void refineSuites(const Suites* sslSuites, const Suites* peerSuites,
2387+
Suites* outSuites, byte useClientOrder);
2388+
void sslRefineSuites(WOLFSSL* ssl, Suites* peerSuites);
2389+
23862390
typedef struct TLSX TLSX;
23872391
WOLFSSL_LOCAL int MatchSuite_ex(const WOLFSSL* ssl, Suites* peerSuites,
23882392
CipherSuite* cs, TLSX* extensions);

0 commit comments

Comments
 (0)