Skip to content

Commit 795bcac

Browse files
Merge pull request #12161 from embhorn/gh12153
Harden fix when TLS 1.3 PSK callback omits cipher name
2 parents 7c42580 + d930ebf commit 795bcac

7 files changed

Lines changed: 160 additions & 12 deletions

File tree

‎doc/dox_comments/header_files-ja/ssl.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13053,7 +13053,7 @@ int wolfSSL_inject(WOLFSSL* ssl, const void* data, int sz);
1305313053
/*!
1305413054
\ingroup Setup
1305513055

13056-
\brief この関数はTLS v1.3接続のための事前共有鍵(PSK)クライアント側コールバックを設定します。コールバックはPSKアイデンティティを検索し、その鍵とハンドシェイクで使用する暗号の名前を返すために使用されます。この関数はWOLFSSL_CTX構造体のclient_psk_tls13_cbメンバーを設定します。
13056+
\brief この関数はTLS v1.3接続のための事前共有鍵(PSK)クライアント側コールバックを設定します。コールバックはPSKアイデンティティを検索し、その鍵とハンドシェイクで使用する暗号の名前を返すために使用されます。コールバックが暗号名を設定しない(NULLのままの)場合、デフォルトのPSK暗号スイートTLS13-AES128-GCM-SHA256(WOLFSSL_DEF_PSK_CIPHER)が使用されます。鍵長0を返すことはPSKが無いことを示します。この関数はWOLFSSL_CTX構造体のclient_psk_tls13_cbメンバーを設定します。
1305713057

1305813058
\param [in,out] ctx wolfSSL_CTX_new()で作成されたWOLFSSL_CTX構造体へのポインタ。
1305913059
\param [in] cb TLS 1.3クライアント用の事前共有鍵(PSK)コールバック。
@@ -13075,7 +13075,7 @@ void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx,
1307513075
/*!
1307613076
\ingroup Setup
1307713077

13078-
\brief この関数はTLS v1.3接続のための事前共有鍵(PSK)クライアント側コールバックを設定します。コールバックはPSKアイデンティティを検索し、その鍵とハンドシェイクで使用する暗号の名前を返すために使用されます。この関数はWOLFSSL構造体のoptionsフィールドのclient_psk_tls13_cbメンバーを設定します。
13078+
\brief この関数はTLS v1.3接続のための事前共有鍵(PSK)クライアント側コールバックを設定します。コールバックはPSKアイデンティティを検索し、その鍵とハンドシェイクで使用する暗号の名前を返すために使用されます。コールバックが暗号名を設定しない(NULLのままの)場合、デフォルトのPSK暗号スイートTLS13-AES128-GCM-SHA256(WOLFSSL_DEF_PSK_CIPHER)が使用されます。鍵長0を返すことはPSKが無いことを示します。この関数はWOLFSSL構造体のoptionsフィールドのclient_psk_tls13_cbメンバーを設定します。
1307913079

1308013080
\param [in,out] ssl wolfSSL_new()を使用して作成されたWOLFSSL構造体へのポインタ。
1308113081
\param [in] cb TLS 1.3クライアント用の事前共有鍵(PSK)コールバック。
@@ -13097,7 +13097,7 @@ void wolfSSL_set_psk_client_tls13_callback(WOLFSSL* ssl,
1309713097
/*!
1309813098
\ingroup Setup
1309913099

13100-
\brief この関数はTLS v1.3接続のための事前共有鍵(PSK)サーバー側コールバックを設定します。コールバックはPSKアイデンティティを検索し、その鍵とハンドシェイクで使用する暗号の名前を返すために使用されます。この関数はWOLFSSL_CTX構造体のserver_psk_tls13_cbメンバーを設定します。
13100+
\brief この関数はTLS v1.3接続のための事前共有鍵(PSK)サーバー側コールバックを設定します。コールバックはPSKアイデンティティを検索し、その鍵とハンドシェイクで使用する暗号の名前を返すために使用されます。コールバックが0以外の鍵長を返し暗号名を設定しない(NULLのままの)場合、デフォルトのPSK暗号スイートTLS13-AES128-GCM-SHA256(WOLFSSL_DEF_PSK_CIPHER)でPSKが受け入れられ、TLS 1.3以外のサーバーPSKコールバックは呼び出されません。鍵長0を返すことはアイデンティティが不明であることを示します。この関数はWOLFSSL_CTX構造体のserver_psk_tls13_cbメンバーを設定します。
1310113101

1310213102
\param [in,out] ctx wolfSSL_CTX_new()で作成されたWOLFSSL_CTX構造体へのポインタ。
1310313103
\param [in] cb TLS 1.3サーバー用の事前共有鍵(PSK)コールバック。
@@ -13119,7 +13119,7 @@ void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx,
1311913119
/*!
1312013120
\ingroup Setup
1312113121

13122-
\brief この関数はTLS v1.3接続のための事前共有鍵(PSK)サーバー側コールバックを設定します。コールバックはPSKアイデンティティを検索し、その鍵とハンドシェイクで使用する暗号の名前を返すために使用されます。この関数はWOLFSSL構造体のoptionsフィールドのserver_psk_tls13_cbメンバーを設定します。
13122+
\brief この関数はTLS v1.3接続のための事前共有鍵(PSK)サーバー側コールバックを設定します。コールバックはPSKアイデンティティを検索し、その鍵とハンドシェイクで使用する暗号の名前を返すために使用されます。コールバックが0以外の鍵長を返し暗号名を設定しない(NULLのままの)場合、デフォルトのPSK暗号スイートTLS13-AES128-GCM-SHA256(WOLFSSL_DEF_PSK_CIPHER)でPSKが受け入れられ、TLS 1.3以外のサーバーPSKコールバックは呼び出されません。鍵長0を返すことはアイデンティティが不明であることを示します。この関数はWOLFSSL構造体のoptionsフィールドのserver_psk_tls13_cbメンバーを設定します。
1312313123

1312413124
\param [in,out] ssl wolfSSL_new()を使用して作成されたWOLFSSL構造体へのポインタ。
1312513125
\param [in] cb TLS 1.3サーバー用の事前共有鍵(PSK)コールバック。

‎doc/dox_comments/header_files/ssl.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17472,6 +17472,10 @@ int wolfSSL_inject(WOLFSSL* ssl, const void* data, int sz);
1747217472
for TLS v1.3 connections.
1747317473
The callback is used to find a PSK identity and return its key and
1747417474
the name of the cipher to use for the handshake.
17475+
The callback may leave the cipher name unset (NULL), in which case the
17476+
default PSK cipher suite TLS13-AES128-GCM-SHA256
17477+
(WOLFSSL_DEF_PSK_CIPHER) is used. Returning a key length of 0 means no
17478+
PSK is available.
1747517479
The function sets the client_psk_tls13_cb member of the
1747617480
WOLFSSL_CTX structure.
1747717481

@@ -17500,6 +17504,10 @@ void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx,
1750017504
for TLS v1.3 connections.
1750117505
The callback is used to find a PSK identity and return its key and
1750217506
the name of the cipher to use for the handshake.
17507+
The callback may leave the cipher name unset (NULL), in which case the
17508+
default PSK cipher suite TLS13-AES128-GCM-SHA256
17509+
(WOLFSSL_DEF_PSK_CIPHER) is used. Returning a key length of 0 means no
17510+
PSK is available.
1750317511
The function sets the client_psk_tls13_cb member of the options field in
1750417512
WOLFSSL structure.
1750517513

@@ -17527,6 +17535,11 @@ void wolfSSL_set_psk_client_tls13_callback(WOLFSSL* ssl,
1752717535
for TLS v1.3 connections.
1752817536
The callback is used to find a PSK identity and return its key and
1752917537
the name of the cipher to use for the handshake.
17538+
If the callback returns a non-zero key length and leaves the cipher
17539+
name unset (NULL), the PSK is accepted with the default PSK cipher suite
17540+
TLS13-AES128-GCM-SHA256 (WOLFSSL_DEF_PSK_CIPHER) and the non-TLS 1.3
17541+
server PSK callback is not called. Returning a key length of 0 means the
17542+
identity is not known.
1753017543
The function sets the server_psk_tls13_cb member of the
1753117544
WOLFSSL_CTX structure.
1753217545

@@ -17555,6 +17568,11 @@ void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx,
1755517568
for TLS v1.3 connections.
1755617569
The callback is used to find a PSK identity and return its key and
1755717570
the name of the cipher to use for the handshake.
17571+
If the callback returns a non-zero key length and leaves the cipher
17572+
name unset (NULL), the PSK is accepted with the default PSK cipher suite
17573+
TLS13-AES128-GCM-SHA256 (WOLFSSL_DEF_PSK_CIPHER) and the non-TLS 1.3
17574+
server PSK callback is not called. Returning a key length of 0 means the
17575+
identity is not known.
1755817576
The function sets the server_psk_tls13_cb member of the options field in
1755917577
WOLFSSL structure.
1756017578

‎src/internal.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34545,6 +34545,9 @@ int GetCipherSuiteFromName(const char* name, byte* cipherSuite0,
3454534545
(void)major;
3454634546
(void)minor;
3454734547

34548+
if (name == NULL)
34549+
return ret;
34550+
3454834551
/* Support trailing : */
3454934552
nameDelim = XSTRSTR(name, ":");
3455034553
if (nameDelim)

‎src/tls.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17042,7 +17042,9 @@ int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
1704217042
ssl, ssl->arrays->server_hint,
1704317043
ssl->arrays->client_identity, MAX_PSK_ID_LEN,
1704417044
ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
17045-
if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
17045+
/* No name from the callback keeps the default suite. */
17046+
if (cipherName != NULL &&
17047+
GetCipherSuiteFromName(cipherName, &cipherSuite0,
1704617048
&cipherSuite, NULL, NULL, &cipherSuiteFlags) != 0) {
1704717049
return PSK_KEY_ERROR;
1704817050
}
@@ -17053,9 +17055,10 @@ int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
1705317055
MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
1705417056
}
1705517057
if (
17056-
#ifdef OPENSSL_EXTRA
17058+
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_PSK_ONE_ID)
1705717059
/* OpenSSL treats a PSK key length of 0
17058-
* to indicate no PSK available.
17060+
* to indicate no PSK available. With one PSK ID this is
17061+
* the only check before the key is offered.
1705917062
*/
1706017063
ssl->arrays->psk_keySz == 0 ||
1706117064
#endif

‎src/tls13.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4752,15 +4752,17 @@ static int SetupPskKey(WOLFSSL* ssl, PreSharedKey* psk, int clientHello)
47524752
}
47534753
}
47544754
else if (ssl->options.client_psk_tls13_cb != NULL) {
4755-
byte cipherSuite0;
4756-
byte cipherSuite;
4755+
byte cipherSuite0 = TLS13_BYTE;
4756+
byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
47574757
int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
47584758

47594759
ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(ssl,
47604760
ssl->arrays->server_hint, ssl->arrays->client_identity,
47614761
MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN,
47624762
&cipherName);
4763-
if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
4763+
/* No name from the callback keeps the default suite. */
4764+
if (cipherName != NULL &&
4765+
GetCipherSuiteFromName(cipherName, &cipherSuite0,
47644766
&cipherSuite, NULL, NULL, &cipherSuiteFlags) != 0) {
47654767
WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
47664768
return PSK_KEY_ERROR;
@@ -6899,7 +6901,9 @@ int FindPskSuite(const WOLFSSL* ssl, PreSharedKey* psk, byte* psk_key,
68996901
(char*)psk->identity, psk_key, MAX_PSK_KEY_LEN, &cipherName);
69006902
if (*psk_keySz != 0) {
69016903
int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
6902-
*found = (GetCipherSuiteFromName(cipherName, &cipherSuite0,
6904+
/* No name from the callback keeps the default suite. */
6905+
*found = (cipherName == NULL) ||
6906+
(GetCipherSuiteFromName(cipherName, &cipherSuite0,
69036907
&cipherSuite, NULL, NULL, &cipherSuiteFlags) == 0);
69046908
(void)cipherSuiteFlags;
69056909
}

‎tests/api/test_tls13.c‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19391,3 +19391,121 @@ int test_tls13_new_session_ticket_max_fragment(void)
1939119391
#endif
1939219392
return EXPECT_RESULT();
1939319393
}
19394+
19395+
#if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
19396+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
19397+
defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && !defined(NO_SHA256) && \
19398+
!defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
19399+
static const unsigned char test_tls13_psk_no_name_key[32] = {
19400+
0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A,
19401+
0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A,
19402+
0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A,
19403+
0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A
19404+
};
19405+
19406+
/* Supplies a key but leaves the cipher suite name unset. */
19407+
static unsigned int test_tls13_psk_no_name_client_cb(WOLFSSL* ssl,
19408+
const char* hint, char* identity, unsigned int id_max_len,
19409+
unsigned char* key, unsigned int key_max_len, const char** ciphersuite)
19410+
{
19411+
(void)ssl;
19412+
(void)hint;
19413+
(void)ciphersuite;
19414+
if (id_max_len == 0 || key_max_len < sizeof(test_tls13_psk_no_name_key))
19415+
return 0;
19416+
XSTRNCPY(identity, "no_name_client", id_max_len);
19417+
XMEMCPY(key, test_tls13_psk_no_name_key,
19418+
sizeof(test_tls13_psk_no_name_key));
19419+
return (unsigned int)sizeof(test_tls13_psk_no_name_key);
19420+
}
19421+
19422+
/* Declines to supply a PSK and leaves the cipher suite name unset. */
19423+
static unsigned int test_tls13_psk_none_client_cb(WOLFSSL* ssl,
19424+
const char* hint, char* identity, unsigned int id_max_len,
19425+
unsigned char* key, unsigned int key_max_len, const char** ciphersuite)
19426+
{
19427+
(void)ssl;
19428+
(void)hint;
19429+
(void)identity;
19430+
(void)id_max_len;
19431+
(void)key;
19432+
(void)key_max_len;
19433+
(void)ciphersuite;
19434+
return 0;
19435+
}
19436+
19437+
static unsigned int test_tls13_psk_no_name_server_cb(WOLFSSL* ssl,
19438+
const char* id, unsigned char* key, unsigned int key_max_len,
19439+
const char** ciphersuite)
19440+
{
19441+
(void)ssl;
19442+
(void)ciphersuite;
19443+
if (key_max_len < sizeof(test_tls13_psk_no_name_key) || id == NULL)
19444+
return 0;
19445+
if (XSTRCMP(id, "no_name_client") != 0)
19446+
return 0;
19447+
XMEMCPY(key, test_tls13_psk_no_name_key,
19448+
sizeof(test_tls13_psk_no_name_key));
19449+
return (unsigned int)sizeof(test_tls13_psk_no_name_key);
19450+
}
19451+
#endif
19452+
19453+
int test_tls13_psk_cb_no_cipher_name(void)
19454+
{
19455+
EXPECT_DECLS;
19456+
#if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
19457+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
19458+
defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && !defined(NO_SHA256) && \
19459+
!defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
19460+
WOLFSSL_CTX *ctx_c = NULL;
19461+
WOLFSSL_CTX *ctx_s = NULL;
19462+
WOLFSSL *ssl_c = NULL;
19463+
WOLFSSL *ssl_s = NULL;
19464+
struct test_memio_ctx test_ctx;
19465+
19466+
/* Both callbacks omit the suite name, so the default suite is used. */
19467+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
19468+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
19469+
wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
19470+
wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, NULL);
19471+
wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, NULL);
19472+
wolfSSL_set_psk_client_tls13_callback(ssl_c,
19473+
test_tls13_psk_no_name_client_cb);
19474+
wolfSSL_set_psk_server_tls13_callback(ssl_s,
19475+
test_tls13_psk_no_name_server_cb);
19476+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
19477+
ExpectIntEQ(wolfSSL_get_current_cipher_suite(ssl_c),
19478+
(TLS13_BYTE << 8) | WOLFSSL_DEF_PSK_CIPHER);
19479+
ExpectIntEQ(ssl_s->options.pskNegotiated, 1);
19480+
19481+
wolfSSL_free(ssl_c);
19482+
ssl_c = NULL;
19483+
wolfSSL_CTX_free(ctx_c);
19484+
ctx_c = NULL;
19485+
wolfSSL_free(ssl_s);
19486+
ssl_s = NULL;
19487+
wolfSSL_CTX_free(ctx_s);
19488+
ctx_s = NULL;
19489+
19490+
/* A client callback returning 0 must not fault on the unset name. */
19491+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
19492+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
19493+
wolfTLSv1_3_client_method, NULL), 0);
19494+
wolfSSL_set_psk_client_tls13_callback(ssl_c,
19495+
test_tls13_psk_none_client_cb);
19496+
ExpectIntEQ(wolfSSL_connect(ssl_c), WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR));
19497+
#ifdef OPENSSL_EXTRA
19498+
/* A zero length key means no PSK, so the ClientHello is sent without
19499+
* one and the client waits for the server. */
19500+
ExpectIntEQ(wolfSSL_get_error(ssl_c, WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR)),
19501+
WOLFSSL_ERROR_WANT_READ);
19502+
#else
19503+
ExpectIntEQ(wolfSSL_get_error(ssl_c, WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR)),
19504+
WC_NO_ERR_TRACE(PSK_KEY_ERROR));
19505+
#endif
19506+
19507+
wolfSSL_free(ssl_c);
19508+
wolfSSL_CTX_free(ctx_c);
19509+
#endif
19510+
return EXPECT_RESULT();
19511+
}

‎tests/api/test_tls13.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ int test_tls13_psk_mode_mismatch_falls_back(void);
199199
int test_tls13_ticket_psk_modes_uses_policy(void);
200200
int test_tls13_ecdsa_scheme_curve_binding(void);
201201
int test_tls13_new_session_ticket_max_fragment(void);
202+
int test_tls13_psk_cb_no_cipher_name(void);
202203
int test_tls13_send_session_ticket_psk_modes(void);
203204
int test_tls13_new_session_ticket_ext_framing(void);
204205
int test_tls13_new_session_ticket_keeps_ems(void);
@@ -420,6 +421,7 @@ int test_tls13_exporter_suite_decision_coverage(void);
420421
TEST_DECL_GROUP("tls13", test_tls13_AEAD_limit_KeyUpdate_want_write), \
421422
TEST_DECL_GROUP("tls13", test_tls13_exporter_suite_decision_coverage), \
422423
TEST_DECL_GROUP("tls13", test_tls13_ecdsa_scheme_curve_binding), \
423-
TEST_DECL_GROUP("tls13", test_tls13_new_session_ticket_max_fragment)
424+
TEST_DECL_GROUP("tls13", test_tls13_new_session_ticket_max_fragment), \
425+
TEST_DECL_GROUP("tls13", test_tls13_psk_cb_no_cipher_name)
424426

425427
#endif /* WOLFCRYPT_TEST_TLS13_H */

0 commit comments

Comments
 (0)