Skip to content

Commit 9337cfb

Browse files
committed
Add wolfSSL_get_sigalg_info
1 parent 7c2344c commit 9337cfb

6 files changed

Lines changed: 200 additions & 35 deletions

File tree

doc/dox_comments/header_files/ssl.h

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14882,16 +14882,16 @@ int wolfSSL_dtls_cid_get_tx(WOLFSSL* ssl, unsigned char* buffer,
1488214882
wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
1488314883
&hashSigAlgoSz);
1488414884
14885-
// Choose certificate to load based on ciphersuites
14885+
// Choose certificate to load based on ciphersuites and sigalgs
1488614886
}
1488714887
1488814888
WOLFSSL* ctx;
1488914889
ctx = wolfSSL_CTX_new(wolfTLSv1_3_method_ex(NULL));
1489014890
wolfSSL_CTX_set_cert_cb(ctx, certCB, NULL);
1489114891
\endcode
1489214892
14893-
\sa wolfSSL_new
14894-
\sa wolfSSL_free
14893+
\sa wolfSSL_get_ciphersuite_info
14894+
\sa wolfSSL_get_sigalg_info
1489514895
*/
1489614896
void wolfSSL_get_client_suites_sigalgs(const WOLFSSL* ssl,
1489714897
const byte** suites, word16* suiteSz,
@@ -14919,8 +14919,39 @@ void wolfSSL_get_client_suites_sigalgs(const WOLFSSL* ssl,
1491914919
haveECC = 1;
1492014920
\endcode
1492114921
14922-
\sa wolfSSL_new
14923-
\sa wolfSSL_free
14922+
\sa wolfSSL_get_client_suites_sigalgs
14923+
\sa wolfSSL_get_sigalg_info
1492414924
*/
1492514925
WOLFSSL_CIPHERSUITE_INFO wolfSSL_get_ciphersuite_info(byte first,
1492614926
byte second);
14927+
14928+
/*!
14929+
\ingroup TLS
14930+
14931+
\brief This returns information about the hash and signature algorithm
14932+
directly from the raw ciphersuite bytes.
14933+
14934+
\param [in] first First byte of the hash and signature algorith
14935+
\param [in] second Second byte of the hash and signature algorith
14936+
\param [out] hashAlgo The enum wc_HashType of the MAC algorithm
14937+
\param [out] sigAlgo The enum Key_Sum of the authentication algorithm
14938+
14939+
_Example_
14940+
\code
14941+
enum wc_HashType hashAlgo;
14942+
enum Key_Sum sigAlgo;
14943+
14944+
wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
14945+
&hashAlgo, &sigAlgo);
14946+
14947+
if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
14948+
haveRSA = 1;
14949+
else if (sigAlgo == ECDSAk)
14950+
haveECC = 1;
14951+
\endcode
14952+
14953+
\sa wolfSSL_get_client_suites_sigalgs
14954+
\sa wolfSSL_get_ciphersuite_info
14955+
*/
14956+
void wolfSSL_get_sigalg_info(byte first, byte second,
14957+
enum wc_HashType* hashAlgo, enum Key_Sum* sigAlgo);

src/internal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4236,7 +4236,7 @@ void InitSuites(Suites* suites, ProtocolVersion pv, int keySz, word16 haveRSA,
42364236
* hashalgo The hash algorithm.
42374237
* hsType The signature type.
42384238
*/
4239-
static WC_INLINE void DecodeSigAlg(const byte* input, byte* hashAlgo, byte* hsType)
4239+
WC_INLINE void DecodeSigAlg(const byte* input, byte* hashAlgo, byte* hsType)
42404240
{
42414241
*hsType = invalid_sa_algo;
42424242
switch (input[0]) {
@@ -4324,7 +4324,7 @@ static WC_INLINE void DecodeSigAlg(const byte* input, byte* hashAlgo, byte* hsTy
43244324
#if !defined(NO_DH) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
43254325
defined(HAVE_CURVE448) || (!defined(NO_RSA) && defined(WC_RSA_PSS))
43264326

4327-
static enum wc_HashType HashAlgoToType(int hashAlgo)
4327+
enum wc_HashType HashAlgoToType(int hashAlgo)
43284328
{
43294329
switch (hashAlgo) {
43304330
#ifdef WOLFSSL_SHA512

src/ssl.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16338,6 +16338,112 @@ int wolfSSL_set_compression(WOLFSSL* ssl)
1633816338
return info;
1633916339
}
1634016340

16341+
void wolfSSL_get_sigalg_info(byte first, byte second,
16342+
enum wc_HashType* hashAlgo, enum Key_Sum* sigAlgo)
16343+
{
16344+
byte input[2];
16345+
byte hashType;
16346+
byte sigType;
16347+
16348+
if (hashAlgo == NULL || sigAlgo == NULL)
16349+
return;
16350+
16351+
input[0] = first;
16352+
input[1] = second;
16353+
DecodeSigAlg(input, &hashType, &sigType);
16354+
16355+
/* cast so that compiler reminds us of unimplemented values */
16356+
switch ((enum SignatureAlgorithm)sigType) {
16357+
case anonymous_sa_algo:
16358+
*sigAlgo = (enum Key_Sum)0;
16359+
break;
16360+
case rsa_sa_algo:
16361+
*sigAlgo = RSAk;
16362+
break;
16363+
case dsa_sa_algo:
16364+
*sigAlgo = DSAk;
16365+
break;
16366+
case ecc_dsa_sa_algo:
16367+
*sigAlgo = ECDSAk;
16368+
break;
16369+
case rsa_pss_sa_algo:
16370+
*sigAlgo = RSAPSSk;
16371+
break;
16372+
case ed25519_sa_algo:
16373+
*sigAlgo = ED25519k;
16374+
break;
16375+
case rsa_pss_pss_algo:
16376+
*sigAlgo = RSAPSSk;
16377+
break;
16378+
case ed448_sa_algo:
16379+
*sigAlgo = ED448k;
16380+
break;
16381+
case falcon_level1_sa_algo:
16382+
*sigAlgo = FALCON_LEVEL1k;
16383+
break;
16384+
case falcon_level5_sa_algo:
16385+
*sigAlgo = FALCON_LEVEL5k;
16386+
break;
16387+
case dilithium_level2_sa_algo:
16388+
*sigAlgo = DILITHIUM_LEVEL2k;
16389+
break;
16390+
case dilithium_level3_sa_algo:
16391+
*sigAlgo = DILITHIUM_LEVEL3k;
16392+
break;
16393+
case dilithium_level5_sa_algo:
16394+
*sigAlgo = DILITHIUM_LEVEL5k;
16395+
break;
16396+
case sm2_sa_algo:
16397+
*sigAlgo = SM2k;
16398+
break;
16399+
case invalid_sa_algo:
16400+
default:
16401+
*hashAlgo = WC_HASH_TYPE_NONE;
16402+
*sigAlgo = (enum Key_Sum)0;
16403+
return;
16404+
}
16405+
16406+
/* cast so that compiler reminds us of unimplemented values */
16407+
switch((enum wc_MACAlgorithm)hashType) {
16408+
case no_mac:
16409+
case rmd_mac: /* Don't have a RIPEMD type in wc_HashType */
16410+
*hashAlgo = WC_HASH_TYPE_NONE;
16411+
break;
16412+
case md5_mac:
16413+
*hashAlgo = WC_HASH_TYPE_MD5;
16414+
break;
16415+
case sha_mac:
16416+
*hashAlgo = WC_HASH_TYPE_SHA;
16417+
break;
16418+
case sha224_mac:
16419+
*hashAlgo = WC_HASH_TYPE_SHA224;
16420+
break;
16421+
case sha256_mac:
16422+
*hashAlgo = WC_HASH_TYPE_SHA256;
16423+
break;
16424+
case sha384_mac:
16425+
*hashAlgo = WC_HASH_TYPE_SHA384;
16426+
break;
16427+
case sha512_mac:
16428+
*hashAlgo = WC_HASH_TYPE_SHA512;
16429+
break;
16430+
case blake2b_mac:
16431+
*hashAlgo = WC_HASH_TYPE_BLAKE2B;
16432+
break;
16433+
case sm3_mac:
16434+
#ifdef WOLFSSL_SM3
16435+
*hashAlgo = WC_HASH_TYPE_SM3;
16436+
#else
16437+
*hashAlgo = WC_HASH_TYPE_NONE;
16438+
#endif
16439+
break;
16440+
default:
16441+
*hashAlgo = WC_HASH_TYPE_NONE;
16442+
*sigAlgo = (enum Key_Sum)0;
16443+
return;
16444+
}
16445+
}
16446+
1634116447
/**
1634216448
* Internal wrapper for calling certSetupCb
1634316449
* @param ssl The SSL/TLS Object

tests/api.c

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44865,31 +44865,16 @@ static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
4486544865
haveECC = 0;
4486644866
}
4486744867
for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
44868-
/* Based on DecodeSigAlg. Enums are not exposed so need to use magic
44869-
* numbers. */
44870-
switch (hashSigAlgo[idx+0]) {
44871-
case 8:
44872-
switch (hashSigAlgo[idx+1]) {
44873-
case 7: /* ED25519 */
44874-
case 8: /* ED448 */
44875-
haveECC = 1;
44876-
break;
44877-
default:
44878-
/* RSA-PSS */
44879-
haveRSA = 1;
44880-
break;
44881-
}
44882-
break;
44883-
default:
44884-
switch (hashSigAlgo[idx+1]) {
44885-
case 1: /* RSA */
44886-
haveRSA = 1;
44887-
break;
44888-
case 3: /* ECC */
44889-
haveECC = 1;
44890-
break;
44891-
}
44892-
}
44868+
enum wc_HashType hashAlgo;
44869+
enum Key_Sum sigAlgo;
44870+
44871+
wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
44872+
&hashAlgo, &sigAlgo);
44873+
44874+
if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
44875+
haveRSA = 1;
44876+
else if (sigAlgo == ECDSAk)
44877+
haveECC = 1;
4489344878
}
4489444879

4489544880
if (haveRSA) {
@@ -45082,6 +45067,43 @@ static int test_wolfSSL_ciphersuite_auth(void)
4508245067
return EXPECT_RESULT();
4508345068
}
4508445069

45070+
static int test_wolfSSL_sigalg_info(void)
45071+
{
45072+
EXPECT_DECLS;
45073+
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
45074+
byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
45075+
word16 len = 0;
45076+
word16 idx = 0;
45077+
int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
45078+
45079+
InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
45080+
for (idx = 0; idx < len; idx += 2) {
45081+
enum wc_HashType hashAlgo;
45082+
enum Key_Sum sigAlgo;
45083+
45084+
wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
45085+
&hashAlgo, &sigAlgo);
45086+
45087+
ExpectIntNE(hashAlgo, 0);
45088+
ExpectIntNE(sigAlgo, 0);
45089+
}
45090+
45091+
InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
45092+
0xFFFFFFFF, &len);
45093+
for (idx = 0; idx < len; idx += 2) {
45094+
enum wc_HashType hashAlgo;
45095+
enum Key_Sum sigAlgo;
45096+
45097+
wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
45098+
&hashAlgo, &sigAlgo);
45099+
45100+
ExpectIntNE(hashAlgo, 0);
45101+
}
45102+
45103+
#endif
45104+
return EXPECT_RESULT();
45105+
}
45106+
4508545107
static int test_wolfSSL_SESSION(void)
4508645108
{
4508745109
EXPECT_DECLS;
@@ -69268,6 +69290,7 @@ TEST_CASE testCases[] = {
6926869290
TEST_DECL(test_wolfSSL_cert_cb),
6926969291
TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
6927069292
TEST_DECL(test_wolfSSL_ciphersuite_auth),
69293+
TEST_DECL(test_wolfSSL_sigalg_info),
6927169294
/* Can't memory test as tcp_connect aborts. */
6927269295
TEST_DECL(test_wolfSSL_SESSION),
6927369296
TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),

wolfssl/internal.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2352,7 +2352,8 @@ WOLFSSL_LOCAL void InitSuitesHashSigAlgo_ex(byte* hashSigAlgo, int haveECDSAsig,
23522352
int haveRSAsig, int haveFalconSig,
23532353
int haveDilithiumSig, int haveAnon,
23542354
int tls1_2, int keySz, word16* len);
2355-
WOLFSSL_LOCAL void InitSuitesHashSigAlgo_ex2(byte* hashSigAlgo, int have,
2355+
/* use wolfSSL_API visibility to be able to test in tests/api.c */
2356+
WOLFSSL_API void InitSuitesHashSigAlgo_ex2(byte* hashSigAlgo, int have,
23562357
int tls1_2, int keySz,
23572358
word16* len);
23582359
WOLFSSL_LOCAL int AllocateCtxSuites(WOLFSSL_CTX* ctx);
@@ -6325,6 +6326,10 @@ WOLFSSL_LOCAL word32 LowResTimer(void);
63256326

63266327
WOLFSSL_LOCAL int FindSuiteSSL(const WOLFSSL* ssl, byte* suite);
63276328

6329+
WOLFSSL_LOCAL void DecodeSigAlg(const byte* input, byte* hashAlgo,
6330+
byte* hsType);
6331+
WOLFSSL_LOCAL enum wc_HashType HashAlgoToType(int hashAlgo);
6332+
63286333
#ifndef NO_CERTS
63296334
WOLFSSL_LOCAL void InitX509Name(WOLFSSL_X509_NAME* name, int dynamicFlag,
63306335
void* heap);

wolfssl/ssl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,9 +1531,7 @@ WOLFSSL_API int wolfSSL_sk_push_node(WOLFSSL_STACK** stack, WOLFSSL_STACK* in);
15311531
WOLFSSL_API WOLFSSL_STACK* wolfSSL_sk_get_node(WOLFSSL_STACK* sk, int idx);
15321532
WOLFSSL_API int wolfSSL_sk_push(WOLFSSL_STACK *st, const void *data);
15331533

1534-
#if defined(HAVE_OCSP) || defined(HAVE_CRL)
15351534
#include "wolfssl/wolfcrypt/asn.h"
1536-
#endif
15371535

15381536
#if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || defined(WOLFSSL_QT)
15391537
WOLFSSL_API int wolfSSL_sk_ACCESS_DESCRIPTION_push(
@@ -2132,6 +2130,8 @@ typedef struct WOLFSSL_CIPHERSUITE_INFO {
21322130
} WOLFSSL_CIPHERSUITE_INFO;
21332131
WOLFSSL_API WOLFSSL_CIPHERSUITE_INFO wolfSSL_get_ciphersuite_info(byte first,
21342132
byte second);
2133+
WOLFSSL_API void wolfSSL_get_sigalg_info(byte first,
2134+
byte second, enum wc_HashType* hashAlgo, enum Key_Sum* sigAlgo);
21352135
WOLFSSL_LOCAL int CertSetupCbWrapper(WOLFSSL* ssl);
21362136

21372137
WOLFSSL_API void* wolfSSL_X509_STORE_CTX_get_ex_data(

0 commit comments

Comments
 (0)