#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef WOLFSSL_USER_SETTINGS
#include "user_settings.h"
#else
#include <wolfssl/options.h>
#endif
#include "examples/async/async_tls.h"
#include <wolfssl/ssl.h>
#include <wolfssl/wolfio.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#ifndef NET_CUSTOM
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <sys/stat.h>
#include <sys/select.h>
static int async_readyfile_exists(const char* path)
{
struct stat st;
if (path == NULL || path[0] == '\0') {
return 0;
}
return (stat(path, &st) == 0);
}
int async_readyfile_touch(const char* path)
{
FILE* f;
if (path == NULL || path[0] == '\0') {
return -1;
}
f = fopen(path, "w");
if (f == NULL) {
return -1;
}
fclose(f);
return 0;
}
void async_readyfile_clear(const char* path)
{
if (path == NULL || path[0] == '\0') {
return;
}
(void)remove(path);
}
int async_readyfile_wait(const char* path, int timeout_ms)
{
int waited_ms = 0;
const int step_ms = 50;
struct timeval tv;
while (waited_ms < timeout_ms) {
if (async_readyfile_exists(path)) {
return 0;
}
tv.tv_sec = 0;
tv.tv_usec = step_ms * 1000;
(void)select(0, NULL, NULL, NULL, &tv);
waited_ms += step_ms;
}
return -1;
}
#ifndef NET_CUSTOM
int async_posix_send_cb(WOLFSSL* ssl, char* buf, int sz, void* ctx)
{
(void)ssl;
int fd = (int)(intptr_t)ctx;
int ret = (int)NET_SEND(fd, buf, sz);
if (ret >= 0) {
return ret;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return WOLFSSL_CBIO_ERR_WANT_WRITE;
}
return WOLFSSL_CBIO_ERR_GENERAL;
}
int async_posix_recv_cb(WOLFSSL* ssl, char* buf, int sz, void* ctx)
{
(void)ssl;
int fd = (int)(intptr_t)ctx;
int ret = (int)NET_RECV(fd, buf, sz);
if (ret >= 0) {
return ret;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return WOLFSSL_CBIO_ERR_WANT_READ;
}
return WOLFSSL_CBIO_ERR_GENERAL;
}
int async_posix_getdevrandom(unsigned char *out, unsigned int sz)
{
ssize_t ret;
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
return -1;
}
ret = read(fd, out, sz);
close(fd);
if (ret != (ssize_t)sz) {
return -1;
}
return 0;
}
#endif
int posix_getdevrandom(unsigned char *out, unsigned int sz)
{
#ifdef NET_CUSTOM
return NET_GETDEVRANDOM(out, sz);
#else
return async_posix_getdevrandom(out, sz);
#endif
}
#ifdef WOLF_CRYPTO_CB
#ifndef TEST_PEND_COUNT
#define TEST_PEND_COUNT 2
#endif
int AsyncTlsCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
{
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
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
if (info->pk.type == WC_PK_TYPE_RSA ||
info->pk.type == WC_PK_TYPE_ECDSA_SIGN)
{
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) {
info->pk.rsa.key->devId = INVALID_DEVID;
switch (info->pk.rsa.type) {
case RSA_PUBLIC_ENCRYPT:
case RSA_PUBLIC_DECRYPT:
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:
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;
}
info->pk.rsa.key->devId = devIdArg;
}
#endif
#ifdef HAVE_ECC
if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
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);
info->pk.eckg.key->devId = devIdArg;
}
else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
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);
info->pk.eccsign.key->devId = devIdArg;
}
else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
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);
info->pk.eccverify.key->devId = devIdArg;
}
else if (info->pk.type == WC_PK_TYPE_ECDH) {
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);
info->pk.ecdh.private_key->devId = devIdArg;
}
#endif
}
(void)devIdArg;
(void)myCtx;
return ret;
}
#endif
#ifdef HAVE_PK_CALLBACKS
#endif