Skip to content

Commit 2a48ac0

Browse files
committed
Feature: allow the usage of
wolfSSL_alert_type_string wolfSSL_alert_desc_string wolfSSL_EVP_DigestSign wolfSSL_EVP_DigestVerify in the openssl compatiility layer for wolfssl
1 parent b5874a6 commit 2a48ac0

8 files changed

Lines changed: 312 additions & 1 deletion

File tree

src/ssl.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12050,12 +12050,96 @@ const char* wolfSSL_alert_type_string_long(int alertID)
1205012050
return AlertTypeToString(alertID);
1205112051
}
1205212052

12053+
const char* wolfSSL_alert_type_string(int alertID)
12054+
{
12055+
WOLFSSL_ENTER("wolfSSL_alert_type_string");
12056+
12057+
switch (alertID) {
12058+
case alert_warning:
12059+
return "W";
12060+
case alert_fatal:
12061+
return "F";
12062+
default:
12063+
return "U";
12064+
}
12065+
}
12066+
1205312067
const char* wolfSSL_alert_desc_string_long(int alertID)
1205412068
{
1205512069
WOLFSSL_ENTER("wolfSSL_alert_desc_string_long");
1205612070

1205712071
return AlertTypeToString(alertID);
1205812072
}
12073+
12074+
const char* wolfSSL_alert_desc_string(int alertID)
12075+
{
12076+
WOLFSSL_ENTER("wolfSSL_alert_desc_string");
12077+
12078+
switch (alertID) {
12079+
case close_notify:
12080+
return "CN";
12081+
case unexpected_message:
12082+
return "UM";
12083+
case bad_record_mac:
12084+
return "BM";
12085+
case record_overflow:
12086+
return "RO";
12087+
case decompression_failure:
12088+
return "DF";
12089+
case handshake_failure:
12090+
return "HF";
12091+
case no_certificate:
12092+
return "NC";
12093+
case bad_certificate:
12094+
return "BC";
12095+
case unsupported_certificate:
12096+
return "UC";
12097+
case certificate_revoked:
12098+
return "CR";
12099+
case certificate_expired:
12100+
return "CE";
12101+
case certificate_unknown:
12102+
return "CU";
12103+
case illegal_parameter:
12104+
return "IP";
12105+
case unknown_ca:
12106+
return "CA";
12107+
case access_denied:
12108+
return "AD";
12109+
case decode_error:
12110+
return "DE";
12111+
case decrypt_error:
12112+
return "DC";
12113+
case wolfssl_alert_protocol_version:
12114+
return "PV";
12115+
case insufficient_security:
12116+
return "IS";
12117+
case internal_error:
12118+
return "IE";
12119+
case inappropriate_fallback:
12120+
return "IF";
12121+
case user_canceled:
12122+
return "US";
12123+
case no_renegotiation:
12124+
return "NR";
12125+
case missing_extension:
12126+
return "ME";
12127+
case unsupported_extension:
12128+
return "UE";
12129+
case unrecognized_name:
12130+
return "UN";
12131+
case bad_certificate_status_response:
12132+
return "BR";
12133+
case unknown_psk_identity:
12134+
return "UP";
12135+
case certificate_required:
12136+
return "CQ";
12137+
case no_application_protocol:
12138+
return "AP";
12139+
default:
12140+
return "UK";
12141+
}
12142+
}
1205912143
#endif /* !NO_TLS */
1206012144

1206112145
#define STATE_STRINGS_PROTO(s) \

tests/api/test_evp_pkey.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,121 @@ int test_wolfSSL_EVP_MD_ecc_signing(void)
19271927
}
19281928

19291929

1930+
int test_wolfSSL_EVP_DigestSign(void)
1931+
{
1932+
EXPECT_DECLS;
1933+
#if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
1934+
WOLFSSL_EVP_PKEY* privKey = NULL;
1935+
WOLFSSL_EVP_PKEY* pubKey = NULL;
1936+
const unsigned char testData[] = "Hi There";
1937+
WOLFSSL_EVP_MD_CTX mdCtx;
1938+
int ret;
1939+
const unsigned char* cp;
1940+
const unsigned char* p;
1941+
unsigned char sig[2048/8];
1942+
size_t sigSz;
1943+
1944+
cp = client_key_der_2048;
1945+
ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
1946+
sizeof_client_key_der_2048)));
1947+
p = client_keypub_der_2048;
1948+
ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
1949+
sizeof_client_keypub_der_2048)));
1950+
1951+
/* One-shot sign: query size first */
1952+
wolfSSL_EVP_MD_CTX_init(&mdCtx);
1953+
ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
1954+
NULL, privKey), 1);
1955+
sigSz = 0;
1956+
ExpectIntEQ(wolfSSL_EVP_DigestSign(&mdCtx, NULL, &sigSz, testData,
1957+
(unsigned int)XSTRLEN((const char*)testData)), 1);
1958+
ExpectIntGT((int)sigSz, 0);
1959+
ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
1960+
ExpectIntEQ(ret, 1);
1961+
1962+
/* One-shot sign: actually produce the signature */
1963+
wolfSSL_EVP_MD_CTX_init(&mdCtx);
1964+
ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
1965+
NULL, privKey), 1);
1966+
sigSz = sizeof(sig);
1967+
ExpectIntEQ(wolfSSL_EVP_DigestSign(&mdCtx, sig, &sigSz, testData,
1968+
(unsigned int)XSTRLEN((const char*)testData)), 1);
1969+
ExpectIntGT((int)sigSz, 0);
1970+
ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
1971+
ExpectIntEQ(ret, 1);
1972+
1973+
/* One-shot verify */
1974+
wolfSSL_EVP_MD_CTX_init(&mdCtx);
1975+
ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL,
1976+
wolfSSL_EVP_sha256(), NULL, pubKey), 1);
1977+
ExpectIntEQ(wolfSSL_EVP_DigestVerify(&mdCtx, sig, sigSz, testData,
1978+
(unsigned int)XSTRLEN((const char*)testData)), 1);
1979+
ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
1980+
ExpectIntEQ(ret, 1);
1981+
1982+
/* One-shot sign + verify with NULL ctx should fail */
1983+
ExpectIntEQ(wolfSSL_EVP_DigestSign(NULL, sig, &sigSz, testData,
1984+
(unsigned int)XSTRLEN((const char*)testData)),
1985+
WOLFSSL_FAILURE);
1986+
ExpectIntEQ(wolfSSL_EVP_DigestVerify(NULL, sig, sigSz, testData,
1987+
(unsigned int)XSTRLEN((const char*)testData)),
1988+
WOLFSSL_FAILURE);
1989+
1990+
wolfSSL_EVP_PKEY_free(pubKey);
1991+
wolfSSL_EVP_PKEY_free(privKey);
1992+
#endif
1993+
return EXPECT_RESULT();
1994+
}
1995+
1996+
1997+
int test_wolfSSL_EVP_DigestSign_ecc(void)
1998+
{
1999+
EXPECT_DECLS;
2000+
#if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
2001+
WOLFSSL_EVP_PKEY* privKey = NULL;
2002+
WOLFSSL_EVP_PKEY* pubKey = NULL;
2003+
const unsigned char testData[] = "ECC one-shot test";
2004+
WOLFSSL_EVP_MD_CTX mdCtx;
2005+
int ret;
2006+
const unsigned char* cp;
2007+
const unsigned char* p;
2008+
unsigned char sig[256];
2009+
size_t sigSz;
2010+
2011+
cp = ecc_clikey_der_256;
2012+
ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
2013+
sizeof_ecc_clikey_der_256));
2014+
p = ecc_clikeypub_der_256;
2015+
ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
2016+
sizeof_ecc_clikeypub_der_256)));
2017+
2018+
/* One-shot sign */
2019+
wolfSSL_EVP_MD_CTX_init(&mdCtx);
2020+
ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
2021+
NULL, privKey), 1);
2022+
sigSz = sizeof(sig);
2023+
ExpectIntEQ(wolfSSL_EVP_DigestSign(&mdCtx, sig, &sigSz, testData,
2024+
(unsigned int)XSTRLEN((const char*)testData)), 1);
2025+
ExpectIntGT((int)sigSz, 0);
2026+
ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
2027+
ExpectIntEQ(ret, 1);
2028+
2029+
/* One-shot verify */
2030+
wolfSSL_EVP_MD_CTX_init(&mdCtx);
2031+
ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL,
2032+
wolfSSL_EVP_sha256(), NULL, pubKey), 1);
2033+
ExpectIntEQ(wolfSSL_EVP_DigestVerify(&mdCtx, sig, sigSz, testData,
2034+
(unsigned int)XSTRLEN((const char*)testData)), 1);
2035+
ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
2036+
ExpectIntEQ(ret, 1);
2037+
2038+
wolfSSL_EVP_PKEY_free(pubKey);
2039+
wolfSSL_EVP_PKEY_free(privKey);
2040+
#endif
2041+
return EXPECT_RESULT();
2042+
}
2043+
2044+
19302045
int test_wolfSSL_EVP_PKEY_encrypt(void)
19312046
{
19322047
EXPECT_DECLS;

tests/api/test_evp_pkey.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ int test_wolfSSL_EVP_PKEY_sign_verify_ec(void);
5757
int test_wolfSSL_EVP_MD_rsa_signing(void);
5858
int test_wc_RsaPSS_DigitalSignVerify(void);
5959
int test_wolfSSL_EVP_MD_ecc_signing(void);
60+
int test_wolfSSL_EVP_DigestSign(void);
61+
int test_wolfSSL_EVP_DigestSign_ecc(void);
6062
int test_wolfSSL_EVP_PKEY_encrypt(void);
6163
int test_wolfSSL_EVP_PKEY_derive(void);
6264
int test_wolfSSL_EVP_PKEY_print_public(void);
@@ -95,6 +97,8 @@ int test_wolfSSL_EVP_PKEY_print_public(void);
9597
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_MD_rsa_signing), \
9698
TEST_DECL_GROUP("evp_pkey", test_wc_RsaPSS_DigitalSignVerify), \
9799
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_MD_ecc_signing), \
100+
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_DigestSign), \
101+
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_DigestSign_ecc), \
98102
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_encrypt), \
99103
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_derive), \
100104
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_print_public)

tests/api/test_tls.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <tests/utils.h>
3232
#include <tests/api/test_tls.h>
3333
#include <wolfssl/internal.h>
34+
#include <wolfssl/openssl/ssl.h>
3435

3536

3637
int test_utils_memio_move_message(void)
@@ -768,3 +769,60 @@ int test_tls_set_curves_list_ecc_fallback(void)
768769
return EXPECT_RESULT();
769770
}
770771

772+
773+
int test_wolfSSL_alert_type_string(void)
774+
{
775+
EXPECT_DECLS;
776+
#ifndef NO_TLS
777+
/* wolfSSL_alert_type_string returns short code for alert level */
778+
ExpectStrEQ(wolfSSL_alert_type_string(alert_warning), "W");
779+
ExpectStrEQ(wolfSSL_alert_type_string(alert_fatal), "F");
780+
ExpectStrEQ(wolfSSL_alert_type_string(0), "U");
781+
ExpectStrEQ(wolfSSL_alert_type_string(-1), "U");
782+
ExpectStrEQ(wolfSSL_alert_type_string(99), "U");
783+
#endif
784+
return EXPECT_RESULT();
785+
}
786+
787+
788+
int test_wolfSSL_alert_desc_string(void)
789+
{
790+
EXPECT_DECLS;
791+
#ifndef NO_TLS
792+
/* wolfSSL_alert_desc_string returns short 2-letter code */
793+
ExpectStrEQ(wolfSSL_alert_desc_string(close_notify), "CN");
794+
ExpectStrEQ(wolfSSL_alert_desc_string(unexpected_message), "UM");
795+
ExpectStrEQ(wolfSSL_alert_desc_string(bad_record_mac), "BM");
796+
ExpectStrEQ(wolfSSL_alert_desc_string(record_overflow), "RO");
797+
ExpectStrEQ(wolfSSL_alert_desc_string(decompression_failure), "DF");
798+
ExpectStrEQ(wolfSSL_alert_desc_string(handshake_failure), "HF");
799+
ExpectStrEQ(wolfSSL_alert_desc_string(no_certificate), "NC");
800+
ExpectStrEQ(wolfSSL_alert_desc_string(bad_certificate), "BC");
801+
ExpectStrEQ(wolfSSL_alert_desc_string(unsupported_certificate), "UC");
802+
ExpectStrEQ(wolfSSL_alert_desc_string(certificate_revoked), "CR");
803+
ExpectStrEQ(wolfSSL_alert_desc_string(certificate_expired), "CE");
804+
ExpectStrEQ(wolfSSL_alert_desc_string(certificate_unknown), "CU");
805+
ExpectStrEQ(wolfSSL_alert_desc_string(illegal_parameter), "IP");
806+
ExpectStrEQ(wolfSSL_alert_desc_string(unknown_ca), "CA");
807+
ExpectStrEQ(wolfSSL_alert_desc_string(access_denied), "AD");
808+
ExpectStrEQ(wolfSSL_alert_desc_string(decode_error), "DE");
809+
ExpectStrEQ(wolfSSL_alert_desc_string(decrypt_error), "DC");
810+
ExpectStrEQ(wolfSSL_alert_desc_string(wolfssl_alert_protocol_version), "PV");
811+
ExpectStrEQ(wolfSSL_alert_desc_string(insufficient_security), "IS");
812+
ExpectStrEQ(wolfSSL_alert_desc_string(internal_error), "IE");
813+
ExpectStrEQ(wolfSSL_alert_desc_string(inappropriate_fallback), "IF");
814+
ExpectStrEQ(wolfSSL_alert_desc_string(user_canceled), "US");
815+
ExpectStrEQ(wolfSSL_alert_desc_string(no_renegotiation), "NR");
816+
ExpectStrEQ(wolfSSL_alert_desc_string(missing_extension), "ME");
817+
ExpectStrEQ(wolfSSL_alert_desc_string(unsupported_extension), "UE");
818+
ExpectStrEQ(wolfSSL_alert_desc_string(unrecognized_name), "UN");
819+
ExpectStrEQ(wolfSSL_alert_desc_string(bad_certificate_status_response), "BR");
820+
ExpectStrEQ(wolfSSL_alert_desc_string(unknown_psk_identity), "UP");
821+
ExpectStrEQ(wolfSSL_alert_desc_string(certificate_required), "CQ");
822+
ExpectStrEQ(wolfSSL_alert_desc_string(no_application_protocol), "AP");
823+
/* Unknown alert description returns "UK" */
824+
ExpectStrEQ(wolfSSL_alert_desc_string(255), "UK");
825+
#endif
826+
return EXPECT_RESULT();
827+
}
828+

tests/api/test_tls.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ int test_tls_certreq_order(void);
3131
int test_tls12_bad_cv_sig_alg(void);
3232
int test_tls12_no_null_compression(void);
3333
int test_tls_set_curves_list_ecc_fallback(void);
34+
int test_wolfSSL_alert_type_string(void);
35+
int test_wolfSSL_alert_desc_string(void);
3436

3537
#define TEST_TLS_DECLS \
3638
TEST_DECL_GROUP("tls", test_utils_memio_move_message), \
@@ -41,6 +43,8 @@ int test_tls_set_curves_list_ecc_fallback(void);
4143
TEST_DECL_GROUP("tls", test_tls_certreq_order), \
4244
TEST_DECL_GROUP("tls", test_tls12_bad_cv_sig_alg), \
4345
TEST_DECL_GROUP("tls", test_tls12_no_null_compression), \
44-
TEST_DECL_GROUP("tls", test_tls_set_curves_list_ecc_fallback)
46+
TEST_DECL_GROUP("tls", test_tls_set_curves_list_ecc_fallback), \
47+
TEST_DECL_GROUP("tls", test_wolfSSL_alert_type_string), \
48+
TEST_DECL_GROUP("tls", test_wolfSSL_alert_desc_string)
4549

4650
#endif /* TESTS_API_TEST_TLS_H */

wolfcrypt/src/evp.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4949,6 +4949,25 @@ int wolfSSL_EVP_DigestSignFinal(WOLFSSL_EVP_MD_CTX *ctx, unsigned char *sig,
49494949
return ret;
49504950
}
49514951

4952+
int wolfSSL_EVP_DigestSign(WOLFSSL_EVP_MD_CTX *ctx, unsigned char *sigret,
4953+
size_t *siglen, const unsigned char *tbs,
4954+
size_t tbslen)
4955+
{
4956+
WOLFSSL_ENTER("EVP_DigestSign");
4957+
4958+
if (ctx == NULL || siglen == NULL)
4959+
return WOLFSSL_FAILURE;
4960+
4961+
if (sigret != NULL) {
4962+
if (tbs == NULL || tbslen == 0)
4963+
return WOLFSSL_FAILURE;
4964+
if (wolfSSL_EVP_DigestSignUpdate(ctx, tbs, (unsigned int)tbslen)
4965+
!= WOLFSSL_SUCCESS)
4966+
return WOLFSSL_FAILURE;
4967+
}
4968+
return wolfSSL_EVP_DigestSignFinal(ctx, sigret, siglen);
4969+
}
4970+
49524971
int wolfSSL_EVP_DigestVerifyInit(WOLFSSL_EVP_MD_CTX *ctx,
49534972
WOLFSSL_EVP_PKEY_CTX **pctx,
49544973
const WOLFSSL_EVP_MD *type,
@@ -5044,6 +5063,21 @@ int wolfSSL_EVP_DigestVerifyFinal(WOLFSSL_EVP_MD_CTX *ctx,
50445063
return WOLFSSL_FAILURE;
50455064
}
50465065

5066+
int wolfSSL_EVP_DigestVerify(WOLFSSL_EVP_MD_CTX *ctx,
5067+
const unsigned char *sigret, size_t siglen,
5068+
const unsigned char *tbs, size_t tbslen)
5069+
{
5070+
WOLFSSL_ENTER("EVP_DigestVerify");
5071+
5072+
if (ctx == NULL || sigret == NULL || tbs == NULL)
5073+
return WOLFSSL_FAILURE;
5074+
5075+
if (wolfSSL_EVP_DigestVerifyUpdate(ctx, tbs, tbslen) != WOLFSSL_SUCCESS)
5076+
return WOLFSSL_FAILURE;
5077+
5078+
return wolfSSL_EVP_DigestVerifyFinal(ctx, sigret, siglen);
5079+
}
5080+
50475081

50485082
#ifdef WOLFSSL_APACHE_HTTPD
50495083
#if !defined(USE_WINDOWS_API) && !defined(MICROCHIP_PIC32)

0 commit comments

Comments
 (0)