Skip to content

Commit eef9950

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 fb64844 commit eef9950

8 files changed

Lines changed: 307 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
@@ -1997,6 +1997,121 @@ int test_wolfSSL_EVP_MD_ecc_signing(void)
19971997
}
19981998

19991999

2000+
int test_wolfSSL_EVP_DigestSign(void)
2001+
{
2002+
EXPECT_DECLS;
2003+
#if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
2004+
WOLFSSL_EVP_PKEY* privKey = NULL;
2005+
WOLFSSL_EVP_PKEY* pubKey = NULL;
2006+
const unsigned char testData[] = "Hi There";
2007+
WOLFSSL_EVP_MD_CTX mdCtx;
2008+
int ret;
2009+
const unsigned char* cp;
2010+
const unsigned char* p;
2011+
unsigned char sig[2048/8];
2012+
size_t sigSz;
2013+
2014+
cp = client_key_der_2048;
2015+
ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
2016+
sizeof_client_key_der_2048)));
2017+
p = client_keypub_der_2048;
2018+
ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
2019+
sizeof_client_keypub_der_2048)));
2020+
2021+
/* One-shot sign: query size first */
2022+
wolfSSL_EVP_MD_CTX_init(&mdCtx);
2023+
ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
2024+
NULL, privKey), 1);
2025+
sigSz = 0;
2026+
ExpectIntEQ(wolfSSL_EVP_DigestSign(&mdCtx, NULL, &sigSz, testData,
2027+
(unsigned int)XSTRLEN((const char*)testData)), 1);
2028+
ExpectIntGT((int)sigSz, 0);
2029+
ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
2030+
ExpectIntEQ(ret, 1);
2031+
2032+
/* One-shot sign: actually produce the signature */
2033+
wolfSSL_EVP_MD_CTX_init(&mdCtx);
2034+
ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
2035+
NULL, privKey), 1);
2036+
sigSz = sizeof(sig);
2037+
ExpectIntEQ(wolfSSL_EVP_DigestSign(&mdCtx, sig, &sigSz, testData,
2038+
(unsigned int)XSTRLEN((const char*)testData)), 1);
2039+
ExpectIntGT((int)sigSz, 0);
2040+
ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
2041+
ExpectIntEQ(ret, 1);
2042+
2043+
/* One-shot verify */
2044+
wolfSSL_EVP_MD_CTX_init(&mdCtx);
2045+
ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL,
2046+
wolfSSL_EVP_sha256(), NULL, pubKey), 1);
2047+
ExpectIntEQ(wolfSSL_EVP_DigestVerify(&mdCtx, sig, sigSz, testData,
2048+
(unsigned int)XSTRLEN((const char*)testData)), 1);
2049+
ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
2050+
ExpectIntEQ(ret, 1);
2051+
2052+
/* One-shot sign + verify with NULL ctx should fail */
2053+
ExpectIntEQ(wolfSSL_EVP_DigestSign(NULL, sig, &sigSz, testData,
2054+
(unsigned int)XSTRLEN((const char*)testData)),
2055+
WOLFSSL_FAILURE);
2056+
ExpectIntEQ(wolfSSL_EVP_DigestVerify(NULL, sig, sigSz, testData,
2057+
(unsigned int)XSTRLEN((const char*)testData)),
2058+
WOLFSSL_FAILURE);
2059+
2060+
wolfSSL_EVP_PKEY_free(pubKey);
2061+
wolfSSL_EVP_PKEY_free(privKey);
2062+
#endif
2063+
return EXPECT_RESULT();
2064+
}
2065+
2066+
2067+
int test_wolfSSL_EVP_DigestSign_ecc(void)
2068+
{
2069+
EXPECT_DECLS;
2070+
#if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
2071+
WOLFSSL_EVP_PKEY* privKey = NULL;
2072+
WOLFSSL_EVP_PKEY* pubKey = NULL;
2073+
const unsigned char testData[] = "ECC one-shot test";
2074+
WOLFSSL_EVP_MD_CTX mdCtx;
2075+
int ret;
2076+
const unsigned char* cp;
2077+
const unsigned char* p;
2078+
unsigned char sig[256];
2079+
size_t sigSz;
2080+
2081+
cp = ecc_clikey_der_256;
2082+
ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
2083+
sizeof_ecc_clikey_der_256));
2084+
p = ecc_clikeypub_der_256;
2085+
ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
2086+
sizeof_ecc_clikeypub_der_256)));
2087+
2088+
/* One-shot sign */
2089+
wolfSSL_EVP_MD_CTX_init(&mdCtx);
2090+
ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
2091+
NULL, privKey), 1);
2092+
sigSz = sizeof(sig);
2093+
ExpectIntEQ(wolfSSL_EVP_DigestSign(&mdCtx, sig, &sigSz, testData,
2094+
(unsigned int)XSTRLEN((const char*)testData)), 1);
2095+
ExpectIntGT((int)sigSz, 0);
2096+
ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
2097+
ExpectIntEQ(ret, 1);
2098+
2099+
/* One-shot verify */
2100+
wolfSSL_EVP_MD_CTX_init(&mdCtx);
2101+
ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL,
2102+
wolfSSL_EVP_sha256(), NULL, pubKey), 1);
2103+
ExpectIntEQ(wolfSSL_EVP_DigestVerify(&mdCtx, sig, sigSz, testData,
2104+
(unsigned int)XSTRLEN((const char*)testData)), 1);
2105+
ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
2106+
ExpectIntEQ(ret, 1);
2107+
2108+
wolfSSL_EVP_PKEY_free(pubKey);
2109+
wolfSSL_EVP_PKEY_free(privKey);
2110+
#endif
2111+
return EXPECT_RESULT();
2112+
}
2113+
2114+
20002115
int test_wolfSSL_EVP_PKEY_encrypt(void)
20012116
{
20022117
EXPECT_DECLS;

tests/api/test_evp_pkey.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ int test_wolfSSL_EVP_PKEY_sign_verify_ec(void);
5858
int test_wolfSSL_EVP_MD_rsa_signing(void);
5959
int test_wc_RsaPSS_DigitalSignVerify(void);
6060
int test_wolfSSL_EVP_MD_ecc_signing(void);
61+
int test_wolfSSL_EVP_DigestSign(void);
62+
int test_wolfSSL_EVP_DigestSign_ecc(void);
6163
int test_wolfSSL_EVP_PKEY_encrypt(void);
6264
int test_wolfSSL_EVP_PKEY_derive(void);
6365
int test_wolfSSL_EVP_PKEY_print_public(void);
@@ -98,6 +100,8 @@ int test_wolfSSL_EVP_PKEY_print_public(void);
98100
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_MD_rsa_signing), \
99101
TEST_DECL_GROUP("evp_pkey", test_wc_RsaPSS_DigitalSignVerify), \
100102
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_MD_ecc_signing), \
103+
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_DigestSign), \
104+
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_DigestSign_ecc), \
101105
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_encrypt), \
102106
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_derive), \
103107
TEST_DECL_GROUP("evp_pkey", test_wolfSSL_EVP_PKEY_print_public)

tests/api/test_tls.c

Lines changed: 53 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)
@@ -1058,6 +1059,17 @@ int test_tls12_corrupted_finished(void)
10581059
wolfSSL_CTX_free(ctx_c);
10591060
wolfSSL_free(ssl_s);
10601061
wolfSSL_CTX_free(ctx_s);
1062+
1063+
int test_wolfSSL_alert_type_string(void)
1064+
{
1065+
EXPECT_DECLS;
1066+
#ifndef NO_TLS
1067+
/* wolfSSL_alert_type_string returns short code for alert level */
1068+
ExpectStrEQ(wolfSSL_alert_type_string(alert_warning), "W");
1069+
ExpectStrEQ(wolfSSL_alert_type_string(alert_fatal), "F");
1070+
ExpectStrEQ(wolfSSL_alert_type_string(0), "U");
1071+
ExpectStrEQ(wolfSSL_alert_type_string(-1), "U");
1072+
ExpectStrEQ(wolfSSL_alert_type_string(99), "U");
10611073
#endif
10621074
return EXPECT_RESULT();
10631075
}
@@ -1121,3 +1133,44 @@ int test_tls12_peerauth_failsafe(void)
11211133
#endif
11221134
return EXPECT_RESULT();
11231135
}
1136+
1137+
int test_wolfSSL_alert_desc_string(void)
1138+
{
1139+
EXPECT_DECLS;
1140+
#ifndef NO_TLS
1141+
/* wolfSSL_alert_desc_string returns short 2-letter code */
1142+
ExpectStrEQ(wolfSSL_alert_desc_string(close_notify), "CN");
1143+
ExpectStrEQ(wolfSSL_alert_desc_string(unexpected_message), "UM");
1144+
ExpectStrEQ(wolfSSL_alert_desc_string(bad_record_mac), "BM");
1145+
ExpectStrEQ(wolfSSL_alert_desc_string(record_overflow), "RO");
1146+
ExpectStrEQ(wolfSSL_alert_desc_string(decompression_failure), "DF");
1147+
ExpectStrEQ(wolfSSL_alert_desc_string(handshake_failure), "HF");
1148+
ExpectStrEQ(wolfSSL_alert_desc_string(no_certificate), "NC");
1149+
ExpectStrEQ(wolfSSL_alert_desc_string(bad_certificate), "BC");
1150+
ExpectStrEQ(wolfSSL_alert_desc_string(unsupported_certificate), "UC");
1151+
ExpectStrEQ(wolfSSL_alert_desc_string(certificate_revoked), "CR");
1152+
ExpectStrEQ(wolfSSL_alert_desc_string(certificate_expired), "CE");
1153+
ExpectStrEQ(wolfSSL_alert_desc_string(certificate_unknown), "CU");
1154+
ExpectStrEQ(wolfSSL_alert_desc_string(illegal_parameter), "IP");
1155+
ExpectStrEQ(wolfSSL_alert_desc_string(unknown_ca), "CA");
1156+
ExpectStrEQ(wolfSSL_alert_desc_string(access_denied), "AD");
1157+
ExpectStrEQ(wolfSSL_alert_desc_string(decode_error), "DE");
1158+
ExpectStrEQ(wolfSSL_alert_desc_string(decrypt_error), "DC");
1159+
ExpectStrEQ(wolfSSL_alert_desc_string(wolfssl_alert_protocol_version), "PV");
1160+
ExpectStrEQ(wolfSSL_alert_desc_string(insufficient_security), "IS");
1161+
ExpectStrEQ(wolfSSL_alert_desc_string(internal_error), "IE");
1162+
ExpectStrEQ(wolfSSL_alert_desc_string(inappropriate_fallback), "IF");
1163+
ExpectStrEQ(wolfSSL_alert_desc_string(user_canceled), "US");
1164+
ExpectStrEQ(wolfSSL_alert_desc_string(no_renegotiation), "NR");
1165+
ExpectStrEQ(wolfSSL_alert_desc_string(missing_extension), "ME");
1166+
ExpectStrEQ(wolfSSL_alert_desc_string(unsupported_extension), "UE");
1167+
ExpectStrEQ(wolfSSL_alert_desc_string(unrecognized_name), "UN");
1168+
ExpectStrEQ(wolfSSL_alert_desc_string(bad_certificate_status_response), "BR");
1169+
ExpectStrEQ(wolfSSL_alert_desc_string(unknown_psk_identity), "UP");
1170+
ExpectStrEQ(wolfSSL_alert_desc_string(certificate_required), "CQ");
1171+
ExpectStrEQ(wolfSSL_alert_desc_string(no_application_protocol), "AP");
1172+
/* Unknown alert description returns "UK" */
1173+
ExpectStrEQ(wolfSSL_alert_desc_string(255), "UK");
1174+
#endif
1175+
return EXPECT_RESULT();
1176+
}

tests/api/test_tls.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ int test_tls12_etm_failed_resumption(void);
3535
int test_tls_set_curves_list_ecc_fallback(void);
3636
int test_tls12_corrupted_finished(void);
3737
int test_tls12_peerauth_failsafe(void);
38+
int test_wolfSSL_alert_type_string(void);
39+
int test_wolfSSL_alert_desc_string(void);
3840

3941
#define TEST_TLS_DECLS \
4042
TEST_DECL_GROUP("tls", test_utils_memio_move_message), \
@@ -49,6 +51,8 @@ int test_tls12_peerauth_failsafe(void);
4951
TEST_DECL_GROUP("tls", test_tls12_etm_failed_resumption), \
5052
TEST_DECL_GROUP("tls", test_tls_set_curves_list_ecc_fallback), \
5153
TEST_DECL_GROUP("tls", test_tls12_corrupted_finished), \
52-
TEST_DECL_GROUP("tls", test_tls12_peerauth_failsafe)
54+
TEST_DECL_GROUP("tls", test_tls12_peerauth_failsafe), \
55+
TEST_DECL_GROUP("tls", test_wolfSSL_alert_type_string), \
56+
TEST_DECL_GROUP("tls", test_wolfSSL_alert_desc_string)
5357

5458
#endif /* TESTS_API_TEST_TLS_H */

wolfcrypt/src/evp.c

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

4953+
int wolfSSL_EVP_DigestSign(WOLFSSL_EVP_MD_CTX *ctx, unsigned char *sigret,
4954+
size_t *siglen, const unsigned char *tbs,
4955+
size_t tbslen)
4956+
{
4957+
WOLFSSL_ENTER("EVP_DigestSign");
4958+
4959+
if (ctx == NULL || siglen == NULL)
4960+
return WOLFSSL_FAILURE;
4961+
4962+
if (sigret != NULL) {
4963+
if (tbs == NULL || tbslen == 0)
4964+
return WOLFSSL_FAILURE;
4965+
if (wolfSSL_EVP_DigestSignUpdate(ctx, tbs, (unsigned int)tbslen)
4966+
!= WOLFSSL_SUCCESS)
4967+
return WOLFSSL_FAILURE;
4968+
}
4969+
return wolfSSL_EVP_DigestSignFinal(ctx, sigret, siglen);
4970+
}
4971+
49534972
int wolfSSL_EVP_DigestVerifyInit(WOLFSSL_EVP_MD_CTX *ctx,
49544973
WOLFSSL_EVP_PKEY_CTX **pctx,
49554974
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)