2023-09-11 12:05:11 -04:00
|
|
|
/* async-tls.c
|
|
|
|
|
*
|
2025-01-21 09:55:03 -07:00
|
|
|
* Copyright (C) 2006-2025 wolfSSL Inc.
|
2023-09-11 12:05:11 -04:00
|
|
|
*
|
2024-07-19 13:15:05 -06:00
|
|
|
* This file is part of wolfSSL.
|
2023-09-11 12:05:11 -04:00
|
|
|
*
|
|
|
|
|
* wolfSSL is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2025-07-10 16:01:52 -06:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2023-09-11 12:05:11 -04:00
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* wolfSSL is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2024-07-19 13:15:05 -06:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
2023-09-11 12:05:11 -04:00
|
|
|
*/
|
|
|
|
|
|
2024-08-09 14:51:51 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-09-11 12:05:11 -04:00
|
|
|
#ifndef WOLFSSL_USER_SETTINGS
|
|
|
|
|
#include <wolfssl/options.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <wolfssl/ssl.h>
|
|
|
|
|
#include <wolfssl/wolfio.h>
|
|
|
|
|
#include <wolfssl/wolfcrypt/error-crypt.h>
|
|
|
|
|
#include "examples/async/async_tls.h"
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------*/
|
|
|
|
|
/* --- Example Crypto Callback --- */
|
|
|
|
|
/* ---------------------------------------------------------------------------*/
|
|
|
|
|
#ifdef WOLF_CRYPTO_CB
|
|
|
|
|
|
|
|
|
|
/* Example custom context for crypto callback */
|
|
|
|
|
#ifndef TEST_PEND_COUNT
|
|
|
|
|
#define TEST_PEND_COUNT 2
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Example crypto dev callback function that calls software version */
|
|
|
|
|
/* This is where you would plug-in calls to your own hardware crypto */
|
|
|
|
|
int AsyncTlsCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
|
|
|
|
|
{
|
src/internal.c: in wolfSSL_ERR_reason_error_string(), add missing error string for SCR_DIFFERENT_CERT_E, and de-gate error strings previously gated on HAVE_HTTP_CLIENT.
tests/api.c: add error_test() adapted from wolfcrypt/test/test.c, checking all error strings for expected presence/absence and length, called from existing test_wolfSSL_ERR_strings().
wolfssl/ssl.h, wolfssl/error-ssl.h, and wolfssl/wolfcrypt/error-crypt.h:
* move several negative error return codes from ssl.h to error-ssl.h,
* renumber them to conform to existing sequence, and
* include error-ssl.h from ssl.h;
* add special-case WOLFSSL_DEBUG_TRACE_ERROR_CODES macros for WOLFSSL_FAILURE;
* add missing WOLFSSL_API attribute to wc_backtrace_render().
add numerous WC_NO_ERR_TRACE()s to operand error code uses, cleaning up error traces in general, and particularly when WOLFSSL_DEBUG_TRACE_ERROR_CODES_ALWAYS.
* crypto lib (36),
* crypto test&benchmark (20),
* TLS lib (179),
* examples (122),
* linuxkm (3),
* tests/api.c (2272).
2024-08-28 23:05:04 -05:00
|
|
|
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); /* bypass HW by default */
|
2023-09-11 12:05:11 -04:00
|
|
|
AsyncTlsCryptoCbCtx* myCtx = (AsyncTlsCryptoCbCtx*)ctx;
|
|
|
|
|
|
|
|
|
|
if (info == NULL)
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_CRYPTOCB
|
|
|
|
|
wc_CryptoCb_InfoString(info);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (info->algo_type == WC_ALGO_TYPE_PK) {
|
|
|
|
|
#ifdef WOLFSSL_ASYNC_CRYPT
|
|
|
|
|
/* Test pending response */
|
|
|
|
|
if (info->pk.type == WC_PK_TYPE_RSA ||
|
|
|
|
|
info->pk.type == WC_PK_TYPE_EC_KEYGEN ||
|
|
|
|
|
info->pk.type == WC_PK_TYPE_ECDSA_SIGN ||
|
|
|
|
|
info->pk.type == WC_PK_TYPE_ECDSA_VERIFY ||
|
|
|
|
|
info->pk.type == WC_PK_TYPE_ECDH)
|
|
|
|
|
{
|
|
|
|
|
if (myCtx->pendingCount++ < TEST_PEND_COUNT) return WC_PENDING_E;
|
|
|
|
|
myCtx->pendingCount = 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef NO_RSA
|
|
|
|
|
if (info->pk.type == WC_PK_TYPE_RSA) {
|
|
|
|
|
/* set devId to invalid, so software is used */
|
|
|
|
|
info->pk.rsa.key->devId = INVALID_DEVID;
|
|
|
|
|
|
|
|
|
|
switch (info->pk.rsa.type) {
|
|
|
|
|
case RSA_PUBLIC_ENCRYPT:
|
|
|
|
|
case RSA_PUBLIC_DECRYPT:
|
|
|
|
|
/* perform software based RSA public op */
|
|
|
|
|
ret = wc_RsaFunction(
|
|
|
|
|
info->pk.rsa.in, info->pk.rsa.inLen,
|
|
|
|
|
info->pk.rsa.out, info->pk.rsa.outLen,
|
|
|
|
|
info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
|
|
|
|
|
break;
|
|
|
|
|
case RSA_PRIVATE_ENCRYPT:
|
|
|
|
|
case RSA_PRIVATE_DECRYPT:
|
|
|
|
|
/* perform software based RSA private op */
|
|
|
|
|
ret = wc_RsaFunction(
|
|
|
|
|
info->pk.rsa.in, info->pk.rsa.inLen,
|
|
|
|
|
info->pk.rsa.out, info->pk.rsa.outLen,
|
|
|
|
|
info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* reset devId */
|
|
|
|
|
info->pk.rsa.key->devId = devIdArg;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_ECC
|
|
|
|
|
if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
|
|
|
|
|
/* set devId to invalid, so software is used */
|
|
|
|
|
info->pk.eckg.key->devId = INVALID_DEVID;
|
|
|
|
|
|
|
|
|
|
ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
|
|
|
|
|
info->pk.eckg.key, info->pk.eckg.curveId);
|
|
|
|
|
|
|
|
|
|
/* reset devId */
|
|
|
|
|
info->pk.eckg.key->devId = devIdArg;
|
|
|
|
|
}
|
|
|
|
|
else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
|
|
|
|
|
/* set devId to invalid, so software is used */
|
|
|
|
|
info->pk.eccsign.key->devId = INVALID_DEVID;
|
|
|
|
|
|
|
|
|
|
ret = wc_ecc_sign_hash(
|
|
|
|
|
info->pk.eccsign.in, info->pk.eccsign.inlen,
|
|
|
|
|
info->pk.eccsign.out, info->pk.eccsign.outlen,
|
|
|
|
|
info->pk.eccsign.rng, info->pk.eccsign.key);
|
|
|
|
|
|
|
|
|
|
/* reset devId */
|
|
|
|
|
info->pk.eccsign.key->devId = devIdArg;
|
|
|
|
|
}
|
|
|
|
|
else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
|
|
|
|
|
/* set devId to invalid, so software is used */
|
|
|
|
|
info->pk.eccverify.key->devId = INVALID_DEVID;
|
|
|
|
|
|
|
|
|
|
ret = wc_ecc_verify_hash(
|
|
|
|
|
info->pk.eccverify.sig, info->pk.eccverify.siglen,
|
|
|
|
|
info->pk.eccverify.hash, info->pk.eccverify.hashlen,
|
|
|
|
|
info->pk.eccverify.res, info->pk.eccverify.key);
|
|
|
|
|
|
|
|
|
|
/* reset devId */
|
|
|
|
|
info->pk.eccverify.key->devId = devIdArg;
|
|
|
|
|
}
|
|
|
|
|
else if (info->pk.type == WC_PK_TYPE_ECDH) {
|
|
|
|
|
/* set devId to invalid, so software is used */
|
|
|
|
|
info->pk.ecdh.private_key->devId = INVALID_DEVID;
|
|
|
|
|
|
|
|
|
|
ret = wc_ecc_shared_secret(
|
|
|
|
|
info->pk.ecdh.private_key, info->pk.ecdh.public_key,
|
|
|
|
|
info->pk.ecdh.out, info->pk.ecdh.outlen);
|
|
|
|
|
|
|
|
|
|
/* reset devId */
|
|
|
|
|
info->pk.ecdh.private_key->devId = devIdArg;
|
|
|
|
|
}
|
|
|
|
|
#endif /* HAVE_ECC */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(void)devIdArg;
|
|
|
|
|
(void)myCtx;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLF_CRYPTO_CB */
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------*/
|
|
|
|
|
/* --- Example PK (Public Key) Callback --- */
|
|
|
|
|
/* ---------------------------------------------------------------------------*/
|
|
|
|
|
#ifdef HAVE_PK_CALLBACKS
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|