Skip to content

Commit 50ef56a

Browse files
Merge pull request #8630 from kojiws/kojiws/more_strict_key_format_check
Detect unknown key format on ProcessBufferTryDecode()
2 parents 05ac520 + 71ebad1 commit 50ef56a

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/ssl_load.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,7 @@ static int ProcessBufferTryDecode(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
10901090
int devId = wolfSSL_CTX_GetDevId(ctx, ssl);
10911091
byte* keyType = NULL;
10921092
int* keySz = NULL;
1093+
int matchAnyKey = 0;
10931094

10941095
(void)heap;
10951096
(void)devId;
@@ -1141,8 +1142,19 @@ static int ProcessBufferTryDecode(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
11411142
ret = ProcessBufferTryDecodeRsa(ctx, ssl, der, keyFormat, heap, devId,
11421143
keyType, keySz);
11431144
#endif
1145+
matchAnyKey = 1;
11441146
}
1145-
#endif
1147+
#ifdef WC_RSA_PSS
1148+
if(*keyFormat == RSAPSSk) {
1149+
/*
1150+
Require logic to verify that the der is RSAPSSk (when *keyFormat == RSAPSSK),
1151+
and to detect that the der is RSAPSSk (when *keyFormat == 0).
1152+
*/
1153+
1154+
matchAnyKey = 1;
1155+
}
1156+
#endif /* WC_RSA_PSS */
1157+
#endif /* NO_RSA */
11461158
#ifdef HAVE_ECC
11471159
/* Try ECC if key format is ECDSA or SM2, or yet unknown. */
11481160
if ((ret == 0) && ((*keyFormat == 0) || (*keyFormat == ECDSAk)
@@ -1152,20 +1164,23 @@ static int ProcessBufferTryDecode(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
11521164
)) {
11531165
ret = ProcessBufferTryDecodeEcc(ctx, ssl, der, keyFormat, heap, devId,
11541166
keyType, keySz);
1167+
matchAnyKey = 1;
11551168
}
11561169
#endif /* HAVE_ECC */
11571170
#if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
11581171
/* Try Ed25519 if key format is Ed25519 or yet unknown. */
11591172
if ((ret == 0) && ((*keyFormat == 0 || *keyFormat == ED25519k))) {
11601173
ret = ProcessBufferTryDecodeEd25519(ctx, ssl, der, keyFormat, heap,
11611174
devId, keyType, keySz);
1175+
matchAnyKey = 1;
11621176
}
11631177
#endif /* HAVE_ED25519 && HAVE_ED25519_KEY_IMPORT */
11641178
#if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
11651179
/* Try Ed448 if key format is Ed448 or yet unknown. */
11661180
if ((ret == 0) && ((*keyFormat == 0 || *keyFormat == ED448k))) {
11671181
ret = ProcessBufferTryDecodeEd448(ctx, ssl, der, keyFormat, heap, devId,
11681182
keyType, keySz);
1183+
matchAnyKey = 1;
11691184
}
11701185
#endif /* HAVE_ED448 && HAVE_ED448_KEY_IMPORT */
11711186
#if defined(HAVE_FALCON)
@@ -1174,6 +1189,7 @@ static int ProcessBufferTryDecode(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
11741189
(*keyFormat == FALCON_LEVEL5k))) {
11751190
ret = ProcessBufferTryDecodeFalcon(ctx, ssl, der, keyFormat, heap,
11761191
keyType, keySz);
1192+
matchAnyKey = 1;
11771193
}
11781194
#endif /* HAVE_FALCON */
11791195
#if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) && \
@@ -1193,11 +1209,13 @@ static int ProcessBufferTryDecode(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
11931209
)) {
11941210
ret = ProcessBufferTryDecodeDilithium(ctx, ssl, der, keyFormat, heap,
11951211
keyType, keySz);
1212+
matchAnyKey = 1;
11961213
}
11971214
#endif /* HAVE_DILITHIUM */
11981215

11991216
/* Check we know the format. */
1200-
if ((ret == 0) && (*keyFormat == 0)) {
1217+
if ((ret == 0) &&
1218+
((*keyFormat == 0) || ((*keyFormat != 0) && (matchAnyKey == 0)))) {
12011219
WOLFSSL_MSG("Not a supported key type");
12021220
/* Not supported key format. */
12031221
ret = WOLFSSL_BAD_FILE;

tests/api.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,6 +2312,10 @@ static int test_wolfSSL_CTX_use_PrivateKey_file(void)
23122312
/* invalid key type */
23132313
ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
23142314

2315+
/* invalid key format */
2316+
ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, "./certs/dh-priv-2048.pem",
2317+
WOLFSSL_FILETYPE_PEM));
2318+
23152319
/* success */
23162320
#ifdef NO_RSA
23172321
/* rsa needed */

0 commit comments

Comments
 (0)