Skip to content

Commit 08903de

Browse files
committed
dtls: Check PSK ciphersuite against local list
1 parent 0de7b8e commit 08903de

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
@@ -37406,6 +37406,74 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
3740637406
return 1;
3740737407
}
3740837408

37409+
void refineSuites(const Suites* sslSuites, const Suites* peerSuites,
37410+
Suites* outSuites, byte useClientOrder)
37411+
{
37412+
byte suites[WOLFSSL_MAX_SUITE_SZ];
37413+
word16 suiteSz = 0;
37414+
word16 i;
37415+
word16 j;
37416+
37417+
XMEMSET(suites, 0, sizeof(suites));
37418+
37419+
if (!useClientOrder) {
37420+
/* Server order refining. */
37421+
for (i = 0; i < sslSuites->suiteSz; i += 2) {
37422+
for (j = 0; j < peerSuites->suiteSz; j += 2) {
37423+
if ((sslSuites->suites[i+0] == peerSuites->suites[j+0]) &&
37424+
(sslSuites->suites[i+1] == peerSuites->suites[j+1])) {
37425+
suites[suiteSz++] = peerSuites->suites[j+0];
37426+
suites[suiteSz++] = peerSuites->suites[j+1];
37427+
break;
37428+
}
37429+
}
37430+
if (suiteSz == WOLFSSL_MAX_SUITE_SZ)
37431+
break;
37432+
}
37433+
}
37434+
else {
37435+
/* Client order refining. */
37436+
for (j = 0; j < peerSuites->suiteSz; j += 2) {
37437+
for (i = 0; i < sslSuites->suiteSz; i += 2) {
37438+
if ((sslSuites->suites[i+0] == peerSuites->suites[j+0]) &&
37439+
(sslSuites->suites[i+1] == peerSuites->suites[j+1])) {
37440+
suites[suiteSz++] = peerSuites->suites[j+0];
37441+
suites[suiteSz++] = peerSuites->suites[j+1];
37442+
break;
37443+
}
37444+
}
37445+
if (suiteSz == WOLFSSL_MAX_SUITE_SZ)
37446+
break;
37447+
}
37448+
}
37449+
37450+
outSuites->suiteSz = suiteSz;
37451+
XMEMCPY(outSuites->suites, &suites, sizeof(suites));
37452+
#ifdef WOLFSSL_DEBUG_TLS
37453+
{
37454+
int ii;
37455+
WOLFSSL_MSG("Refined Ciphers:");
37456+
for (ii = 0 ; ii < suites->suiteSz; ii += 2) {
37457+
WOLFSSL_MSG(GetCipherNameInternal(suites->suites[ii+0],
37458+
suites->suites[ii+1]));
37459+
}
37460+
}
37461+
#endif
37462+
}
37463+
37464+
/* Refine list of supported cipher suites to those common to server and client.
37465+
*
37466+
* ssl SSL/TLS object.
37467+
* peerSuites The peer's advertised list of supported cipher suites.
37468+
*/
37469+
void sslRefineSuites(WOLFSSL* ssl, Suites* peerSuites)
37470+
{
37471+
if (AllocateSuites(ssl) != 0)
37472+
return;
37473+
refineSuites(ssl->suites, peerSuites, ssl->suites,
37474+
ssl->options.useClientOrder);
37475+
}
37476+
3740937477
static int CompareSuites(const WOLFSSL* ssl, const Suites* suites,
3741037478
Suites* peerSuites, word16 i, word16 j,
3741137479
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)