From ac6635593b0fa3ffbf4cbfd75f2e71db1671be15 Mon Sep 17 00:00:00 2001 From: toddouska Date: Mon, 27 Jun 2016 10:53:34 -0700 Subject: [PATCH] Revert "Bio" --- .gitignore | 1 - examples/client/client.c | 2 +- src/include.am | 4 +- src/internal.c | 537 +-- src/ssl.c | 2402 ++++++---- src/tls.c | 8 +- tests/api.c | 762 --- wolfcrypt/src/bio.c | 6352 -------------------------- wolfcrypt/src/compat-wolfcrypt.c | 1592 ------- wolfcrypt/src/error.c | 93 - wolfcrypt/test/test.c | 1059 +---- wolfssl-ntru.vcproj | 14 +- wolfssl.vcproj | 8 - wolfssl.vcxproj | 2 - wolfssl/internal.h | 48 +- wolfssl/openssl/evp.h | 253 +- wolfssl/openssl/hmac.h | 40 +- wolfssl/openssl/md5.h | 24 +- wolfssl/openssl/pem.h | 31 +- wolfssl/openssl/ripemd.h | 22 +- wolfssl/openssl/sha.h | 112 +- wolfssl/openssl/ssl.h | 226 +- wolfssl/ssl.h | 53 +- wolfssl/test.h | 100 +- wolfssl/wolfcrypt/bio.h | 571 --- wolfssl/wolfcrypt/compat-wolfcrypt.h | 352 -- wolfssl/wolfcrypt/error-crypt.h | 34 +- wolfssl/wolfcrypt/include.am | 2 - 28 files changed, 2218 insertions(+), 12486 deletions(-) delete mode 100644 wolfcrypt/src/bio.c delete mode 100644 wolfcrypt/src/compat-wolfcrypt.c delete mode 100644 wolfssl/wolfcrypt/bio.h delete mode 100644 wolfssl/wolfcrypt/compat-wolfcrypt.h diff --git a/.gitignore b/.gitignore index 3439944e5..47c38e3f1 100644 --- a/.gitignore +++ b/.gitignore @@ -82,7 +82,6 @@ pkcs7envelopedData.der diff sslSniffer/sslSnifferTest/tracefile.txt tracefile.txt -test_bio.txt *.gz *.zip *.bak diff --git a/examples/client/client.c b/examples/client/client.c index ba4e08934..45264db6d 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -156,7 +156,7 @@ int ClientBenchmarkConnections(WOLFSSL_CTX* ctx, char* host, word16 port, /* time passed in number of connects give average */ int times = benchmark; int loops = resumeSession ? 2 : 1; - int i = 0; + int i = 0; #ifndef NO_SESSION_CACHE WOLFSSL_SESSION* benchSession = NULL; #endif diff --git a/src/include.am b/src/include.am index 838666b65..5044bd9a4 100644 --- a/src/include.am +++ b/src/include.am @@ -63,9 +63,7 @@ src_libwolfssl_la_SOURCES += \ wolfcrypt/src/hmac.c \ wolfcrypt/src/random.c \ wolfcrypt/src/sha256.c \ - wolfcrypt/src/hash.c \ - wolfcrypt/src/compat-wolfcrypt.c \ - wolfcrypt/src/bio.c + wolfcrypt/src/hash.c if !BUILD_USER_RSA if BUILD_RSA diff --git a/src/internal.c b/src/internal.c index 45d6e4342..7b9d2317a 100755 --- a/src/internal.c +++ b/src/internal.c @@ -33,8 +33,6 @@ #include #include #include -#include -#include #ifdef NO_INLINE #include #else @@ -1604,122 +1602,6 @@ void InitCiphers(WOLFSSL* ssl) #endif } -#ifdef OPENSSL_EXTRA -/* Set cipher pointers to null */ -static void DupCiphers(WOLFSSL* ssl, WOLFSSL* ossl) -{ -#ifdef BUILD_ARC4 - if (ossl->encrypt.arc4 != NULL) { - ssl->encrypt.arc4 = (Arc4*)XMALLOC(sizeof(Arc4), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->encrypt.arc4, ossl->encrypt.arc4, sizeof(Arc4)); - } - if (ossl->decrypt.arc4 != NULL) { - ssl->decrypt.arc4 = (Arc4*)XMALLOC(sizeof(Arc4), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->decrypt.arc4, ossl->decrypt.arc4, sizeof(Arc4)); - } -#endif -#ifdef BUILD_DES3 - if (ossl->encrypt.des3 != NULL) { - ssl->encrypt.des3 = (Des3*)XMALLOC(sizeof(Des3), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->encrypt.des3, ossl->encrypt.des3, sizeof(Des3)); - } - if (ossl->decrypt.des3 != NULL) { - ssl->decrypt.des3 = (Des3*)XMALLOC(sizeof(Des3), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->decrypt.des3, ossl->decrypt.des3, sizeof(Des3)); - } -#endif -#ifdef BUILD_AES - if (ossl->encrypt.aes != NULL) { - ssl->encrypt.aes = (Aes*)XMALLOC(sizeof(Aes), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->encrypt.aes, ossl->encrypt.aes, sizeof(Aes)); - } - if (ossl->decrypt.aes != NULL) { - ssl->decrypt.aes = (Aes*)XMALLOC(sizeof(Aes), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->decrypt.aes, ossl->decrypt.aes, sizeof(Aes)); - } -#endif -#ifdef HAVE_CAMELLIA - if (ossl->encrypt.cam != NULL) { - ssl->encrypt.cam = (Camellia*)XMALLOC(sizeof(Camellia), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->encrypt.cam, ossl->encrypt.cam, sizeof(Camellia)); - } - if (ossl->decrypt.cam != NULL) { - ssl->decrypt.cam = (Camellia*)XMALLOC(sizeof(Camellia), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->decrypt.cam, ossl->decrypt.cam, sizeof(Camellia)); - } -#endif -#ifdef HAVE_HC128 - if (ossl->encrypt.hc128 != NULL) { - ssl->encrypt.hc128 = (HC128*)XMALLOC(sizeof(HC128), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->encrypt.hc128, ossl->encrypt.hc128, sizeof(HC128)); - } - if (ossl->decrypt.hc128 != NULL) { - ssl->decrypt.hc128 = (HC128*)XMALLOC(sizeof(HC128), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->decrypt.hc128, ossl->decrypt.hc128, sizeof(HC128)); - } -#endif -#ifdef BUILD_RABBIT - if (ossl->encrypt.rabbit != NULL) { - ssl->encrypt.rabbit = (Rabbit*)XMALLOC(sizeof(Rabbit), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->encrypt.rabbit, ossl->encrypt.rabbit, sizeof(Rabbit)); - } - if (ossl->decrypt.rabbit != NULL) { - ssl->decrypt.rabbit = (Rabbit*)XMALLOC(sizeof(Rabbit), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->decrypt.rabbit, ossl->decrypt.rabbit, sizeof(Rabbit)); - } -#endif -#ifdef HAVE_CHACHA - if (ossl->encrypt.chacha != NULL) { - ssl->encrypt.chacha = (ChaCha*)XMALLOC(sizeof(ChaCha), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->encrypt.chacha, ossl->encrypt.chacha, sizeof(ChaCha)); - } - if (ossl->decrypt.chacha != NULL) { - ssl->decrypt.chacha = (ChaCha*)XMALLOC(sizeof(ChaCha), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->decrypt.chacha, ossl->decrypt.chacha, sizeof(ChaCha)); - } -#endif -#ifdef HAVE_IDEA - if (ossl->encrypt.idea != NULL) { - ssl->encrypt.idea = (Idea*)XMALLOC(sizeof(Idea), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->encrypt.idea, ossl->encrypt.idea, sizeof(Idea)); - } - if (ossl->decrypt.idea != NULL) { - ssl->decrypt.idea = (Idea*)XMALLOC(sizeof(Idea), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->decrypt.idea, ossl->decrypt.idea, sizeof(Idea)); - } -#endif -#ifdef HAVE_POLY1305 - if (ossl->auth.poly1305 != NULL) { - ssl->auth.poly1305 = (Poly1305*)XMALLOC(sizeof(Poly1305), - ssl->heap, DYNAMIC_TYPE_CIPHER); - XMEMCPY(ssl->auth.poly1305, ossl->auth.poly1305, sizeof(Poly1305)); - } -#endif - - ssl->encrypt.setup = ossl->encrypt.setup; - ssl->decrypt.setup = ossl->decrypt.setup; - -#ifdef HAVE_ONE_TIME_AUTH - ssl->auth.setup = ossl->auth.setup; -#endif -} -#endif /* OPENSSL_EXTRA */ /* Free ciphers */ void FreeCiphers(WOLFSSL* ssl) @@ -1796,23 +1678,6 @@ void InitCipherSpecs(CipherSpecs* cs) cs->block_size = 0; } -#ifdef OPENSSL_EXTRA -static void DupCipherSpecs(CipherSpecs* cs, CipherSpecs* ocs) -{ - cs->bulk_cipher_algorithm = ocs->bulk_cipher_algorithm; - cs->cipher_type = ocs->cipher_type; - cs->mac_algorithm = ocs->mac_algorithm; - cs->kea = ocs->kea; - cs->sig_algo = ocs->sig_algo; - - cs->hash_size = ocs->hash_size; - cs->static_ecdh = ocs->static_ecdh; - cs->key_size = ocs->key_size; - cs->iv_size = ocs->iv_size; - cs->block_size = ocs->block_size; -} -#endif /* OPENSSL_EXTRA */ - static void InitSuitesHashSigAlgo(Suites* suites, int haveECDSAsig, int haveRSAsig, int haveAnon) { @@ -2704,20 +2569,6 @@ void InitX509Name(WOLFSSL_X509_NAME* name, int dynamicFlag) } } -#ifdef OPENSSL_EXTRA -static int DupX509Name(WOLFSSL_X509_NAME* name, WOLFSSL_X509_NAME* oname) -{ - if (name == NULL || oname == NULL) - return BAD_FUNC_ARG; - - name->name = name->staticName; - XMEMCPY(name->staticName, oname->staticName, sizeof(oname->staticName)); - name->dynamicName = oname->dynamicName; - XMEMCPY(&name->fullName, &oname->fullName, sizeof(DecodedName)); - - return 0; -} -#endif /* OPENSSL_EXTRA */ void FreeX509Name(WOLFSSL_X509_NAME* name, void* heap) { @@ -2775,79 +2626,6 @@ void InitX509(WOLFSSL_X509* x509, int dynamicFlag, void* heap) #endif /* OPENSSL_EXTRA */ } -#ifdef OPENSSL_EXTRA -static int DupX509(WOLFSSL_X509* x509, WOLFSSL_X509* ox509) -{ - if (x509 == NULL || ox509 == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return BAD_FUNC_ARG; - } - - DupX509Name(&x509->issuer, &ox509->issuer); - DupX509Name(&x509->subject, &ox509->subject); - x509->version = ox509->version; - - if (ox509->pubKey.buffer != NULL) - XMEMCPY(x509->pubKey.buffer, ox509->pubKey.buffer, sizeof(Buffers)); - if (ox509->sig.buffer != NULL) - XMEMCPY(x509->sig.buffer, ox509->sig.buffer, sizeof(Buffers)); - if (ox509->derCert->buffer != NULL) - XMEMCPY(x509->derCert->buffer, ox509->derCert->buffer, sizeof(Buffers)); - if (ox509->altNames != NULL) - XMEMCPY(x509->altNames, ox509->altNames, sizeof(DNS_entry)); - if (ox509->altNamesNext != NULL) - XMEMCPY(x509->altNamesNext, ox509->altNamesNext, sizeof(DNS_entry)); - - x509->dynamicMemory = ox509->dynamicMemory; - x509->isCa = ox509->isCa; -#ifdef HAVE_ECC - x509->pkCurveOID = ox509->pkCurveOID; -#endif /* HAVE_ECC */ - - x509->pathLength = ox509->pathLength; - x509->basicConstSet = ox509->basicConstSet; - x509->basicConstCrit = ox509->basicConstCrit; - x509->basicConstPlSet = ox509->basicConstPlSet; - x509->subjAltNameSet = ox509->subjAltNameSet; - x509->subjAltNameCrit = ox509->subjAltNameCrit; - x509->authKeyIdSet = ox509->authKeyIdSet; - x509->authKeyIdCrit = ox509->authKeyIdCrit; - - if (ox509->authKeyId != NULL && ox509->authKeyIdSz) { - x509->authKeyId = (byte*)XMALLOC(ox509->authKeyIdSz, NULL, - DYNAMIC_TYPE_X509_EXT); - if (x509->authKeyId == NULL) - return MEMORY_E; - - XMEMCPY(x509->authKeyId, ox509->authKeyId, ox509->authKeyIdSz); - x509->authKeyIdSz = ox509->authKeyIdSz; - } - - x509->subjKeyIdSet = ox509->subjKeyIdSet; - x509->subjKeyIdCrit = ox509->subjKeyIdCrit; - - if (ox509->subjKeyId != NULL && ox509->subjKeyIdSz) { - x509->subjKeyId = (byte*)XMALLOC(ox509->subjKeyIdSz, NULL, - DYNAMIC_TYPE_X509_EXT); - if (x509->subjKeyId == NULL) - return MEMORY_E; - - XMEMCPY(x509->subjKeyId, ox509->subjKeyId, ox509->subjKeyIdSz); - x509->subjKeyIdSz = ox509->subjKeyIdSz; - } - - x509->keyUsageSet = ox509->keyUsageSet; - x509->keyUsageCrit = ox509->keyUsageCrit; - x509->keyUsage = ox509->keyUsage; -#ifdef WOLFSSL_SEP - x509->certPolicySet = ox509->certPolicySet; - x509->certPolicyCrit = ox509->certPolicyCrit; -#endif /* WOLFSSL_SEP */ - - - return 0; -} -#endif /* OPENSSL_EXTRA */ /* Free wolfSSL X509 type */ void FreeX509(WOLFSSL_X509* x509) @@ -3693,317 +3471,6 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx) } -#ifdef OPENSSL_EXTRA -static int DupArrays(Arrays *arrays, Arrays *oarrays, void *heap) -{ - if (arrays == NULL || oarrays == NULL) { - WOLFSSL_MSG("need valid arrays objects"); - return BAD_FUNC_ARG; - } - - if (oarrays->pendingMsg != NULL && oarrays->pendingMsgSz) { - arrays->pendingMsg = (byte *)XMALLOC(oarrays->pendingMsgSz, heap, DYNAMIC_TYPE_SSL); - if (arrays->pendingMsg == NULL) - return MEMORY_E; - - XMEMCPY(arrays->pendingMsg, oarrays->pendingMsg, oarrays->pendingMsgSz); - arrays->pendingMsgSz = oarrays->pendingMsgSz; - } - arrays->pendingMsgOffset = oarrays->pendingMsgOffset; - arrays->preMasterSz = oarrays->preMasterSz; - -#ifndef NO_PSK - arrays->psk_keySz = oarrays->psk_keySz; - XMEMCPY(arrays->client_identity, oarrays->client_identity, - sizeof(oarrays->client_identity)); - XMEMCPY(arrays->server_hint, oarrays->server_hint, - sizeof(oarrays->server_hint)); - XMEMCPY(arrays->psk_key, oarrays->psk_key, - sizeof(oarrays->psk_key)); -#endif - - XMEMCPY(arrays->clientRandom, oarrays->clientRandom, - sizeof(oarrays->clientRandom)); - XMEMCPY(arrays->serverRandom, oarrays->serverRandom, - sizeof(oarrays->serverRandom)); - XMEMCPY(arrays->sessionID, oarrays->sessionID, - sizeof(oarrays->sessionID)); - XMEMCPY(arrays->preMasterSecret, oarrays->preMasterSecret, - sizeof(oarrays->preMasterSecret)); - XMEMCPY(arrays->masterSecret, oarrays->masterSecret, - sizeof(oarrays->masterSecret)); - arrays->sessionIDSz = oarrays->sessionIDSz; - -#ifdef WOLFSSL_DTLS - XMEMCPY(arrays->cookie, oarrays->cookie, sizeof(oarrays->cookie)); - arrays->cookieSz = oarrays->cookieSz; -#endif - - arrays->pendingMsgType = oarrays->pendingMsgType; - - return 0; -} - -static int DupBufferStatic(bufferStatic *buf, bufferStatic *obuf, - void *heap, int type) -{ - if (buf == NULL || obuf == NULL) { - WOLFSSL_MSG("need valid BufferStatic objects"); - return BAD_FUNC_ARG; - } - buf->dynamicFlag = obuf->dynamicFlag; - buf->bufferSize = obuf->bufferSize; - buf->length = obuf->length; - buf->idx = obuf->idx; - buf->offset = obuf->offset; - - XMEMCPY(buf->staticBuffer, obuf->staticBuffer, sizeof(obuf->staticBuffer)); - - if (buf->dynamicFlag) { - buf->buffer = (byte *)XMALLOC(buf->bufferSize, heap, type); - if (buf->buffer == NULL) - return MEMORY_E; - - XMEMCPY(buf->buffer, obuf->buffer, obuf->bufferSize); - } - else - buf->buffer = buf->staticBuffer; - - return 0; -} - -static int DupBuffer(buffer *buf, buffer *obuf, void *heap) -{ - if (buf == NULL || obuf == NULL) { - WOLFSSL_MSG("need valid Buffer objects"); - return BAD_FUNC_ARG; - } - - buf->length = obuf->length; - - if (obuf->buffer != NULL && obuf->length) { - buf->buffer = (byte *)XMALLOC(buf->length, - heap, DYNAMIC_TYPE_TMP_BUFFER); - if (buf->buffer == NULL) - return MEMORY_E; - - XMEMCPY(buf->buffer, obuf->buffer, obuf->length); - } - - return 0; -} - -static int DupBuffers(Buffers *buf, Buffers *obuf, void *heap) -{ - int ret; - - if (buf == NULL || obuf == NULL) { - WOLFSSL_MSG("need valid Buffers objects"); - return BAD_FUNC_ARG; - } - - ret = DupBufferStatic(&buf->inputBuffer, &obuf->inputBuffer, - heap, DYNAMIC_TYPE_IN_BUFFER); - if (ret != 0) - return ret; - - ret = DupBufferStatic(&buf->outputBuffer, &obuf->outputBuffer, - heap, DYNAMIC_TYPE_OUT_BUFFER); - if (ret != 0) - return ret; - - ret = DupBuffer(&buf->domainName, &obuf->domainName, heap); - if (ret != 0) - return ret; - - buf->clearOutputBuffer = obuf->clearOutputBuffer; - - buf->prevSent = obuf->prevSent; - buf->plainSz = obuf->plainSz; - - buf->weOwnCert = obuf->weOwnCert; - buf->weOwnCertChain = obuf->weOwnCertChain; - buf->weOwnDH = obuf->weOwnDH; - buf->weOwnKey = obuf->weOwnKey; -#ifndef NO_DH - ret = DupBuffer(&buf->serverDH_Pub, &obuf->serverDH_Pub, heap); - if (ret != 0) - return ret; - ret = DupBuffer(&buf->serverDH_Priv, &obuf->serverDH_Priv, heap); - if (ret != 0) - return ret; -#endif -#ifndef NO_CERTS - /* ctx still owns certificate, certChain, key, dh, and cm */ - buf->certificate = obuf->certificate; - buf->certChain = obuf->certChain; - buf->key = obuf->key; -#endif -#ifdef WOLFSSL_DTLS - XMEMCPY(&buf->dtlsCtx, &obuf->dtlsCtx, sizeof(WOLFSSL_DTLS_CTX)); -#ifndef NO_WOLFSSL_SERVER - ret = DupBuffer(&buf->dtlsCookieSecret, &obuf->dtlsCookieSecret, heap); - if (ret != 0) - return ret; -#endif /* NO_WOLFSSL_SERVER */ -#endif -#ifdef HAVE_PK_CALLBACKS -#ifdef HAVE_ECC - ret = DupBuffer(&buf->peerEccDsaKey, &obuf->peerEccDsaKey, heap); - if (ret != 0) - return ret; -#endif /* HAVE_ECC */ -#ifndef NO_RSA - ret = DupBuffer(&buf->peerRsaKey, &obuf->peerRsaKey, heap); - if (ret != 0) - return ret; -#endif /* NO_RSA */ -#endif /* HAVE_PK_CALLBACKS */ - - return 0; -} - - -/* init everything to 0, NULL, default values before calling anything that may - fail so that destructor has a "good" state to cleanup - 0 on success */ -int DupSSL(WOLFSSL* ssl, WOLFSSL* ossl) -{ - int ret; - - if (ssl == NULL || ossl == NULL) { - WOLFSSL_MSG("need valid ssl objects"); - return BAD_FUNC_ARG; - } - - ret = DupBuffers(&ssl->buffers, &ossl->buffers, ssl->heap); - if (ret != 0) - return ret; - -#ifndef NO_DH - /* Don't copy if (p,g) owned by ctx */ - if (ssl->buffers.weOwnDH || ssl->options.side == WOLFSSL_CLIENT_END) { - ret = DupBuffer(&ssl->buffers.serverDH_P, - &ossl->buffers.serverDH_P, ssl->heap); - if (ret != 0) - return ret; - - ret = DupBuffer(&ssl->buffers.serverDH_G, - &ossl->buffers.serverDH_G, ssl->heap); - if (ret != 0) - return ret; - } -#endif - -#ifdef KEEP_PEER_CERT - ret = DupX509(&ssl->peerCert, &ossl->peerCert); - if (ret != 0) - return ret; -#endif - - ssl->rfd = ossl->rfd; - ssl->wfd = ossl->wfd; - - ssl->IOCB_ReadCtx = &ssl->rfd; - ssl->IOCB_WriteCtx = &ssl->wfd; - -#ifdef HAVE_NETX - ssl->IOCB_ReadCtx = &ssl->nxCtx; - ssl->IOCB_WriteCtx = &ssl->nxCtx; -#endif - -#ifdef WOLFSSL_DTLS - ssl->dtls_expected_rx = ossl->dtls_expected_rx; -#endif - - /* options */ - XMEMCPY(&ssl->options, &ossl->options, sizeof(Options)); - -#ifdef WOLFSSL_DTLS - ssl->dtls_timeout_init = ossl->dtls_timeout_init; - ssl->dtls_timeout_max = ossl->dtls_timeout_max; - ssl->dtls_timeout = ossl->dtls_timeout; -#endif - - ssl->hmac = ossl->hmac; - -#ifdef WOLFSSL_DTLS - ssl->buffers.dtlsCtx.fd = ossl->buffers.dtlsCtx.fd; -#endif - - ssl->cipher.ssl = ssl; - -#ifdef HAVE_TLS_EXTENSIONS -#ifdef HAVE_MAX_FRAGMENT - ssl->max_fragment = ossl->max_fragment; -#endif -#ifdef HAVE_ALPN - ssl->alpn_client_list = ossl->alpn_client_list; -#endif -#endif - - /* default alert state (none) */ - ssl->alert_history.last_rx.code = ossl->alert_history.last_rx.code; - ssl->alert_history.last_rx.level = ossl->alert_history.last_rx.level; - ssl->alert_history.last_tx.code = ossl->alert_history.last_tx.code; - ssl->alert_history.last_tx.level = ossl->alert_history.last_tx.level; - - DupCiphers(ssl, ossl); - DupCipherSpecs(&ssl->specs, &ossl->specs); - - /* arrays */ - ret = DupArrays(ssl->arrays, ossl->arrays, ssl->heap); - if (ret != 0) - return ret; - - /* suites */ - XMEMCPY(ssl->suites, ossl->suites, sizeof(Suites)); - - /* versions */ - XMEMCPY(&ssl->version, &ossl->version, sizeof(ProtocolVersion)); - XMEMCPY(&ssl->chVersion, &ossl->chVersion, sizeof(ProtocolVersion)); - - /* hsHashes */ - XMEMCPY(&ssl->hsHashes->verifyHashes, &ossl->hsHashes->verifyHashes, - sizeof(Hashes)); - XMEMCPY(&ssl->hsHashes->certHashes, &ossl->hsHashes->certHashes, - sizeof(Hashes)); -#ifndef NO_OLD_TLS -#ifndef NO_MD5 - XMEMCPY(&ssl->hsHashes->hashMd5, &ossl->hsHashes->hashMd5, sizeof(Md5)); -#endif -#ifndef NO_SHA - XMEMCPY(&ssl->hsHashes->hashSha, &ossl->hsHashes->hashSha, sizeof(Sha)); -#endif -#endif -#ifndef NO_SHA256 - XMEMCPY(&ssl->hsHashes->hashSha256, &ossl->hsHashes->hashSha256, sizeof(Sha256)); -#endif -#ifdef WOLFSSL_SHA384 - XMEMCPY(&ssl->hsHashes->hashSha384, &ossl->hsHashes->hashSha384, sizeof(Sha384)); -#endif -#ifdef WOLFSSL_SHA512 - XMEMCPY(&ssl->hsHashes->hashSha512, &ossl->hsHashes->hashSha512, sizeof(Sha512)); -#endif - -#if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER) - if (ssl->options.dtls && ssl->options.side == WOLFSSL_SERVER_END) { - ssl->buffers.dtlsCookieSecret.length = ossl->buffers.dtlsCookieSecret.length; - XMEMCPY(ssl->buffers.dtlsCookieSecret.buffer, - ossl->buffers.dtlsCookieSecret.buffer, - ossl->buffers.dtlsCookieSecret.length); - } -#endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */ - -#ifdef HAVE_SECRET_CALLBACK - ssl->sessionSecretCb = ossl->sessionSecretCb; - ssl->sessionSecretCtx = ossl->sessionSecretCtx; -#endif - - return 0; -} -#endif /* OPENSSL_EXTRA */ - /* free use of temporary arrays */ void FreeArrays(WOLFSSL* ssl, int keep) { @@ -4122,8 +3589,8 @@ void SSL_ResourceFree(WOLFSSL* ssl) #endif /* WOLFSSL_DTLS */ #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS) if (ssl->biord != ssl->biowr) /* only free write if different */ - wc_BioFree(ssl->biowr); - wc_BioFree(ssl->biord); /* always free read bio */ + wolfSSL_BIO_free(ssl->biowr); + wolfSSL_BIO_free(ssl->biord); /* always free read bio */ #endif #ifdef HAVE_LIBZ FreeStreams(ssl); diff --git a/src/ssl.c b/src/ssl.c index 96503fb4c..7c1ea1935 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -87,8 +87,6 @@ #ifdef WOLFSSL_SHA512 #include #endif - #include - #include #endif #ifdef NO_ASN @@ -387,70 +385,6 @@ void wolfSSL_free(WOLFSSL* ssl) WOLFSSL_LEAVE("SSL_free", 0); } -#ifdef OPENSSL_EXTRA -WOLFSSL* wolfSSL_dup(WOLFSSL* s) -{ - WOLFSSL *ret; - - if (s == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return NULL; - } - - ret = wolfSSL_new(s->ctx); - if (ret == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return NULL; - } - - if (DupSSL(ret, s) != 0) { - WOLFSSL_MSG("DupSSL failed\n"); - wolfSSL_free(ret); - return NULL; - } - - /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */ - if (DupSession(ret, s) != SSL_SUCCESS) { - WOLFSSL_MSG("DupSession failed\n"); - wolfSSL_free(ret); - return NULL; - } - - XMEMCPY(ret->keys.server_write_IV, s->keys.server_write_IV, - CHACHA20_IMP_IV_SZ); - XMEMCPY(ret->keys.client_write_IV, s->keys.client_write_IV, - CHACHA20_IMP_IV_SZ); -#ifdef HAVE_AEAD - XMEMCPY(ret->keys.aead_enc_imp_IV, ret->keys.server_write_IV, - CHACHA20_IMP_IV_SZ); - XMEMCPY(ret->keys.aead_dec_imp_IV, ret->keys.client_write_IV, - CHACHA20_IMP_IV_SZ); -#endif - - /* setup biord, and biowr */ - if (s->biord != NULL) { - if (!wc_BioDupState(s->biord, &ret->biord)) { - wolfSSL_free(ret); - return NULL; - } - } - - if (s->biowr != NULL) { - if (s->biowr != s->biord) { - if (!wc_BioDupState(s->biowr, &ret->biowr)) { - wolfSSL_free(ret); - return NULL; - } - } - else - ret->biowr = ret->biord; - } - - return ret; -} -#endif /* OPENSSL_EXTRA */ - - #ifdef HAVE_POLY1305 /* set if to use old poly 1 for yes 0 to use new poly */ int wolfSSL_use_old_poly(WOLFSSL* ssl, int value) @@ -2397,6 +2331,29 @@ int wolfSSL_CertPemToDer(const unsigned char* pem, int pemSz, #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) +static const char *EVP_AES_128_CBC = "AES-128-CBC"; +static const char *EVP_AES_192_CBC = "AES-192-CBC"; +static const char *EVP_AES_256_CBC = "AES-256-CBC"; +#if defined(OPENSSL_EXTRA) + static const char *EVP_AES_128_CTR = "AES-128-CTR"; + static const char *EVP_AES_192_CTR = "AES-192-CTR"; + static const char *EVP_AES_256_CTR = "AES-256-CTR"; +#endif +static const int EVP_AES_SIZE = 11; + +#ifndef NO_DES3 +static const char *EVP_DES_CBC = "DES-CBC"; +static const int EVP_DES_SIZE = 7; + +static const char *EVP_DES_EDE3_CBC = "DES-EDE3-CBC"; +static const int EVP_DES_EDE3_SIZE = 12; +#endif + +#ifdef HAVE_IDEA +static const char *EVP_IDEA_CBC = "IDEA-CBC"; +static const int EVP_IDEA_SIZE = 8; +#endif + /* our KeyPemToDer password callback, password in userData */ static INLINE int OurPasswordCb(char* passwd, int sz, int rw, void* userdata) { @@ -3320,8 +3277,8 @@ static int wolfssl_decrypt_buffer_key(DerBuffer* der, byte* password, } #endif /* WOLFSSL_SMALL_STACK */ - if ((ret = wc_EVP_BytesToKey(info->name, "MD5", info->iv, - password, passwordSz, 1, key, NULL)) <= 0) { + if ((ret = wolfSSL_EVP_BytesToKey(info->name, "MD5", info->iv, + password, passwordSz, 1, key, NULL)) <= 0) { WOLFSSL_MSG("bytes to key failure"); #ifdef WOLFSSL_SMALL_STACK XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); @@ -3395,8 +3352,8 @@ static int wolfssl_encrypt_buffer_key(byte* der, word32 derSz, byte* password, } #endif /* WOLFSSL_SMALL_STACK */ - if ((ret = wc_EVP_BytesToKey(info->name, "MD5", info->iv, - password, passwordSz, 1, key, NULL)) <= 0) { + if ((ret = wolfSSL_EVP_BytesToKey(info->name, "MD5", info->iv, + password, passwordSz, 1, key, NULL)) <= 0) { WOLFSSL_MSG("bytes to key failure"); #ifdef WOLFSSL_SMALL_STACK XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); @@ -6660,7 +6617,9 @@ int wolfSSL_dtls_got_timeout(WOLFSSL* ssl) { int result = SSL_SUCCESS; - if (DtlsPoolTimeout(ssl) < 0 || DtlsPoolSend(ssl) < 0) { + if (!ssl->options.handShakeDone && + (DtlsPoolTimeout(ssl) < 0 || DtlsPoolSend(ssl) < 0)) { + result = SSL_FATAL_ERROR; } return result; @@ -7756,78 +7715,6 @@ int SetSession(WOLFSSL* ssl, WOLFSSL_SESSION* session) return SSL_FAILURE; /* session timed out */ } -int DupSession(WOLFSSL* ssl, WOLFSSL* ossl) -{ - if (ssl == NULL || ossl == NULL) - return SSL_FAILURE; - - ssl->session.bornOn = ossl->session.bornOn; - ssl->session.timeout = ossl->session.timeout; - ssl->session.sessionIDSz = ossl->session.sessionIDSz; - XMEMCPY(ssl->session.sessionID, ossl->session.sessionID, ossl->session.sessionIDSz); - XMEMCPY(ssl->session.masterSecret, ossl->session.masterSecret, - sizeof(ossl->session.masterSecret)); - -#ifdef SESSION_CERTS - ssl->session.chain.count = ossl->session.chain.count; - XMEMCPY(ssl->session.chain.certs, ossl->session.chain.certs, - sizeof(x509_buffer)); - - ssl->session.version.major = ossl->session.version.major; - ssl->session.version.minor = ossl->session.version.minor; - - ssl->session.cipherSuite0 = ossl->session.cipherSuite0; - ssl->session.cipherSuite = ossl->session.cipherSuite; -#endif - -#ifndef NO_CLIENT_CACHE - ssl->session.idLen = ossl->session.idLen; - XMEMCPY(ssl->session.serverID, ossl->session.serverID, - sizeof(ossl->session.serverID)); -#endif -#ifdef HAVE_SESSION_TICKET - ssl->session.isDynamic = ossl->session.isDynamic; - ssl->session.ticketLen = ossl->session.ticketLen; - - if (ossl->session.isDynamic) { - if (ssl->session.ticket != NULL) - XFREE(ssl->session.ticket, ssl->heap, DYNAMIC_TYPE_SESSION_TICK); - ssl->session.ticket = (byte*)XMALLOC(ossl->session.ticketLen, ssl->heap, - DYNAMIC_TYPE_SESSION_TICK); - if (ssl->session.ticket == NULL) - return SSL_FAILURE; - - XMEMCPY(ssl->session.ticket, ossl->session.ticket, - ossl->session.ticketLen); - } - else { - if (ossl->session.ticketLen > SESSION_TICKET_LEN) { - /* size of static ticket is not large enough, should be dynamic!!! - * do not try copy */ - return SSL_FAILURE; - } - XMEMCPY(ssl->session.staticTicket, ossl->session.staticTicket, - sizeof(ossl->session.staticTicket)); - ssl->session.ticket = ssl->session.staticTicket; - } -#endif -#ifdef HAVE_STUNNEL - { - void *data; - int idx; - - for (idx = 0; ; idx++) { - data = wolfSSL_SESSION_get_ex_data(&ossl->session, idx); - if (data == NULL) - break; - wolfSSL_SESSION_set_ex_data(&ssl->session, idx, data); - } - } -#endif - - return SSL_SUCCESS; -} - #ifdef WOLFSSL_SESSION_STATS static int get_locked_session_stats(word32* active, word32* total, @@ -8902,6 +8789,17 @@ int wolfSSL_set_compression(WOLFSSL* ssl) } + void wolfSSL_set_bio(WOLFSSL* ssl, WOLFSSL_BIO* rd, WOLFSSL_BIO* wr) + { + WOLFSSL_ENTER("SSL_set_bio"); + wolfSSL_set_rfd(ssl, rd->fd); + wolfSSL_set_wfd(ssl, wr->fd); + + ssl->biord = rd; + ssl->biowr = wr; + } + + void wolfSSL_CTX_set_client_CA_list(WOLFSSL_CTX* ctx, STACK_OF(WOLFSSL_X509_NAME)* names) { @@ -9085,6 +8983,250 @@ int wolfSSL_set_compression(WOLFSSL* ssl) return SSL_FATAL_ERROR; } + + WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_buffer(void) + { + static WOLFSSL_BIO_METHOD meth; + + WOLFSSL_ENTER("BIO_f_buffer"); + meth.type = BIO_BUFFER; + + return &meth; + } + + + long wolfSSL_BIO_set_write_buffer_size(WOLFSSL_BIO* bio, long size) + { + /* wolfSSL has internal buffer, compatibility only */ + WOLFSSL_ENTER("BIO_set_write_buffer_size"); + (void)bio; + return size; + } + + + WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_ssl(void) + { + static WOLFSSL_BIO_METHOD meth; + + WOLFSSL_ENTER("BIO_f_ssl"); + meth.type = BIO_SSL; + + return &meth; + } + + + WOLFSSL_BIO* wolfSSL_BIO_new_socket(int sfd, int closeF) + { + WOLFSSL_BIO* bio = (WOLFSSL_BIO*) XMALLOC(sizeof(WOLFSSL_BIO), 0, + DYNAMIC_TYPE_OPENSSL); + + WOLFSSL_ENTER("BIO_new_socket"); + if (bio) { + bio->type = BIO_SOCKET; + bio->close = (byte)closeF; + bio->eof = 0; + bio->ssl = 0; + bio->fd = sfd; + bio->prev = 0; + bio->next = 0; + bio->mem = NULL; + bio->memLen = 0; + } + return bio; + } + + + int wolfSSL_BIO_eof(WOLFSSL_BIO* b) + { + WOLFSSL_ENTER("BIO_eof"); + if (b->eof) + return 1; + + return 0; + } + + + long wolfSSL_BIO_set_ssl(WOLFSSL_BIO* b, WOLFSSL* ssl, int closeF) + { + WOLFSSL_ENTER("BIO_set_ssl"); + b->ssl = ssl; + b->close = (byte)closeF; + /* add to ssl for bio free if SSL_free called before/instead of free_all? */ + + return 0; + } + + + WOLFSSL_BIO* wolfSSL_BIO_new(WOLFSSL_BIO_METHOD* method) + { + WOLFSSL_BIO* bio = (WOLFSSL_BIO*) XMALLOC(sizeof(WOLFSSL_BIO), 0, + DYNAMIC_TYPE_OPENSSL); + WOLFSSL_ENTER("BIO_new"); + if (bio) { + bio->type = method->type; + bio->close = 0; + bio->eof = 0; + bio->ssl = NULL; + bio->mem = NULL; + bio->memLen = 0; + bio->fd = 0; + bio->prev = NULL; + bio->next = NULL; + } + return bio; + } + + + int wolfSSL_BIO_get_mem_data(WOLFSSL_BIO* bio, const byte** p) + { + if (bio == NULL || p == NULL) + return SSL_FATAL_ERROR; + + *p = bio->mem; + + return bio->memLen; + } + + + WOLFSSL_BIO* wolfSSL_BIO_new_mem_buf(void* buf, int len) + { + WOLFSSL_BIO* bio = NULL; + if (buf == NULL) + return bio; + + bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()); + if (bio == NULL) + return bio; + + bio->memLen = len; + bio->mem = (byte*)XMALLOC(len, 0, DYNAMIC_TYPE_OPENSSL); + if (bio->mem == NULL) { + XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL); + return NULL; + } + + XMEMCPY(bio->mem, buf, len); + + return bio; + } + + +#ifdef USE_WINDOWS_API + #define CloseSocket(s) closesocket(s) +#elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) + #define CloseSocket(s) closesocket(s) + extern int closesocket(int) ; +#else + #define CloseSocket(s) close(s) +#endif + + int wolfSSL_BIO_free(WOLFSSL_BIO* bio) + { + /* unchain?, doesn't matter in goahead since from free all */ + WOLFSSL_ENTER("BIO_free"); + if (bio) { + if (bio->close) { + if (bio->ssl) + wolfSSL_free(bio->ssl); + if (bio->fd) + CloseSocket(bio->fd); + } + if (bio->mem) + XFREE(bio->mem, 0, DYNAMIC_TYPE_OPENSSL); + XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL); + } + return 0; + } + + + int wolfSSL_BIO_free_all(WOLFSSL_BIO* bio) + { + WOLFSSL_ENTER("BIO_free_all"); + while (bio) { + WOLFSSL_BIO* next = bio->next; + wolfSSL_BIO_free(bio); + bio = next; + } + return 0; + } + + + int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len) + { + int ret; + WOLFSSL* ssl = 0; + WOLFSSL_BIO* front = bio; + + WOLFSSL_ENTER("BIO_read"); + /* already got eof, again is error */ + if (front->eof) + return SSL_FATAL_ERROR; + + while(bio && ((ssl = bio->ssl) == 0) ) + bio = bio->next; + + if (ssl == 0) return BAD_FUNC_ARG; + + ret = wolfSSL_read(ssl, buf, len); + if (ret == 0) + front->eof = 1; + else if (ret < 0) { + int err = wolfSSL_get_error(ssl, 0); + if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) ) + front->eof = 1; + } + return ret; + } + + + int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len) + { + int ret; + WOLFSSL* ssl = 0; + WOLFSSL_BIO* front = bio; + + WOLFSSL_ENTER("BIO_write"); + /* already got eof, again is error */ + if (front->eof) + return SSL_FATAL_ERROR; + + while(bio && ((ssl = bio->ssl) == 0) ) + bio = bio->next; + + if (ssl == 0) return BAD_FUNC_ARG; + + ret = wolfSSL_write(ssl, data, len); + if (ret == 0) + front->eof = 1; + else if (ret < 0) { + int err = wolfSSL_get_error(ssl, 0); + if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) ) + front->eof = 1; + } + + return ret; + } + + + WOLFSSL_BIO* wolfSSL_BIO_push(WOLFSSL_BIO* top, WOLFSSL_BIO* append) + { + WOLFSSL_ENTER("BIO_push"); + top->next = append; + append->prev = top; + + return top; + } + + + int wolfSSL_BIO_flush(WOLFSSL_BIO* bio) + { + /* for wolfSSL no flushing needed */ + WOLFSSL_ENTER("BIO_flush"); + (void)bio; + return 1; + } + + #endif /* OPENSSL_EXTRA || GOAHEAD_WS */ @@ -9125,6 +9267,116 @@ int wolfSSL_set_compression(WOLFSSL* ssl) return 0; } +#ifndef NO_MD5 + + int wolfSSL_EVP_BytesToKey(const WOLFSSL_EVP_CIPHER* type, + const WOLFSSL_EVP_MD* md, const byte* salt, + const byte* data, int sz, int count, byte* key, byte* iv) + { + int keyLen = 0; + int ivLen = 0; + int j; + int keyLeft; + int ivLeft; + int keyOutput = 0; + byte digest[MD5_DIGEST_SIZE]; + #ifdef WOLFSSL_SMALL_STACK + Md5* md5 = NULL; + #else + Md5 md5[1]; + #endif + + #ifdef WOLFSSL_SMALL_STACK + md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (md5 == NULL) + return 0; + #endif + + WOLFSSL_ENTER("wolfSSL_EVP_BytesToKey"); + wc_InitMd5(md5); + + /* only support MD5 for now */ + if (XSTRNCMP(md, "MD5", 3) != 0) return 0; + + /* only support CBC DES and AES for now */ + #ifndef NO_DES3 + if (XSTRNCMP(type, EVP_DES_CBC, EVP_DES_SIZE) == 0) { + keyLen = DES_KEY_SIZE; + ivLen = DES_IV_SIZE; + } + else if (XSTRNCMP(type, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0) { + keyLen = DES3_KEY_SIZE; + ivLen = DES_IV_SIZE; + } + else + #endif /* NO_DES3 */ + if (XSTRNCMP(type, EVP_AES_128_CBC, EVP_AES_SIZE) == 0) { + keyLen = AES_128_KEY_SIZE; + ivLen = AES_IV_SIZE; + } + else if (XSTRNCMP(type, EVP_AES_192_CBC, EVP_AES_SIZE) == 0) { + keyLen = AES_192_KEY_SIZE; + ivLen = AES_IV_SIZE; + } + else if (XSTRNCMP(type, EVP_AES_256_CBC, EVP_AES_SIZE) == 0) { + keyLen = AES_256_KEY_SIZE; + ivLen = AES_IV_SIZE; + } + else { + #ifdef WOLFSSL_SMALL_STACK + XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER); + #endif + return 0; + } + + keyLeft = keyLen; + ivLeft = ivLen; + + while (keyOutput < (keyLen + ivLen)) { + int digestLeft = MD5_DIGEST_SIZE; + /* D_(i - 1) */ + if (keyOutput) /* first time D_0 is empty */ + wc_Md5Update(md5, digest, MD5_DIGEST_SIZE); + /* data */ + wc_Md5Update(md5, data, sz); + /* salt */ + if (salt) + wc_Md5Update(md5, salt, EVP_SALT_SIZE); + wc_Md5Final(md5, digest); + /* count */ + for (j = 1; j < count; j++) { + wc_Md5Update(md5, digest, MD5_DIGEST_SIZE); + wc_Md5Final(md5, digest); + } + + if (keyLeft) { + int store = min(keyLeft, MD5_DIGEST_SIZE); + XMEMCPY(&key[keyLen - keyLeft], digest, store); + + keyOutput += store; + keyLeft -= store; + digestLeft -= store; + } + + if (ivLeft && digestLeft) { + int store = min(ivLeft, digestLeft); + if (iv != NULL) + XMEMCPY(&iv[ivLen - ivLeft], + &digest[MD5_DIGEST_SIZE - digestLeft], store); + keyOutput += store; + ivLeft -= store; + } + } + + #ifdef WOLFSSL_SMALL_STACK + XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER); + #endif + + return keyOutput == (keyLen + ivLen) ? keyOutput : 0; + } + +#endif /* NO_MD5 */ + #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */ @@ -9143,8 +9395,688 @@ int wolfSSL_set_compression(WOLFSSL* ssl) return version; } + +#ifndef NO_MD5 + void wolfSSL_MD5_Init(WOLFSSL_MD5_CTX* md5) + { + typedef char md5_test[sizeof(MD5_CTX) >= sizeof(Md5) ? 1 : -1]; + (void)sizeof(md5_test); + + WOLFSSL_ENTER("MD5_Init"); + wc_InitMd5((Md5*)md5); + } + + + void wolfSSL_MD5_Update(WOLFSSL_MD5_CTX* md5, const void* input, + unsigned long sz) + { + WOLFSSL_ENTER("wolfSSL_MD5_Update"); + wc_Md5Update((Md5*)md5, (const byte*)input, (word32)sz); + } + + + void wolfSSL_MD5_Final(byte* input, WOLFSSL_MD5_CTX* md5) + { + WOLFSSL_ENTER("MD5_Final"); + wc_Md5Final((Md5*)md5, input); + } +#endif /* NO_MD5 */ + + +#ifndef NO_SHA + void wolfSSL_SHA_Init(WOLFSSL_SHA_CTX* sha) + { + typedef char sha_test[sizeof(SHA_CTX) >= sizeof(Sha) ? 1 : -1]; + (void)sizeof(sha_test); + + WOLFSSL_ENTER("SHA_Init"); + wc_InitSha((Sha*)sha); /* OpenSSL compat, no ret */ + } + + + void wolfSSL_SHA_Update(WOLFSSL_SHA_CTX* sha, const void* input, + unsigned long sz) + { + WOLFSSL_ENTER("SHA_Update"); + wc_ShaUpdate((Sha*)sha, (const byte*)input, (word32)sz); + } + + + void wolfSSL_SHA_Final(byte* input, WOLFSSL_SHA_CTX* sha) + { + WOLFSSL_ENTER("SHA_Final"); + wc_ShaFinal((Sha*)sha, input); + } + + + void wolfSSL_SHA1_Init(WOLFSSL_SHA_CTX* sha) + { + WOLFSSL_ENTER("SHA1_Init"); + SHA_Init(sha); + } + + + void wolfSSL_SHA1_Update(WOLFSSL_SHA_CTX* sha, const void* input, + unsigned long sz) + { + WOLFSSL_ENTER("SHA1_Update"); + SHA_Update(sha, input, sz); + } + + + void wolfSSL_SHA1_Final(byte* input, WOLFSSL_SHA_CTX* sha) + { + WOLFSSL_ENTER("SHA1_Final"); + SHA_Final(input, sha); + } +#endif /* NO_SHA */ + + + void wolfSSL_SHA256_Init(WOLFSSL_SHA256_CTX* sha256) + { + typedef char sha_test[sizeof(SHA256_CTX) >= sizeof(Sha256) ? 1 : -1]; + (void)sizeof(sha_test); + + WOLFSSL_ENTER("SHA256_Init"); + wc_InitSha256((Sha256*)sha256); /* OpenSSL compat, no error */ + } + + + void wolfSSL_SHA256_Update(WOLFSSL_SHA256_CTX* sha, const void* input, + unsigned long sz) + { + WOLFSSL_ENTER("SHA256_Update"); + wc_Sha256Update((Sha256*)sha, (const byte*)input, (word32)sz); + /* OpenSSL compat, no error */ + } + + + void wolfSSL_SHA256_Final(byte* input, WOLFSSL_SHA256_CTX* sha) + { + WOLFSSL_ENTER("SHA256_Final"); + wc_Sha256Final((Sha256*)sha, input); + /* OpenSSL compat, no error */ + } + + + #ifdef WOLFSSL_SHA384 + + void wolfSSL_SHA384_Init(WOLFSSL_SHA384_CTX* sha) + { + typedef char sha_test[sizeof(SHA384_CTX) >= sizeof(Sha384) ? 1 : -1]; + (void)sizeof(sha_test); + + WOLFSSL_ENTER("SHA384_Init"); + wc_InitSha384((Sha384*)sha); /* OpenSSL compat, no error */ + } + + + void wolfSSL_SHA384_Update(WOLFSSL_SHA384_CTX* sha, const void* input, + unsigned long sz) + { + WOLFSSL_ENTER("SHA384_Update"); + wc_Sha384Update((Sha384*)sha, (const byte*)input, (word32)sz); + /* OpenSSL compat, no error */ + } + + + void wolfSSL_SHA384_Final(byte* input, WOLFSSL_SHA384_CTX* sha) + { + WOLFSSL_ENTER("SHA384_Final"); + wc_Sha384Final((Sha384*)sha, input); + /* OpenSSL compat, no error */ + } + + #endif /* WOLFSSL_SHA384 */ + + + #ifdef WOLFSSL_SHA512 + + void wolfSSL_SHA512_Init(WOLFSSL_SHA512_CTX* sha) + { + typedef char sha_test[sizeof(SHA512_CTX) >= sizeof(Sha512) ? 1 : -1]; + (void)sizeof(sha_test); + + WOLFSSL_ENTER("SHA512_Init"); + wc_InitSha512((Sha512*)sha); /* OpenSSL compat, no error */ + } + + + void wolfSSL_SHA512_Update(WOLFSSL_SHA512_CTX* sha, const void* input, + unsigned long sz) + { + WOLFSSL_ENTER("SHA512_Update"); + wc_Sha512Update((Sha512*)sha, (const byte*)input, (word32)sz); + /* OpenSSL compat, no error */ + } + + + void wolfSSL_SHA512_Final(byte* input, WOLFSSL_SHA512_CTX* sha) + { + WOLFSSL_ENTER("SHA512_Final"); + wc_Sha512Final((Sha512*)sha, input); + /* OpenSSL compat, no error */ + } + + #endif /* WOLFSSL_SHA512 */ + + + #ifndef NO_MD5 + + const WOLFSSL_EVP_MD* wolfSSL_EVP_md5(void) + { + static const char* type = "MD5"; + WOLFSSL_ENTER("EVP_md5"); + return type; + } + + #endif /* NO_MD5 */ + + +#ifndef NO_SHA + const WOLFSSL_EVP_MD* wolfSSL_EVP_sha1(void) + { + static const char* type = "SHA"; + WOLFSSL_ENTER("EVP_sha1"); + return type; + } +#endif /* NO_SHA */ + + + const WOLFSSL_EVP_MD* wolfSSL_EVP_sha256(void) + { + static const char* type = "SHA256"; + WOLFSSL_ENTER("EVP_sha256"); + return type; + } + + #ifdef WOLFSSL_SHA384 + + const WOLFSSL_EVP_MD* wolfSSL_EVP_sha384(void) + { + static const char* type = "SHA384"; + WOLFSSL_ENTER("EVP_sha384"); + return type; + } + + #endif /* WOLFSSL_SHA384 */ + + #ifdef WOLFSSL_SHA512 + + const WOLFSSL_EVP_MD* wolfSSL_EVP_sha512(void) + { + static const char* type = "SHA512"; + WOLFSSL_ENTER("EVP_sha512"); + return type; + } + + #endif /* WOLFSSL_SHA512 */ + + + void wolfSSL_EVP_MD_CTX_init(WOLFSSL_EVP_MD_CTX* ctx) + { + WOLFSSL_ENTER("EVP_CIPHER_MD_CTX_init"); + (void)ctx; + /* do nothing */ + } + + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_cbc(void) + { + WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cbc"); + return EVP_AES_128_CBC; + } + + + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_cbc(void) + { + WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cbc"); + return EVP_AES_192_CBC; + } + + + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_cbc(void) + { + WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cbc"); + return EVP_AES_256_CBC; + } + + + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ctr(void) + { + WOLFSSL_ENTER("wolfSSL_EVP_aes_128_ctr"); + return EVP_AES_128_CTR; + } + + + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_ctr(void) + { + WOLFSSL_ENTER("wolfSSL_EVP_aes_192_ctr"); + return EVP_AES_192_CTR; + } + + + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_ctr(void) + { + WOLFSSL_ENTER("wolfSSL_EVP_aes_256_ctr"); + return EVP_AES_256_CTR; + } + + +#ifndef NO_DES3 + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_cbc(void) + { + WOLFSSL_ENTER("wolfSSL_EVP_des_cbc"); + return EVP_DES_CBC; + } + + + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ede3_cbc(void) + { + WOLFSSL_ENTER("wolfSSL_EVP_des_ede3_cbc"); + return EVP_DES_EDE3_CBC; + } +#endif /* NO_DES3 */ + + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_rc4(void) + { + static const char* type = "ARC4"; + WOLFSSL_ENTER("wolfSSL_EVP_rc4"); + return type; + } + +#ifdef HAVE_IDEA + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_idea_cbc(void) + { + WOLFSSL_ENTER("wolfSSL_EVP_idea_cbc"); + return EVP_IDEA_CBC; + } +#endif + const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_enc_null(void) + { + static const char* type = "NULL"; + WOLFSSL_ENTER("wolfSSL_EVP_enc_null"); + return type; + } + + + int wolfSSL_EVP_MD_CTX_cleanup(WOLFSSL_EVP_MD_CTX* ctx) + { + WOLFSSL_ENTER("EVP_MD_CTX_cleanup"); + (void)ctx; + return 0; + } + + + + void wolfSSL_EVP_CIPHER_CTX_init(WOLFSSL_EVP_CIPHER_CTX* ctx) + { + WOLFSSL_ENTER("EVP_CIPHER_CTX_init"); + if (ctx) { + ctx->cipherType = 0xff; /* no init */ + ctx->keyLen = 0; + ctx->enc = 1; /* start in encrypt mode */ + } + } + + + /* SSL_SUCCESS on ok */ + int wolfSSL_EVP_CIPHER_CTX_cleanup(WOLFSSL_EVP_CIPHER_CTX* ctx) + { + WOLFSSL_ENTER("EVP_CIPHER_CTX_cleanup"); + if (ctx) { + ctx->cipherType = 0xff; /* no more init */ + ctx->keyLen = 0; + } + + return SSL_SUCCESS; + } + + + /* return SSL_SUCCESS on ok, 0 on failure to match API compatibility */ + int wolfSSL_EVP_CipherInit(WOLFSSL_EVP_CIPHER_CTX* ctx, + const WOLFSSL_EVP_CIPHER* type, byte* key, + byte* iv, int enc) + { + int ret = -1; /* failure local, during function 0 means success + because internal functions work that way */ + (void)iv; + (void)enc; + + WOLFSSL_ENTER("wolfSSL_EVP_CipherInit"); + if (ctx == NULL) { + WOLFSSL_MSG("no ctx"); + return 0; /* failure */ + } + + if (type == NULL && ctx->cipherType == 0xff) { + WOLFSSL_MSG("no type set"); + return 0; /* failure */ + } + +#ifndef NO_AES + if (ctx->cipherType == AES_128_CBC_TYPE || + (type && XSTRNCMP(type, EVP_AES_128_CBC, EVP_AES_SIZE) == 0)) { + WOLFSSL_MSG(EVP_AES_128_CBC); + ctx->cipherType = AES_128_CBC_TYPE; + ctx->keyLen = 16; + if (enc == 0 || enc == 1) + ctx->enc = enc ? 1 : 0; + if (key) { + ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv, + ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION); + if (ret != 0) + return ret; + } + if (iv && key == NULL) { + ret = wc_AesSetIV(&ctx->cipher.aes, iv); + if (ret != 0) + return ret; + } + } + else if (ctx->cipherType == AES_192_CBC_TYPE || + (type && XSTRNCMP(type, EVP_AES_192_CBC, EVP_AES_SIZE) == 0)) { + WOLFSSL_MSG(EVP_AES_192_CBC); + ctx->cipherType = AES_192_CBC_TYPE; + ctx->keyLen = 24; + if (enc == 0 || enc == 1) + ctx->enc = enc ? 1 : 0; + if (key) { + ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv, + ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION); + if (ret != 0) + return ret; + } + if (iv && key == NULL) { + ret = wc_AesSetIV(&ctx->cipher.aes, iv); + if (ret != 0) + return ret; + } + } + else if (ctx->cipherType == AES_256_CBC_TYPE || + (type && XSTRNCMP(type, EVP_AES_256_CBC, EVP_AES_SIZE) == 0)) { + WOLFSSL_MSG(EVP_AES_256_CBC); + ctx->cipherType = AES_256_CBC_TYPE; + ctx->keyLen = 32; + if (enc == 0 || enc == 1) + ctx->enc = enc ? 1 : 0; + if (key) { + ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv, + ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION); + if (ret != 0) + return ret; + } + if (iv && key == NULL) { + ret = wc_AesSetIV(&ctx->cipher.aes, iv); + if (ret != 0) + return ret; + } + } +#ifdef WOLFSSL_AES_COUNTER + else if (ctx->cipherType == AES_128_CTR_TYPE || + (type && XSTRNCMP(type, EVP_AES_128_CTR, EVP_AES_SIZE) == 0)) { + WOLFSSL_MSG(EVP_AES_128_CTR); + ctx->cipherType = AES_128_CTR_TYPE; + ctx->keyLen = 16; + if (enc == 0 || enc == 1) + ctx->enc = enc ? 1 : 0; + if (key) { + ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv, + AES_ENCRYPTION); + if (ret != 0) + return ret; + } + if (iv && key == NULL) { + ret = wc_AesSetIV(&ctx->cipher.aes, iv); + if (ret != 0) + return ret; + } + } + else if (ctx->cipherType == AES_192_CTR_TYPE || + (type && XSTRNCMP(type, EVP_AES_192_CTR, EVP_AES_SIZE) == 0)) { + WOLFSSL_MSG(EVP_AES_192_CTR); + ctx->cipherType = AES_192_CTR_TYPE; + ctx->keyLen = 24; + if (enc == 0 || enc == 1) + ctx->enc = enc ? 1 : 0; + if (key) { + ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv, + AES_ENCRYPTION); + if (ret != 0) + return ret; + } + if (iv && key == NULL) { + ret = wc_AesSetIV(&ctx->cipher.aes, iv); + if (ret != 0) + return ret; + } + } + else if (ctx->cipherType == AES_256_CTR_TYPE || + (type && XSTRNCMP(type, EVP_AES_256_CTR, EVP_AES_SIZE) == 0)) { + WOLFSSL_MSG(EVP_AES_256_CTR); + ctx->cipherType = AES_256_CTR_TYPE; + ctx->keyLen = 32; + if (enc == 0 || enc == 1) + ctx->enc = enc ? 1 : 0; + if (key) { + ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv, + AES_ENCRYPTION); + if (ret != 0) + return ret; + } + if (iv && key == NULL) { + ret = wc_AesSetIV(&ctx->cipher.aes, iv); + if (ret != 0) + return ret; + } + } +#endif /* WOLFSSL_AES_CTR */ +#endif /* NO_AES */ + +#ifndef NO_DES3 + if (ctx->cipherType == DES_CBC_TYPE || + (type && XSTRNCMP(type, EVP_DES_CBC, EVP_DES_SIZE) == 0)) { + WOLFSSL_MSG(EVP_DES_CBC); + ctx->cipherType = DES_CBC_TYPE; + ctx->keyLen = 8; + if (enc == 0 || enc == 1) + ctx->enc = enc ? 1 : 0; + if (key) { + ret = wc_Des_SetKey(&ctx->cipher.des, key, iv, + ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION); + if (ret != 0) + return ret; + } + + if (iv && key == NULL) + wc_Des_SetIV(&ctx->cipher.des, iv); + } + else if (ctx->cipherType == DES_EDE3_CBC_TYPE || + (type && + XSTRNCMP(type, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0)) { + WOLFSSL_MSG(EVP_DES_EDE3_CBC); + ctx->cipherType = DES_EDE3_CBC_TYPE; + ctx->keyLen = 24; + if (enc == 0 || enc == 1) + ctx->enc = enc ? 1 : 0; + if (key) { + ret = wc_Des3_SetKey(&ctx->cipher.des3, key, iv, + ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION); + if (ret != 0) + return ret; + } + + if (iv && key == NULL) { + ret = wc_Des3_SetIV(&ctx->cipher.des3, iv); + if (ret != 0) + return ret; + } + } +#endif /* NO_DES3 */ +#ifndef NO_RC4 + if (ctx->cipherType == ARC4_TYPE || (type && + XSTRNCMP(type, "ARC4", 4) == 0)) { + WOLFSSL_MSG("ARC4"); + ctx->cipherType = ARC4_TYPE; + if (ctx->keyLen == 0) /* user may have already set */ + ctx->keyLen = 16; /* default to 128 */ + if (key) + wc_Arc4SetKey(&ctx->cipher.arc4, key, ctx->keyLen); + ret = 0; /* success */ + } +#endif /* NO_RC4 */ +#ifdef HAVE_IDEA + if (ctx->cipherType == IDEA_CBC_TYPE || + (type && XSTRNCMP(type, EVP_IDEA_CBC, EVP_IDEA_SIZE) == 0)) { + WOLFSSL_MSG(EVP_IDEA_CBC); + ctx->cipherType = IDEA_CBC_TYPE; + ctx->keyLen = IDEA_KEY_SIZE; + if (enc == 0 || enc == 1) + ctx->enc = enc ? 1 : 0; + if (key) { + ret = wc_IdeaSetKey(&ctx->cipher.idea, key, (word16)ctx->keyLen, + iv, ctx->enc ? IDEA_ENCRYPTION : + IDEA_DECRYPTION); + if (ret != 0) + return ret; + } + + if (iv && key == NULL) + wc_IdeaSetIV(&ctx->cipher.idea, iv); + } +#endif /* HAVE_IDEA */ + if (ctx->cipherType == NULL_CIPHER_TYPE || (type && + XSTRNCMP(type, "NULL", 4) == 0)) { + WOLFSSL_MSG("NULL cipher"); + ctx->cipherType = NULL_CIPHER_TYPE; + ctx->keyLen = 0; + ret = 0; /* success */ + } + + if (ret == 0) + return SSL_SUCCESS; + else + return 0; /* overall failure */ + } + + + /* SSL_SUCCESS on ok */ + int wolfSSL_EVP_CIPHER_CTX_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx) + { + WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_key_length"); + if (ctx) + return ctx->keyLen; + + return 0; /* failure */ + } + + + /* SSL_SUCCESS on ok */ + int wolfSSL_EVP_CIPHER_CTX_set_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx, + int keylen) + { + WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_set_key_length"); + if (ctx) + ctx->keyLen = keylen; + else + return 0; /* failure */ + + return SSL_SUCCESS; + } + + + /* SSL_SUCCESS on ok */ + int wolfSSL_EVP_Cipher(WOLFSSL_EVP_CIPHER_CTX* ctx, byte* dst, byte* src, + word32 len) + { + int ret = 0; + WOLFSSL_ENTER("wolfSSL_EVP_Cipher"); + + if (ctx == NULL || dst == NULL || src == NULL) { + WOLFSSL_MSG("Bad function argument"); + return 0; /* failure */ + } + + if (ctx->cipherType == 0xff) { + WOLFSSL_MSG("no init"); + return 0; /* failure */ + } + + switch (ctx->cipherType) { + +#ifndef NO_AES +#ifdef HAVE_AES_CBC + case AES_128_CBC_TYPE : + case AES_192_CBC_TYPE : + case AES_256_CBC_TYPE : + WOLFSSL_MSG("AES CBC"); + if (ctx->enc) + ret = wc_AesCbcEncrypt(&ctx->cipher.aes, dst, src, len); + else + ret = wc_AesCbcDecrypt(&ctx->cipher.aes, dst, src, len); + break; +#endif /* HAVE_AES_CBC */ +#ifdef WOLFSSL_AES_COUNTER + case AES_128_CTR_TYPE : + case AES_192_CTR_TYPE : + case AES_256_CTR_TYPE : + WOLFSSL_MSG("AES CTR"); + wc_AesCtrEncrypt(&ctx->cipher.aes, dst, src, len); + break; +#endif /* WOLFSSL_AES_COUNTER */ +#endif /* NO_AES */ + +#ifndef NO_DES3 + case DES_CBC_TYPE : + if (ctx->enc) + wc_Des_CbcEncrypt(&ctx->cipher.des, dst, src, len); + else + wc_Des_CbcDecrypt(&ctx->cipher.des, dst, src, len); + break; + + case DES_EDE3_CBC_TYPE : + if (ctx->enc) + ret = wc_Des3_CbcEncrypt(&ctx->cipher.des3, dst, src, len); + else + ret = wc_Des3_CbcDecrypt(&ctx->cipher.des3, dst, src, len); + break; +#endif + +#ifndef NO_RC4 + case ARC4_TYPE : + wc_Arc4Process(&ctx->cipher.arc4, dst, src, len); + break; +#endif + +#ifdef HAVE_IDEA + case IDEA_CBC_TYPE : + if (ctx->enc) + wc_IdeaCbcEncrypt(&ctx->cipher.idea, dst, src, len); + else + wc_IdeaCbcDecrypt(&ctx->cipher.idea, dst, src, len); + break; +#endif + case NULL_CIPHER_TYPE : + XMEMCPY(dst, src, len); + break; + + default: { + WOLFSSL_MSG("bad type"); + return 0; /* failure */ + } + } + + if (ret != 0) { + WOLFSSL_MSG("wolfSSL_EVP_Cipher failure"); + return 0; /* failuer */ + } + + WOLFSSL_MSG("wolfSSL_EVP_Cipher success"); + return SSL_SUCCESS; /* success */ + } + + /* store for external read of iv, SSL_SUCCESS on success */ - int wolfSSL_StoreExternalIV(WOLFCRYPT_EVP_CIPHER_CTX* ctx) + int wolfSSL_StoreExternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx) { WOLFSSL_ENTER("wolfSSL_StoreExternalIV"); @@ -9210,7 +10142,7 @@ int wolfSSL_set_compression(WOLFSSL* ssl) /* set internal IV from external, SSL_SUCCESS on success */ - int wolfSSL_SetInternalIV(WOLFCRYPT_EVP_CIPHER_CTX* ctx) + int wolfSSL_SetInternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx) { WOLFSSL_ENTER("wolfSSL_SetInternalIV"); @@ -9275,6 +10207,191 @@ int wolfSSL_set_compression(WOLFSSL* ssl) return SSL_SUCCESS; } + + /* SSL_SUCCESS on ok */ + int wolfSSL_EVP_DigestInit(WOLFSSL_EVP_MD_CTX* ctx, + const WOLFSSL_EVP_MD* type) + { + WOLFSSL_ENTER("EVP_DigestInit"); + if (XSTRNCMP(type, "SHA256", 6) == 0) { + ctx->macType = SHA256; + wolfSSL_SHA256_Init((SHA256_CTX*)&ctx->hash); + } + #ifdef WOLFSSL_SHA384 + else if (XSTRNCMP(type, "SHA384", 6) == 0) { + ctx->macType = SHA384; + wolfSSL_SHA384_Init((SHA384_CTX*)&ctx->hash); + } + #endif + #ifdef WOLFSSL_SHA512 + else if (XSTRNCMP(type, "SHA512", 6) == 0) { + ctx->macType = SHA512; + wolfSSL_SHA512_Init((SHA512_CTX*)&ctx->hash); + } + #endif + #ifndef NO_MD5 + else if (XSTRNCMP(type, "MD5", 3) == 0) { + ctx->macType = MD5; + wolfSSL_MD5_Init((MD5_CTX*)&ctx->hash); + } + #endif + #ifndef NO_SHA + /* has to be last since would pick or 256, 384, or 512 too */ + else if (XSTRNCMP(type, "SHA", 3) == 0) { + ctx->macType = SHA; + wolfSSL_SHA_Init((SHA_CTX*)&ctx->hash); + } + #endif /* NO_SHA */ + else + return BAD_FUNC_ARG; + + return SSL_SUCCESS; + } + + + /* SSL_SUCCESS on ok */ + int wolfSSL_EVP_DigestUpdate(WOLFSSL_EVP_MD_CTX* ctx, const void* data, + unsigned long sz) + { + WOLFSSL_ENTER("EVP_DigestUpdate"); + + switch (ctx->macType) { +#ifndef NO_MD5 + case MD5: + wolfSSL_MD5_Update((MD5_CTX*)&ctx->hash, data, + (unsigned long)sz); + break; +#endif +#ifndef NO_SHA + case SHA: + wolfSSL_SHA_Update((SHA_CTX*)&ctx->hash, data, + (unsigned long)sz); + break; +#endif +#ifndef NO_SHA256 + case SHA256: + wolfSSL_SHA256_Update((SHA256_CTX*)&ctx->hash, data, + (unsigned long)sz); + break; +#endif +#ifdef WOLFSSL_SHA384 + case SHA384: + wolfSSL_SHA384_Update((SHA384_CTX*)&ctx->hash, data, + (unsigned long)sz); + break; +#endif +#ifdef WOLFSSL_SHA512 + case SHA512: + wolfSSL_SHA512_Update((SHA512_CTX*)&ctx->hash, data, + (unsigned long)sz); + break; +#endif + default: + return BAD_FUNC_ARG; + } + + return SSL_SUCCESS; + } + + + /* SSL_SUCCESS on ok */ + int wolfSSL_EVP_DigestFinal(WOLFSSL_EVP_MD_CTX* ctx, unsigned char* md, + unsigned int* s) + { + WOLFSSL_ENTER("EVP_DigestFinal"); + switch (ctx->macType) { +#ifndef NO_MD5 + case MD5: + wolfSSL_MD5_Final(md, (MD5_CTX*)&ctx->hash); + if (s) *s = MD5_DIGEST_SIZE; + break; +#endif +#ifndef NO_SHA + case SHA: + wolfSSL_SHA_Final(md, (SHA_CTX*)&ctx->hash); + if (s) *s = SHA_DIGEST_SIZE; + break; +#endif +#ifndef NO_SHA256 + case SHA256: + wolfSSL_SHA256_Final(md, (SHA256_CTX*)&ctx->hash); + if (s) *s = SHA256_DIGEST_SIZE; + break; +#endif +#ifdef WOLFSSL_SHA384 + case SHA384: + wolfSSL_SHA384_Final(md, (SHA384_CTX*)&ctx->hash); + if (s) *s = SHA384_DIGEST_SIZE; + break; +#endif +#ifdef WOLFSSL_SHA512 + case SHA512: + wolfSSL_SHA512_Final(md, (SHA512_CTX*)&ctx->hash); + if (s) *s = SHA512_DIGEST_SIZE; + break; +#endif + default: + return BAD_FUNC_ARG; + } + + return SSL_SUCCESS; + } + + + /* SSL_SUCCESS on ok */ + int wolfSSL_EVP_DigestFinal_ex(WOLFSSL_EVP_MD_CTX* ctx, unsigned char* md, + unsigned int* s) + { + WOLFSSL_ENTER("EVP_DigestFinal_ex"); + return EVP_DigestFinal(ctx, md, s); + } + + + unsigned char* wolfSSL_HMAC(const WOLFSSL_EVP_MD* evp_md, const void* key, + int key_len, const unsigned char* d, int n, + unsigned char* md, unsigned int* md_len) + { + int type; + unsigned char* ret = NULL; +#ifdef WOLFSSL_SMALL_STACK + Hmac* hmac = NULL; +#else + Hmac hmac[1]; +#endif + + WOLFSSL_ENTER("HMAC"); + if (!md) + return NULL; /* no static buffer support */ + + if (XSTRNCMP(evp_md, "MD5", 3) == 0) + type = MD5; + else if (XSTRNCMP(evp_md, "SHA", 3) == 0) + type = SHA; + else + return NULL; + + #ifdef WOLFSSL_SMALL_STACK + hmac = (Hmac*)XMALLOC(sizeof(Hmac), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (hmac == NULL) + return NULL; + #endif + + if (wc_HmacSetKey(hmac, type, (const byte*)key, key_len) == 0) + if (wc_HmacUpdate(hmac, d, n) == 0) + if (wc_HmacFinal(hmac, md) == 0) { + if (md_len) + *md_len = (type == MD5) ? (int)MD5_DIGEST_SIZE + : (int)SHA_DIGEST_SIZE; + ret = md; + } + + #ifdef WOLFSSL_SMALL_STACK + XFREE(hmac, NULL, DYNAMIC_TYPE_TMP_BUFFER); + #endif + + return ret; + } + void wolfSSL_ERR_clear_error(void) { /* TODO: */ @@ -10645,6 +11762,8 @@ const char* wolfSSL_CIPHER_get_name(const WOLFSSL_CIPHER* cipher) return "TLS_DHE_PSK_WITH_AES_256_CCM"; #endif #ifdef HAVE_ECC + case TLS_ECDHE_ECDSA_WITH_AES_128_CCM: + return "TLS_ECDHE_ECDSA_WITH_AES_128_CCM"; case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8: return "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8"; case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 : @@ -10951,6 +12070,47 @@ void wolfSSL_MD4_Final(unsigned char* digest, WOLFSSL_MD4_CTX* md4) #endif /* NO_MD4 */ + +WOLFSSL_BIO* wolfSSL_BIO_pop(WOLFSSL_BIO* top) +{ + (void)top; + return 0; +} + + +int wolfSSL_BIO_pending(WOLFSSL_BIO* bio) +{ + (void)bio; + return 0; +} + + + +WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_mem(void) +{ + static WOLFSSL_BIO_METHOD meth; + + WOLFSSL_ENTER("BIO_s_mem"); + meth.type = BIO_MEMORY; + + return &meth; +} + + +WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_base64(void) +{ + return 0; +} + + +void wolfSSL_BIO_set_flags(WOLFSSL_BIO* bio, int flags) +{ + (void)bio; + (void)flags; +} + + + void wolfSSL_RAND_screen(void) { @@ -11336,7 +12496,7 @@ WOLFSSL_ASN1_INTEGER* wolfSSL_X509_get_serialNumber(WOLFSSL_X509* x509) } -int wolfSSL_ASN1_TIME_print(WOLFCRYPT_BIO* bio, const WOLFSSL_ASN1_TIME* asnTime) +int wolfSSL_ASN1_TIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_TIME* asnTime) { (void)bio; (void)asnTime; @@ -11608,7 +12768,15 @@ void wolfSSL_DES_ecb_encrypt(WOLFSSL_DES_cblock* desa, #endif /* NO_DES3 */ -int wolfSSL_ASN1_UTCTIME_print(WOLFCRYPT_BIO* bio, const WOLFSSL_ASN1_UTCTIME* a) +int wolfSSL_BIO_printf(WOLFSSL_BIO* bio, const char* format, ...) +{ + (void)bio; + (void)format; + return 0; +} + + +int wolfSSL_ASN1_UTCTIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_UTCTIME* a) { (void)bio; (void)a; @@ -13953,6 +15121,121 @@ int wolfSSL_RSA_GenAdd(WOLFSSL_RSA* rsa) #endif /* NO_RSA */ +void wolfSSL_HMAC_Init(WOLFSSL_HMAC_CTX* ctx, const void* key, int keylen, + const EVP_MD* type) +{ + WOLFSSL_MSG("wolfSSL_HMAC_Init"); + + if (ctx == NULL) { + WOLFSSL_MSG("no ctx on init"); + return; + } + + if (type) { + WOLFSSL_MSG("init has type"); + + if (XSTRNCMP(type, "MD5", 3) == 0) { + WOLFSSL_MSG("md5 hmac"); + ctx->type = MD5; + } + else if (XSTRNCMP(type, "SHA256", 6) == 0) { + WOLFSSL_MSG("sha256 hmac"); + ctx->type = SHA256; + } + + /* has to be last since would pick or 256, 384, or 512 too */ + else if (XSTRNCMP(type, "SHA", 3) == 0) { + WOLFSSL_MSG("sha hmac"); + ctx->type = SHA; + } + else { + WOLFSSL_MSG("bad init type"); + } + } + + if (key && keylen) { + WOLFSSL_MSG("keying hmac"); + wc_HmacSetKey(&ctx->hmac, ctx->type, (const byte*)key, (word32)keylen); + /* OpenSSL compat, no error */ + } +} + + +void wolfSSL_HMAC_Update(WOLFSSL_HMAC_CTX* ctx, const unsigned char* data, + int len) +{ + WOLFSSL_MSG("wolfSSL_HMAC_Update"); + + if (ctx && data) { + WOLFSSL_MSG("updating hmac"); + wc_HmacUpdate(&ctx->hmac, data, (word32)len); + /* OpenSSL compat, no error */ + } +} + + +void wolfSSL_HMAC_Final(WOLFSSL_HMAC_CTX* ctx, unsigned char* hash, + unsigned int* len) +{ + WOLFSSL_MSG("wolfSSL_HMAC_Final"); + + if (ctx && hash) { + WOLFSSL_MSG("final hmac"); + wc_HmacFinal(&ctx->hmac, hash); + /* OpenSSL compat, no error */ + + if (len) { + WOLFSSL_MSG("setting output len"); + switch (ctx->type) { + case MD5: + *len = MD5_DIGEST_SIZE; + break; + + case SHA: + *len = SHA_DIGEST_SIZE; + break; + + case SHA256: + *len = SHA256_DIGEST_SIZE; + break; + + default: + WOLFSSL_MSG("bad hmac type"); + } + } + } +} + + +void wolfSSL_HMAC_cleanup(WOLFSSL_HMAC_CTX* ctx) +{ + (void)ctx; + + WOLFSSL_MSG("wolfSSL_HMAC_cleanup"); +} + + +const WOLFSSL_EVP_MD* wolfSSL_EVP_get_digestbynid(int id) +{ + WOLFSSL_MSG("wolfSSL_get_digestbynid"); + + switch(id) { +#ifndef NO_MD5 + case NID_md5: + return wolfSSL_EVP_md5(); +#endif +#ifndef NO_SHA + case NID_sha1: + return wolfSSL_EVP_sha1(); +#endif + default: + WOLFSSL_MSG("Bad digest id value"); + } + + return NULL; +} + + WOLFSSL_RSA* wolfSSL_EVP_PKEY_get1_RSA(WOLFSSL_EVP_PKEY* key) { (void)key; @@ -13980,7 +15263,7 @@ WOLFSSL_EC_KEY* wolfSSL_EVP_PKEY_get1_EC_KEY(WOLFSSL_EVP_PKEY* key) } -void* wolfSSL_EVP_X_STATE(const WOLFCRYPT_EVP_CIPHER_CTX* ctx) +void* wolfSSL_EVP_X_STATE(const WOLFSSL_EVP_CIPHER_CTX* ctx) { WOLFSSL_MSG("wolfSSL_EVP_X_STATE"); @@ -14000,7 +15283,7 @@ void* wolfSSL_EVP_X_STATE(const WOLFCRYPT_EVP_CIPHER_CTX* ctx) } -int wolfSSL_EVP_X_STATE_LEN(const WOLFCRYPT_EVP_CIPHER_CTX* ctx) +int wolfSSL_EVP_X_STATE_LEN(const WOLFSSL_EVP_CIPHER_CTX* ctx) { WOLFSSL_MSG("wolfSSL_EVP_X_STATE_LEN"); @@ -14022,7 +15305,7 @@ int wolfSSL_EVP_X_STATE_LEN(const WOLFCRYPT_EVP_CIPHER_CTX* ctx) #ifndef NO_DES3 -void wolfSSL_3des_iv(WOLFCRYPT_EVP_CIPHER_CTX* ctx, int doset, +void wolfSSL_3des_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset, unsigned char* iv, int len) { (void)len; @@ -14045,7 +15328,7 @@ void wolfSSL_3des_iv(WOLFCRYPT_EVP_CIPHER_CTX* ctx, int doset, #ifndef NO_AES -void wolfSSL_aes_ctr_iv(WOLFCRYPT_EVP_CIPHER_CTX* ctx, int doset, +void wolfSSL_aes_ctr_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset, unsigned char* iv, int len) { (void)len; @@ -14065,6 +15348,101 @@ void wolfSSL_aes_ctr_iv(WOLFCRYPT_EVP_CIPHER_CTX* ctx, int doset, #endif /* NO_AES */ + +const WOLFSSL_EVP_MD* wolfSSL_EVP_ripemd160(void) +{ + WOLFSSL_MSG("wolfSSL_ripemd160"); + + return NULL; +} + + +int wolfSSL_EVP_MD_size(const WOLFSSL_EVP_MD* type) +{ + WOLFSSL_MSG("wolfSSL_EVP_MD_size"); + + if (type == NULL) { + WOLFSSL_MSG("No md type arg"); + return BAD_FUNC_ARG; + } + + if (XSTRNCMP(type, "SHA256", 6) == 0) { + return SHA256_DIGEST_SIZE; + } +#ifndef NO_MD5 + else if (XSTRNCMP(type, "MD5", 3) == 0) { + return MD5_DIGEST_SIZE; + } +#endif +#ifdef WOLFSSL_SHA384 + else if (XSTRNCMP(type, "SHA384", 6) == 0) { + return SHA384_DIGEST_SIZE; + } +#endif +#ifdef WOLFSSL_SHA512 + else if (XSTRNCMP(type, "SHA512", 6) == 0) { + return SHA512_DIGEST_SIZE; + } +#endif +#ifndef NO_SHA + /* has to be last since would pick or 256, 384, or 512 too */ + else if (XSTRNCMP(type, "SHA", 3) == 0) { + return SHA_DIGEST_SIZE; + } +#endif + + return BAD_FUNC_ARG; +} + + +int wolfSSL_EVP_CIPHER_CTX_iv_length(const WOLFSSL_EVP_CIPHER_CTX* ctx) +{ + WOLFSSL_MSG("wolfSSL_EVP_CIPHER_CTX_iv_length"); + + switch (ctx->cipherType) { + + case AES_128_CBC_TYPE : + case AES_192_CBC_TYPE : + case AES_256_CBC_TYPE : + WOLFSSL_MSG("AES CBC"); + return AES_BLOCK_SIZE; + +#ifdef WOLFSSL_AES_COUNTER + case AES_128_CTR_TYPE : + case AES_192_CTR_TYPE : + case AES_256_CTR_TYPE : + WOLFSSL_MSG("AES CTR"); + return AES_BLOCK_SIZE; +#endif + + case DES_CBC_TYPE : + WOLFSSL_MSG("DES CBC"); + return DES_BLOCK_SIZE; + + case DES_EDE3_CBC_TYPE : + WOLFSSL_MSG("DES EDE3 CBC"); + return DES_BLOCK_SIZE; +#ifdef HAVE_IDEA + case IDEA_CBC_TYPE : + WOLFSSL_MSG("IDEA CBC"); + return IDEA_BLOCK_SIZE; +#endif + case ARC4_TYPE : + WOLFSSL_MSG("ARC4"); + return 0; + + case NULL_CIPHER_TYPE : + WOLFSSL_MSG("NULL"); + return 0; + + default: { + WOLFSSL_MSG("bad type"); + } + } + return 0; +} + + void wolfSSL_OPENSSL_free(void* p) { WOLFSSL_MSG("wolfSSL_OPENSSL_free"); @@ -14324,7 +15702,7 @@ int wolfSSL_PEM_write_RSAPrivateKey(FILE *fp, WOLFSSL_RSA *rsa, } #endif /* NO_FILESYSTEM */ -int wolfSSL_PEM_write_bio_RSAPrivateKey(WOLFCRYPT_BIO* bio, RSA* rsa, +int wolfSSL_PEM_write_bio_RSAPrivateKey(WOLFSSL_BIO* bio, RSA* rsa, const EVP_CIPHER* cipher, unsigned char* passwd, int len, pem_password_cb cb, void* arg) @@ -15552,7 +16930,7 @@ int wolfSSL_PEM_write_EC_PUBKEY(FILE *fp, WOLFSSL_EC_KEY *x) /* return code compliant with OpenSSL : * 1 if success, 0 if error */ -int wolfSSL_PEM_write_bio_ECPrivateKey(WOLFCRYPT_BIO* bio, WOLFSSL_EC_KEY* ecc, +int wolfSSL_PEM_write_bio_ECPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EC_KEY* ecc, const EVP_CIPHER* cipher, unsigned char* passwd, int len, pem_password_cb cb, void* arg) @@ -15727,7 +17105,7 @@ int wolfSSL_PEM_write_ECPrivateKey(FILE *fp, WOLFSSL_EC_KEY *ecc, /* return code compliant with OpenSSL : * 1 if success, 0 if error */ -int wolfSSL_PEM_write_bio_DSAPrivateKey(WOLFCRYPT_BIO* bio, WOLFSSL_DSA* dsa, +int wolfSSL_PEM_write_bio_DSAPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_DSA* dsa, const EVP_CIPHER* cipher, unsigned char* passwd, int len, pem_password_cb cb, void* arg) @@ -15909,7 +17287,7 @@ int wolfSSL_PEM_write_DSA_PUBKEY(FILE *fp, WOLFSSL_DSA *x) #endif /* #ifndef NO_DSA */ -WOLFSSL_EVP_PKEY* wolfSSL_PEM_read_bio_PrivateKey(WOLFCRYPT_BIO* bio, +WOLFSSL_EVP_PKEY* wolfSSL_PEM_read_bio_PrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EVP_PKEY** key, pem_password_cb cb, void* arg) { (void)bio; @@ -16458,6 +17836,23 @@ void* wolfSSL_GetRsaDecCtx(WOLFSSL* ssl) return 0; } + + int wolfSSL_BIO_read_filename(WOLFSSL_BIO *b, const char *name) { + (void)b; + (void)name; + WOLFSSL_ENTER("wolfSSL_BIO_read_filename"); + WOLFSSL_STUB("wolfSSL_BIO_read_filename"); + + return 0; + } + + WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_file(void) { + WOLFSSL_ENTER("wolfSSL_BIO_s_file"); + WOLFSSL_STUB("wolfSSL_BIO_s_file"); + + return NULL; + } + const char * wolf_OBJ_nid2sn(int n) { (void)n; WOLFSSL_ENTER("wolf_OBJ_nid2sn"); @@ -16483,7 +17878,7 @@ void* wolfSSL_GetRsaDecCtx(WOLFSSL* ssl) } - WOLFSSL_X509 *PEM_read_bio_WOLFSSL_X509(WOLFCRYPT_BIO *bp, WOLFSSL_X509 **x, pem_password_cb *cb, void *u) { + WOLFSSL_X509 *PEM_read_bio_WOLFSSL_X509(WOLFSSL_BIO *bp, WOLFSSL_X509 **x, pem_password_cb *cb, void *u) { (void)bp; (void)x; (void)cb; @@ -16706,7 +18101,17 @@ int wolf_OBJ_txt2nid(const char* s) { } -WOLFSSL_DH *wolfSSL_PEM_read_bio_DHparams(WOLFCRYPT_BIO *bp, WOLFSSL_DH **x, pem_password_cb *cb, void *u) +WOLFSSL_BIO *wolfSSL_BIO_new_file(const char *filename, const char *mode) { + (void)filename; + (void)mode; + WOLFSSL_ENTER("wolfSSL_BIO_new_file"); + WOLFSSL_STUB("wolfSSL_BIO_new_file"); + + return NULL; +} + + +WOLFSSL_DH *wolfSSL_PEM_read_bio_DHparams(WOLFSSL_BIO *bp, WOLFSSL_DH **x, pem_password_cb *cb, void *u) { (void) bp; (void) x; @@ -16719,7 +18124,7 @@ WOLFSSL_DH *wolfSSL_PEM_read_bio_DHparams(WOLFCRYPT_BIO *bp, WOLFSSL_DH **x, pem return NULL; } -int PEM_write_bio_WOLFSSL_X509(WOLFCRYPT_BIO *bp, WOLFSSL_X509 *x) { +int PEM_write_bio_WOLFSSL_X509(WOLFSSL_BIO *bp, WOLFSSL_X509 *x) { (void)bp; (void)x; WOLFSSL_ENTER("PEM_write_bio_WOLFSSL_X509"); @@ -16942,7 +18347,7 @@ int wolfSSL_sk_X509_num(const STACK_OF(WOLFSSL_X509) *s) } -int wolfSSL_X509_NAME_print_ex(WOLFCRYPT_BIO* bio, WOLFSSL_X509_NAME* nm, +int wolfSSL_X509_NAME_print_ex(WOLFSSL_BIO* bio, WOLFSSL_X509_NAME* nm, int indent, unsigned long flags) { (void)bio; @@ -17686,683 +19091,4 @@ int wolfSSL_poll(WOLFSSL* ssl, WOLF_EVENT* events, #endif /* HAVE_WOLF_EVENT */ -#ifdef OPENSSL_EXTRA - -typedef struct { - WOLFSSL *ssl; - /* re-negotiate every time the total number of bytes is this size */ - int num_renegotiates; - unsigned long renegotiate_count; - unsigned long byte_count; - unsigned long renegotiate_timeout; - unsigned long last_time; -} WOLFCRYPT_BIO_SSL; - -static int wc_BioSSL_write(WOLFCRYPT_BIO *bio, const char *data, int size); -static int wc_BioSSL_read(WOLFCRYPT_BIO *bio, char *data, int size); -static int wc_BioSSL_puts(WOLFCRYPT_BIO *bio, const char *str); -static long wc_BioSSL_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioSSL_new(WOLFCRYPT_BIO *bio); -static int wc_BioSSL_free(WOLFCRYPT_BIO *bio); -static long wc_BioSSL_callback_ctrl(WOLFCRYPT_BIO *bio, int cmd, - WOLFCRYPT_BIO_info_cb *fp); - -static WOLFCRYPT_BIO_METHOD wc_BioSSL_method = { - BIO_TYPE_SSL, - "SSL", - wc_BioSSL_write, - wc_BioSSL_read, - wc_BioSSL_puts, - NULL, /* gets */ - wc_BioSSL_ctrl, - wc_BioSSL_new, - wc_BioSSL_free, - wc_BioSSL_callback_ctrl, -}; - -WOLFCRYPT_BIO_METHOD *wc_Bio_f_ssl(void) -{ - return (&wc_BioSSL_method); -} - -static int wc_BioSSL_new(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - bio->ptr = (WOLFCRYPT_BIO_SSL *)XMALLOC(sizeof(WOLFCRYPT_BIO_SSL), - 0, DYNAMIC_TYPE_OPENSSL); - if (bio->ptr == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return 0; - } - - XMEMSET(bio->ptr, 0, sizeof(WOLFCRYPT_BIO_SSL)); - - bio->init = 0; - bio->flags = 0; - return 1; -} - -static int wc_BioSSL_free(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioSSL_free"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - if (bio->ptr != NULL) { - WOLFCRYPT_BIO_SSL *bssl = (WOLFCRYPT_BIO_SSL *)bio->ptr; - if (bssl->ssl != NULL) { - wolfSSL_shutdown(bssl->ssl); - - if (bio->shutdown && bio->init) { - WOLFSSL_MSG("Free BIO ssl"); - wolfSSL_free(bssl->ssl); - } - } - - XFREE(bio->ptr, 0, DYNAMIC_TYPE_OPENSSL); - bio->ptr = NULL; - } - - if (bio->shutdown) { - bio->init = 0; - bio->flags = 0; - } - - return 1; -} - -static int wc_BioSSL_read(WOLFCRYPT_BIO *bio, char *data, int size) -{ - int ret = 1; - WOLFCRYPT_BIO_SSL *bssl; - - if (bio == NULL || bio->ptr == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - if (data == NULL) - return 0; - - bssl = (WOLFCRYPT_BIO_SSL *)bio->ptr; - - wc_BioClearRetryFlags(bio); - - ret = wolfSSL_read(bssl->ssl, data, size); - - switch (wolfSSL_get_error(bssl->ssl, ret)) { - case SSL_ERROR_NONE: - if (ret <= 0) - break; - -#ifdef HAVE_SECURE_RENEGOTIATION - { - int r = 0; - - if (bssl->renegotiate_count > 0) { - bssl->byte_count += ret; - if (bssl->byte_count > bssl->renegotiate_count) { - bssl->byte_count = 0; - bssl->num_renegotiates++; - wolfSSL_Rehandshake(bssl->ssl); - r = 1; - } - } - - if ((bssl->renegotiate_timeout > 0) && !r) { - unsigned long tm; - tm = (unsigned long)time(NULL); - if (tm > bssl->last_time + bssl->renegotiate_timeout) { - bssl->last_time = tm; - bssl->num_renegotiates++; - wolfSSL_Rehandshake(bssl->ssl); - } - } - } -#endif - break; - - case SSL_ERROR_WANT_READ: - wc_BioSetRetryRead(bio); - break; - - case SSL_ERROR_WANT_WRITE: - wc_BioSetRetryWrite(bio); - break; - - case SSL_ERROR_WANT_X509_LOOKUP: - wc_BioSetRetrySpecial(bio); - bio->retry_reason = BIO_RR_SSL_X509_LOOKUP; - break; - - case SSL_ERROR_WANT_ACCEPT: - wc_BioSetRetrySpecial(bio); - bio->retry_reason = BIO_RR_ACCEPT; - break; - - case SSL_ERROR_WANT_CONNECT: - wc_BioSetRetrySpecial(bio); - bio->retry_reason = BIO_RR_CONNECT; - break; - - case SSL_ERROR_SYSCALL: - case SSL_ERROR_SSL: - case SSL_ERROR_ZERO_RETURN: - break; - - default: - break; - } - - return ret; -} - -static int wc_BioSSL_write(WOLFCRYPT_BIO *bio, const char *data, int size) -{ - int ret; - WOLFCRYPT_BIO_SSL *bssl; - - if (bio == NULL || bio->ptr == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - if (data == NULL) - return 0; - - bssl = (WOLFCRYPT_BIO_SSL *)bio->ptr; - - wc_BioClearRetryFlags(bio); - - ret = wolfSSL_write(bssl->ssl, data, size); - - switch (wolfSSL_get_error(bssl->ssl, ret)) { - case SSL_ERROR_NONE: - if (ret <= 0) - break; - -#ifdef HAVE_SECURE_RENEGOTIATION - { - int r = 0; - - if (bssl->renegotiate_count > 0) { - bssl->byte_count += ret; - if (bssl->byte_count > bssl->renegotiate_count) { - bssl->byte_count = 0; - bssl->num_renegotiates++; - wolfSSL_Rehandshake(bssl->ssl); - r = 1; - } - } - - if ((bssl->renegotiate_timeout > 0) && !r) { - unsigned long tm; - - tm = (unsigned long)time(NULL); - if (tm > bssl->last_time + bssl->renegotiate_timeout) { - bssl->last_time = tm; - bssl->num_renegotiates++; - wolfSSL_Rehandshake(bssl->ssl); - } - } - } -#endif - break; - - case SSL_ERROR_WANT_WRITE: - wc_BioSetRetryWrite(bio); - break; - - case SSL_ERROR_WANT_READ: - wc_BioSetRetryRead(bio); - break; - - case SSL_ERROR_WANT_X509_LOOKUP: - wc_BioSetRetrySpecial(bio); - bio->retry_reason = BIO_RR_SSL_X509_LOOKUP; - break; - - case SSL_ERROR_WANT_CONNECT: - wc_BioSetRetrySpecial(bio); - bio->retry_reason = BIO_RR_CONNECT; - break; - - case SSL_ERROR_SYSCALL: - case SSL_ERROR_SSL: - break; - - default: - break; - } - - return ret; -} - -static int wc_BioSSL_set_bio(WOLFSSL *ssl, WOLFCRYPT_BIO *rbio, - WOLFCRYPT_BIO *wbio) -{ - WOLFSSL_ENTER("wc_BioSSL_set_bio"); - - if (ssl == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - if (ssl->biord != NULL && ssl->biord != rbio) - wc_BioFreeAll(ssl->biord); - if (ssl->biowr != NULL && ssl->biowr != wbio && ssl->biord != ssl->biowr) - wc_BioFreeAll(ssl->biowr); - - ssl->biord = rbio; - wolfSSL_set_rfd(ssl, rbio->num); - - ssl->biowr = wbio; - wolfSSL_set_wfd(ssl, wbio->num); - - return 1; -} - -static long wc_BioSSL_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - WOLFCRYPT_BIO_SSL *bssl; - long ret = 1; - - WOLFSSL_ENTER("wc_BioSSL_ctrl"); - - if (bio == NULL || bio->ptr == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - bssl = (WOLFCRYPT_BIO_SSL *)bio->ptr; - - if ((bssl->ssl == NULL) && (cmd != BIO_C_SET_SSL)) { - WOLFSSL_MSG("Set SSL not possible, ssl pointer NULL\n"); - return 0; - } - - switch (cmd) { - case BIO_CTRL_RESET: - wolfSSL_shutdown(bssl->ssl); - ret = (long)wolfSSL_negotiate(bssl->ssl); - if (ret <= 0) - break; - wolfSSL_clear(bssl->ssl); - - if (bio->next_bio != NULL) - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - else if (bssl->ssl->biord != NULL) - ret = wc_BioCtrl(bssl->ssl->biord, cmd, num, ptr); - else - ret = 1; - break; - - case BIO_CTRL_INFO: - ret = 0; - break; - - case BIO_C_SSL_MODE: - if (num) /* client mode */ - wolfSSL_set_connect_state(bssl->ssl); - else - wolfSSL_set_accept_state(bssl->ssl); - break; - - case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT: - ret = bssl->renegotiate_timeout; - if (num < 60) - num = 5; - bssl->renegotiate_timeout = (unsigned long)num; - bssl->last_time = (unsigned long)time(NULL); - break; - - case BIO_C_SET_SSL_RENEGOTIATE_BYTES: - ret = bssl->renegotiate_count; - if (num >= 512) - bssl->renegotiate_count = (unsigned long)num; - break; - - case BIO_C_GET_SSL_NUM_RENEGOTIATES: - ret = bssl->num_renegotiates; - break; - - case BIO_C_SET_SSL: - if (bssl->ssl != NULL) { - wc_BioSSL_free(bio); - if (!wc_BioSSL_new(bio)) - return 0; - } - - bio->shutdown = (int)num; - bssl->ssl = (WOLFSSL *)ptr; - - if (bssl->ssl->biord != NULL) { - if (bio->next_bio != NULL) - wc_BioPush(bssl->ssl->biord, bio->next_bio); - bio->next_bio = bssl->ssl->biord; - - if (LockMutex(&bssl->ssl->biord->refMutex) != 0) { - WOLFSSL_MSG("Couldn't lock count mutex"); - ret = 0; - break; - } - bssl->ssl->biord->references++; - UnLockMutex(&bssl->ssl->biord->refMutex); - } - bio->init = 1; - break; - - case BIO_C_GET_SSL: - if (ptr != NULL) - *(WOLFSSL **)ptr = bssl->ssl; - else - ret = 0; - break; - - case BIO_CTRL_GET_CLOSE: - ret = bio->shutdown; - break; - - case BIO_CTRL_SET_CLOSE: - bio->shutdown = (int)num; - break; - - case BIO_CTRL_WPENDING: - ret = wc_BioCtrl(bssl->ssl->biowr, cmd, num, ptr); - break; - - case BIO_CTRL_PENDING: - ret = wolfSSL_pending(bssl->ssl); - if (!ret) - ret = wc_BioPending(bssl->ssl->biord); - break; - - case BIO_CTRL_FLUSH: - wc_BioClearRetryFlags(bio); - ret = wc_BioCtrl(bssl->ssl->biowr, cmd, num, ptr); - wc_BioCopyNextRetry(bio); - break; - - case BIO_CTRL_PUSH: - if (bio->next_bio != NULL && bio->next_bio != bssl->ssl->biord) { - ret = wc_BioSSL_set_bio(bssl->ssl, - bio->next_bio, bio->next_bio); - if (LockMutex(&bio->next_bio->refMutex) != 0) { - WOLFSSL_MSG("Couldn't lock count mutex"); - ret = 0; - break; - } - bio->next_bio->references++; - UnLockMutex(&bio->next_bio->refMutex); - } - break; - - case BIO_CTRL_POP: - if (bio == ptr) { - if (bssl->ssl->biord != bssl->ssl->biowr) - wc_BioFreeAll(bssl->ssl->biowr); - if (bio->next_bio != NULL) { - if (LockMutex(&bio->next_bio->refMutex) != 0) { - WOLFSSL_MSG("Couldn't lock count mutex"); - ret = 0; - break; - } - bio->next_bio->references--; - UnLockMutex(&bio->next_bio->refMutex); - } - bssl->ssl->biowr = NULL; - bssl->ssl->biord = NULL; - } - break; - - case BIO_C_DO_STATE_MACHINE: - wc_BioClearRetryFlags(bio); - - bio->retry_reason = 0; - ret = (long)wolfSSL_negotiate(bssl->ssl); - - switch (wolfSSL_get_error(bssl->ssl, (int)ret)) { - case SSL_ERROR_WANT_READ: - wc_BioSetFlags(bio, BIO_FLAGS_READ | - BIO_FLAGS_SHOULD_RETRY); - break; - - case SSL_ERROR_WANT_WRITE: - wc_BioSetFlags(bio, BIO_FLAGS_WRITE | - BIO_FLAGS_SHOULD_RETRY); - break; - - case SSL_ERROR_WANT_CONNECT: - wc_BioSetFlags(bio, BIO_FLAGS_IO_SPECIAL | - BIO_FLAGS_SHOULD_RETRY); - bio->retry_reason = bio->next_bio->retry_reason; - break; - - case SSL_ERROR_WANT_X509_LOOKUP: - wc_BioSetRetrySpecial(bio); - bio->retry_reason = BIO_RR_SSL_X509_LOOKUP; - break; - - default: - break; - } - break; - - case BIO_CTRL_DUP: - { - WOLFCRYPT_BIO *dbio; - dbio = (WOLFCRYPT_BIO *)ptr; - - if (((WOLFCRYPT_BIO_SSL *)dbio->ptr)->ssl != NULL) - wolfSSL_free(((WOLFCRYPT_BIO_SSL *)dbio->ptr)->ssl); - - /* add copy ssl */ - ((WOLFCRYPT_BIO_SSL *)dbio->ptr)->ssl = wolfSSL_dup(bssl->ssl); - - ((WOLFCRYPT_BIO_SSL *)dbio->ptr)->renegotiate_count = - bssl->renegotiate_count; - - ((WOLFCRYPT_BIO_SSL *)dbio->ptr)->byte_count = bssl->byte_count; - - ((WOLFCRYPT_BIO_SSL *)dbio->ptr)->renegotiate_timeout = - bssl->renegotiate_timeout; - - ((WOLFCRYPT_BIO_SSL *)dbio->ptr)->last_time = bssl->last_time; - - if (((WOLFCRYPT_BIO_SSL *)dbio->ptr)->ssl == NULL) - ret = 0; - } - break; - - case BIO_C_GET_FD: - ret = wc_BioCtrl(bssl->ssl->biord, cmd, num, ptr); - break; - - case BIO_CTRL_SET_CALLBACK: - /* not supported */ - WOLFSSL_MSG("BIO_CTRL_SET_CALLBACK not supported\n"); - ret = 0; - break; - - case BIO_CTRL_GET_CALLBACK: - /* not supported */ - WOLFSSL_MSG("BIO_CTRL_GET_CALLBACK not supported\n"); - ptr = NULL; - ret = 0; - break; - - default: - ret = wc_BioCtrl(bssl->ssl->biord, cmd, num, ptr); - break; - } - - return ret; -} - -static long wc_BioSSL_callback_ctrl(WOLFCRYPT_BIO *bio, int cmd, - WOLFCRYPT_BIO_info_cb *fp) -{ - long ret = 1; - WOLFCRYPT_BIO_SSL *bssl; - - WOLFSSL_ENTER("wc_BioSSL_callback_ctrl"); - - if (bio == NULL || bio->ptr == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - bssl = (WOLFCRYPT_BIO_SSL *)bio->ptr; - - switch (cmd) { - case BIO_CTRL_SET_CALLBACK: - /* not supported */ - WOLFSSL_MSG("BIO_CTRL_GET_CALLBACK not supported\n"); - ret = 0; - break; - - default: - ret = wc_BioCallbackCtrl(bssl->ssl->biord, cmd, fp); - break; - } - - return ret; -} - -static int wc_BioSSL_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - WOLFSSL_ENTER("wc_BioSSL_puts"); - - if (bio == NULL || str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - return wc_BioSSL_write(bio, str, (int)strlen(str)); -} - -void wc_BioSSLShutdown(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioSSL_shutdown"); - - while (bio != NULL) { - if (bio->method->type == BIO_TYPE_SSL) { - wolfSSL_shutdown(((WOLFCRYPT_BIO_SSL *)bio->ptr)->ssl); - break; - } - - bio = bio->next_bio; - } -} - - -WOLFCRYPT_BIO *wolfSSL_BioNewBufferSSLConnect(WOLFSSL_CTX *ctx) -{ - WOLFCRYPT_BIO *bio = NULL, *buf = NULL, *ssl = NULL; - - WOLFSSL_ENTER("wolfSSL_BioNewBufferSSLConnect"); - - if (ctx == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return NULL; - } - - buf = wc_BioNew(wc_Bio_f_buffer()); - if (buf == NULL) - return NULL; - - ssl = wolfSSL_BioNewSSLConnect(ctx); - if (ssl == NULL) - goto err; - - bio = wc_BioPush(buf, ssl); - if (bio == NULL) - goto err; - - return bio; - -err: - if (buf != NULL) - wc_BioFree(buf); - if (ssl != NULL) - wc_BioFree(ssl); - - return NULL; -} - -WOLFCRYPT_BIO *wolfSSL_BioNewSSLConnect(WOLFSSL_CTX *ctx) -{ - WOLFCRYPT_BIO *bio = NULL, *con = NULL, *ssl = NULL; - - WOLFSSL_ENTER("wolfSSL_BioNewSSLConnect"); - - if (ctx == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return NULL; - } - - con = wc_BioNew(wc_Bio_s_connect()); - if (con == NULL) - return NULL; - - ssl = wolfSSL_BioNewSSL(ctx, BIO_CLOSE); - if (ssl == NULL) - goto err; - - bio = wc_BioPush(ssl, con); - if (bio == NULL) - goto err; - - return bio; - -err: - if (con != NULL) - wc_BioFree(con); - if (ssl != NULL) - wc_BioFree(ssl); - return NULL; -} - -WOLFCRYPT_BIO *wolfSSL_BioNewSSL(WOLFSSL_CTX *ctx, int mode) -{ - WOLFCRYPT_BIO *bio; - WOLFSSL *ssl; - - WOLFSSL_ENTER("wolfSSL_BioNewSSL"); - - if (ctx == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return NULL; - } - - bio = wc_BioNew(wc_Bio_f_ssl()); - if (bio == NULL) - return NULL; - - ssl = wolfSSL_new(ctx); - if (ssl == NULL) { - wc_BioFree(bio); - return NULL; - } - - if (mode) /* client */ - wolfSSL_set_connect_state(ssl); - else - wolfSSL_set_accept_state(ssl); - - wc_BioSetSSL(bio, ssl, mode); - - return bio; -} - -#endif /* OPENSSL_EXTRA */ - - #endif /* WOLFCRYPT_ONLY */ diff --git a/src/tls.c b/src/tls.c index 0105378a6..d4593ba8f 100644 --- a/src/tls.c +++ b/src/tls.c @@ -3077,10 +3077,10 @@ int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap) } -#define SCR_FREE_ALL(data, heap) XFREE((data), (heap), DYNAMIC_TYPE_TLSX) -#define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize -#define SCR_WRITE TLSX_SecureRenegotiation_Write -#define SCR_PARSE TLSX_SecureRenegotiation_Parse +#define SCR_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX) +#define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize +#define SCR_WRITE TLSX_SecureRenegotiation_Write +#define SCR_PARSE TLSX_SecureRenegotiation_Parse #else diff --git a/tests/api.c b/tests/api.c index 03783116e..29465d4a8 100644 --- a/tests/api.c +++ b/tests/api.c @@ -39,7 +39,6 @@ #include #include /* compatibility layer */ -#include #include #include @@ -743,588 +742,6 @@ done2: return; } -/* BIO test */ -#if defined(OPENSSL_EXTRA) -static THREAD_RETURN WOLFSSL_THREAD test_server_bio(void* args) -{ - SOCKET_T sockfd = 0; - SOCKET_T clientfd = 0; - word16 port = wolfSSLPort; - - WOLFCRYPT_BIO *bio = 0; - char msg[] = "Server BIO, I hear you fa shizzle!"; - char input[1024]; - int idx; - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - - ((func_args*)args)->return_code = TEST_FAIL; - tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 1); - CloseSocket(sockfd); - - bio = wc_BioNew(wc_Bio_s_socket()); - if (bio == NULL) { - printf("wc_BioNew failed\n"); - goto done; - } - - wc_BioSetFd(bio, clientfd, BIO_NOCLOSE); - -read_again: - idx = wc_BioRead(bio, input, sizeof(input)-1); - if (idx <= 0) { - if (wc_BioShouldRetry(bio)) { - printf("Retry read\n"); - goto read_again; - } - printf("wc_BioWrite failed\n"); - goto done; - } - - input[idx] = 0; - printf("BioSrv, Client message: %s\n", input); - - if (wc_BioWrite(bio, msg, sizeof(msg)) != sizeof(msg)) { - printf("wc_BioWrite failed\n"); -#ifdef WOLFSSL_TIRTOS - return; -#else - return 0; -#endif - } - -#ifdef WOLFSSL_TIRTOS - Task_yield(); -#endif - -done: - if (bio != 0) - wc_BioFreeAll(bio); - - CloseSocket(clientfd); - ((func_args*)args)->return_code = TEST_SUCCESS; - -#ifdef WOLFSSL_TIRTOS - fdCloseSession(Task_self()); -#endif - -#ifndef WOLFSSL_TIRTOS - return 0; -#endif -} - -static void test_client_bio(void* args) -{ - WOLFCRYPT_BIO* bio = 0; - char msg[64] = "Client BIO, hello wolfssl!"; - char reply[1024]; -#ifdef TEST_IPV6 - SOCKET_T sockfd = 0; -#else - char ip[] = {127, 0, 0, 1}; -#endif - int input, port; - int msgSz = (int)strlen(msg); - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - - ((func_args*)args)->return_code = TEST_FAIL; - -#ifdef TEST_IPV6 - bio = wc_BioNew(wc_Bio_s_socket()); -#else - bio = wc_BioNew(wc_Bio_s_connect()); -#endif - if (bio == NULL) { - printf("wc_BioNew failed\n"); - goto done2; - } - - port = ((func_args*)args)->signal->port; - -#ifdef TEST_IPV6 - tcp_connect(&sockfd, wolfSSLIP, port, 0, NULL); - wc_BioSetFd(bio, sockfd, BIO_NOCLOSE); -#else - wc_BioSetConnIp(bio, ip); - wc_BioSetConnIntPort(bio, &port); - - /* start connection */ - input = (int)wc_BioDoConnect(bio); - if (input <= 0) { - printf("wc_BioDoConnect failed : %d\n", input); - goto done2; - } -#endif - - if (wc_BioWrite(bio, msg, msgSz) != msgSz) { - printf("wc_BioWrite failed"); - goto done2; - } - -read_again: - input = wc_BioRead(bio, reply, sizeof(reply)-1); - if (input <= 0) { - if (wc_BioShouldRetry(bio)) { - printf("Retry read\n"); - goto read_again; - } - printf("wc_BioRead failed"); - goto done2; - } - - reply[input] = 0; - printf("BioCli, Server response: %s\n", reply); - -done2: - if (bio != 0) - wc_BioFreeAll(bio); - - ((func_args*)args)->return_code = TEST_SUCCESS; - -#ifdef WOLFSSL_TIRTOS - fdCloseSession(Task_self()); -#endif - - return; -} - -#ifndef TEST_IPV6 -static THREAD_RETURN WOLFSSL_THREAD test_server_full_bio(void* args) -{ - WOLFCRYPT_BIO *abio = 0, *cbio = 0; - char buf[256]; - char msg[] = "I hear you fa shizzle!"; - int r; - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - - ((func_args*)args)->return_code = TEST_FAIL; - - abio = wc_BioNew(wc_Bio_s_accept()); - if (abio == NULL) { - printf("wc_Bio_s_accept failed\n"); - goto done; - } - - snprintf(buf, sizeof(buf), "%s:%d", wolfSSLIP, wolfSSLPort); - if (wc_BioSetAcceptPort(abio, buf) <= 0) { - printf("wc_BioSetAcceptPort failed\n"); - goto done; - } - XMEMSET(buf, 0, sizeof(buf)); - - /* force SO_REUSEADDR */ - wc_BioSetBindMode(abio, BIO_BIND_REUSEADDR); - - /* force NO_SIGPIPE and TCP_NODELAY */ - wc_BioSetSocketOptions(abio, BIO_OPT_TCP_NO_DELAY|BIO_OPT_IGN_SIGPIPE); - - /* First call to wc_BioAccept() sets up accept BIO */ - if (wc_BioDoAccept(abio) <= 0) { - printf("Error setting up accept\n"); - goto done; - } - - /* activate Thread */ - tcp_set_ready(args, wolfSSLPort, 0); - - /* Wait for incoming connection */ - if (wc_BioDoAccept(abio) <= 0) { - printf("Error accepting connection\n"); - goto done; - } - - /* Retrieve BIO for connection */ - cbio = wc_BioPop(abio); - wc_BioPuts(cbio, msg); - - /* Close accept BIO to refuse further connections */ - wc_BioFree(abio); - abio = 0; - - /* Read msg, send ack */ - do { - XMEMSET(buf, 0, sizeof(buf)); - r = wc_BioRead(cbio, buf, sizeof(buf)); - if (r <= 0) { - if (wc_BioShouldRetry(cbio)) { - printf("Retry read\n"); - continue; - } - printf("wc_BioRead failed\n"); - break; - } - if (r >= 3 && !XSTRNCMP("end", buf, 3)) { - printf("BioFullSrv, Client close connection\n"); - break; - } - - buf[r] = 0; - printf("BioFullSrv, Client sent: %s\n", buf); - wc_BioPuts(cbio, "Server ACK"); - } while (1); - -done: - if (abio != 0) - wc_BioFree(abio); - if (cbio != 0) - wc_BioFree(cbio); - - ((func_args*)args)->return_code = TEST_SUCCESS; - -#ifdef WOLFSSL_TIRTOS - fdCloseSession(Task_self()); -#endif - -#ifndef WOLFSSL_TIRTOS - return 0; -#endif -} - -static void test_client_full_bio(void* args) -{ - WOLFCRYPT_BIO* bio = 0; - char msg[64] = "Hello wolfssl!"; - char reply[1024]; -#ifdef TEST_IPV6 - SOCKET_T sockfd = 0; -#else - char ip[] = {127, 0, 0, 1}; -#endif - int input, port; - int msgSz = (int)strlen(msg); - - printf("client\n"); -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - - ((func_args*)args)->return_code = TEST_FAIL; - -#ifdef TEST_IPV6 - bio = wc_BioNew(wc_Bio_s_socket()); -#else - bio = wc_BioNew(wc_Bio_s_connect()); -#endif - if (bio == NULL) { - printf("wc_BioNew failed\n"); - goto done; - } - - port = ((func_args*)args)->signal->port; - -#ifdef TEST_IPV6 - tcp_connect(&sockfd, wolfSSLIP, port, 0, NULL); - wc_BioSetFd(bio, sockfd, BIO_NOCLOSE); -#else - wc_BioSetConnIp(bio, ip); - wc_BioSetConnIntPort(bio, &port); - - printf("client do connect\n"); - /* start connection */ - input = (int)wc_BioDoConnect(bio); - if (input <= 0) { - printf("wc_BioDoConnect failed : %d\n", input); - goto done; - } - printf("done\n"); -#endif - -read_again: - input = wc_BioRead(bio, reply, sizeof(reply)-1); - if (input <= 0) { - if (wc_BioShouldRetry(bio)) { - printf("Retry read\n"); - goto read_again; - } - printf("wc_BioRead failed"); - goto done; - } - reply[input] = 0; - printf("BioFullCli, Server sent: %s\n", reply); - - if (wc_BioWrite(bio, msg, msgSz) != msgSz) { - printf("wc_BioWrite failed"); - goto done; - } - -read_again2: - input = wc_BioRead(bio, reply, sizeof(reply)-1); - if (input <= 0) { - if (wc_BioShouldRetry(bio)) { - printf("Retry read\n"); - goto read_again2; - } - printf("wc_BioRead failed"); - goto done; - } - - reply[input] = 0; - printf("BioFullCli, Server response: %s\n", reply); - - /* close */ - if (wc_BioWrite(bio, "end", 3) != 3) { - printf("wc_BioWrite failed"); - goto done; - } - -done: - if (bio != 0) - wc_BioFreeAll(bio); - - ((func_args*)args)->return_code = TEST_SUCCESS; - -#ifdef WOLFSSL_TIRTOS - fdCloseSession(Task_self()); -#endif -} -#endif /* TEST_IPV6 */ - - - -/* BIO SSL test */ -static THREAD_RETURN WOLFSSL_THREAD test_server_bio_ssl(void* args) -{ - SOCKET_T sockfd = 0; - SOCKET_T clientfd = 0; - word16 port = wolfSSLPort; - - WOLFCRYPT_BIO *ssl_bio = 0; - WOLFSSL_METHOD* method = 0; - WOLFSSL_CTX* ctx = 0; - WOLFSSL* ssl = 0; - - char msg[] = "Server BIO_SSL, I hear you fa shizzle!"; - char input[1024]; - int idx; - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - - ((func_args*)args)->return_code = TEST_FAIL; - method = wolfSSLv23_server_method(); - ctx = wolfSSL_CTX_new(method); - - wolfSSL_CTX_set_verify(ctx, - SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0); - -#ifdef OPENSSL_EXTRA - wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack); -#endif - - if (wolfSSL_CTX_load_verify_locations(ctx, cliCert, 0) != SSL_SUCCESS) - { - /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/ - goto done; - } - if (wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM) - != SSL_SUCCESS) - { - /*err_sys("can't load server cert chain file, " - "Please run from wolfSSL home dir");*/ - goto done; - } - if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM) - != SSL_SUCCESS) - { - /*err_sys("can't load server key file, " - "Please run from wolfSSL home dir");*/ - goto done; - } - - ssl = wolfSSL_new(ctx); - tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 1); - CloseSocket(sockfd); - - wolfSSL_set_fd(ssl, clientfd); - -#ifdef NO_PSK -#if !defined(NO_FILESYSTEM) && !defined(NO_DH) - wolfSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM); -#elif !defined(NO_DH) - SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */ -#endif -#endif - - ssl_bio = wc_BioNew(wc_Bio_f_ssl()); - if (ssl_bio == NULL) { - printf("wc_BioNew failed\n"); - goto done; - } - - wc_BioSetSSL(ssl_bio, ssl, BIO_NOCLOSE); - - /* Setup accept BIO */ - if (wc_BioDoAccept(ssl_bio) <= 0) { - printf("wc_BioDoAccept failed\n"); - goto done; - } - - /* Now wait for incoming connection */ - if (wc_BioDoHandshake(ssl_bio) <= 0) { - printf("wc_BioDoHandshake failed\n"); - goto done; - } - -read_again: - idx = wc_BioRead(ssl_bio, input, sizeof(input)-1); - if (idx <= 0) { - if (wc_BioShouldRetry(ssl_bio)) { - printf("Retry read\n"); - goto read_again; - } - printf("wc_BioRead failed\n"); - goto done; - } - - input[idx] = 0; - printf("BioSrvSSL, Client message: %s\n", input); - - if (wc_BioWrite(ssl_bio, msg, sizeof(msg)) != sizeof(msg)) { - printf("wc_BioWrite failed\n"); - goto done; - } - -#ifdef WOLFSSL_TIRTOS - Task_yield(); -#endif - -done: - if (ssl_bio != 0) - wc_BioFreeAll(ssl_bio); - - wolfSSL_free(ssl); - wolfSSL_CTX_free(ctx); - - CloseSocket(clientfd); - ((func_args*)args)->return_code = TEST_SUCCESS; - -#ifdef WOLFSSL_TIRTOS - fdCloseSession(Task_self()); -#endif - -#if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \ -&& defined(HAVE_THREAD_LS) - wc_ecc_fp_free(); /* free per thread cache */ -#endif - -#ifndef WOLFSSL_TIRTOS - return 0; -#endif -} - - -static void test_client_bio_ssl(void* args) -{ - SOCKET_T sockfd = 0; - - WOLFCRYPT_BIO* ssl_bio = 0; - WOLFSSL_METHOD* method = 0; - WOLFSSL_CTX* ctx = 0; - WOLFSSL* ssl = 0; - - char msg[64] = "Client BIO_SSL, hello wolfssl!"; - char reply[1024]; - int input; - int msgSz = (int)strlen(msg); - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - - ((func_args*)args)->return_code = TEST_FAIL; - method = wolfSSLv23_client_method(); - ctx = wolfSSL_CTX_new(method); - -#ifdef OPENSSL_EXTRA - wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack); -#endif - - if (wolfSSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS) - { - /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/ - goto done2; - } - if (wolfSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM) - != SSL_SUCCESS) - { - /*err_sys("can't load client cert file, " - "Please run from wolfSSL home dir");*/ - goto done2; - } - if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM) - != SSL_SUCCESS) - { - /*err_sys("can't load client key file, " - "Please run from wolfSSL home dir");*/ - goto done2; - } - - ssl = wolfSSL_new(ctx); - tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, ssl); - wolfSSL_set_fd(ssl, sockfd); - - ssl_bio = wc_BioNew(wc_Bio_f_ssl()); - if (ssl_bio == NULL) { - printf("wc_BioNew failed\n"); - goto done2; - } - - wc_BioSetSSL(ssl_bio, ssl, BIO_NOCLOSE); - - /* start connection */ - if (wc_BioDoConnect(ssl_bio) <= 0) { - printf("wc_BioDoConnect failed\n"); - goto done2; - } - - if (wc_BioWrite(ssl_bio, msg, msgSz) != msgSz) { - printf("wc_BioWrite failed"); - goto done2; - } - -read_again: - input = wc_BioRead(ssl_bio, reply, sizeof(reply)-1); - if (input <= 0) { - if (wc_BioShouldRetry(ssl_bio)) { - printf("Retry read\n"); - goto read_again; - } - printf("wc_BioRead failed"); - goto done2; - } - - reply[input] = 0; - printf("BioCliSSL, Server response: %s\n", reply); - -done2: - if (ssl_bio != 0) - wc_BioFreeAll(ssl_bio); - - wolfSSL_free(ssl); - wolfSSL_CTX_free(ctx); - - CloseSocket(sockfd); - ((func_args*)args)->return_code = TEST_SUCCESS; - -#ifdef WOLFSSL_TIRTOS - fdCloseSession(Task_self()); -#endif - - return; -} - -#endif /* OPENSSL_EXTRA */ - - /* SNI / ALPN / session export helper functions */ #if defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLFSSL_SESSION_EXPORT) @@ -1602,179 +1019,6 @@ static void test_wolfSSL_read_write(void) } -static void test_wolfSSL_read_write_bio(void) -{ -#if defined(OPENSSL_EXTRA) -#ifdef HAVE_IO_TESTS_DEPENDENCIES - /* The unit testing for read and write shall happen simutaneously, since - * one can't do anything with one without the other. (Except for a failure - * test case.) This function will call all the others that will set up, - * execute, and report their test findings. - * - * Set up the success case first. This function will become the template - * for the other tests. This should eventually be renamed - * - * The success case isn't interesting, how can this fail? - * - Do not give the client context a CA certificate. The connect should - * fail. Do not need server for this? - * - Using NULL for the ssl object on server. Do not need client for this. - * - Using NULL for the ssl object on client. Do not need server for this. - * - Good ssl objects for client and server. Client write() without server - * read(). - * - Good ssl objects for client and server. Server write() without client - * read(). - * - Forgetting the password callback? - */ - tcp_ready ready; - func_args client_args; - func_args server_args; - THREAD_TYPE serverThread; - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - - StartTCP(); - InitTcpReady(&ready); - - server_args.signal = &ready; - client_args.signal = &ready; - - start_thread(test_server_bio, &server_args, &serverThread); - wait_tcp_ready(&server_args); - test_client_bio(&client_args); - join_thread(serverThread); - - AssertTrue(client_args.return_code); - AssertTrue(server_args.return_code); - - FreeTcpReady(&ready); - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - -#endif /* HAVE_IO_TESTS_DEPENDENCIES */ -#endif /* OPENSSL_EXTRA */ -} - -static void test_wolfSSL_read_write_bio_full(void) -{ -#if defined(OPENSSL_EXTRA) -#ifdef TEST_IPV6 - /* nothing to do */ -#else -#ifdef HAVE_IO_TESTS_DEPENDENCIES - /* The unit testing for read and write shall happen simutaneously, since - * one can't do anything with one without the other. (Except for a failure - * test case.) This function will call all the others that will set up, - * execute, and report their test findings. - * - * Set up the success case first. This function will become the template - * for the other tests. This should eventually be renamed - * - * The success case isn't interesting, how can this fail? - * - Do not give the client context a CA certificate. The connect should - * fail. Do not need server for this? - * - Using NULL for the ssl object on server. Do not need client for this. - * - Using NULL for the ssl object on client. Do not need server for this. - * - Good ssl objects for client and server. Client write() without server - * read(). - * - Good ssl objects for client and server. Server write() without client - * read(). - * - Forgetting the password callback? - */ - tcp_ready ready; - func_args client_args; - func_args server_args; - THREAD_TYPE serverThread; - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - - StartTCP(); - InitTcpReady(&ready); - - server_args.signal = &ready; - client_args.signal = &ready; - - start_thread(test_server_full_bio, &server_args, &serverThread); - wait_tcp_ready(&server_args); - test_client_full_bio(&client_args); - join_thread(serverThread); - - AssertTrue(client_args.return_code); - AssertTrue(server_args.return_code); - - FreeTcpReady(&ready); - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - -#endif /* HAVE_IO_TESTS_DEPENDENCIES */ -#endif /* TEST_IPV6 */ -#endif /* OPENSSL_EXTRA */ -} - -static void test_wolfSSL_read_write_bio_ssl(void) -{ -#if defined(OPENSSL_EXTRA) -#ifdef HAVE_IO_TESTS_DEPENDENCIES - /* The unit testing for read and write shall happen simutaneously, since - * one can't do anything with one without the other. (Except for a failure - * test case.) This function will call all the others that will set up, - * execute, and report their test findings. - * - * Set up the success case first. This function will become the template - * for the other tests. This should eventually be renamed - * - * The success case isn't interesting, how can this fail? - * - Do not give the client context a CA certificate. The connect should - * fail. Do not need server for this? - * - Using NULL for the ssl object on server. Do not need client for this. - * - Using NULL for the ssl object on client. Do not need server for this. - * - Good ssl objects for client and server. Client write() without server - * read(). - * - Good ssl objects for client and server. Server write() without client - * read(). - * - Forgetting the password callback? - */ - tcp_ready ready; - func_args client_args; - func_args server_args; - THREAD_TYPE serverThread; - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - - StartTCP(); - InitTcpReady(&ready); - - server_args.signal = &ready; - client_args.signal = &ready; - - start_thread(test_server_bio_ssl, &server_args, &serverThread); - wait_tcp_ready(&server_args); - test_client_bio_ssl(&client_args); - join_thread(serverThread); - - AssertTrue(client_args.return_code); - AssertTrue(server_args.return_code); - - FreeTcpReady(&ready); - -#ifdef WOLFSSL_TIRTOS - fdOpenSession(Task_self()); -#endif - -#endif /* HAVE_IO_TESTS_DEPENDENCIES */ -#endif /* OPENSSL_EXTRA */ -} - - static void test_wolfSSL_dtls_export(void) { #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) && \ @@ -2606,12 +1850,6 @@ void ApiTest(void) test_wolfSSL_SetTmpDH_file(); test_wolfSSL_SetTmpDH_buffer(); test_wolfSSL_read_write(); - - /* BIO tests */ - test_wolfSSL_read_write_bio_full(); - test_wolfSSL_read_write_bio(); - test_wolfSSL_read_write_bio_ssl(); - test_wolfSSL_dtls_export(); /* TLS extensions tests */ diff --git a/wolfcrypt/src/bio.c b/wolfcrypt/src/bio.c deleted file mode 100644 index 79458aab2..000000000 --- a/wolfcrypt/src/bio.c +++ /dev/null @@ -1,6352 +0,0 @@ -/* bio.c - * - * Copyright (C) 2006-2015 wolfSSL Inc. - * - * This file is part of wolfSSL. (formerly known as CyaSSL) - * - * wolfSSL is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (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 - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#ifdef HAVE_CONFIG_H - #include -#endif - -#include - -#ifdef OPENSSL_EXTRA -#include -#include - -#ifndef NO_STDIO_FILESYSTEM -#include -#endif - -#include -#ifdef HAVE_ERRNO_H -#include -#endif - -#ifdef USE_WINDOWS_API -#include -#include -#include -#include -#include -#define SHUT_RDWR SD_BOTH -#ifdef _MSC_VER -/* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */ -#pragma warning(disable: 4996) -/* 4127 warning to use check on size of component of an union */ -#pragma warning(disable: 4127) -#endif -#else /* USE_WINDOWS_API */ -#include -#include -#include -#include -#include -#ifndef SO_NOSIGPIPE -#include -#endif -#endif /* USE_WINDOWS_API */ - - -#include -#include - -#ifdef NO_INLINE -#include -#else -#define WOLFSSL_MISC_INCLUDED -#include -#endif - - -#include -#include -#include - -static int wc_BioIntToStr(int i, char *str, int strSz){ - const char digit[] = "0123456789"; - int shift, count = 0; - - if (i < 0) - return -1; - - shift = i; - - do { - ++str; - shift = shift/10; - count++; - } while(shift); - - /* check size */ - if (strSz <= count) - return -1; - - *str = '\0'; - - do { - *--str = digit[i%10]; - i = i/10; - } while(i); - - return count; -} - -WOLFCRYPT_BIO *wc_BioNew(WOLFCRYPT_BIO_METHOD *method) -{ - WOLFCRYPT_BIO *bio; - - WOLFSSL_ENTER("wc_BioNew"); - - if (method == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return NULL; - } - - bio = (WOLFCRYPT_BIO *)XMALLOC(sizeof(WOLFCRYPT_BIO),0,DYNAMIC_TYPE_OPENSSL); - if (bio == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return NULL; - } - - if (!wc_BioSet(bio, method)) { - XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL); - return NULL; - } - - if (InitMutex(&bio->refMutex) < 0) { - WOLFSSL_MSG("Mutex error on BIO init"); - return NULL; - } - - WOLFSSL_LEAVE("wc_BioNew", 1); - - return bio; -} - -int wc_BioSet(WOLFCRYPT_BIO *bio, WOLFCRYPT_BIO_METHOD *method) -{ - WOLFSSL_ENTER("wc_BioSet"); - - if (bio == NULL || method == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioSet", 0); - return 0; - } - - bio->method = method; - bio->callback = NULL; - bio->cb_arg = NULL; - bio->init = 0; - bio->shutdown = 1; - bio->flags = 0; - bio->retry_reason = 0; - bio->num = 0; - bio->ptr = NULL; - bio->prev_bio = NULL; - bio->next_bio = NULL; - bio->references = 1; - bio->num_read = 0; - bio->num_write = 0; - - if (method->create != NULL) - if (!method->create(bio)) { - WOLFSSL_ERROR(BIO_CREATE_METHOD_E); - WOLFSSL_LEAVE("wc_BioSet", 0); - return 0; - } - - WOLFSSL_LEAVE("wc_BioSet", 1); - - return 1; -} - -int wc_BioFree(WOLFCRYPT_BIO *bio) -{ - long ret; - - WOLFSSL_ENTER("wc_BioFree"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioFree", 0); - return 0; - } - - if (LockMutex(&bio->refMutex) != 0) { - WOLFSSL_MSG("Couldn't lock bio mutex"); - return 0; - } - bio->references--; - if (bio->references > 0) { - UnLockMutex(&bio->refMutex); - return 1; - } - else if (bio->references < 0) { - WOLFSSL_ERROR(BIO_BAD_REF); - WOLFSSL_MSG("wc_BioFree bad bio references"); - UnLockMutex(&bio->refMutex); - return 0; - } - UnLockMutex(&bio->refMutex); - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_FREE, NULL, 0, 0, 1); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - WOLFSSL_LEAVE("wc_BioFree", (int)ret); - return (int)(ret); - } - } - - if (bio->method != NULL && bio->method->destroy != NULL) - bio->method->destroy(bio); - - /* free refMutex */ - FreeMutex(&bio->refMutex); - - /* free bio */ - XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL); - bio = NULL; - - WOLFSSL_LEAVE("wc_BioFree", 1); - return 1; -} - -void wc_BioClearFlags(WOLFCRYPT_BIO *bio, int flags) -{ - bio->flags &= ~flags; -} - -void wc_BioSetFlags(WOLFCRYPT_BIO *bio, int flags) -{ - bio->flags |= flags; -} - -int wc_BioTestFlags(const WOLFCRYPT_BIO *bio, int flags) -{ - return (bio->flags & flags); -} - -long (*wc_BioGetCallback(const WOLFCRYPT_BIO *bio)) - (WOLFCRYPT_BIO *, int, const char *, int, long, long) -{ - return bio->callback; -} - -void wc_BioSetCallback(WOLFCRYPT_BIO *bio, - long (*cb) (WOLFCRYPT_BIO *, int, const char *, - int, long, long)) -{ - bio->callback = cb; -} - -void wc_BioSetCallbackArg(WOLFCRYPT_BIO *bio, char *arg) -{ - bio->cb_arg = arg; -} - -char *wc_BioGetCallbackArg(const WOLFCRYPT_BIO *bio) -{ - return bio->cb_arg; -} - -const char *wc_BioMethodName(const WOLFCRYPT_BIO *bio) -{ - return bio->method->name; -} - -int wc_BioMethodType(const WOLFCRYPT_BIO *bio) -{ - return bio->method->type; -} - -int wc_BioRead(WOLFCRYPT_BIO *bio, void *data, int size) -{ - long ret; - - WOLFSSL_ENTER("wc_BioRead"); - - if ((bio == NULL) || (bio->method == NULL) || - (bio->method->bread == NULL) || (data == NULL)) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioRead", -2); - return -2; - } - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_READ, (const char *)data, size, 0, 1); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - WOLFSSL_LEAVE("wc_BioRead", (int)ret); - return (int)(ret); - } - } - - if (!bio->init) { - WOLFSSL_ERROR(BIO_UNINITIALIZED_E); - WOLFSSL_LEAVE("wc_BioRead", -2); - return -2; - } - - ret = bio->method->bread(bio, (char *)data, size); - if (ret > 0) - bio->num_read += (unsigned long)ret; - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_READ | BIO_CB_RETURN, - (const char *)data, size, 0, ret); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - } - } - - WOLFSSL_LEAVE("wc_BioRead", (int)ret); - - return (int)ret; -} - -int wc_BioWrite(WOLFCRYPT_BIO *bio, const void *data, int size) -{ - long ret; - - WOLFSSL_ENTER("wc_BioWrite"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioWrite", 0); - return 0; - } - - if ((bio->method == NULL) || (bio->method->bwrite == NULL) || (data == NULL)) - { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioWrite", -2); - return -2; - } - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_WRITE, (const char *)data, size, 0, 1); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - WOLFSSL_LEAVE("wc_BioWrite", (int)ret); - return (int)(ret); - } - } - - if (!bio->init) { - WOLFSSL_ERROR(BIO_UNINITIALIZED_E); - WOLFSSL_LEAVE("wc_BioWrite", -2); - return -2; - } - - ret = bio->method->bwrite(bio, (const char *)data, size); - if (ret > 0) - bio->num_write += (unsigned long)ret; - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_WRITE | BIO_CB_RETURN, - (const char *)data, size, 0, ret); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - } - } - - WOLFSSL_LEAVE("wc_BioWrite", (int)ret); - - return (int)ret; -} - -int wc_BioPuts(WOLFCRYPT_BIO *bio, const char *data) -{ - long ret; - - WOLFSSL_ENTER("wc_BioPuts"); - - if ((bio == NULL) || (bio->method == NULL) || - (bio->method->bputs == NULL) || (data == NULL)) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioPuts", -2); - return -2; - } - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_PUTS, data, 0, 0, 1); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - WOLFSSL_LEAVE("wc_BioPuts", (int)ret); - return (int)(ret); - } - } - - if (!bio->init) { - WOLFSSL_ERROR(BIO_UNINITIALIZED_E); - WOLFSSL_LEAVE("wc_BioPuts", -2); - return -2; - } - - ret = bio->method->bputs(bio, data); - if (ret > 0) - bio->num_write += (unsigned long)ret; - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_PUTS | BIO_CB_RETURN, - data, 0, 0, ret); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - } - } - - WOLFSSL_LEAVE("wc_BioPuts", (int)ret); - - return (int)ret; -} - -int wc_BioGets(WOLFCRYPT_BIO *bio, char *data, int size) -{ - long ret; - - WOLFSSL_ENTER("wc_BioGets"); - - if ((bio == NULL) || (bio->method == NULL) || - (bio->method->bgets == NULL) || (data == NULL)) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioGets", -2); - return -2; - } - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_GETS, data, size, 0, 1); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - WOLFSSL_LEAVE("wc_BioGets", (int)ret); - return (int)(ret); - } - } - - - if (!bio->init) { - WOLFSSL_ERROR(BIO_UNINITIALIZED_E); - WOLFSSL_LEAVE("wc_BioGets", -2); - return -2; - } - - ret = bio->method->bgets(bio, data, size); - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_GETS | BIO_CB_RETURN, - data, size, 0, ret); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - } - } - - WOLFSSL_LEAVE("wc_BioGets", (int)ret); - - return (int)ret; -} - -long wc_BioCtrl(WOLFCRYPT_BIO *bio, int cmd, long larg, void *parg) -{ - long ret; - - WOLFSSL_ENTER("wc_BioCtrl"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioCtrl bio", 0); - return 0; - } - - if ((bio->method == NULL) || (bio->method->ctrl == NULL)) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioCtrl method method-ctrl", -2); - return -2; - } - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_CTRL, (const char *)parg, cmd, larg, 1); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - WOLFSSL_LEAVE("wc_BioCtrl callback", (int)ret); - return ret; - } - } - - ret = bio->method->ctrl(bio, cmd, larg, parg); - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_CTRL | BIO_CB_RETURN, - (const char *)parg, cmd, larg, ret); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - } - } - - WOLFSSL_LEAVE("wc_BioCtrl", (int)ret); - return ret; -} - -long wc_BioCallbackCtrl(WOLFCRYPT_BIO *bio, int cmd, - void (*fp) (WOLFCRYPT_BIO *, int, const char *, - int, long, long)) -{ - long ret; - - WOLFSSL_ENTER("wc_BioCallbackCtrl"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioCallbackCtrl", 0); - return 0; - } - - if ((bio->method == NULL) || (bio->method->callback_ctrl == NULL)) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioCallbackCtrl", -2); - return -2; - } - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_CTRL, (const char *)&fp, cmd, 0, 1); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - WOLFSSL_LEAVE("wc_BioCallbackCtrl", (int)ret); - return ret; - } - } - - ret = bio->method->callback_ctrl(bio, cmd, fp); - - /* callback if set */ - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_CTRL | BIO_CB_RETURN, - (const char *)&fp, cmd, 0, ret); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - } - } - - WOLFSSL_LEAVE("wc_BioCallbackCtrl", (int)ret); - return ret; -} - - -int wc_BioIndent(WOLFCRYPT_BIO *bio, int indent, int max) -{ - WOLFSSL_ENTER("wc_BioIndent"); - - if (indent < 0) - indent = 0; - if (indent > max) - indent = max; - while (indent--) - if (wc_BioPuts(bio, " ") != 1) { - WOLFSSL_ERROR(BIO_PUTS_E); - WOLFSSL_LEAVE("wc_BioIndent", 0); - return 0; - } - - WOLFSSL_LEAVE("wc_BioIndent", 1); - return 1; -} - -long wc_BioIntCtrl(WOLFCRYPT_BIO *bio, int cmd, long larg, int iarg) -{ - int i = iarg; - - return wc_BioCtrl(bio, cmd, larg, (char *)&i); -} - -char *wc_BioPtrCtrl(WOLFCRYPT_BIO *bio, int cmd, long larg) -{ - char *p = NULL; - - WOLFSSL_ENTER("wc_BioPtrCtrl"); - - if (wc_BioCtrl(bio, cmd, larg, (char *)&p) <= 0) { - WOLFSSL_ERROR(BIO_CTRL_E); - WOLFSSL_LEAVE("wc_BioPtrCtrl", 0); - return NULL; - } - - WOLFSSL_LEAVE("wc_BioPtrCtrl", 1); - - return p; -} - -size_t wc_BioCtrlPending(WOLFCRYPT_BIO *bio) -{ - return wc_BioCtrl(bio, BIO_CTRL_PENDING, 0, NULL); -} - -size_t wc_BioCtrlWpending(WOLFCRYPT_BIO *bio) -{ - return wc_BioCtrl(bio, BIO_CTRL_WPENDING, 0, NULL); -} - -/* put the 'bio' on the end of b's list of operators */ -WOLFCRYPT_BIO *wc_BioPush(WOLFCRYPT_BIO *top, WOLFCRYPT_BIO *next) -{ - WOLFCRYPT_BIO *tmp; - - WOLFSSL_ENTER("wc_BioPush"); - - if (top == NULL) { - WOLFSSL_LEAVE("wc_BioPush", 0); - return next; - } - - tmp = top; - while (tmp->next_bio != NULL) - tmp = tmp->next_bio; - tmp->next_bio = next; - if (next != NULL) - next->prev_bio = tmp; - - /* called to do internal processing */ - wc_BioCtrl(top, BIO_CTRL_PUSH, 0, tmp); - - WOLFSSL_LEAVE("wc_BioPush", 1); - - return top; -} - -/* Remove the first and return the rest */ -WOLFCRYPT_BIO *wc_BioPop(WOLFCRYPT_BIO *bio) -{ - WOLFCRYPT_BIO *ret; - - WOLFSSL_ENTER("wc_BioPop"); - - if (bio == NULL) - return NULL; - - ret = bio->next_bio; - - wc_BioCtrl(bio, BIO_CTRL_POP, 0, bio); - - if (bio->prev_bio != NULL) - bio->prev_bio->next_bio = bio->next_bio; - if (bio->next_bio != NULL) - bio->next_bio->prev_bio = bio->prev_bio; - - bio->next_bio = NULL; - bio->prev_bio = NULL; - - WOLFSSL_LEAVE("wc_BioPop", 1); - - return ret; -} - -WOLFCRYPT_BIO *wc_BioGetRetryBio(WOLFCRYPT_BIO *bio, int *reason) -{ - WOLFCRYPT_BIO *b, *last; - - WOLFSSL_ENTER("wc_BioGetRetryBio"); - - b = last = bio; - for (; b != NULL; ) { - if (!wc_BioShouldRetry(b)) - break; - last = b; - b = b->next_bio; - } - - if (reason != NULL && last != NULL) - *reason = last->retry_reason; - - WOLFSSL_LEAVE("wc_BioGetRetryBio", 1); - - return last; -} - -int wc_BioGetRetryReason(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioGetRetryReason"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioGetRetryReason", -1); - return -1; - } - - WOLFSSL_LEAVE("wc_BioGetRetryReason", (int)bio->retry_reason); - - return bio->retry_reason; -} - -WOLFCRYPT_BIO *wc_BioFindType(WOLFCRYPT_BIO *bio, int type) -{ - int mt, mask; - - WOLFSSL_ENTER("wc_BioFindType"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioFindType", -1); - return NULL; - } - - mask = type & 0xff; - do { - if (bio->method != NULL) { - mt = bio->method->type; - - if (!mask) { - if (mt & type) { - WOLFSSL_LEAVE("wc_BioFindType", type); - return bio; - } - } - else if (mt == type) { - WOLFSSL_LEAVE("wc_BioFindType", type); - return bio; - } - } - bio = bio->next_bio; - } while (bio != NULL); - - WOLFSSL_ERROR(BIO_FIND_TYPE_E); - return NULL; -} - -WOLFCRYPT_BIO *wc_BioNext(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioNext"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioNext", 0); - return NULL; - } - - WOLFSSL_LEAVE("wc_BioNext", 1); - - return bio->next_bio; -} - -void wc_BioFreeAll(WOLFCRYPT_BIO *bio) -{ - WOLFCRYPT_BIO *b; - int ref; - - WOLFSSL_ENTER("wc_BioFreeAll"); - - while (bio != NULL) { - b = bio; - ref = b->references; - bio = bio->next_bio; - wc_BioFree(b); - - if (ref > 1) - break; - } - - WOLFSSL_LEAVE("wc_BioFreeAll", 1); -} - -unsigned long wc_BioNumberRead(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioNumberRead"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioNumberRead", 0); - return 0; - } - - WOLFSSL_LEAVE("wc_BioNumberRead", (int)bio->num_read); - - return bio->num_read; -} - -unsigned long wc_BioNumberWritten(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioNumberWritten"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - WOLFSSL_LEAVE("wc_BioNumberWritten", 0); - return 0; - } - - WOLFSSL_LEAVE("wc_BioNumberWritten", (int)bio->num_write); - - return bio->num_write; -} - - -#ifndef NO_STDIO_FILESYSTEM - -#ifndef va_copy -#define va_copy(dest, src) ((dest) = (src)) -#endif - -#ifndef USE_WINDOWS_API -__attribute__((format(printf, 2, 3))) -#endif -int wc_BioPrintf(WOLFCRYPT_BIO *bio, const char *format, ...) -{ - int size, ret; - va_list args, args2; - char *buffer = NULL; - - va_start(args, format); - - /* save a copy of va_list to be able to parse 2 times */ - va_copy(args2, args); - - /* compute the required size for buffer */ -#if defined(USE_WINDOWS_API) - size = _vscprintf(format, args); -#else - size = vsnprintf(NULL, 0, format, args); -#endif - va_end(args); - - if (size <= 0) - return -1; - - buffer = (char *)XMALLOC(size+1, 0, DYNAMIC_TYPE_OPENSSL); - if (buffer == NULL) - return -1; - - XMEMSET(buffer, 0, size+1); - -#if defined(USE_WINDOWS_API) - ret = vsnprintf_s(buffer, size+1, size+1, format, args2); -#else - ret = vsnprintf(buffer, size+1, format, args2); -#endif - - va_end(args2); - - if (ret != size) { - XFREE(buffer, 0, DYNAMIC_TYPE_OPENSSL); - return -1; - } - - ret = wc_BioWrite(bio, buffer, size); - - XFREE(buffer, 0, DYNAMIC_TYPE_OPENSSL); - return ret; -} -#endif /* NO_STDIO_FILESYSTEM */ - -void wc_BioCopyNextRetry(WOLFCRYPT_BIO *bio) -{ - wc_BioSetFlags(bio, wc_BioGetRetryFlags(bio->next_bio)); - bio->retry_reason = bio->next_bio->retry_reason; -} - -WOLFCRYPT_BIO *wc_BioDupChain(WOLFCRYPT_BIO *in) -{ - WOLFCRYPT_BIO *ret = NULL, *eoc = NULL, *bio, *new_bio; - - for (bio = in; bio != NULL; bio = bio->next_bio) { - new_bio = wc_BioNew(bio->method); - if (new_bio == NULL) { - goto err; - } - new_bio->callback = bio->callback; - new_bio->cb_arg = bio->cb_arg; - new_bio->init = bio->init; - new_bio->shutdown = bio->shutdown; - new_bio->flags = bio->flags; - new_bio->num = bio->num; - - if (!wc_BioDupState(bio, new_bio)) { - wc_BioFree(new_bio); - goto err; - } - - if (ret == NULL) { - eoc = new_bio; - ret = eoc; - } else { - wc_BioPush(eoc, new_bio); - eoc = new_bio; - } - } - - return ret; - -err: - wc_BioFreeAll(ret); - return NULL; - -} - -/* start BIO Filter base64 */ - -static int wc_BioB64_write(WOLFCRYPT_BIO *bio, const char *buf, int size); -static int wc_BioB64_read(WOLFCRYPT_BIO *bio, char *buf, int size); -static int wc_BioB64_puts(WOLFCRYPT_BIO *bio, const char *str); -static long wc_BioB64_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioB64_new(WOLFCRYPT_BIO *bio); -static int wc_BioB64_free(WOLFCRYPT_BIO *bio); -static long wc_BioB64_callback_ctrl(WOLFCRYPT_BIO *bio, int cmd, - WOLFCRYPT_BIO_info_cb *fp); - -#define WOLFCRYPT_B64_BLOCK_SIZE 20*48 -#define WOLFCRYPT_B64_ENCODE_SIZE 20*64 + 40 // 40 : 20 CR LF -#define WOLFCRYPT_B64_NONE 0 -#define WOLFCRYPT_B64_ENCODE 1 -#define WOLFCRYPT_B64_DECODE 2 - - -typedef struct { - int dataLen; /* data length */ - int dataIdx; /* data index */ - int workLen; /* working buffer length */ - int workNl; /* used to stop when find a '\n' */ - int encode; /* base64 operation */ - int start; /* decoding started */ - int cont; /* <= 0 when finished */ - - char data[WOLFCRYPT_B64_ENCODE_SIZE]; - char work[WOLFCRYPT_B64_BLOCK_SIZE]; -} WOLFCRYPT_BIO_F_B64_CTX; - -static WOLFCRYPT_BIO_METHOD wc_BioB64_method = { - BIO_TYPE_BASE64, - "Base64", - wc_BioB64_write, - wc_BioB64_read, - wc_BioB64_puts, - NULL, /* gets */ - wc_BioB64_ctrl, - wc_BioB64_new, - wc_BioB64_free, - wc_BioB64_callback_ctrl, -}; - - -WOLFCRYPT_BIO_METHOD *wc_Bio_f_base64(void) -{ - return (&wc_BioB64_method); -} - -static int wc_BioB64_new(WOLFCRYPT_BIO *bio) -{ - WOLFCRYPT_BIO_F_B64_CTX *ctx; - - WOLFSSL_ENTER("wc_BioB64_new"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - bio->ptr = (WOLFCRYPT_BIO_F_B64_CTX *) - XMALLOC(sizeof(WOLFCRYPT_BIO_F_B64_CTX), - 0, DYNAMIC_TYPE_OPENSSL); - if (bio->ptr == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return 0; - } - - ctx = (WOLFCRYPT_BIO_F_B64_CTX *)bio->ptr; - - ctx->dataLen = 0; - ctx->workLen = 0; - ctx->workNl = 0; - ctx->dataIdx = 0; - ctx->cont = 1; - ctx->start = 1; - ctx->encode = 0; - - bio->init = 1; - bio->flags = 0; - bio->num = 0; - - WOLFSSL_LEAVE("wc_BioB64_new", 1); - return 1; -} - -static int wc_BioB64_free(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioB64_free"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - if (bio->ptr != NULL) { - XFREE(bio->ptr, 0, DYNAMIC_TYPE_OPENSSL); - bio->ptr = NULL; - } - - bio->init = 0; - bio->flags = 0; - - WOLFSSL_LEAVE("wc_BioB64_free", 1); - - return 1; -} - - -static int wc_BioB64_read(WOLFCRYPT_BIO *bio, char *data, int size) -{ - int ret = 0, idx, bread, j, k, num, ret_code = 0; - WOLFCRYPT_BIO_F_B64_CTX *ctx; - - WOLFSSL_ENTER("wc_BioB64_read"); - - if (bio == NULL || !bio->init || bio->ptr == NULL || - bio->next_bio == NULL || data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - wc_BioClearRetryFlags(bio); - - ctx = (WOLFCRYPT_BIO_F_B64_CTX *)bio->ptr; - - /* decode when reading */ - if (ctx->encode != WOLFCRYPT_B64_DECODE) { - ctx->encode = WOLFCRYPT_B64_DECODE; - ctx->dataLen = 0; - ctx->dataIdx = 0; - ctx->workLen = 0; - } - - /* First check if there are bytes decoded/encoded */ - if (ctx->dataLen > 0) { - if (ctx->dataLen < ctx->dataIdx) { - WOLFSSL_LEAVE("wc_BioB64_read", -1); - return -1; - } - - bread = ctx->dataLen - ctx->dataIdx; - if (bread > size) - bread = size; - - if (ctx->dataIdx + bread >= (int)sizeof(ctx->data)) { - WOLFSSL_LEAVE("wc_BioB64_read", -1); - return -1; - } - - XMEMCPY(data, &(ctx->data[ctx->dataIdx]), bread); - - ret = bread; - data += bread; - size -= bread; - ctx->dataIdx += bread; - - if (ctx->dataLen == ctx->dataIdx) { - ctx->dataLen = 0; - ctx->dataIdx = 0; - } - } - - /* - * At this point, we have room of size bytes and an empty buffer, so we - * should read in some more. - */ - - ret_code = 0; - idx = 0; - - while (size > 0) { - if (ctx->cont <= 0) - break; - - bread = wc_BioRead(bio->next_bio, &ctx->work[ctx->workLen], - sizeof(ctx->work) - ctx->workLen); - - if (bread <= 0) { - ret_code = bread; - - /* Should we continue next time we are called? */ - if (!wc_BioShouldRetry(bio->next_bio)) { - ctx->cont = bread; - /* If buffer empty break */ - if (!ctx->workLen) - break; - else - bread = 0; - } - /* else we retry and add more data to buffer */ - else - break; - } - - bread += ctx->workLen; - ctx->workLen = bread; - - /* - * We need to scan, a line at a time until we have a valid line if we - * are starting. - */ - if (ctx->start && (wc_BioGetFlags(bio) & BIO_FLAGS_BASE64_NO_NL)) - ctx->workLen = 0; - else if (ctx->start) { - /* search \n */ - ctx->workNl = -1; - - /* parse working buffer to find line endings */ - for (j = 0; j < bread; j++) { - - /* no end of line, continue */ - if (ctx->work[j] != '\n') - continue; - - /* we found an end of line, keep the position to - * decode the line */ - ctx->workNl = j; - - /* decode the line found */ - num = sizeof(ctx->data) - ctx->dataIdx; - - k = Base64_Decode((const byte*)ctx->work+idx,ctx->workNl-idx, - (byte *)ctx->data+ctx->dataIdx, (word32 *)&num); - if (k < 0 && !num && ctx->start) { - WOLFSSL_ERROR(BIO_B64_DECODE_E); - return -1; - } - else - ctx->start = 0; - - /* +1 => skip \n */ - idx = (ctx->workNl + 1); - - ctx->dataLen += num; - ctx->dataIdx += num; - } - } else if ((bread < WOLFCRYPT_B64_BLOCK_SIZE) && (ctx->cont > 0)) { - /* - * If buffer isn't full and we can retry then restart to read in - * more data. - */ - continue; - } - - if (wc_BioGetFlags(bio) & BIO_FLAGS_BASE64_NO_NL) { - int z, jj; - - jj = bread & ~3; - - z = sizeof(ctx->data); - k = Base64_Decode((const byte*)ctx->work, jj, - (byte *)ctx->data, (word32 *)&z); - if (k < 0 || !z) { - WOLFSSL_ERROR(BIO_B64_DECODE_E); - return -1; - } - - /* z is now number of output bytes and jj is the number consumed - */ - if (jj != bread) { - ctx->workLen = bread - jj; - XMEMMOVE(ctx->work, &ctx->work[jj], ctx->workLen); - } - - if (z > 0) - ctx->dataLen = z; - else - ctx->dataLen = 0; - - bread = z; - } - - ctx->dataIdx = 0; - if (bread < 0) { - ret_code = 0; - ctx->dataLen = 0; - break; - } - - if (!(wc_BioGetFlags(bio) & BIO_FLAGS_BASE64_NO_NL)) { - /* keep no parsed data in working buffer */ - XMEMMOVE(ctx->work, ctx->work+idx, ctx->workLen-idx); - ctx->workLen -= idx; - idx = 0; - ctx->start = 1; - } - - bread = (ctx->dataLen <= size ? ctx->dataLen : size); - - XMEMCPY(data, ctx->data, bread); - ret += bread; - - if (bread == ctx->dataLen) { - ctx->dataLen = 0; - ctx->dataIdx = 0; - } - else - ctx->dataIdx = bread; - - size -= bread; - data += bread; - } - - wc_BioCopyNextRetry(bio); - - WOLFSSL_LEAVE("wc_BioB64_read", (!ret ? ret_code : ret)); - - return (!ret ? ret_code : ret); -} - -static int wc_BioB64_write(WOLFCRYPT_BIO *bio, const char *data, int size) -{ - int ret = 0; - int n; - int i; - WOLFCRYPT_BIO_F_B64_CTX *ctx; - - WOLFSSL_ENTER("wc_BioB64_write"); - - if (bio == NULL || !bio->init || bio->ptr == NULL || - bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - wc_BioClearRetryFlags(bio); - - ctx = (WOLFCRYPT_BIO_F_B64_CTX *)bio->ptr; - - /* encode when writing */ - if (ctx->encode != WOLFCRYPT_B64_ENCODE) { - ctx->encode = WOLFCRYPT_B64_ENCODE; - ctx->dataLen = 0; - ctx->dataIdx = 0; - ctx->workLen = 0; - } - - if (ctx->dataIdx >= (int)sizeof(ctx->data) || - ctx->dataLen > (int)sizeof(ctx->data) || - ctx->dataLen < ctx->dataIdx) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - - n = ctx->dataLen - ctx->dataIdx; - while (n > 0) { - i = wc_BioWrite(bio->next_bio, &ctx->data[ctx->dataIdx], n); - if (i <= 0) { - wc_BioCopyNextRetry(bio); - return i; - } - - /* mustn't appen, just to be sure */ - if (i > n) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - - ctx->dataIdx += i; - n -= i; - - if (ctx->dataIdx > (int)sizeof(ctx->data) || - ctx->dataLen < ctx->dataIdx) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - } - - /* at this point all pending data has been written */ - ctx->dataIdx = 0; - ctx->dataLen = 0; - - if (data == NULL || size <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - while (size > 0) { - n = (size > WOLFCRYPT_B64_BLOCK_SIZE) ? WOLFCRYPT_B64_BLOCK_SIZE : size; - - if (ctx->workLen > 0) { - if (ctx->workLen > WOLFCRYPT_B64_BLOCK_SIZE) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - - n = WOLFCRYPT_B64_BLOCK_SIZE - ctx->workLen; - - if (n > size) - n = size; - - XMEMCPY(&ctx->work[ctx->workLen], data, n); - ctx->workLen += n; - ret += n; - if (ctx->workLen < WOLFCRYPT_B64_BLOCK_SIZE) - break; - - ctx->dataLen = sizeof(ctx->data); - - if (wc_BioGetFlags(bio) & BIO_FLAGS_BASE64_NO_NL) - Base64_Encode_NoNl((const byte *)ctx->work, ctx->workLen, - (byte *)ctx->data, - (word32 *)&ctx->dataLen); - else - Base64_Encode((const byte *)ctx->work, ctx->workLen, - (byte *)ctx->data, (word32 *)&ctx->dataLen); - - if (ctx->dataLen > (int)sizeof(ctx->data) || - ctx->dataLen < ctx->dataIdx) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - - ctx->workLen = 0; - } - else { - /* keep data and wait for more before encoding */ - if (n < WOLFCRYPT_B64_BLOCK_SIZE) { - XMEMCPY(ctx->work, data, n); - ctx->workLen = n; - ret += n; - break; - } - n -= n % WOLFCRYPT_B64_BLOCK_SIZE; - - ctx->dataLen = sizeof(ctx->data); - - if (wc_BioGetFlags(bio) & BIO_FLAGS_BASE64_NO_NL) - Base64_Encode_NoNl((const byte *)data, n, - (byte *)ctx->data, - (word32 *)&ctx->dataLen); - else - Base64_Encode((const byte *)data, n, - (byte *)ctx->data, (word32 *)&ctx->dataLen); - - if (ctx->dataLen > (int)sizeof(ctx->data) || - ctx->dataLen < ctx->dataIdx) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - - ret += n; - } - - size -= n; - data += n; - - ctx->dataIdx = 0; - n = ctx->dataLen; - while (n > 0) { - i = wc_BioWrite(bio->next_bio, &(ctx->data[ctx->dataIdx]), n); - if (i <= 0) { - wc_BioCopyNextRetry(bio); - WOLFSSL_LEAVE("wc_BioB64_write", !ret ? i : ret); - return (!ret ? i : ret); - } - - if (i > n) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - - n -= i; - ctx->dataIdx += i; - - if (ctx->dataLen > (int)sizeof(ctx->data) || - ctx->dataLen < ctx->dataIdx) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - } - - ctx->dataLen = 0; - ctx->dataIdx = 0; - } - - WOLFSSL_LEAVE("wc_BioB64_write", ret); - - return ret; -} - -static long wc_BioB64_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - WOLFCRYPT_BIO_F_B64_CTX *ctx; - long ret = 1; - int i; - - WOLFSSL_ENTER("wc_BioB64_ctrl"); - - if (bio == NULL || bio->ptr == NULL || bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - ctx = (WOLFCRYPT_BIO_F_B64_CTX *)bio->ptr; - - switch (cmd) { - case BIO_CTRL_RESET: - ctx->cont = 1; - ctx->start = 1; - ctx->encode = WOLFCRYPT_B64_NONE; - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_CTRL_EOF: - ret = (ctx->cont <= 0 ? 1 : - wc_BioCtrl(bio->next_bio, cmd, num, ptr)); - break; - - case BIO_CTRL_WPENDING: - if (ctx->dataLen < ctx->dataIdx) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - - ret = ctx->dataLen - ctx->dataIdx; - if (!ret && (ctx->encode != WOLFCRYPT_B64_NONE) && - (ctx->workLen != 0)) - ret = 1; - else if (ret <= 0) - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_CTRL_PENDING: - if (ctx->dataLen < ctx->dataIdx) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - - ret = ctx->dataLen - ctx->dataIdx; - if (ret <= 0) - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_CTRL_FLUSH: - /* do a final write */ - again: - while (ctx->dataLen != ctx->dataIdx) { - i = wc_BioB64_write(bio, NULL, 0); - if (i < 0) - return i; - } - - if (ctx->workLen != 0) { - ctx->dataLen = sizeof(ctx->data); - - if (wc_BioGetFlags(bio) & BIO_FLAGS_BASE64_NO_NL) - Base64_Encode_NoNl((const byte *)ctx->work, ctx->workLen, - (byte *)ctx->data, - (word32 *)&ctx->dataLen); - else { - Base64_Encode((const byte *)ctx->work, ctx->workLen, - (byte *)ctx->data, (word32 *)&ctx->dataLen); - - if (ctx->dataLen > (int)sizeof(ctx->data)) { - WOLFSSL_LEAVE("wc_BioB64_write", -1); - return -1; - } - } - - ctx->dataIdx = 0; - ctx->workLen = 0; - - goto again; - } - - /* Finally flush the underlying BIO */ - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_C_DO_STATE_MACHINE: - wc_BioClearRetryFlags(bio); - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - wc_BioCopyNextRetry(bio); - break; - - case BIO_CTRL_DUP: - break; - - case BIO_CTRL_INFO: - case BIO_CTRL_GET: - case BIO_CTRL_SET: - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - default: - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - } - - WOLFSSL_LEAVE("wc_BioB64_ctrl", (int)ret); - return ret; -} - -static long wc_BioB64_callback_ctrl(WOLFCRYPT_BIO *bio, - int cmd, WOLFCRYPT_BIO_info_cb *fp) -{ - if (bio == NULL || bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - WOLFSSL_ENTER("wc_BioB64_callback_ctrl"); - - return wc_BioCallbackCtrl(bio->next_bio, cmd, fp); -} - -static int wc_BioB64_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - WOLFSSL_ENTER("wc_BioB64_puts"); - - if (bio == NULL || str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - return wc_BioB64_write(bio, str, (int)XSTRLEN(str)); -} - -/* end BIO Filter base64 */ - -/* start BIO Filter buffer */ - -typedef struct { - /*- - * Buffers are setup like this: - * - * <---------------------- size -----------------------> - * +---------------------------------------------------+ - * | consumed | remaining | free space | - * +---------------------------------------------------+ - * <-- off --><------- len -------> - */ - - /* input buffer */ - char *in; /* the char array */ - int inSz; /* how big is the input buffer */ - int inLen; /* how many bytes are in it */ - int inIdx; /* write/read offset */ - - /* output buffer */ - char *out; /* the char array */ - int outSz; /* how big is the output buffer */ - int outLen; /* how many bytes are in it */ - int outIdx; /* write/read offset */ - -} WOLFCRYPT_BIO_F_BUFFER_CTX; - -/* OpenSSL default value */ -#define WOLFSSL_F_BUFFER_SIZE_DEFAULT 4096 - -static int wc_BioBuffer_write(WOLFCRYPT_BIO *bio, const char *buf, int size); -static int wc_BioBuffer_read(WOLFCRYPT_BIO *bio, char *buf, int size); -static int wc_BioBuffer_puts(WOLFCRYPT_BIO *bio, const char *str); -static int wc_BioBuffer_gets(WOLFCRYPT_BIO *bio, char *buf, int size); -static long wc_BioBuffer_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioBuffer_new(WOLFCRYPT_BIO *bio); -static int wc_BioBuffer_free(WOLFCRYPT_BIO *bio); -static long wc_BioBuffer_callback_ctrl(WOLFCRYPT_BIO *bio, int cmd, - WOLFCRYPT_BIO_info_cb *fp); - -static WOLFCRYPT_BIO_METHOD wc_BioBuffer_method = { - BIO_TYPE_BUFFER, - "Buffer", - wc_BioBuffer_write, - wc_BioBuffer_read, - wc_BioBuffer_puts, - wc_BioBuffer_gets, - wc_BioBuffer_ctrl, - wc_BioBuffer_new, - wc_BioBuffer_free, - wc_BioBuffer_callback_ctrl, -}; - - -static long wc_BioBuffer_callback_ctrl(WOLFCRYPT_BIO *bio, int cmd, - WOLFCRYPT_BIO_info_cb *fp) -{ - if (bio == NULL || bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - return wc_BioCallbackCtrl(bio->next_bio, cmd, fp); -} - -WOLFCRYPT_BIO_METHOD *wc_Bio_f_buffer(void) -{ - return (&wc_BioBuffer_method); -} - -static int wc_BioBuffer_new(WOLFCRYPT_BIO *bio) -{ - WOLFCRYPT_BIO_F_BUFFER_CTX *ctx; - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - bio->ptr = (WOLFCRYPT_BIO_F_BUFFER_CTX *) - XMALLOC(sizeof(WOLFCRYPT_BIO_F_BUFFER_CTX), - 0, DYNAMIC_TYPE_OPENSSL); - if (bio->ptr == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return 0; - } - - ctx = (WOLFCRYPT_BIO_F_BUFFER_CTX *)bio->ptr; - - ctx->in = (char *)XMALLOC(WOLFSSL_F_BUFFER_SIZE_DEFAULT, 0, - DYNAMIC_TYPE_OPENSSL); - if (ctx->in == NULL) { - XFREE(bio->ptr, 0, DYNAMIC_TYPE_OPENSSL); - WOLFSSL_ERROR(MEMORY_E); - return 0; - } - - ctx->out = (char *)XMALLOC(WOLFSSL_F_BUFFER_SIZE_DEFAULT, 0, - DYNAMIC_TYPE_OPENSSL); - if (ctx->out == NULL) { - XFREE(ctx->in, 0, DYNAMIC_TYPE_OPENSSL); - XFREE(bio->ptr, 0, DYNAMIC_TYPE_OPENSSL); - WOLFSSL_ERROR(MEMORY_E); - return 0; - } - - ctx->inSz = WOLFSSL_F_BUFFER_SIZE_DEFAULT; - ctx->inLen = 0; - ctx->inIdx = 0; - ctx->outSz = WOLFSSL_F_BUFFER_SIZE_DEFAULT; - ctx->outLen = 0; - ctx->outIdx = 0; - - bio->init = 1; - bio->flags = 0; - return 1; -} - -static int wc_BioBuffer_free(WOLFCRYPT_BIO *bio) -{ - WOLFCRYPT_BIO_F_BUFFER_CTX *ctx; - - WOLFSSL_ENTER("wc_BioBuffer_free"); - - if (bio == NULL) - return 0; - - if (!bio->init || bio->ptr == NULL) - return 1; - - ctx = (WOLFCRYPT_BIO_F_BUFFER_CTX *)bio->ptr; - - if (ctx->in != NULL) { - XFREE(ctx->in, 0, DYNAMIC_TYPE_OPENSSL); - ctx->in = NULL; - } - - if (ctx->out != NULL) { - XFREE(ctx->out, 0, DYNAMIC_TYPE_OPENSSL); - ctx->out = NULL; - } - - XFREE(bio->ptr, 0, DYNAMIC_TYPE_OPENSSL); - bio->ptr = NULL; - - bio->init = 0; - bio->flags = 0; - return 1; -} - -static int wc_BioBuffer_read(WOLFCRYPT_BIO *bio, char *data, int size) -{ - int i, num = 0; - WOLFCRYPT_BIO_F_BUFFER_CTX *ctx; - - if (bio == NULL || !bio->init || - bio->ptr == NULL || bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - wc_BioClearRetryFlags(bio); - - ctx = (WOLFCRYPT_BIO_F_BUFFER_CTX *)bio->ptr; - - for (;;) { - i = ctx->inLen; - if (i != 0) { - if (i > size) - i = size; - XMEMCPY(data, &(ctx->in[ctx->inIdx]), i); - ctx->inIdx += i; - ctx->inLen -= i; - num += i; - if (size == i) - return num; - size -= i; - data += i; - } - - /* case of partial read */ - if (size > ctx->inSz) { - for (;;) { - i = wc_BioRead(bio->next_bio, data, size); - if (i <= 0) { - wc_BioCopyNextRetry(bio); - if (i < 0) - return (num > 0 ? num : i); - else if (i == 0) - return num; - } - num += i; - - if (size == i) - return num; - data += i; - size -= i; - } - } - - /* we are going to be doing some buffering */ - i = wc_BioRead(bio->next_bio, ctx->in, ctx->inSz); - if (i <= 0) { - wc_BioCopyNextRetry(bio); - if (i < 0) - return (num > 0 ? num : i); - if (i == 0) - return num; - } - ctx->inIdx = 0; - ctx->inLen = i; - } - -#ifndef USE_WINDOWS_API - return 1; -#endif -} - -static int wc_BioBuffer_write(WOLFCRYPT_BIO *bio, - const char *data, int size) -{ - int i, num = 0; - WOLFCRYPT_BIO_F_BUFFER_CTX *ctx; - - if (bio == NULL || !bio->init || bio->ptr == NULL || - bio->next_bio == NULL || size <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - wc_BioClearRetryFlags(bio); - - ctx = (WOLFCRYPT_BIO_F_BUFFER_CTX *)bio->ptr; - - for (;;) { - i = ctx->outSz - (ctx->outLen + ctx->outIdx); - - /* add to buffer and return */ - if (i >= size) { - XMEMCPY(&(ctx->out[ctx->outIdx + ctx->outLen]), data, size); - ctx->outLen += size; - return (num + size); - } - - /* stuff already in buffer, so add to it first, then flush */ - if (ctx->outLen != 0) { - if (i > 0) { - XMEMCPY(&(ctx->out[ctx->outIdx + ctx->outLen]), data, i); - data += i; - size -= i; - num += i; - ctx->outLen += i; - } - - /* we now have a full buffer needing flushing */ - do { - i = wc_BioWrite(bio->next_bio, - &(ctx->out[ctx->outIdx]), ctx->outLen); - if (i <= 0) { - wc_BioCopyNextRetry(bio); - - if (i < 0) - return (num > 0 ? num : i); - if (i == 0) - return num; - } - - ctx->outIdx += i; - ctx->outLen -= i; - - } while (ctx->outLen != 0); - } - - ctx->outIdx = 0; - - /* we now have size bytes to write */ - while (size >= ctx->outSz) { - i = wc_BioWrite(bio->next_bio, data, size); - if (i <= 0) { - wc_BioCopyNextRetry(bio); - if (i < 0) - return (num > 0 ? num : i); - if (i == 0) - return num; - } - num += i; - data += i; - size -= i; - if (size == 0) - return num; - } - } - -#ifndef USE_WINDOWS_API - return 1; -#endif -} - -static long wc_BioBuffer_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - - int i, *ip, ibs, obs; - long ret = 1; - WOLFCRYPT_BIO_F_BUFFER_CTX *ctx; - - if (bio == NULL || bio->ptr == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - ctx = (WOLFCRYPT_BIO_F_BUFFER_CTX *)bio->ptr; - - switch (cmd) { - case BIO_CTRL_RESET: - ctx->inLen = 0; - ctx->inIdx = 0; - ctx->outLen = 0; - ctx->outIdx = 0; - - if (bio->next_bio == NULL) - return 0; - - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_CTRL_INFO: - ret = (long)ctx->outLen; - break; - - case BIO_C_GET_BUFF_NUM_LINES: - ret = 0; - for (i = 0; i < ctx->inLen; i++) { - if (ctx->in[ctx->inIdx + i] == '\n') - ret++; - } - break; - - case BIO_CTRL_WPENDING: - ret = (long)ctx->outLen; - if (ret == 0) { - if (bio->next_bio == NULL) - return 0; - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - } - break; - - case BIO_CTRL_PENDING: - ret = (long)ctx->inLen; - if (ret == 0) { - if (bio->next_bio == NULL) - return 0; - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - } - break; - - case BIO_C_SET_BUFF_READ_DATA: - if (num > ctx->inSz) { - ctx->in = (char *)XREALLOC(ctx->in, num, - 0, DYNAMIC_TYPE_OPENSSL); - if (ctx->in == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return 0; - } - } - - ctx->inIdx = 0; - ctx->inLen = (int)num; - XMEMCPY(ctx->in, ptr, num); - ret = 1; - break; - - case BIO_C_SET_BUFF_SIZE: - if (ptr != NULL) { - ip = (int *)ptr; - if (*ip == 0) { - ibs = (int)num; - obs = ctx->outSz; - } else { - ibs = ctx->inSz; - obs = (int)num; - } - } else { - ibs = (int)num; - obs = (int)num; - } - - if ((ibs > WOLFSSL_F_BUFFER_SIZE_DEFAULT) && (ibs != ctx->inSz)) { - ctx->in = (char *)XREALLOC(ctx->in, num, - 0, DYNAMIC_TYPE_OPENSSL); - if (ctx->in == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return 0; - } - - ctx->inIdx = 0; - ctx->inLen = 0; - ctx->inSz = ibs; - } - - if ((obs > WOLFSSL_F_BUFFER_SIZE_DEFAULT) && (obs != ctx->outSz)) { - ctx->out = (char *)XREALLOC(ctx->out, num, - 0, DYNAMIC_TYPE_OPENSSL); - if (ctx->out == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return 0; - } - - ctx->outIdx = 0; - ctx->outLen = 0; - ctx->outSz = obs; - } - break; - - case BIO_C_DO_STATE_MACHINE: - if (bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - wc_BioClearRetryFlags(bio); - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - wc_BioCopyNextRetry(bio); - break; - - case BIO_CTRL_FLUSH: - if (bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - if (ctx->outLen <= 0) { - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - } - - for (;;) { - wc_BioClearRetryFlags(bio); - if (ctx->outLen > 0) { - ret = wc_BioWrite(bio->next_bio, - &(ctx->out[ctx->outIdx]), ctx->outLen); - wc_BioCopyNextRetry(bio); - if (ret <= 0) - return ret; - ctx->outIdx += ret; - ctx->outLen -= ret; - } else { - ctx->outLen = 0; - ctx->outIdx = 0; - break; - } - } - - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_CTRL_DUP: - ret = wc_BioSetReadBufferSize((WOLFCRYPT_BIO *)ptr, ctx->inSz); - if (!ret) - break; - - ret = wc_BioSetWriteBufferSize((WOLFCRYPT_BIO *)ptr, ctx->outSz); - if (!ret) - break; - - ret = 1; - break; - - default: - if (bio->next_bio == NULL) - return 0; - - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - } - - return ret; -} - -static int wc_BioBuffer_gets(WOLFCRYPT_BIO *bio, char *buf, int size) -{ - WOLFCRYPT_BIO_F_BUFFER_CTX *ctx; - int num = 0, i, flag; - - if (bio == NULL || bio->ptr == NULL || buf == NULL || size <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - wc_BioClearRetryFlags(bio); - - ctx = (WOLFCRYPT_BIO_F_BUFFER_CTX *)bio->ptr; - - /* to put end of string */ - size--; - - for (;;) { - if (ctx->inLen > 0) { - flag = 0; - - for (i = 0; (i < ctx->inLen) && (i < size); i++) { - *(buf++) = ctx->in[ctx->inIdx+i]; - if (ctx->in[ctx->inIdx+i] == '\n') { - flag = 1; - i++; - break; - } - } - num += i; - size -= i; - ctx->inLen -= i; - ctx->inIdx += i; - if (flag || !size) { - *buf = '\0'; - return num; - } - } else { - i = wc_BioRead(bio->next_bio, ctx->in, ctx->inSz); - if (i <= 0) { - wc_BioCopyNextRetry(bio); - *buf = '\0'; - if (i < 0) - return (num > 0 ? num : i); - if (i == 0) - return num; - } - ctx->inLen = i; - ctx->inIdx = 0; - } - } - -#ifndef USE_WINDOWS_API - return i; -#endif -} - -static int wc_BioBuffer_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - if (bio == NULL || str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - return wc_BioBuffer_write(bio, str, (int)XSTRLEN(str)); -} - -/* end BIO Filter buffer */ - -/* start BIO Filter cipher */ - -typedef struct { - int dataLen; - int dataIdx; - int cont; - int finished; - int ok; /* bad decrypt */ - - WOLFCRYPT_EVP_CIPHER_CTX cipher; - /* - * buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate can return - * up to a block more data than is presented to it - */ - -#define WOLFCRYPT_ENC_BLOCK_SIZE 128 -#define WOLFCRYPT_BUF_OFFSET 64 - - byte data[WOLFCRYPT_ENC_BLOCK_SIZE + WOLFCRYPT_BUF_OFFSET + 2]; -} WOLFCRYPT_BIO_ENC_CTX; - - -static int wc_BioCipher_write(WOLFCRYPT_BIO *bio, const char *buf, int size); -static int wc_BioCipher_read(WOLFCRYPT_BIO *bio, char *buf, int size); -static long wc_BioCipher_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioCipher_new(WOLFCRYPT_BIO *bio); -static int wc_BioCipher_free(WOLFCRYPT_BIO *bio); -static long wc_BioCipher_callback_ctrl(WOLFCRYPT_BIO *bio, int cmd, - WOLFCRYPT_BIO_info_cb *fp); - -static WOLFCRYPT_BIO_METHOD wc_BioCipher_method = { - BIO_TYPE_CIPHER, - "Cipher", - wc_BioCipher_write, - wc_BioCipher_read, - NULL, /* puts */ - NULL, /* gets */ - wc_BioCipher_ctrl, - wc_BioCipher_new, - wc_BioCipher_free, - wc_BioCipher_callback_ctrl, -}; - -static long wc_BioCipher_callback_ctrl(WOLFCRYPT_BIO *bio, int cmd, - WOLFCRYPT_BIO_info_cb *fp) -{ - WOLFSSL_ENTER("wc_BioCipher_callback_ctrl"); - if (bio == NULL || bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - return wc_BioCallbackCtrl(bio->next_bio, cmd, fp); -} - -WOLFCRYPT_BIO_METHOD *wc_Bio_f_cipher(void) -{ - WOLFSSL_ENTER("WOLFCRYPT_BIO_f_cipher"); - return (&wc_BioCipher_method); -} - -void wc_BioSetCipher(WOLFCRYPT_BIO *bio, const WOLFCRYPT_EVP_CIPHER *cipher, - const unsigned char *key, const unsigned char *iv, int enc) -{ - WOLFCRYPT_BIO_ENC_CTX *ctx; - - WOLFSSL_ENTER("wc_BioSetCipher"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return; - } - - - if ((bio->callback != NULL) && - bio->callback(bio, BIO_CB_CTRL, (const char *)cipher, - BIO_CTRL_SET, enc, 0) <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - return; - } - - bio->init = 1; - - ctx = (WOLFCRYPT_BIO_ENC_CTX *)bio->ptr; - - wc_EVP_CipherInit(&(ctx->cipher), cipher, (unsigned char *)key, - (unsigned char *)iv, enc); - - if ((bio->callback != NULL) && - bio->callback(bio, BIO_CB_CTRL, (const char *)cipher, - BIO_CTRL_SET, enc, 1) <= 0) { - WOLFSSL_ERROR(BIO_CALLBACK_E); - } -} - -static int wc_BioCipher_new(WOLFCRYPT_BIO *bio) -{ - WOLFCRYPT_BIO_ENC_CTX *ctx; - - WOLFSSL_ENTER("wc_BioCipher_new"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - bio->ptr = (WOLFCRYPT_BIO_ENC_CTX *)XMALLOC(sizeof(WOLFCRYPT_BIO_ENC_CTX), - 0, DYNAMIC_TYPE_OPENSSL); - if (bio->ptr == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return -1; - } - - ctx = (WOLFCRYPT_BIO_ENC_CTX *)bio->ptr; - - wc_EVP_CIPHER_CTX_init(&ctx->cipher); - - ctx->dataLen = 0; - ctx->dataIdx = 0; - ctx->cont = 1; - ctx->finished = 0; - ctx->ok = 1; - - bio->init = 0; - bio->flags = 0; - - WOLFSSL_LEAVE("wc_BioCipher_new", 1); - return 1; -} - -static int wc_BioCipher_free(WOLFCRYPT_BIO *bio) -{ - WOLFCRYPT_BIO_ENC_CTX *ctx; - - WOLFSSL_ENTER("wc_BioCipher_free"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - ctx = (WOLFCRYPT_BIO_ENC_CTX *)bio->ptr; - - wc_EVP_CIPHER_CTX_cleanup(&(ctx->cipher)); - - XMEMSET(bio->ptr, 0, sizeof(WOLFCRYPT_BIO_ENC_CTX)); - XFREE(bio->ptr, 0, DYNAMIC_TYPE_OPENSSL); - bio->ptr = NULL; - - bio->init = 0; - bio->flags = 0; - - WOLFSSL_LEAVE("wc_BioCipher_free", 1); - return 1; -} - -static int wc_BioCipher_read(WOLFCRYPT_BIO *bio, char *data, int size) -{ - int ret = 0, i; - WOLFCRYPT_BIO_ENC_CTX *ctx; - - WOLFSSL_ENTER("wc_BioCipher_read"); - - if (bio == NULL || data == NULL || - bio->ptr == NULL || bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - ctx = (WOLFCRYPT_BIO_ENC_CTX *)bio->ptr; - - /* First check if there are bytes decoded/encoded */ - if (ctx->dataLen > 0) { - i = ctx->dataLen - ctx->dataIdx; - if (i > size) - i = size; - - XMEMCPY(data, &ctx->data[ctx->dataIdx], i); - ret = i; - data += i; - size -= i; - ctx->dataIdx += i; - - /* all read */ - if (ctx->dataLen == ctx->dataIdx) - ctx->dataLen = ctx->dataIdx = 0; - } - - /* - * At this point, we have room of size bytes and an empty buffer, so we - * should read in some more. - */ - while (size > 0) { - if (ctx->cont <= 0) - break; - - /* read in at IV offset, read the EVP_Cipher documentation about why - */ - i = wc_BioRead(bio->next_bio, &ctx->data[WOLFCRYPT_BUF_OFFSET], - WOLFCRYPT_ENC_BLOCK_SIZE); - if (i <= 0) { - /* Should be continue next time we are called ? */ - if (!wc_BioShouldRetry(bio->next_bio)) { - ctx->cont = i; - - i = wc_EVP_CipherFinal(&ctx->cipher, ctx->data, - &ctx->dataLen); - - ctx->ok = i; - ctx->dataIdx = 0; - } else { - if (!ret) - ret = i; - break; - } - } else { - wc_EVP_CipherUpdate(&ctx->cipher, - ctx->data, &ctx->dataLen, - &ctx->data[WOLFCRYPT_BUF_OFFSET], i); - ctx->cont = 1; - - if (!ctx->dataLen) - continue; - } - - i = (ctx->dataLen <= size ? ctx->dataLen : size); - if (i <= 0) - break; - - XMEMCPY(data, ctx->data, i); - - ret += i; - ctx->dataIdx = i; - size -= i; - data += i; - } - - wc_BioClearRetryFlags(bio); - wc_BioCopyNextRetry(bio); - - return (!ret ? ctx->cont : ret); -} - -static int wc_BioCipher_write(WOLFCRYPT_BIO *bio, - const char *data, int size) -{ - int ret, n, i; - WOLFCRYPT_BIO_ENC_CTX *ctx; - - WOLFSSL_ENTER("wc_BioCipher_write"); - - if (bio == NULL || bio->ptr == NULL || bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - ctx = (WOLFCRYPT_BIO_ENC_CTX *)bio->ptr; - - ret = size; - - wc_BioClearRetryFlags(bio); - - n = ctx->dataLen - ctx->dataIdx; - while (n > 0) { - i = wc_BioWrite(bio->next_bio, &ctx->data[ctx->dataIdx], n); - if (i <= 0) { - wc_BioCopyNextRetry(bio); - return i; - } - ctx->dataIdx += i; - n -= i; - } - - /* at this point all pending data has been written - * return if we haven't new data to write */ - if (data == NULL || size <= 0) - return 0; - - while (size > 0) { - n = (size > WOLFCRYPT_ENC_BLOCK_SIZE ? WOLFCRYPT_ENC_BLOCK_SIZE : size); - wc_EVP_CipherUpdate(&ctx->cipher, ctx->data, &ctx->dataLen, - (byte *)data, n); - - size -= n; - data += n; - - ctx->dataIdx = 0; - n = ctx->dataLen; - while (n > 0) { - i = wc_BioWrite(bio->next_bio, &ctx->data[ctx->dataIdx], n); - if (i <= 0) { - wc_BioCopyNextRetry(bio); - return (ret == size ? i : ret - size); - } - n -= i; - ctx->dataIdx += i; - } - ctx->dataLen = 0; - ctx->dataIdx = 0; - } - - wc_BioCopyNextRetry(bio); - return ret; -} - -static long wc_BioCipher_ctrl(WOLFCRYPT_BIO *bio, int cmd, - long num, void *ptr) -{ - WOLFCRYPT_BIO_ENC_CTX *ctx; - long ret = 1; - int i; - - WOLFSSL_ENTER("wc_BioCipher_ctrl"); - - if (bio == NULL || bio->ptr == NULL || bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - ctx = (WOLFCRYPT_BIO_ENC_CTX *)bio->ptr; - - switch (cmd) { - case BIO_CTRL_RESET: - ctx->ok = 1; - ctx->finished = 0; - wc_EVP_CipherInit(&ctx->cipher, NULL, NULL, NULL, - ctx->cipher.enc); - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_CTRL_EOF: /* More to read */ - if (ctx->cont <= 0) - ret = 1; - else - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_CTRL_WPENDING: - case BIO_CTRL_PENDING: - ret = ctx->dataLen - ctx->dataIdx; - if (ret <= 0) - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_CTRL_FLUSH: - loop: - while (ctx->dataLen != ctx->dataIdx) { - i = wc_BioCipher_write(bio, NULL, 0); - if (i < 0) - return i; - } - - if (!ctx->finished) { - ctx->finished = 1; - ctx->dataIdx = 0; - - ret = wc_EVP_CipherFinal(&ctx->cipher, ctx->data, - &ctx->dataLen); - ctx->ok = (int)ret; - if (ret <= 0) - break; - - goto loop; - } - - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_C_GET_CIPHER_STATUS: - ret = (long)ctx->ok; - break; - - case BIO_C_DO_STATE_MACHINE: - wc_BioClearRetryFlags(bio); - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - wc_BioCopyNextRetry(bio); - break; - - case BIO_C_GET_CIPHER_CTX: - { - WOLFCRYPT_EVP_CIPHER_CTX **c_ctx; - c_ctx = (WOLFCRYPT_EVP_CIPHER_CTX **)ptr; - *c_ctx = &ctx->cipher; - bio->init = 1; - } - break; - - case BIO_CTRL_DUP: - { - WOLFCRYPT_BIO *dbio; - WOLFCRYPT_BIO_ENC_CTX *dctx; - - dbio = (WOLFCRYPT_BIO *)ptr; - dctx = (WOLFCRYPT_BIO_ENC_CTX *)dbio->ptr; - - wc_EVP_CIPHER_CTX_init(&dctx->cipher); - ret = wc_EVP_CIPHER_CTX_copy(&dctx->cipher, &ctx->cipher); - if (ret) - dbio->init = 1; - } - break; - - default: - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - } - - WOLFSSL_LEAVE("wc_BioCipher_ctrl", (int)ret); - return ret; -} - -/* end BIO Filter cipher */ - -/* start BIO Filter digest */ - -static int wc_BioDigest_write(WOLFCRYPT_BIO *bio, const char *buf, int size); -static int wc_BioDigest_read(WOLFCRYPT_BIO *bio, char *buf, int size); -static int wc_BioDigest_gets(WOLFCRYPT_BIO *bio, char *buf, int size); -static long wc_BioDigest_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioDigest_new(WOLFCRYPT_BIO *bio); -static int wc_BioDigest_free(WOLFCRYPT_BIO *bio); -static long wc_BioDigest_callback_ctrl(WOLFCRYPT_BIO *bio, int cmd, - WOLFCRYPT_BIO_info_cb *fp); - -static WOLFCRYPT_BIO_METHOD wc_BioDigest_method = { - BIO_TYPE_MD, - "Message digest", - wc_BioDigest_write, - wc_BioDigest_read, - NULL, /* puts */ - wc_BioDigest_gets, - wc_BioDigest_ctrl, - wc_BioDigest_new, - wc_BioDigest_free, - wc_BioDigest_callback_ctrl, -}; - -static long wc_BioDigest_callback_ctrl(WOLFCRYPT_BIO *bio, int cmd, - WOLFCRYPT_BIO_info_cb *fp) -{ - WOLFSSL_ENTER("wc_BioDigest_callback_ctrl"); - if (bio == NULL || bio->next_bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - return wc_BioCallbackCtrl(bio->next_bio, cmd, fp); -} - -WOLFCRYPT_BIO_METHOD *wc_Bio_f_md(void) -{ - return (&wc_BioDigest_method); -} - -static int wc_BioDigest_new(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioDigest_new"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - bio->ptr = (WOLFCRYPT_EVP_MD_CTX *)XMALLOC(sizeof(WOLFCRYPT_EVP_MD_CTX), - 0, DYNAMIC_TYPE_OPENSSL); - if (bio->ptr == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return -1; - } - - wc_EVP_MD_CTX_init((WOLFCRYPT_EVP_MD_CTX *)bio->ptr); - - bio->init = 0; - bio->flags = 0; - - WOLFSSL_LEAVE("wc_BioDigest_new", 1); - return 1; -} - -static int wc_BioDigest_free(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioDigest_free"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - wc_EVP_MD_CTX_cleanup((WOLFCRYPT_EVP_MD_CTX *)bio->ptr); - - XMEMSET(bio->ptr, 0, sizeof(WOLFCRYPT_EVP_MD_CTX)); - XFREE(bio->ptr, 0, DYNAMIC_TYPE_OPENSSL); - bio->ptr = NULL; - - bio->init = 0; - bio->flags = 0; - - WOLFSSL_LEAVE("wc_BioDigest_free", 1); - return 1; -} - -static int wc_BioDigest_read(WOLFCRYPT_BIO *bio, char *data, int size) -{ - WOLFCRYPT_EVP_MD_CTX *ctx; - int ret = 0; - - WOLFSSL_ENTER("wc_BioDigest_read"); - - if (bio == NULL || bio->ptr == NULL || bio->next_bio == NULL || - data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - ctx = (WOLFCRYPT_EVP_MD_CTX *)bio->ptr; - - ret = wc_BioRead(bio->next_bio, data, size); - if (bio->init && ret > 0) { - if (wc_EVP_DigestUpdate(ctx, data, (word32)ret) != 1) { - WOLFSSL_ERROR(BIO_DGST_UPDATE_E); - return -1; - } - } - - wc_BioClearRetryFlags(bio); - wc_BioCopyNextRetry(bio); - - WOLFSSL_LEAVE("wc_BioDigest_read", ret); - return ret; -} - -static int wc_BioDigest_write(WOLFCRYPT_BIO *bio, - const char *data, int size) -{ - WOLFCRYPT_EVP_MD_CTX *ctx; - int ret = 0; - - WOLFSSL_ENTER("wc_BioDigest_write"); - - if (bio == NULL || bio->ptr == NULL || data == NULL || size <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - ctx = (WOLFCRYPT_EVP_MD_CTX *)bio->ptr; - - ret = wc_BioWrite(bio->next_bio, data, size); - - if (bio->init && ret > 0) { - if (wc_EVP_DigestUpdate(ctx, data, (word32)ret) != 1) { - WOLFSSL_ERROR(BIO_DGST_UPDATE_E); - wc_BioClearRetryFlags(bio); - return -1; - } - } - - if (bio->next_bio != NULL) { - wc_BioClearRetryFlags(bio); - wc_BioCopyNextRetry(bio); - } - - WOLFSSL_LEAVE("wc_BioDigest_write", ret); - - return ret; -} - -static long wc_BioDigest_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - WOLFCRYPT_EVP_MD_CTX *ctx; - long ret = 1; - - WOLFSSL_ENTER("wc_BioDigest_ctrl"); - - if (bio == NULL || bio->ptr == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - ctx = (WOLFCRYPT_EVP_MD_CTX *)bio->ptr; - - switch (cmd) { - case BIO_CTRL_RESET: - if (bio->init) - ret = wc_EVP_DigestInit(ctx, ctx->digest); - else - ret = 0; - - if (ret > 0) - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - - case BIO_C_GET_MD: - if (bio->init) { - const WOLFCRYPT_EVP_MD **pmd; - pmd = (const WOLFCRYPT_EVP_MD **)ptr; - *pmd = ctx->digest; - } else - ret = 0; - break; - - case BIO_C_GET_MD_CTX: - { - WOLFCRYPT_EVP_MD_CTX **pctx; - pctx = (WOLFCRYPT_EVP_MD_CTX **)ptr; - *pctx = ctx; - } - bio->init = 1; - break; - - case BIO_C_SET_MD_CTX: - if (bio->init) - bio->ptr = ptr; - else - ret = 0; - break; - - case BIO_C_DO_STATE_MACHINE: - wc_BioClearRetryFlags(bio); - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - wc_BioCopyNextRetry(bio); - break; - - case BIO_C_SET_MD: - ret = wc_EVP_DigestInit(ctx, (WOLFCRYPT_EVP_MD *)ptr); - if (ret > 0) - bio->init = 1; - else { - WOLFSSL_ERROR(BIO_DGST_INIT_E); - } - break; - - case BIO_CTRL_DUP: - { - WOLFCRYPT_BIO *dbio; - WOLFCRYPT_EVP_MD_CTX *dctx; - - dbio = (WOLFCRYPT_BIO *)ptr; - dctx = (WOLFCRYPT_EVP_MD_CTX *)dbio->ptr; - - ret = wc_EVP_MD_CTX_copy(dctx, ctx); - if (ret) - bio->init = 1; - } - break; - - default: - ret = wc_BioCtrl(bio->next_bio, cmd, num, ptr); - break; - } - - WOLFSSL_LEAVE("wc_BioDigest_ctrl", (int)ret); - return ret; -} - -static int wc_BioDigest_gets(WOLFCRYPT_BIO *bio, char *buf, int size) -{ - WOLFCRYPT_EVP_MD_CTX *ctx; - unsigned int dgstLen = 0; - - WOLFSSL_ENTER("wc_BioDigest_gets"); - - if (bio == NULL || bio->ptr == NULL || buf == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - ctx = (WOLFCRYPT_EVP_MD_CTX *)bio->ptr; - - if (size < ctx->macSize) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - if (wc_EVP_DigestFinal(ctx, (byte *)buf, &dgstLen) != 1) { - WOLFSSL_ERROR(BIO_DGST_FINAL_E); - return -1; - } - - return dgstLen; -} - -/* end BIO Filter digest */ - -/* start BIO Filter socket */ - -/* Socket Handling */ -#ifndef WOLFSSL_SOCKET_INVALID -#ifdef USE_WINDOWS_API -#define WOLFSSL_SOCKET_INVALID ((SOCKET)INVALID_SOCKET) -#else -#define WOLFSSL_SOCKET_INVALID (0) -#endif -#endif /* WOLFSSL_SOCKET_INVALID */ - -#define MAX_LISTEN 32 - -#ifdef USE_WINDOWS_API -static int wsa_init_done = 0; -#endif - -int wc_BioGetHostIp(const char *str, unsigned char *ip) -{ - struct hostent *he; - unsigned int iip[4]; - - if (wc_BioSockInit() != 1) - return 0; - - /* IP found */ - if (sscanf(str, "%d.%d.%d.%d", &iip[0], &iip[1], &iip[2], &iip[3]) == 4) - { - ip[0] = (iip[0] & 0xff); - ip[1] = (iip[1] & 0xff); - ip[2] = (iip[2] & 0xff); - ip[3] = (iip[3] & 0xff); - return 1; - } - - /* IP not found, check with a gethostbyname */ - he = gethostbyname(str); - if (he == NULL) { - WOLFSSL_ERROR(BIO_NO_HOSTNAME_E); - return 0; - } - - if (he->h_addrtype != AF_INET) { - WOLFSSL_ERROR(BIO_ADDR_AF_INET_E); - return 0; - } - - XMEMCPY(ip, he->h_addr_list[0], 4); - - return 1; -} - -int wc_BioGetPort(const char *str, unsigned short *port_ptr) -{ - int i; - - if (str == NULL) { - WOLFSSL_ERROR(BIO_NO_PORT_E); - return 0; - } - - /* str is directly the port number */ - i = atoi(str); - if (i != 0) { - *port_ptr = (unsigned short)i; - return 1; - } - -#if 0 /* getservbyname remove due to memory leaks !? */ - { - struct servent *s; - - s = getservbyname(str, "tcp"); - if (s != NULL) { - *port_ptr = ntohs((unsigned short)s->s_port); - return 1; - } - } -#endif - - /* search port number from protocol */ - if (!strcmp(str, "http")) - *port_ptr = 80; - else if (!strcmp(str, "https") || !strcmp(str, "ssl") || !strcmp(str, "tls")) - *port_ptr = 443; - else if (!strcmp(str, "telnet")) - *port_ptr = 23; - else if (!strcmp(str, "ftp")) - *port_ptr = 21; - else if (!strcmp(str, "ftps")) - *port_ptr = 990; - else if (!strcmp(str, "tftp")) - *port_ptr = 69; - else if (!strcmp(str, "tftps")) - *port_ptr = 3713; - else if (!strcmp(str, "imap")) - *port_ptr = 143; - else if (!strcmp(str, "imaps")) - *port_ptr = 993; - else if (!strcmp(str, "pop3")) - *port_ptr = 110; - else if (!strcmp(str, "pop3s")) - *port_ptr = 995; - else if (!strcmp(str, "smtp")) - *port_ptr = 25; - else if (!strcmp(str, "ldap")) - *port_ptr = 389; - else if (!strcmp(str, "ldaps")) - *port_ptr = 636; - else if (!strcmp(str, "snmp")) - *port_ptr = 161; - else if (!strcmp(str, "ntp")) - *port_ptr = 123; - else if (!strcmp(str, "nntp")) - *port_ptr = 119; - else if (!strcmp(str, "gopher")) - *port_ptr = 70; - else if (!strcmp(str, "socks")) - *port_ptr = 1080; - else { - WOLFSSL_ERROR(BIO_SRV_PROTO_E); - return 0; - } - - return 1; -} - -int wc_BioSockError(int sock) -{ - int j = 0, i; - union { - size_t s; - int i; - } size; - - /* heuristic way to adapt for platforms that expect 64-bit optlen */ - size.s = 0, size.i = sizeof(j); - /* - * Note: under Windows the third parameter is of type (char *) whereas - * under other systems it is (void *) if you don't have a cast it will - * choke the compiler: if you do have a cast then you can either go for - * (char *) or (void *). - */ - i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, (socklen_t *)&size); - if (i < 0) - return 1; - - return j; -} - -int wc_BioSockInit(void) -{ -# ifdef USE_WINDOWS_API - static struct WSAData wsa_state; - - if (!wsa_init_done) { - int err; - - wsa_init_done = 1; - XMEMSET(&wsa_state, 0, sizeof(wsa_state)); - /* - * Not making wsa_state available to the rest of the code is formally - * wrong. But the structures we use are [beleived to be] invariable - * among Winsock DLLs, while API availability is [expected to be] - * probed at run-time with DSO_global_lookup. - */ - if (WSAStartup(0x0202, &wsa_state) != 0) { - err = WSAGetLastError(); - WOLFSSL_ERROR(BIO_WSASTARTUP_E); - return -1; - } - } -# endif /* USE_WINDOWS_API */ - - return 1; -} - -void wc_BioSockCleanup(void) -{ -#ifdef USE_WINDOWS_API - if (wsa_init_done) { - wsa_init_done = 0; - WSACleanup(); - } -#endif -} - -int wc_BioGetAcceptSocket(char *host, int bind_mode) -{ - int ret = 0; - union { - struct sockaddr sa; - struct sockaddr_in sa_in; - struct sockaddr_in6 sa_in6; - } server, client; - int s = WOLFSSL_SOCKET_INVALID, cs, addrlen; - unsigned char ip[4]; - unsigned short port; - char *str = NULL; - char *h, *p, *e; - unsigned long l; - int err_num; - - if (wc_BioSockInit() != 1) - return WOLFSSL_SOCKET_INVALID; - - str = (char *)XMALLOC(strlen(host)+1, 0, DYNAMIC_TYPE_TMP_BUFFER); - if (str == NULL) - return WOLFSSL_SOCKET_INVALID; - - XSTRNCPY(str, host, strlen(host)+1); - - h = p = NULL; - h = str; - for (e = str; *e; e++) { - if (*e == ':') { - p = e; - } else if (*e == '/') { - *e = '\0'; - break; - } - } - if (p) - *p++ = '\0'; /* points at last ':', '::port' is special - * [see below] */ - else - p = h, h = NULL; - - if (!wc_BioGetPort(p, &port)) - goto err; - - XMEMSET((char *)&server, 0, sizeof(server)); - server.sa_in.sin_family = AF_INET; - server.sa_in.sin_port = htons(port); - addrlen = sizeof(server.sa_in); - - if (h == NULL || strcmp(h, "*") == 0) - server.sa_in.sin_addr.s_addr = INADDR_ANY; - else { - if (!wc_BioGetHostIp(h, &(ip[0]))) - goto err; - l = (unsigned long) - ((unsigned long)ip[0] << 24L) | - ((unsigned long)ip[1] << 16L) | - ((unsigned long)ip[2] << 8L) | - ((unsigned long)ip[3]); - server.sa_in.sin_addr.s_addr = htonl(l); - } - -again: - s = (int)socket(server.sa.sa_family, SOCK_STREAM, IPPROTO_TCP); - if (s == WOLFSSL_SOCKET_INVALID) { - WOLFSSL_ERROR(BIO_CREATE_SOCKET_E); - goto err; - } - -#ifdef SO_REUSEADDR - if (bind_mode == BIO_BIND_REUSEADDR) { - int i = 1; - - ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i)); - bind_mode = BIO_BIND_NORMAL; - } -#endif /* SO_REUSEADDR */ - if (bind(s, &server.sa, addrlen) == -1) { -#ifdef SO_REUSEADDR -#ifdef USE_WINDOWS_API - err_num = WSAGetLastError(); - if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) && - (err_num == WSAEADDRINUSE)) -#else -#ifdef HAVE_ERRNO_H - err_num = errno; -#endif - if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) -#ifdef HAVE_ERRNO_H - && (err_num == EADDRINUSE) -#endif - ) -#endif /* USE_WINDOWS_API */ - { - client = server; - if (h == NULL || !strcmp(h, "*")) { - if (client.sa.sa_family == AF_INET6) { - XMEMSET(&client.sa_in6.sin6_addr, 0, - sizeof(client.sa_in6.sin6_addr)); - client.sa_in6.sin6_addr.s6_addr[15] = 1; - } - else if (client.sa.sa_family == AF_INET) - client.sa_in.sin_addr.s_addr = htonl(0x7F000001); - else - goto err; - } - - cs = (int)socket(client.sa.sa_family, SOCK_STREAM, IPPROTO_TCP); - if (cs != WOLFSSL_SOCKET_INVALID) { - int ii; - ii = connect(cs, &client.sa, addrlen); -#ifdef USE_WINDOWS_API - closesocket(cs); -#else - close(cs); -#endif - if (ii == WOLFSSL_SOCKET_INVALID) { - bind_mode = BIO_BIND_REUSEADDR; -#ifdef USE_WINDOWS_API - closesocket(s); -#else - close(s); -#endif - goto again; - } - } - } -#endif /* SO_REUSEADDR */ - - WOLFSSL_ERROR(BIO_BIND_SOCKET_E); - goto err; - } - - if (listen(s, MAX_LISTEN) == -1) { - WOLFSSL_ERROR(BIO_LISTEN_SOCKET_E); - goto err; - } - - ret = 1; - -err: - - if (str != NULL) - XFREE(str, 0, DYNAMIC_TYPE_TMP_BUFFER); - - if (!ret && (s != WOLFSSL_SOCKET_INVALID)) { -#ifdef USE_WINDOWS_API - closesocket(s); -#else - close(s); -#endif - s = WOLFSSL_SOCKET_INVALID; - } - - return s; -} - -int wc_BioAccept(int sock, char **addr) -{ - int dsock = WOLFSSL_SOCKET_INVALID, idx, ret; - unsigned long l; - - struct { - union { - size_t s; - int i; - } len; - union { - struct sockaddr sa; - struct sockaddr_in sa_in; - struct sockaddr_in sa_in6; - } from; - } sa; - - sa.len.s = 0; - sa.len.i = sizeof(sa.from); - XMEMSET(&sa.from, 0, sizeof(sa.from)); - - dsock = (int)accept(sock, &sa.from.sa, (socklen_t *)&sa.len); - if (sizeof(sa.len.i) != sizeof(sa.len.s) && !sa.len.i) { - if (sa.len.s > sizeof(sa.from)) { - WOLFSSL_ERROR(MEMORY_E); - dsock = WOLFSSL_SOCKET_INVALID; - goto end; - } - - sa.len.i = (int)sa.len.s; - } - - if (dsock == WOLFSSL_SOCKET_INVALID) { - if (wc_BioSockShouldRetry(dsock)) - return -2; - WOLFSSL_ERROR(BIO_ACCEPT_E); - goto end; - } - - if (addr == NULL || sa.from.sa.sa_family != AF_INET) - goto end; - - if (*addr == NULL) { - *addr = (char *)XMALLOC(24, 0, DYNAMIC_TYPE_OPENSSL); - if (*addr == NULL) { - WOLFSSL_ERROR(MEMORY_E); - goto end; - } - } - - l = ntohl(sa.from.sa_in.sin_addr.s_addr); - - ret = wc_BioIntToStr((unsigned char)(l >> 24L) & 0xff, *addr, 24); - if (ret <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - dsock = WOLFSSL_SOCKET_INVALID; - goto end; - } - idx = ret; - *(*addr+(idx++)) = '.'; - ret = wc_BioIntToStr((unsigned char)(l >> 16L) & 0xff, *addr+idx, 24-idx); - if (ret <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - dsock = WOLFSSL_SOCKET_INVALID; - goto end; - } - idx += ret; - *(*addr+(idx++)) = '.'; - ret = wc_BioIntToStr((unsigned char)(l >> 8L) & 0xff, *addr+idx, 24-idx); - if (ret <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - dsock = WOLFSSL_SOCKET_INVALID; - goto end; - } - idx += ret; - *(*addr+(idx++)) = '.'; - ret = wc_BioIntToStr((unsigned char)(l) & 0xff, *addr+idx, 24-idx); - if (ret <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - dsock = WOLFSSL_SOCKET_INVALID; - goto end; - } - idx += ret; - *(*addr+(idx++)) = ':'; - ret = wc_BioIntToStr(ntohs(sa.from.sa_in.sin_port), *addr+idx, 24-idx); - if (ret <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - dsock = WOLFSSL_SOCKET_INVALID; - goto end; - } - -end: - return dsock; -} - -int wc_BioSetTcpNsigpipe(int s, int on) -{ - int ret = 0; - -#ifndef USE_WINDOWS_API -#ifdef SO_NOSIGPIPE - ret = setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on)); -#else /* no S_NOSIGPIPE */ - (void) s; - (void) on; - - signal(SIGPIPE, SIG_IGN); -#endif /* S_NOSIGPIPE */ -#else /* USE_WINDOWS_API */ - (void) s; - (void) on; -#endif /* USE_WINDOWS_API */ - - return (ret == 0); -} - -int wc_BioSetTcpNdelay(int s, int on) -{ - int ret = 0; -#if defined(TCP_NODELAY) -#ifdef SOL_TCP - int opt = SOL_TCP; -#else - int opt = IPPROTO_TCP; -#endif - - ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on)); -#else - (void) s; - (void) on; -#endif /* TCP_NODELAY */ - - return (ret == 0); -} - -int wc_BioSocketNbio(int s, int mode) -{ -#ifdef USE_WINDOWS_API - unsigned long blocking = mode; - int ret = ioctlsocket(s, FIONBIO, &blocking); - return (ret == 0); -#elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \ -|| defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS) - /* non blocking not supported, for now */ - return -1; -#else - int flags = fcntl(s, F_GETFL, 0); - if (flags) - flags = fcntl(s, F_SETFL, flags | mode); - return (flags == 0); -#endif -} - -/* end BIO Filter socket */ - -/* start BIO Method accept */ - -typedef struct { - int state; - int nbio; - - char *param_addr; - char *ip_port; - int accept_sock; - int accept_nbio; - - /* - * If 0, it means normal, if 1, do a connect on bind failure, and if - * there is no-one listening, bind with SO_REUSEADDR. If 2, always use - * SO_REUSEADDR. - */ - int bind_mode; - - /* used to force some socket options like NO_SIGPIPE, TCP_NODELAY */ - int options; - - WOLFCRYPT_BIO *bio_chain; -} WOLFCRYPT_BIO_ACCEPT; - -static int wc_BioAccept_write(WOLFCRYPT_BIO *bio, const char *data, int size); -static int wc_BioAccept_read(WOLFCRYPT_BIO *bio, char *data, int size); -static int wc_BioAccept_puts(WOLFCRYPT_BIO *bio, const char *str); -static long wc_BioAccept_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioAccept_new(WOLFCRYPT_BIO *bio); -static int wc_BioAccept_free(WOLFCRYPT_BIO *bio); -static void wc_BioAccept_close_socket(WOLFCRYPT_BIO *bio); - -static int wc_BioAccept_state(WOLFCRYPT_BIO *bio, WOLFCRYPT_BIO_ACCEPT *c); - -# define ACPT_S_BEFORE 1 -# define ACPT_S_GET_ACCEPT_SOCKET 2 -# define ACPT_S_OK 3 - -static WOLFCRYPT_BIO_METHOD wc_BioAccept_method = { - BIO_TYPE_ACCEPT, - "Socket accept", - wc_BioAccept_write, - wc_BioAccept_read, - wc_BioAccept_puts, - NULL, /* gets */ - wc_BioAccept_ctrl, - wc_BioAccept_new, - wc_BioAccept_free, - NULL, -}; - -WOLFCRYPT_BIO_METHOD *wc_Bio_s_accept(void) -{ - return (&wc_BioAccept_method); -} - -static int wc_BioAccept_new(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioAccept_new"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - bio->init = 0; - bio->num = WOLFSSL_SOCKET_INVALID; - bio->flags = 0; - - bio->ptr = (WOLFCRYPT_BIO_ACCEPT *) - XMALLOC(sizeof(WOLFCRYPT_BIO_ACCEPT), 0, DYNAMIC_TYPE_OPENSSL); - if (bio->ptr == NULL) - return 0; - - XMEMSET(bio->ptr, 0, sizeof(WOLFCRYPT_BIO_ACCEPT)); - - ((WOLFCRYPT_BIO_ACCEPT *)bio->ptr)->accept_sock = WOLFSSL_SOCKET_INVALID; - ((WOLFCRYPT_BIO_ACCEPT *)bio->ptr)->bind_mode = BIO_BIND_NORMAL; - ((WOLFCRYPT_BIO_ACCEPT *)bio->ptr)->options = 0; - ((WOLFCRYPT_BIO_ACCEPT *)bio->ptr)->state = ACPT_S_BEFORE; - - bio->shutdown = 1; - - return 1; -} - -static void wc_BioAccept_close_socket(WOLFCRYPT_BIO *bio) -{ - WOLFCRYPT_BIO_ACCEPT *baccept; - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return; - } - - baccept = (WOLFCRYPT_BIO_ACCEPT *)bio->ptr; - if (baccept->accept_sock != WOLFSSL_SOCKET_INVALID) { - shutdown(baccept->accept_sock, SHUT_RDWR); -#ifdef USE_WINDOWS_API - closesocket(baccept->accept_sock); -#else - close(baccept->accept_sock); -#endif - baccept->accept_sock = WOLFSSL_SOCKET_INVALID; - bio->num = WOLFSSL_SOCKET_INVALID; - } -} - -static int wc_BioAccept_free(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioAccept_free"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - if (!bio->shutdown) - return 1; - - wc_BioAccept_close_socket(bio); - - if (bio->ptr != NULL) { - WOLFCRYPT_BIO_ACCEPT *baccept = (WOLFCRYPT_BIO_ACCEPT *)bio->ptr; - - if (baccept->param_addr != NULL) - XFREE(baccept->param_addr, 0, DYNAMIC_TYPE_OPENSSL); - if (baccept->ip_port != NULL) - XFREE(baccept->ip_port, 0, DYNAMIC_TYPE_OPENSSL); - if (baccept->bio_chain != NULL) - wc_BioFree(baccept->bio_chain); - - XFREE(bio->ptr, 0, DYNAMIC_TYPE_OPENSSL); - bio->ptr = NULL; - } - - bio->flags = 0; - bio->init = 0; - - return 1; -} - -static int wc_BioAccept_state(WOLFCRYPT_BIO *bio, WOLFCRYPT_BIO_ACCEPT *baccept) -{ - WOLFCRYPT_BIO *nbio = NULL; - int s = -1; - int dsock; - - if (bio == NULL || baccept == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - -again: - switch (baccept->state) { - case ACPT_S_BEFORE: - if (baccept->param_addr == NULL) { - WOLFSSL_ERROR(BIO_NO_PORT_E); - return -1; - } - - s = wc_BioGetAcceptSocket(baccept->param_addr, baccept->bind_mode); - if (s == WOLFSSL_SOCKET_INVALID) - return -1; - - if (baccept->accept_nbio) { - if (!wc_BioSocketNbio(s, 1)) { -#ifdef USE_WINDOWS_API - closesocket(s); -#else - close(s); -#endif - WOLFSSL_ERROR(BIO_NBIO_E); - return -1; - } - } - - /* TCP NO DELAY */ - if (baccept->options & BIO_OPT_TCP_NO_DELAY) { - if (!wc_BioSetTcpNdelay(s, 1)) { -#ifdef USE_WINDOWS_API - closesocket(s); -#else - close(s); -#endif - WOLFSSL_ERROR(BIO_OPTIONS_E); - return -1; - } - } - - /* IGNORE SIGPIPE */ - if (baccept->options & BIO_OPT_IGN_SIGPIPE) { - if (!wc_BioSetTcpNsigpipe(s, 1)) { -#ifdef USE_WINDOWS_API - closesocket(s); -#else - close(s); -#endif - WOLFSSL_ERROR(BIO_OPTIONS_E); - return -1; - } - } - - baccept->accept_sock = s; - bio->num = s; - baccept->state = ACPT_S_GET_ACCEPT_SOCKET; - return 1; - break; - - case ACPT_S_GET_ACCEPT_SOCKET: - if (bio->next_bio != NULL) { - baccept->state = ACPT_S_OK; - goto again; - } - - wc_BioClearRetryFlags(bio); - bio->retry_reason = 0; - dsock = wc_BioAccept(baccept->accept_sock, &baccept->ip_port); - - /* retry case */ - if (dsock == -2) { - wc_BioSetRetrySpecial(bio); - bio->retry_reason = BIO_RR_ACCEPT; - return -1; - } - - if (dsock < 0) - return dsock; - - nbio = wc_BioNewSocket(dsock, BIO_CLOSE); - if (nbio == NULL) - goto err; - - wc_BioSetCallback(nbio, wc_BioGetCallback(bio)); - wc_BioSetCallbackArg(nbio, wc_BioGetCallbackArg(bio)); - - if (baccept->nbio) { - if (!wc_BioSocketNbio(dsock, 1)) { - WOLFSSL_ERROR(BIO_NBIO_E); - goto err; - } - } - - /* - * If the accept BIO has an bio_chain, we dup it and put the new - * socket at the end. - */ - if (baccept->bio_chain != NULL) { - WOLFCRYPT_BIO *dbio = wc_BioDupChain(baccept->bio_chain); - if (dbio == NULL) - goto err; - if (!wc_BioPush(dbio, nbio)) - goto err; - nbio = dbio; - } - - if (wc_BioPush(bio, nbio) == NULL) - goto err; - - baccept->state = ACPT_S_OK; - return 1; - err: - if (nbio != NULL) - wc_BioFree(nbio); - else if (s >= 0) -#ifdef USE_WINDOWS_API - closesocket(s); -#else - close(s); -#endif - break; - - case ACPT_S_OK: - if (bio->next_bio == NULL) { - baccept->state = ACPT_S_GET_ACCEPT_SOCKET; - goto again; - } - return 1; - break; - - default: - break; - } - - return 0; -} - -static int wc_BioAccept_read(WOLFCRYPT_BIO *bio, char *data, int size) -{ - int ret = 0; - WOLFCRYPT_BIO_ACCEPT *baccept; - - if (bio == NULL || data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - wc_BioClearRetryFlags(bio); - baccept = (WOLFCRYPT_BIO_ACCEPT *)bio->ptr; - - while (bio->next_bio == NULL) { - ret = wc_BioAccept_state(bio, baccept); - if (ret <= 0) - return ret; - } - - ret = wc_BioRead(bio->next_bio, data, size); - wc_BioCopyNextRetry(bio); - - return ret; -} - -static int wc_BioAccept_write(WOLFCRYPT_BIO *bio, const char *data, int size) -{ - int ret = 0; - WOLFCRYPT_BIO_ACCEPT *baccept; - - if (bio == NULL || data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - wc_BioClearRetryFlags(bio); - baccept = (WOLFCRYPT_BIO_ACCEPT *)bio->ptr; - - while (bio->next_bio == NULL) { - ret = wc_BioAccept_state(bio, baccept); - if (ret <= 0) - return ret; - } - - ret = wc_BioWrite(bio->next_bio, data, size); - wc_BioCopyNextRetry(bio); - - return ret; -} - -static long wc_BioAccept_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - int *ip; - long ret = 1; - WOLFCRYPT_BIO_ACCEPT *baccept; - char **pp; - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - baccept = (WOLFCRYPT_BIO_ACCEPT *)bio->ptr; - - switch (cmd) { - case BIO_CTRL_RESET: - ret = 0; - baccept->state = ACPT_S_BEFORE; - wc_BioAccept_close_socket(bio); - bio->flags = 0; - break; - - case BIO_C_DO_STATE_MACHINE: - /* use this one to start the connection */ - ret = (long)wc_BioAccept_state(bio, baccept); - break; - - case BIO_C_SET_ACCEPT: - if (ptr != NULL) { - if (num == 0) { - bio->init = 1; - if (baccept->param_addr != NULL) - XFREE(baccept->param_addr, 0, DYNAMIC_TYPE_OPENSSL); - baccept->param_addr = strdup((const char *)ptr); - } - else if (num == 1) { - baccept->accept_nbio = (ptr != NULL); - } - else if (num == 2) { - if (baccept->bio_chain != NULL) - wc_BioFree(baccept->bio_chain); - baccept->bio_chain = (WOLFCRYPT_BIO *)ptr; - } - } - break; - - case BIO_C_SET_NBIO: - baccept->nbio = (int)num; - break; - - case BIO_C_SET_FD: - bio->init = 1; - bio->num = *((int *)ptr); - baccept->accept_sock = bio->num; - baccept->state = ACPT_S_GET_ACCEPT_SOCKET; - bio->shutdown = (int)num; - bio->init = 1; - break; - - case BIO_C_GET_FD: - if (bio->init) { - ip = (int *)ptr; - if (ip != NULL) - *ip = baccept->accept_sock; - ret = baccept->accept_sock; - } - else - ret = -1; - break; - - case BIO_C_GET_ACCEPT: - if (bio->init) { - if (ptr != NULL) { - pp = (char **)ptr; - *pp = baccept->param_addr; - } - else - ret = -1; - } - else - ret = -1; - break; - - case BIO_CTRL_GET_CLOSE: - ret = bio->shutdown; - break; - - case BIO_CTRL_SET_CLOSE: - bio->shutdown = (int)num; - break; - - case BIO_CTRL_PENDING: - case BIO_CTRL_WPENDING: - ret = 0; - break; - - case BIO_CTRL_FLUSH: - case BIO_CTRL_DUP: - break; - - case BIO_C_SET_BIND_MODE: - baccept->bind_mode = (int)num; - break; - - case BIO_C_GET_BIND_MODE: - ret = (long)baccept->bind_mode; - break; - - case BIO_C_SET_EX_ARG: - baccept->options = (int)num; - break; - - default: - ret = 0; - break; - } - - return ret; -} - -static int wc_BioAccept_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - if (bio == NULL || str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - return wc_BioAccept_write(bio, str, (int)XSTRLEN(str)); -} - -WOLFCRYPT_BIO *wc_BioNewAccept(const char *str) -{ - WOLFCRYPT_BIO *bio; - - if (str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - bio = wc_BioNew(wc_Bio_s_accept()); - if (bio == NULL) - return NULL; - - if (wc_BioSetAcceptPort(bio, str)) - return bio; - - wc_BioFree(bio); - return NULL; -} - -/* end BIO Method accept */ - -/* start BIO Method connect */ - -static int wc_BioConn_write(WOLFCRYPT_BIO *bio, const char *data, int size); -static int wc_BioConn_read(WOLFCRYPT_BIO *bio, char *data, int size); -static int wc_BioConn_puts(WOLFCRYPT_BIO *bio, const char *str); -static long wc_BioConn_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioConn_new(WOLFCRYPT_BIO *bio); -static int wc_BioConn_free(WOLFCRYPT_BIO *bio); -static long wc_BioConn_callback_ctrl(WOLFCRYPT_BIO *bio, int cmd, - WOLFCRYPT_BIO_info_cb *fp); - -static WOLFCRYPT_BIO_METHOD wc_BioConn_method = { - BIO_TYPE_SOCKET, - "Socket connect", - wc_BioConn_write, - wc_BioConn_read, - wc_BioConn_puts, - NULL, /* gets */ - wc_BioConn_ctrl, - wc_BioConn_new, - wc_BioConn_free, - wc_BioConn_callback_ctrl, -}; - -typedef struct { - int state; - int nbio; - - /* keep received Hostname and Port */ - char *pHostname; - char *pPort; - - /* internal usage */ - unsigned char ip[4]; - unsigned short port; - - struct sockaddr_in them; - - /* - * called when the connection is initially made callback(BIO,state,ret); - * The callback should return 'ret'. state is for compatibility with the - * ssl info_callback - */ - int (*info_callback) (const WOLFCRYPT_BIO *bio, int state, int ret); -} WOLFCRYPT_BIO_CONNECT; - -static int wc_BioConn_state(WOLFCRYPT_BIO *bio, WOLFCRYPT_BIO_CONNECT *conn) -{ - int ret = -1, i; - word32 l; - char *p, *q; - - WOLFSSL_ENTER("wc_BioConn_state"); - - if (bio == NULL || conn == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - for (;;) { - switch (conn->state) { - case BIO_CONN_S_BEFORE: - p = conn->pHostname; - if (p == NULL) { - WOLFSSL_ERROR(BIO_NO_HOSTNAME_E); - goto exit_loop; - } - - for (; *p != '\0'; p++) { - if ((*p == ':') || (*p == '/')) - break; - } - - i = *p; - if ((i == ':') || (i == '/')) { - *(p++) = '\0'; - if (i == ':') { - for (q = p; *q; q++) - if (*q == '/') { - *q = '\0'; - break; - } - - if (conn->pPort != NULL) - XFREE(conn->pPort, 0, DYNAMIC_TYPE_OPENSSL); - - conn->pPort = (char *)XMALLOC(XSTRLEN(p)+1, - 0, DYNAMIC_TYPE_OPENSSL); - if (conn->pPort == NULL) { - WOLFSSL_ERROR(MEMORY_E); - goto exit_loop; - break; - } - XSTRNCPY(conn->pPort, p, XSTRLEN(p)+1); - } - } - - if (conn->pPort == NULL) { - WOLFSSL_ERROR(BIO_NO_PORT_E); - goto exit_loop; - } - - conn->state = BIO_CONN_S_GET_IP; - break; - - case BIO_CONN_S_GET_IP: - if (wc_BioGetHostIp(conn->pHostname, conn->ip) <= 0) - goto exit_loop; - conn->state = BIO_CONN_S_GET_PORT; - break; - - case BIO_CONN_S_GET_PORT: - if (conn->pPort == NULL || - wc_BioGetPort(conn->pPort, &conn->port) <= 0) - goto exit_loop; - conn->state = BIO_CONN_S_CREATE_SOCKET; - break; - - case BIO_CONN_S_CREATE_SOCKET: - /* now setup address */ - XMEMSET(&conn->them, 0, sizeof(conn->them)); - conn->them.sin_family = AF_INET; - conn->them.sin_port = htons((unsigned short)conn->port); - l = ((word32)conn->ip[0] << 24L) | - ((word32)conn->ip[1] << 16L) | - ((word32)conn->ip[2] << 8L) | - ((word32)conn->ip[3]); - conn->them.sin_addr.s_addr = htonl(l); - conn->state = BIO_CONN_S_CREATE_SOCKET; - - ret = (int)socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (ret <= 0) { - WOLFSSL_ERROR(BIO_CREATE_SOCKET_E); - goto exit_loop; - } - - bio->num = ret; - conn->state = BIO_CONN_S_NBIO; - break; - - case BIO_CONN_S_NBIO: - if (conn->nbio) { - if (!wc_BioSocketNbio(bio->num, 1)) { - WOLFSSL_ERROR(BIO_NBIO_E); - goto exit_loop; - } - } - conn->state = BIO_CONN_S_CONNECT; - -# if defined(SO_KEEPALIVE) - i = 1; - i = setsockopt(bio->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, - sizeof(i)); - if (i < 0) { - WOLFSSL_ERROR(BIO_KEEPALIVE_E); - goto exit_loop; - } -# endif - break; - - - case BIO_CONN_S_CONNECT: - wc_BioClearRetryFlags(bio); - ret = connect(bio->num, (struct sockaddr *)&conn->them, - sizeof(conn->them)); - - bio->retry_reason = 0; - if (ret < 0) { - if (wc_BioSockShouldRetry(ret)) { - wc_BioSetRetrySpecial(bio); - conn->state = BIO_CONN_S_BLOCKED_CONNECT; - bio->retry_reason = BIO_RR_CONNECT; - } - else { - WOLFSSL_ERROR(BIO_CONNECT_E); - } - goto exit_loop; - } - else - conn->state = BIO_CONN_S_OK; - break; - - case BIO_CONN_S_BLOCKED_CONNECT: - i = wc_BioSockError(bio->num); - if (i != 0) { - WOLFSSL_ERROR(BIO_CONNECT_E); - ret = 0; - goto exit_loop; - } - else - conn->state = BIO_CONN_S_OK; - break; - - case BIO_CONN_S_OK: - ret = 1; - goto exit_loop; - break; - - default: - goto exit_loop; - break; - } - - if (conn->info_callback != NULL) { - ret = conn->info_callback(bio, conn->state, ret); - if (!ret) - goto end; - } - } - -exit_loop: - if (conn->info_callback != NULL) - ret = conn->info_callback(bio, conn->state, ret); -end: - return ret; -} - -WOLFCRYPT_BIO_METHOD *wc_Bio_s_connect(void) -{ - WOLFSSL_ENTER("wc_Bio_s_connect"); - - return (&wc_BioConn_method); -} - -static int wc_BioConn_new(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioConn_new"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - bio->init = 0; - bio->flags = 0; - bio->num = WOLFSSL_SOCKET_INVALID; - - bio->ptr = (WOLFCRYPT_BIO_CONNECT *)XMALLOC(sizeof(WOLFCRYPT_BIO_CONNECT), - 0, DYNAMIC_TYPE_OPENSSL); - if (bio->ptr == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return 0; - } - - XMEMSET(bio->ptr, 0, sizeof(WOLFCRYPT_BIO_CONNECT)); - - ((WOLFCRYPT_BIO_CONNECT *)bio->ptr)->state = BIO_CONN_S_BEFORE; - - return 1; -} - -static void wc_BioConn_close_socket(WOLFCRYPT_BIO *bio) -{ - WOLFCRYPT_BIO_CONNECT *conn; - - WOLFSSL_ENTER("wc_BioConn_close_socket"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return ; - } - - conn = (WOLFCRYPT_BIO_CONNECT *)bio->ptr; - - if (bio->num > 0) { - /* Only do a shutdown if things were established */ - if (conn->state == BIO_CONN_S_OK) - shutdown(bio->num, SHUT_RDWR); -#ifdef USE_WINDOWS_API - closesocket(bio->num); -#else - close(bio->num); -#endif - bio->num = WOLFSSL_SOCKET_INVALID; - } -} - -static int wc_BioConn_free(WOLFCRYPT_BIO *bio) -{ - WOLFSSL_ENTER("wc_BioConn_free"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - if (bio->shutdown) { - wc_BioConn_close_socket(bio); - if (bio->ptr != NULL) { - WOLFCRYPT_BIO_CONNECT *c = (WOLFCRYPT_BIO_CONNECT*)bio->ptr; - - if (c->pHostname != NULL) { - XFREE(c->pHostname, 0, DYNAMIC_TYPE_OPENSSL); - c->pHostname = NULL; - } - if (c->pPort != NULL) { - XFREE(c->pPort, 0, DYNAMIC_TYPE_OPENSSL); - c->pPort = NULL; - } - XFREE(bio->ptr, 0, DYNAMIC_TYPE_OPENSSL); - bio->ptr = NULL; - } - bio->flags = 0; - bio->init = 0; - } - - return 1; -} - -static int wc_BioConn_read(WOLFCRYPT_BIO *bio, char *data, int size) -{ - int ret = 0; - WOLFCRYPT_BIO_CONNECT *conn; - - WOLFSSL_ENTER("wc_BioConn_read"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - conn = (WOLFCRYPT_BIO_CONNECT *)bio->ptr; - if (conn->state != BIO_CONN_S_OK) { - ret = wc_BioConn_state(bio, conn); - if (ret <= 0) - return (ret); - } - -#ifdef USE_WINDOWS_API - WSASetLastError(0); - ret = (int)recv(bio->num, data, size, 0); -#else -#ifdef HAVE_ERRNO_H - errno = 0; -#endif - ret = (int)read(bio->num, data, size); -#endif - - wc_BioClearRetryFlags(bio); - if (ret <= 0) { - if (wc_BioSockShouldRetry(ret)) - wc_BioSetRetryRead(bio); - } - - return ret; -} - -static int wc_BioConn_write(WOLFCRYPT_BIO *bio, const char *data, int size) -{ - int ret = 0; - WOLFCRYPT_BIO_CONNECT *conn; - - WOLFSSL_ENTER("wc_BioConn_write"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - conn = (WOLFCRYPT_BIO_CONNECT *)bio->ptr; - if (conn->state != BIO_CONN_S_OK) { - ret = wc_BioConn_state(bio, conn); - if (ret <= 0) - return (ret); - } - -#ifdef USE_WINDOWS_API - WSASetLastError(0); - ret = (int)send(bio->num, data, size, 0); -#else -#ifdef HAVE_ERRNO_H - errno = 0; -#endif - ret = (int)write(bio->num, data, size); -#endif - - wc_BioClearRetryFlags(bio); - if (ret <= 0) { - if (wc_BioSockShouldRetry(ret)) - wc_BioSetRetryWrite(bio); - } - - return ret; -} - -static long wc_BioConn_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - long ret = 1; - WOLFCRYPT_BIO_CONNECT *conn; - - WOLFSSL_ENTER("wc_BioConn_ctrl"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - conn = (WOLFCRYPT_BIO_CONNECT *)bio->ptr; - - switch (cmd) { - case BIO_CTRL_RESET: - ret = 0; - conn->state = BIO_CONN_S_BEFORE; - wc_BioConn_close_socket(bio); - bio->flags = 0; - break; - - case BIO_C_DO_STATE_MACHINE: - /* use this one to start the connection */ - if (conn->state != BIO_CONN_S_OK) - ret = (long)wc_BioConn_state(bio, conn); - else - ret = 1; - break; - - case BIO_C_GET_CONNECT: - if (ptr == NULL) - break; - - if (num == 0) - *((const char **)ptr) = conn->pHostname; - else if (num == 1) - *((const char **)ptr) = conn->pPort; - else if (num == 2) - *((const char **)ptr) = (char *)conn->ip; - else if (num == 3) - *((int *)ptr) = conn->port; - - if (!bio->init || ptr == NULL) - *((const char **)ptr) = "not initialized"; - - ret = 1; - break; - - case BIO_C_SET_CONNECT: - if (ptr == NULL) - break; - - bio->init = 1; - if (num == 0) { - if (conn->pHostname != NULL) - XFREE(conn->pHostname, 0, DYNAMIC_TYPE_OPENSSL); - conn->pHostname = (char *)XMALLOC(XSTRLEN((char *)ptr)+1, - 0, DYNAMIC_TYPE_OPENSSL); - if (conn->pHostname == NULL) { - WOLFSSL_ERROR(MEMORY_E); - ret = -1; - break; - } - XSTRNCPY(conn->pHostname, (char *)ptr, XSTRLEN((char *)ptr)+1); - } - else if (num == 1) { - if (conn->pPort != NULL) - XFREE(conn->pPort, 0, DYNAMIC_TYPE_OPENSSL); - - conn->pPort = (char *)XMALLOC(XSTRLEN((char *)ptr)+1, - 0, DYNAMIC_TYPE_OPENSSL); - if (conn->pPort == NULL) { - WOLFSSL_ERROR(MEMORY_E); - ret = -1; - break; - } - XSTRNCPY(conn->pPort, (char *)ptr, XSTRLEN((char *)ptr)+1); - } - else if (num == 2) { - char buf[16]; - unsigned char *p = (unsigned char *)ptr; - int idx, res; - - res = wc_BioIntToStr(p[0], buf, sizeof(buf)); - if (res <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - ret = -1; - break; - } - idx = res; - buf[idx++] = '.'; - res = wc_BioIntToStr(p[1], buf+idx, sizeof(buf)-idx); - if (res <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - ret = -1; - break; - } - idx += res; - buf[idx++] = '.'; - res = wc_BioIntToStr(p[2], buf+idx, sizeof(buf)-idx); - if (res <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - ret = -1; - break; - } - idx += res; - buf[idx++] = '.'; - res = wc_BioIntToStr(p[3], buf+idx, sizeof(buf)-idx); - if (res <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - ret = -1; - break; - } - - if (conn->pHostname != NULL) - XFREE(conn->pHostname, 0, DYNAMIC_TYPE_OPENSSL); - - conn->pHostname = (char *)XMALLOC(XSTRLEN(buf)+1, - 0, DYNAMIC_TYPE_OPENSSL); - if (conn->pHostname == NULL) { - WOLFSSL_ERROR(MEMORY_E); - ret = -1; - break; - } - XSTRNCPY(conn->pHostname, buf, XSTRLEN(buf)+1); - XMEMCPY(conn->ip, ptr, 4); - } - else if (num == 3) { - char buf[6]; - int res; - - res = wc_BioIntToStr(*(int *)ptr, buf, sizeof(buf)); - if (res <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - ret = -1; - break; - } - - if (conn->pPort != NULL) - XFREE(conn->pPort, 0, DYNAMIC_TYPE_OPENSSL); - - conn->pPort = (char *)XMALLOC(XSTRLEN(buf)+1, - 0, DYNAMIC_TYPE_OPENSSL); - if (conn->pPort == NULL) { - WOLFSSL_ERROR(MEMORY_E); - ret = -1; - break; - } - XSTRNCPY(conn->pPort, buf, XSTRLEN(buf)+1); - conn->port = *(unsigned short *)ptr; - } - break; - - case BIO_C_SET_NBIO: - conn->nbio = (int)num; - break; - - case BIO_C_GET_FD: - if (bio->init) { - if (ptr != NULL) - *((int *)ptr) = bio->num; - ret = bio->num; - } - else - ret = -1; - break; - - case BIO_CTRL_GET_CLOSE: - ret = bio->shutdown; - break; - - case BIO_CTRL_SET_CLOSE: - bio->shutdown = (int)num; - break; - - case BIO_CTRL_PENDING: - case BIO_CTRL_WPENDING: - ret = 0; - break; - - case BIO_CTRL_FLUSH: - break; - - case BIO_CTRL_DUP: - { - WOLFCRYPT_BIO *dbio = (WOLFCRYPT_BIO *)ptr; - - if (conn->pPort != NULL) - wc_BioSetConnPort(dbio, conn->pPort); - - if (conn->pHostname != NULL) - wc_BioSetConnHostname(dbio, conn->pHostname); - - wc_BioSetNbio(dbio, conn->nbio); - - (void)wc_BioSetInfoCallback(dbio, - (WOLFCRYPT_BIO_info_cb *)conn->info_callback); - } - break; - - case BIO_CTRL_SET_CALLBACK: - ret = 0; - break; - - case BIO_CTRL_GET_CALLBACK: - { - int (**fptr) (const WOLFCRYPT_BIO *bio, int state, int xret); - - fptr = (int (**)(const WOLFCRYPT_BIO *bio, - int state, int xret))ptr; - *fptr = conn->info_callback; - } - break; - - default: - ret = 0; - break; - } - - return ret; -} - -static long wc_BioConn_callback_ctrl(WOLFCRYPT_BIO *bio, - int cmd, WOLFCRYPT_BIO_info_cb *fp) -{ - long ret = 1; - WOLFCRYPT_BIO_CONNECT *conn; - - WOLFSSL_ENTER("wc_BioConn_callback_ctrl"); - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - conn = (WOLFCRYPT_BIO_CONNECT *)bio->ptr; - - switch (cmd) { - case BIO_CTRL_SET_CALLBACK: - conn->info_callback = (int (*)(const WOLFCRYPT_BIO *, int, int))fp; - break; - - default: - ret = 0; - break; - } - - return ret; -} - -static int wc_BioConn_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - WOLFSSL_ENTER("wc_BioConn_puts"); - - if (bio == NULL || str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - return wc_BioConn_write(bio, str, (int)XSTRLEN(str)); -} - -WOLFCRYPT_BIO *wc_BioNewConnect(const char *str) -{ - WOLFCRYPT_BIO *bio; - - WOLFSSL_ENTER("wc_BioNewConnect"); - - if (str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return NULL; - } - - bio = wc_BioNew(wc_Bio_s_connect()); - if (bio == NULL) - return NULL; - - if (wc_BioSetConnHostname(bio, str)) - return bio; - - wc_BioFree(bio); - return NULL; -} - -/* end BIO Method connect */ - -/* start BIO Method datagramm */ - - -#if !defined(IP_MTU) -#define IP_MTU 14 -#endif - -#if !defined(IPPROTO_IPV6) -#define IPPROTO_IPV6 41 -#endif - -static int wc_BioDgram_write(WOLFCRYPT_BIO *bio, const char *data, int size); -static int wc_BioDgram_read(WOLFCRYPT_BIO *bio, char *data, int size); -static int wc_BioDgram_puts(WOLFCRYPT_BIO *bio, const char *str); -static long wc_BioDgram_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioDgram_new(WOLFCRYPT_BIO *bio); -static int wc_BioDgram_free(WOLFCRYPT_BIO *bio); -static int wc_BioDgram_clear(WOLFCRYPT_BIO *bio); - -static int wc_BioDgram_should_retry(int s); - -static void get_current_time(struct timeval *t); - -static WOLFCRYPT_BIO_METHOD WOLFCRYPT_BIO_dgram_method = { - BIO_TYPE_DGRAM, - "Datagram socket", - wc_BioDgram_write, - wc_BioDgram_read, - wc_BioDgram_puts, - NULL, /* gets */ - wc_BioDgram_ctrl, - wc_BioDgram_new, - wc_BioDgram_free, - NULL, -}; - -typedef struct { - union { - struct sockaddr sa; - struct sockaddr_in sa_in; - struct sockaddr_in6 sa_in6; - } peer; - unsigned int connected; - unsigned int _errno; - unsigned int mtu; - struct timeval next_timeout; - struct timeval socket_timeout; -} WOLFCRYPT_BIO_DATAGRAM; - -static int wc_BioDgram_should_retry(int i) -{ - if (!i || i == -1) { - int ret; -#ifdef USE_WINDOWS_API - ret = WSAGetLastError(); -#else -#ifdef HAVE_ERRNO_H - ret = errno; -#else - ret = -1; -#endif -#endif - return wc_BioSockNonFatalError(ret); - } - - return 0; -} - -static void get_current_time(struct timeval *t) -{ -#ifdef USE_WINDOWS_API - SYSTEMTIME st; - union { - unsigned __int64 ul; - FILETIME ft; - } now; - - GetSystemTime(&st); - SystemTimeToFileTime(&st, &now.ft); - now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */ - t->tv_sec = (long)(now.ul / 10000000); - t->tv_usec = ((int)(now.ul % 10000000)) / 10; -# else - gettimeofday(t, NULL); -# endif -} - - -WOLFCRYPT_BIO_METHOD *wc_Bio_s_datagram(void) -{ - return (&WOLFCRYPT_BIO_dgram_method); -} - -WOLFCRYPT_BIO *wc_BioNewDgram(int fd, int close_flag) -{ - WOLFCRYPT_BIO *bio; - - bio = wc_BioNew(wc_Bio_s_datagram()); - if (bio == NULL) - return NULL; - - wc_BioSetFd(bio, fd, close_flag); - return bio; -} - -static int wc_BioDgram_new(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - bio->init = 0; - bio->num = 0; - bio->flags = 0; - - bio->ptr = XMALLOC(sizeof(WOLFCRYPT_BIO_DATAGRAM), 0, DYNAMIC_TYPE_OPENSSL); - if (bio->ptr == NULL) - return 0; - - XMEMSET(bio->ptr, 0, sizeof(WOLFCRYPT_BIO_DATAGRAM)); - return 1; -} - -static int wc_BioDgram_free(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - if (!wc_BioDgram_clear(bio)) - return 0; - - if (bio->ptr != NULL) - XFREE(bio->ptr, 0, DYNAMIC_TYPE_OPENSSL); - - return 1; -} - -static int wc_BioDgram_clear(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - if (bio->shutdown) { - if (bio->init) { - shutdown(bio->num, SHUT_RDWR); -#ifdef USE_WINDOWS_API - closesocket(bio->num); -#else - close(bio->num); -#endif - } - bio->init = 0; - bio->flags = 0; - } - - return 1; -} - -static void wc_BioDgram_adjust_rcv_timeout(WOLFCRYPT_BIO *bio) -{ -#ifdef SO_RCVTIMEO - WOLFCRYPT_BIO_DATAGRAM *dgram; - - union { - size_t s; - int i; - } sz = { 0 }; - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return; - } - - dgram = (WOLFCRYPT_BIO_DATAGRAM *)bio->ptr; - - /* Is a timer active? */ - if (dgram->next_timeout.tv_sec > 0 || dgram->next_timeout.tv_usec > 0) { - struct timeval timenow, timeleft; - - /* Read current socket timeout */ -#ifdef USE_WINDOWS_API - int timeout; - - sz.i = sizeof(timeout); - if (getsockopt(bio->num, SOL_SOCKET, SO_RCVTIMEO, - (void *)&timeout, &sz.i) >= 0) { - dgram->socket_timeout.tv_sec = timeout / 1000; - dgram->socket_timeout.tv_usec = (timeout % 1000) * 1000; - } -#else - sz.i = sizeof(dgram->socket_timeout); - if (getsockopt(bio->num, SOL_SOCKET, SO_RCVTIMEO, - &(dgram->socket_timeout), (socklen_t *)&sz) >= 0) { - if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) - if (sz.s > sizeof(dgram->socket_timeout)) - return ; - } -#endif /* USE_WINDOWS_API */ - - /* Get current time */ - get_current_time(&timenow); - - /* Calculate time left until timer expires */ - XMEMCPY(&timeleft, &dgram->next_timeout, sizeof(struct timeval)); - if (timeleft.tv_usec < timenow.tv_usec) { - timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec; - timeleft.tv_sec--; - } - else - timeleft.tv_usec -= timenow.tv_usec; - - if (timeleft.tv_sec < timenow.tv_sec) { - timeleft.tv_sec = 0; - timeleft.tv_usec = 1; - } - else - timeleft.tv_sec -= timenow.tv_sec; - - /* - * Adjust socket timeout if next handhake message timer will expire - * earlier. - */ - if ((!dgram->socket_timeout.tv_sec && !dgram->socket_timeout.tv_usec) || - (dgram->socket_timeout.tv_sec > timeleft.tv_sec) || - (dgram->socket_timeout.tv_sec == timeleft.tv_sec && - dgram->socket_timeout.tv_usec >= timeleft.tv_usec)) { -#ifdef USE_WINDOWS_API - timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000; - setsockopt(bio->num, SOL_SOCKET, SO_RCVTIMEO, - (void *)&timeout, sizeof(timeout)); -#else - setsockopt(bio->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft, - sizeof(struct timeval)); -#endif /* USE_WINDOWS_API */ - } - } -#endif /* SO_RCVTIMEO */ -} - -static void wc_BioDgram_reset_rcv_timeout(WOLFCRYPT_BIO *bio) -{ -#if defined(SO_RCVTIMEO) - WOLFCRYPT_BIO_DATAGRAM *dgram; - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return; - } - - dgram = (WOLFCRYPT_BIO_DATAGRAM *)bio->ptr; - - /* Is a timer active? */ - if (dgram->next_timeout.tv_sec > 0 || dgram->next_timeout.tv_usec > 0) { -#ifdef USE_WINDOWS_API - int timeout = dgram->socket_timeout.tv_sec * 1000 + - dgram->socket_timeout.tv_usec / 1000; - setsockopt(bio->num, SOL_SOCKET, SO_RCVTIMEO, - (void *)&timeout, sizeof(timeout)); -#else - setsockopt(bio->num, SOL_SOCKET, SO_RCVTIMEO, &(dgram->socket_timeout), - sizeof(struct timeval)); -#endif - } -#endif -} - -static int wc_BioDgram_read(WOLFCRYPT_BIO *bio, char *data, int size) -{ - int ret = 0; - WOLFCRYPT_BIO_DATAGRAM *dgram; - - struct { - union { - size_t s; - int i; - } len; - union { - struct sockaddr sa; - struct sockaddr_in sa_in; - struct sockaddr_in6 sa_in6; - } peer; - } sa; - - if (bio == NULL || data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - dgram = (WOLFCRYPT_BIO_DATAGRAM *)bio->ptr; - - sa.len.s = 0; - sa.len.i = sizeof(sa.peer); - -#ifdef USE_WINDOWS_API - WSASetLastError(0); -#else -#ifdef HAVE_ERRNO_H - errno = 0; -#endif -#endif - - XMEMSET(&sa.peer, 0, sizeof(sa.peer)); - - wc_BioDgram_adjust_rcv_timeout(bio); - - ret = (int)recvfrom(bio->num, data, size, 0, - &sa.peer.sa, (socklen_t *)&sa.len); - if (sizeof(sa.len.i) != sizeof(sa.len.s) && sa.len.i == 0) { - if (sa.len.s > sizeof(sa.peer)) - return 0; - - sa.len.i = (int)sa.len.s; - } - - if (!dgram->connected && ret >= 0) - wc_BioCtrl(bio, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer); - - wc_BioClearRetryFlags(bio); - if (ret < 0) { - if (wc_BioDgram_should_retry(ret)) { - wc_BioSetRetryRead(bio); -#ifdef USE_WINDOWS_API - dgram->_errno = WSAGetLastError(); -#else -#ifdef HAVE_ERRNO_H - dgram->_errno = errno; -#else - dgram->_errno = -1; -#endif -#endif - } - } - - wc_BioDgram_reset_rcv_timeout(bio); - - return ret; -} - -static int wc_BioDgram_write(WOLFCRYPT_BIO *bio, - const char *data, int size) -{ - int ret; - WOLFCRYPT_BIO_DATAGRAM *dgram; - - if (bio == NULL || data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - dgram = (WOLFCRYPT_BIO_DATAGRAM *)bio->ptr; - -#ifdef USE_WINDOWS_API - WSASetLastError(0); -#else -#ifdef HAVE_ERRNO_H - errno = 0; -#endif -#endif - - if (dgram->connected) -#ifdef USE_WINDOWS_API - ret = (int)send(bio->num, data, size, 0); -#else - ret = (int)write(bio->num, data, size); -#endif - else { - int peerlen = sizeof(dgram->peer); - - if (dgram->peer.sa.sa_family == AF_INET) - peerlen = sizeof(dgram->peer.sa_in); - else if (dgram->peer.sa.sa_family == AF_INET6) - peerlen = sizeof(dgram->peer.sa_in6); - - ret = (int)sendto(bio->num, data, size, - 0, &dgram->peer.sa, peerlen); - } - - wc_BioClearRetryFlags(bio); - - if (ret <= 0 && wc_BioDgram_should_retry(ret)) { - wc_BioSetRetryWrite(bio); -#ifdef USE_WINDOWS_API - dgram->_errno = WSAGetLastError(); -#else -#ifdef HAVE_ERRNO_H - dgram->_errno = errno; -#else - dgram->_errno = -1; -#endif -#endif - } - - return ret; -} - -static long wc_BioDgram_get_mtu_overhead(WOLFCRYPT_BIO_DATAGRAM *dgram) -{ - long ret; - - if (dgram == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - switch (dgram->peer.sa.sa_family) { - case AF_INET: - ret = 28; - break; - case AF_INET6: - ret = 48; - break; - default: - ret = 28; - break; - } - - return ret; -} - -static long wc_BioDgram_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - long ret = 1; - struct sockaddr *to = NULL; - WOLFCRYPT_BIO_DATAGRAM *dgram; - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - dgram = (WOLFCRYPT_BIO_DATAGRAM *)bio->ptr; - - switch (cmd) { - case BIO_CTRL_RESET: - case BIO_CTRL_PENDING: - case BIO_CTRL_WPENDING: - case BIO_C_FILE_SEEK: - case BIO_C_FILE_TELL: - case BIO_CTRL_INFO: - ret = 0; - break; - - case BIO_C_SET_FD: - wc_BioDgram_clear(bio); - bio->num = *((int *)ptr); - bio->shutdown = (int)num; - bio->init = 1; - break; - - case BIO_C_GET_FD: - if (bio->init) { - if (ptr != NULL) - *((int *)ptr) = bio->num; - ret = bio->num; - } - else - ret = -1; - break; - - case BIO_CTRL_GET_CLOSE: - ret = bio->shutdown; - break; - - case BIO_CTRL_SET_CLOSE: - bio->shutdown = (int)num; - break; - - case BIO_CTRL_DUP: - case BIO_CTRL_FLUSH: - ret = 1; - break; - - case BIO_CTRL_DGRAM_CONNECT: - to = (struct sockaddr *)ptr; - switch (to->sa_family) { - case AF_INET: - XMEMCPY(&dgram->peer, to, sizeof(dgram->peer.sa_in)); - break; - - case AF_INET6: - XMEMCPY(&dgram->peer, to, sizeof(dgram->peer.sa_in6)); - break; - - default: - XMEMCPY(&dgram->peer, to, sizeof(dgram->peer.sa)); - break; - } - break; - - /* (Linux)kernel sets DF bit on outgoing IP packets */ - case BIO_CTRL_DGRAM_MTU_DISCOVER: - break; - - case BIO_CTRL_DGRAM_QUERY_MTU: - ret = 0; - break; - - case BIO_CTRL_DGRAM_GET_FALLBACK_MTU: - ret = -wc_BioDgram_get_mtu_overhead(dgram); - switch (dgram->peer.sa.sa_family) { - case AF_INET: - ret += 576; - break; - - case AF_INET6: - ret += 1280; - break; - - default: - ret += 576; - break; - } - break; - - case BIO_CTRL_DGRAM_GET_MTU: - return dgram->mtu; - break; - - case BIO_CTRL_DGRAM_SET_MTU: - dgram->mtu = (int)num; - ret = num; - break; - - case BIO_CTRL_DGRAM_SET_CONNECTED: - to = (struct sockaddr *)ptr; - if (to != NULL) { - dgram->connected = 1; - switch (to->sa_family) { - case AF_INET: - XMEMCPY(&dgram->peer, to, - sizeof(dgram->peer.sa_in)); - break; - - case AF_INET6: - XMEMCPY(&dgram->peer, to, - sizeof(dgram->peer.sa_in6)); - break; - - default: - XMEMCPY(&dgram->peer, to, sizeof(dgram->peer.sa)); - break; - } - } - else { - dgram->connected = 0; - XMEMSET(&dgram->peer, 0, sizeof(dgram->peer)); - } - break; - - case BIO_CTRL_DGRAM_GET_PEER: - switch (dgram->peer.sa.sa_family) { - case AF_INET: - ret = sizeof(dgram->peer.sa_in); - break; - - case AF_INET6: - ret = sizeof(dgram->peer.sa_in6); - break; - - default: - ret = sizeof(dgram->peer.sa); - break; - } - - if (num == 0 || num > ret) - num = ret; - XMEMCPY(ptr, &dgram->peer, (ret = num)); - break; - - case BIO_CTRL_DGRAM_SET_PEER: - to = (struct sockaddr *)ptr; - switch (to->sa_family) { - case AF_INET: - XMEMCPY(&dgram->peer, to, sizeof(dgram->peer.sa_in)); - break; - - case AF_INET6: - XMEMCPY(&dgram->peer, to, sizeof(dgram->peer.sa_in6)); - break; - - default: - XMEMCPY(&dgram->peer, to, sizeof(dgram->peer.sa)); - break; - } - break; - - case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT: - XMEMCPY(&dgram->next_timeout, ptr, sizeof(struct timeval)); - break; - -#if defined(SO_RCVTIMEO) - case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT: -#ifdef USE_WINDOWS_API - { - struct timeval *tv = (struct timeval *)ptr; - int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000; - if (setsockopt(bio->num, SOL_SOCKET, SO_RCVTIMEO, - (void *)&timeout, sizeof(timeout)) < 0) - ret = -1; - } -#else - if (setsockopt(bio->num, SOL_SOCKET, SO_RCVTIMEO, ptr, - sizeof(struct timeval)) < 0) - ret = -1; -#endif /* USE_WINDOWS_API */ - break; - - case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT: - { - union { - size_t s; - int i; - } sz = { 0 }; -#ifdef USE_WINDOWS_API - int timeout; - struct timeval *tv = (struct timeval *)ptr; - - sz.i = sizeof(timeout); - if (getsockopt(bio->num, SOL_SOCKET, SO_RCVTIMEO, - (void *)&timeout, (socklen_t *)&sz.i) < 0) - ret = -1; - else { - tv->tv_sec = timeout / 1000; - tv->tv_usec = (timeout % 1000) * 1000; - ret = sizeof(*tv); - } -#else - sz.i = sizeof(struct timeval); - if (getsockopt(bio->num, SOL_SOCKET, SO_RCVTIMEO, - ptr, (socklen_t *)&sz) < 0) - ret = -1; - else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) { - if (sz.s > sizeof(struct timeval)) - ret = -1; - else - ret = (int)sz.s; - } - else - ret = sz.i; -#endif /* USE_WINDOWS_API */ - } - break; -# endif /* defined(SO_RCVTIMEO) */ - -# if defined(SO_SNDTIMEO) - case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT: -#ifdef USE_WINDOWS_API - { - struct timeval *tv = (struct timeval *)ptr; - int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000; - if (setsockopt(bio->num, SOL_SOCKET, SO_SNDTIMEO, - (void *)&timeout, sizeof(timeout)) < 0) - ret = -1; - } -#else - if (setsockopt(bio->num, SOL_SOCKET, SO_SNDTIMEO, ptr, - sizeof(struct timeval)) < 0) - ret = -1; -#endif /* USE_WINDOWS_API */ - break; - - case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT: - { - union { - size_t s; - int i; - } sz = { 0 }; -#ifdef USE_WINDOWS_API - int timeout; - struct timeval *tv = (struct timeval *)ptr; - - sz.i = sizeof(timeout); - if (getsockopt(bio->num, SOL_SOCKET, SO_SNDTIMEO, - (void *)&timeout, &sz.i) < 0) - ret = -1; - else { - tv->tv_sec = timeout / 1000; - tv->tv_usec = (timeout % 1000) * 1000; - ret = sizeof(*tv); - } -#else - sz.i = sizeof(struct timeval); - if (getsockopt(bio->num, SOL_SOCKET, SO_SNDTIMEO, - ptr, (socklen_t *)&sz) < 0) - ret = -1; - else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) { - if (sz.s > sizeof(struct timeval)) - ret = -1; - else - ret = (int)sz.s; - } - else - ret = sz.i; -#endif /* USE_WINDOWS_API */ - } - break; -#endif /* defined(SO_SNDTIMEO) */ - - case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP: - case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP: -#ifdef USE_WINDOWS_API - if (dgram->_errno == WSAETIMEDOUT) -#else - if (dgram->_errno == EAGAIN) -#endif - { - ret = 1; - dgram->_errno = 0; - } - else - ret = 0; - break; - - case BIO_CTRL_DGRAM_SET_DONT_FRAG: - ret = -1; - break; - - case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD: - ret = wc_BioDgram_get_mtu_overhead(dgram); - break; - - default: - ret = 0; - break; - } - - return ret; -} - -static int wc_BioDgram_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - if (bio == NULL || str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - return wc_BioDgram_write(bio, str, (int)XSTRLEN(str)); -} - -/* end BIO Method datagramm */ - -/* start BIO Method file descriptor */ - - -static int wc_BioFd_write(WOLFCRYPT_BIO *bio, const char *buf, int size); -static int wc_BioFd_read(WOLFCRYPT_BIO *bio, char *buf, int size); -static int wc_BioFd_puts(WOLFCRYPT_BIO *bio, const char *str); -static int wc_BioFd_gets(WOLFCRYPT_BIO *bio, char *buf, int size); -static long wc_BioFd_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioFd_new(WOLFCRYPT_BIO *bio); -static int wc_BioFd_free(WOLFCRYPT_BIO *bio); - -static WOLFCRYPT_BIO_METHOD wc_BioFd_method = { - BIO_TYPE_FD, - "File descriptor", - wc_BioFd_write, - wc_BioFd_read, - wc_BioFd_puts, - wc_BioFd_gets, - wc_BioFd_ctrl, - wc_BioFd_new, - wc_BioFd_free, - NULL, -}; - -/* functions are not implemented as not really used in OpenSSL except for two - * cases : - * * get password giving 'fd file' instead of 'file name' - * (see app_get_pass(), apps/apps.c) - * * open STDOUT and STDERR by giving fd instead of name - * (see main(), crypto/threads/mttest.c) - */ -WOLFCRYPT_BIO_METHOD *wc_Bio_s_fd(void) -{ - WOLFSSL_ERROR(NOT_COMPILED_IN); - return (&wc_BioFd_method); -} - -WOLFCRYPT_BIO *wc_BioNewFd(int fd, int close_flag) -{ - (void)fd; - (void)close_flag; - WOLFSSL_ERROR(NOT_COMPILED_IN); - return NULL; -} - -static int wc_BioFd_new(WOLFCRYPT_BIO *bio) -{ - (void)bio; - WOLFSSL_ERROR(NOT_COMPILED_IN); - return -2; -} - -static int wc_BioFd_free(WOLFCRYPT_BIO *bio) -{ - (void)bio; - WOLFSSL_ERROR(NOT_COMPILED_IN); - return -2; -} - -static int wc_BioFd_read(WOLFCRYPT_BIO *bio, char *buf, int size) -{ - (void)bio; - (void)buf; - (void)size; - WOLFSSL_ERROR(NOT_COMPILED_IN); - return -2; -} - -static int wc_BioFd_write(WOLFCRYPT_BIO *bio, const char *buf, int size) -{ - (void)bio; - (void)buf; - (void)size; - WOLFSSL_ERROR(NOT_COMPILED_IN); - return -2; -} - -static long wc_BioFd_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - (void)bio; - (void)ptr; - (void)cmd; - (void)num; - WOLFSSL_ERROR(NOT_COMPILED_IN); - return -2; -} - -static int wc_BioFd_gets(WOLFCRYPT_BIO *bio, char *buf, int size) -{ - (void)bio; - (void)buf; - (void)size; - WOLFSSL_ERROR(NOT_COMPILED_IN); - return -2; -} - -static int wc_BioFd_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - (void)bio; - (void)str; - WOLFSSL_ERROR(NOT_COMPILED_IN); - return -2; -} - -/* end BIO Method file descriptor */ - -/* start BIO Method file */ - -#ifndef NO_FILESYSTEM - -#ifndef XFERROR -#define XFERROR ferror -#endif - -#ifndef XFILENO -#define XFILENO fileno -#endif - -#if defined(USE_WINDOWS_API) -#ifndef XSETMODE -#define XSETMODE _setmode -#endif -#endif /* USE_WINDOWS_API */ - -#ifndef XFFLUSH -#define XFFLUSH fflush -#endif - -#ifndef XFEOF -#define XFEOF feof -#endif - -#ifndef XFGETS -#define XFGETS fgets -#endif - -static int wc_BioFile_write(WOLFCRYPT_BIO *bio, const char *buf, int size); -static int wc_BioFile_read(WOLFCRYPT_BIO *bio, char *buf, int size); -static int wc_BioFile_puts(WOLFCRYPT_BIO *bio, const char *str); -static int wc_BioFile_gets(WOLFCRYPT_BIO *bio, char *buf, int size); -static long wc_BioFile_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioFile_new(WOLFCRYPT_BIO *bio); -static int wc_BioFile_free(WOLFCRYPT_BIO *bio); - -static WOLFCRYPT_BIO_METHOD wc_BioFile_method = { - BIO_TYPE_FILE, - "FILE pointer", - wc_BioFile_write, - wc_BioFile_read, - wc_BioFile_puts, - wc_BioFile_gets, - wc_BioFile_ctrl, - wc_BioFile_new, - wc_BioFile_free, - NULL, -}; - -WOLFCRYPT_BIO *wc_BioNewFile(const char *name, const char *mode) -{ - XFILE f; - - if (name == NULL || mode == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return NULL; - } - - f = XFOPEN(name, mode); - if (f == NULL) { - WOLFSSL_ERROR(BIO_FILE_OPEN_E); - return NULL; - } - - return wc_BioNewFp(f, BIO_CLOSE); -} - -WOLFCRYPT_BIO *wc_BioNewFp(XFILE f, int close_flag) -{ - WOLFCRYPT_BIO *bio; - - if (f == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return NULL; - } - - bio = wc_BioNew(wc_Bio_s_file()); - if (bio == NULL) - return NULL; - - wc_BioSetFp(bio, f, close_flag); - - return bio; -} - -WOLFCRYPT_BIO_METHOD *wc_Bio_s_file(void) -{ - return (&wc_BioFile_method); -} - -static int wc_BioFile_new(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - bio->init = 0; - bio->num = 0; - bio->ptr = NULL; - - return 1; -} - -static int wc_BioFile_free(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) - return 0; - - if (!bio->shutdown || !bio->init) - return 1; - - if (bio->ptr != NULL) { - XFCLOSE((FILE *)bio->ptr); - bio->ptr = NULL; - } - bio->init = 0; - - return 1; -} - -static int wc_BioFile_read(WOLFCRYPT_BIO *bio, char *buf, int size) -{ - int ret = 0; - - if (bio == NULL || !bio->init || bio->ptr == NULL || buf == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - ret = (int)XFREAD(buf, sizeof(char), size, (FILE *)bio->ptr); - if (ret == 0 && XFERROR((FILE *)bio->ptr)) { - WOLFSSL_ERROR(BIO_FILE_READ_E); - ret = -1; - } - - return ret; -} - -static int wc_BioFile_write(WOLFCRYPT_BIO *bio, const char *buf, int size) -{ - int ret = 0; - - if (bio == NULL || !bio->init || bio->ptr == NULL || buf == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - ret = (int)XFWRITE(buf, sizeof(char), size, (FILE *)bio->ptr); - if (ret == 0 && XFERROR((FILE *)bio->ptr)) { - WOLFSSL_ERROR(BIO_FILE_WRITE_E); - ret = -1; - } - - return ret; -} - -static long wc_BioFile_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - long ret = 1; - char buf[4]; - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - switch (cmd) { - case BIO_C_FILE_SEEK: - case BIO_CTRL_RESET: - ret = (long)XFSEEK((FILE *)bio->ptr, num, 0); - break; - - case BIO_CTRL_EOF: - ret = (long)XFEOF((FILE *)bio->ptr); - break; - - case BIO_C_FILE_TELL: - case BIO_CTRL_INFO: - XFSEEK((FILE *)bio->ptr, 0, XSEEK_END); - ret = XFTELL((FILE *)bio->ptr); - XREWIND((FILE *)bio->ptr); - break; - - case BIO_C_SET_FILE_PTR: - wc_BioFile_free(bio); - bio->shutdown = (int)num & BIO_CLOSE; - bio->ptr = ptr; - bio->init = 1; - -#ifdef USE_WINDOWS_API - { - int fd; - - fd = XFILENO((FILE *)bio->ptr); - if (num & BIO_FP_TEXT) - XSETMODE(fd, O_TEXT); - else - XSETMODE(fd, O_BINARY); - } -#endif /* USE_WINDOWS_API */ - break; - - case BIO_C_SET_FILENAME: - wc_BioFile_free(bio); - bio->shutdown = (int)num & BIO_CLOSE; - if (num & BIO_FP_APPEND) { - if (num & BIO_FP_READ) - XSTRNCPY(buf, "a+", sizeof(buf) - 1); - else - XSTRNCPY(buf, "a", sizeof(buf) - 1); - } - else if (num & BIO_FP_READ) { - if (num & BIO_FP_WRITE) - XSTRNCPY(buf, "r+", sizeof(buf) - 1); - else - XSTRNCPY(buf, "r", sizeof(buf)); - } - else if (num & BIO_FP_WRITE) - XSTRNCPY(buf, "w", sizeof(buf) - 1); - else { - WOLFSSL_ERROR(BIO_FILE_MODE_E); - ret = 0; - break; - } - - if (num & BIO_FP_TEXT) - XSTRNCAT(buf, "t", sizeof(buf) - strlen(buf) - 1); - else - XSTRNCAT(buf, "b", sizeof(buf) - strlen(buf) - 1); - - bio->ptr = XFOPEN((const char *)ptr, buf); - if (bio->ptr == NULL) { - WOLFSSL_ERROR(BIO_FILE_OPEN_E); - ret = 0; - break; - } - bio->init = 1; - - break; - - case BIO_C_GET_FILE_PTR: - /* the ptr parameter is a FILE ** in this case. */ - if (ptr != NULL) - *((FILE **)ptr) = (FILE *)bio->ptr; - break; - - case BIO_CTRL_GET_CLOSE: - ret = (long)bio->shutdown; - break; - - case BIO_CTRL_SET_CLOSE: - bio->shutdown = (int)num; - break; - - case BIO_CTRL_FLUSH: - XFFLUSH((FILE *)bio->ptr); - break; - - case BIO_CTRL_DUP: - ret = 1; - break; - - case BIO_CTRL_WPENDING: - case BIO_CTRL_PENDING: - case BIO_CTRL_PUSH: - case BIO_CTRL_POP: - ret = 0; - break; - - default: - ret = 0; - break; - } - - return ret; -} - -static int wc_BioFile_gets(WOLFCRYPT_BIO *bio, char *buf, int size) -{ - if (bio == NULL || bio->ptr == NULL || buf == NULL || size <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - /* init buffer */ - XMEMSET(buf, 0, size); - - if ((XFGETS(buf, size, (FILE *)bio->ptr) == NULL) && - XFERROR((FILE *)bio->ptr)) { - WOLFSSL_ERROR(BIO_FILE_GETS_E); - return -1; - } - - return (int)XSTRLEN(buf); -} - -static int wc_BioFile_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - if (bio == NULL || bio->ptr == NULL || str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - return wc_BioFile_write(bio, str, (int)XSTRLEN(str)); -} - -#endif /* NO_FILESYSTEM */ - -/* end BIO Method file */ - -/* start BIO Method memory */ - - -/* wolfSSL buffer type */ -typedef struct { - byte* data; - word32 length; -} WOLFCRYPT_BUF_MEM; - -static WOLFCRYPT_BUF_MEM *wolfCrypt_BufMem_new(void) -{ - WOLFCRYPT_BUF_MEM *buf; - - buf = (WOLFCRYPT_BUF_MEM *)XMALLOC(sizeof(WOLFCRYPT_BUF_MEM), - 0, DYNAMIC_TYPE_OPENSSL); - if (buf == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return NULL; - } - - buf->length = 0; - buf->data = NULL; - return buf; -} - -static void wolfCrypt_BufMem_free(WOLFCRYPT_BUF_MEM *buf) -{ - if (buf == NULL) - return; - - if (buf->data != NULL) { - XMEMSET(buf->data, 0, buf->length); - XFREE(buf->data, 0, DYNAMIC_TYPE_OPENSSL); - } - - XFREE(buf, 0, DYNAMIC_TYPE_OPENSSL); -} - -static int wolfCrypt_BufMem_grow(WOLFCRYPT_BUF_MEM *buf, size_t len) -{ - if (buf == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - /* size reduction, clean unused */ - if (buf->length >= len) { - if (buf->data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - buf->length = (word32)len; - return (int)len; - } - - if (buf->data == NULL) - buf->data = (byte *)XMALLOC(len, 0, DYNAMIC_TYPE_OPENSSL); - else - buf->data = (byte *)XREALLOC(buf->data, buf->length+len, - 0, DYNAMIC_TYPE_OPENSSL); - if (buf->data == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return -1; - } - - XMEMSET(&buf->data[buf->length], 0, len - buf->length); - buf->length = (word32)len; - - return (int)len; -} - -static int wolfCrypt_BufMem_grow_clean(WOLFCRYPT_BUF_MEM *buf, size_t len) -{ - int ret, idx = -1; - size_t size = 0; - - if (buf == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - if (buf->length >= len) { - idx = buf->length; - size = buf->length - len; - } - - ret = wolfCrypt_BufMem_grow(buf, len); - if (ret && idx != -1 && buf->data != NULL) - XMEMSET(&buf->data[idx], 0, size); - - return ret; -} - - -static int wc_BioMem_write(WOLFCRYPT_BIO *bio, const char *buf, int size); -static int wc_BioMem_read(WOLFCRYPT_BIO *bio, char *buf, int size); -static int wc_BioMem_puts(WOLFCRYPT_BIO *bio, const char *str); -static int wc_BioMem_gets(WOLFCRYPT_BIO *bio, char *buf, int size); -static long wc_BioMem_ctrl(WOLFCRYPT_BIO *bio, int cmd, - long num, void *ptr); -static int wc_BioMem_new(WOLFCRYPT_BIO *bio); -static int wc_BioMem_free(WOLFCRYPT_BIO *bio); - -static WOLFCRYPT_BIO_METHOD wc_BioMem_method = { - BIO_TYPE_MEM, - "Memory buffer", - wc_BioMem_write, - wc_BioMem_read, - wc_BioMem_puts, - wc_BioMem_gets, - wc_BioMem_ctrl, - wc_BioMem_new, - wc_BioMem_free, - NULL, -}; - -WOLFCRYPT_BIO *wc_BioNewMemBuf(void *data, int len) -{ - WOLFCRYPT_BIO *bio; - size_t size; - - if (data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return NULL; - } - - size = (len < 0) ? XSTRLEN((char *)data) : (size_t)len; - - bio = wc_BioNew(wc_Bio_s_mem()); - if (bio == NULL) - return NULL; - - ((WOLFCRYPT_BUF_MEM *)bio->ptr)->data = (byte*)data; - ((WOLFCRYPT_BUF_MEM *)bio->ptr)->length = (word32)size; - - bio->flags |= BIO_FLAGS_MEM_RDONLY; - bio->num = 0; - - return bio; -} - -WOLFCRYPT_BIO_METHOD *wc_Bio_s_mem(void) -{ - return (&wc_BioMem_method); -} - -static int wc_BioMem_new(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - bio->ptr = wolfCrypt_BufMem_new(); - if (bio->ptr == NULL) - return -1; - - bio->shutdown = 1; - bio->init = 1; - bio->num = -1; - - return 1; -} - -static int wc_BioMem_free(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) - return -1; - - if (!bio->shutdown || !bio->init) - return 1; - - if (bio->ptr != NULL) { - if (bio->flags & BIO_FLAGS_MEM_RDONLY) - ((WOLFCRYPT_BUF_MEM *)bio->ptr)->data = NULL; - - wolfCrypt_BufMem_free((WOLFCRYPT_BUF_MEM *)bio->ptr); - bio->ptr = NULL; - } - - bio->init = 0; - return 1; -} - -static int wc_BioMem_read(WOLFCRYPT_BIO *bio, char *data, int size) -{ - int ret = -1; - WOLFCRYPT_BUF_MEM *wbmptr; - - if (bio == NULL || !bio->init || bio->ptr == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - wc_BioClearRetryFlags(bio); - - wbmptr = (WOLFCRYPT_BUF_MEM *)bio->ptr; - - ret = (size >= 0 && (size_t)size > wbmptr->length) ? - (int)wbmptr->length : size; - - if (data != NULL && ret > 0) { - XMEMCPY(data, wbmptr->data, ret); - wbmptr->length -= ret; - if (bio->flags & BIO_FLAGS_MEM_RDONLY) - wbmptr->data += ret; - else - XMEMMOVE(&(wbmptr->data[0]), &(wbmptr->data[ret]), wbmptr->length); - } - else if (wbmptr->length == 0) { - ret = bio->num; - if (ret != 0) - wc_BioSetRetryRead(bio); - } - - return ret; -} - -static int wc_BioMem_write(WOLFCRYPT_BIO *bio, const char *data, int size) -{ - int init_len; - WOLFCRYPT_BUF_MEM *wbmptr; - - if (bio == NULL || !bio->init || data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - if (bio->flags & BIO_FLAGS_MEM_RDONLY) { - WOLFSSL_ERROR(BIO_MEM_WRITE_E); - return -1; - } - - wc_BioClearRetryFlags(bio); - - wbmptr = (WOLFCRYPT_BUF_MEM *)bio->ptr; - init_len = wbmptr->length; - - if (wolfCrypt_BufMem_grow_clean(wbmptr, wbmptr->length + size) != - (int)(init_len + size)) - return -1; - - if (wbmptr->data == NULL) - return -1; - - XMEMCPY(&wbmptr->data[init_len], data, size); - - return size; -} - -static long wc_BioMem_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - WOLFCRYPT_BUF_MEM *wbmptr; - long ret = 1; - - if (bio == NULL || bio->ptr == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - wbmptr = (WOLFCRYPT_BUF_MEM *)bio->ptr; - - switch (cmd) { - case BIO_CTRL_RESET: - if (wbmptr->data == NULL) - break; - - /* For read only case reset to the start again */ - if (bio->flags & BIO_FLAGS_MEM_RDONLY) - wbmptr->data -= wbmptr->length; - else { - XMEMSET(wbmptr->data, 0, wbmptr->length); - wbmptr->length = 0; - } - break; - - case BIO_CTRL_EOF: - ret = (long)(wbmptr->length == 0); - break; - - case BIO_C_SET_BUF_MEM_EOF_RETURN: - bio->num = (int)num; - break; - - case BIO_CTRL_INFO: - ret = (long)wbmptr->length; - if (ptr != NULL) - *((char **)ptr) = (char *)&(wbmptr->data[0]); - break; - - case BIO_C_SET_BUF_MEM: - wc_BioMem_free(bio); - bio->shutdown = (int)num; - bio->ptr = ptr; - break; - - case BIO_C_GET_BUF_MEM_PTR: - if (ptr != NULL) - *((char **)ptr) = (char *)wbmptr; - break; - - case BIO_CTRL_GET_CLOSE: - ret = (long)bio->shutdown; - break; - - case BIO_CTRL_SET_CLOSE: - bio->shutdown = (int)num; - break; - - case BIO_CTRL_WPENDING: - ret = 0; - break; - - case BIO_CTRL_PENDING: - ret = (long)wbmptr->length; - break; - - case BIO_CTRL_DUP: - case BIO_CTRL_FLUSH: - ret = 1; - break; - - case BIO_CTRL_PUSH: - case BIO_CTRL_POP: - ret = 0; - break; - - default: - ret = 0; - break; - } - - return ret; -} - -static int wc_BioMem_gets(WOLFCRYPT_BIO *bio, char *buf, int size) -{ - WOLFCRYPT_BUF_MEM *wbmptr; - int i, blen; - - if (bio == NULL || bio->ptr == NULL || buf == NULL || size <= 0) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - wc_BioClearRetryFlags(bio); - - wbmptr = (WOLFCRYPT_BUF_MEM *)bio->ptr; - - if ((int)wbmptr->length > (size - 1)) - blen = size - 1; - else if (wbmptr->length <= 0) { - *buf = '\0'; - return 0; - } - else - blen = wbmptr->length; - - for (i = 0; i < blen; i++) { - if (wbmptr->data[i] == '\n') { - i++; - break; - } - } - - i = wc_BioMem_read(bio, buf, i); - if (i > 0) - buf[i] = '\0'; - - return i; -} - -static int wc_BioMem_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - if (bio == NULL || str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - return wc_BioMem_write(bio, str, (int)XSTRLEN(str)); -} - -/* end BIO Method memory */ - -/* start BIO Method null */ - -static int wc_BioNull_write(WOLFCRYPT_BIO *bio, const char *buf, int size); -static int wc_BioNull_read(WOLFCRYPT_BIO *bio, char *buf, int size); -static int wc_BioNull_puts(WOLFCRYPT_BIO *bio, const char *str); -static int wc_BioNull_gets(WOLFCRYPT_BIO *bio, char *buf, int size); -static long wc_BioNull_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioNull_new(WOLFCRYPT_BIO *bio); -static int wc_BioNull_free(WOLFCRYPT_BIO *bio); - -static WOLFCRYPT_BIO_METHOD wc_BioNull_method = { - BIO_TYPE_NULL, - "NULL", - wc_BioNull_write, - wc_BioNull_read, - wc_BioNull_puts, - wc_BioNull_gets, - wc_BioNull_ctrl, - wc_BioNull_new, - wc_BioNull_free, - NULL, -}; - -WOLFCRYPT_BIO_METHOD *wc_Bio_s_null(void) -{ - return (&wc_BioNull_method); -} - -static int wc_BioNull_new(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) - return 0; - - bio->init = 1; - bio->num = 0; - bio->ptr = NULL; - - return 1; -} - -static int wc_BioNull_free(WOLFCRYPT_BIO *bio) -{ - return bio == NULL ? 0 : 1; -} - -static int wc_BioNull_read(WOLFCRYPT_BIO *bio, char *buf, int size) -{ - (void)bio; - (void)buf; - (void)size; - - return 0; -} - -static int wc_BioNull_write(WOLFCRYPT_BIO *bio, const char *buf, int size) -{ - (void)bio; - (void)buf; - - return size; -} - -static long wc_BioNull_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - long ret = 1; - - (void)bio; - (void)ptr; - (void)num; - - switch (cmd) { - case BIO_CTRL_RESET: - case BIO_CTRL_EOF: - case BIO_CTRL_SET: - case BIO_CTRL_SET_CLOSE: - case BIO_CTRL_FLUSH: - case BIO_CTRL_DUP: - ret = 1; - break; - case BIO_CTRL_GET_CLOSE: - case BIO_CTRL_INFO: - case BIO_CTRL_GET: - case BIO_CTRL_PENDING: - case BIO_CTRL_WPENDING: - default: - ret = 0; - break; - } - - return ret; -} - -static int wc_BioNull_gets(WOLFCRYPT_BIO *bio, char *buf, int size) -{ - (void)bio; - (void)buf; - (void)size; - - return 0; -} - -static int wc_BioNull_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - (void)bio; - - if (str == NULL) - return 0; - - return (int)XSTRLEN(str); -} - -/* end BIO Method null */ - -/* start BIO Method socket */ - -static int wc_BioSock_write(WOLFCRYPT_BIO *bio, const char *data, int size); -static int wc_BioSock_read(WOLFCRYPT_BIO *bio, char *data, int size); -static int wc_BioSock_puts(WOLFCRYPT_BIO *bio, const char *str); -static long wc_BioSock_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr); -static int wc_BioSock_new(WOLFCRYPT_BIO *bio); -static int wc_BioSock_free(WOLFCRYPT_BIO *bio); - -static WOLFCRYPT_BIO_METHOD wc_BioSock_method = { - BIO_TYPE_SOCKET, - "Socket", - wc_BioSock_write, - wc_BioSock_read, - wc_BioSock_puts, - NULL, /* gets */ - wc_BioSock_ctrl, - wc_BioSock_new, - wc_BioSock_free, - NULL, -}; - -WOLFCRYPT_BIO_METHOD *wc_Bio_s_socket(void) -{ - return (&wc_BioSock_method); -} - -WOLFCRYPT_BIO *wc_BioNewSocket(int fd, int close_flag) -{ - WOLFCRYPT_BIO *ret; - - ret = wc_BioNew(wc_Bio_s_socket()); - if (ret == NULL) { - WOLFSSL_ERROR(MEMORY_E); - return NULL; - } - - wc_BioSetFd(ret, fd, close_flag); - return ret; -} - -static int wc_BioSock_new(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - bio->init = 0; - bio->num = 0; /* used for fd */ - bio->ptr = NULL; - bio->flags = 0; - - return 1; -} - -static int wc_BioSock_free(WOLFCRYPT_BIO *bio) -{ - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return 0; - } - - if (!bio->shutdown) - return 1; - - if (bio->init) { - shutdown(bio->num, SHUT_RDWR); -#ifdef USE_WINDOWS_API - closesocket(bio->num); -#else - close(bio->num); -#endif - } - - bio->init = 0; - bio->flags = 0; - - return 1; -} - -static int wc_BioSock_read(WOLFCRYPT_BIO *bio, char *data, int size) -{ - int ret; - - if (bio == NULL || !bio->init || data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - -#ifdef USE_WINDOWS_API - WSASetLastError(0); - ret = (int)recv(bio->num, data, size, 0); -#else -#ifdef HAVE_ERRNO_H - errno = 0; -#endif - ret = (int)read(bio->num, data, size); -#endif - - wc_BioClearRetryFlags(bio); - if (ret <= 0) { - if (wc_BioSockShouldRetry(ret)) - wc_BioSetRetryRead(bio); - } - - return ret; -} - -static int wc_BioSock_write(WOLFCRYPT_BIO *bio, const char *data, int size) -{ - int ret; - - if (bio == NULL || !bio->init || data == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - -#ifdef USE_WINDOWS_API - WSASetLastError(0); - ret = (int)send(bio->num, data, size, 0); -#else -#ifdef HAVE_ERRNO_H - errno = 0; -#endif - ret = (int)write(bio->num, data, size); -#endif - - wc_BioClearRetryFlags(bio); - if (ret <= 0) { - if (wc_BioSockShouldRetry(ret)) - wc_BioSetRetryWrite(bio); - } - - return ret; -} - -static long wc_BioSock_ctrl(WOLFCRYPT_BIO *bio, int cmd, long num, void *ptr) -{ - long ret = 1; - - if (bio == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - switch (cmd) { - case BIO_C_SET_FD: - wc_BioSock_free(bio); - bio->num = *((int *)ptr); - bio->shutdown = (int)num; - bio->init = 1; - break; - - case BIO_C_GET_FD: - if (bio->init) { - if (ptr != NULL) - *((int *)ptr) = bio->num; - ret = bio->num; - } - else - ret = -1; - break; - - case BIO_CTRL_GET_CLOSE: - ret = bio->shutdown; - break; - - case BIO_CTRL_SET_CLOSE: - bio->shutdown = (int)num; - break; - - case BIO_CTRL_DUP: - case BIO_CTRL_FLUSH: - ret = 1; - break; - - default: - ret = 0; - break; - } - - return ret; -} - -static int wc_BioSock_puts(WOLFCRYPT_BIO *bio, const char *str) -{ - if (bio == NULL || str == NULL) { - WOLFSSL_ERROR(BAD_FUNC_ARG); - return -1; - } - - return wc_BioSock_write(bio, str, (int)XSTRLEN(str)); -} - -int wc_BioSockNonFatalError(int err) -{ - switch (err) { -#if defined(WSAEWOULDBLOCK) - case WSAEWOULDBLOCK: -#endif - -#ifdef EWOULDBLOCK -#ifdef WSAEWOULDBLOCK -#if WSAEWOULDBLOCK != EWOULDBLOCK - case EWOULDBLOCK: -#endif -#else - case EWOULDBLOCK: -#endif -#endif - -#if defined(ENOTCONN) - case ENOTCONN: -#endif - -#ifdef EINTR - case EINTR: -#endif - -#ifdef EAGAIN -#if EWOULDBLOCK != EAGAIN - case EAGAIN: -#endif -#endif - -#ifdef EPROTO - case EPROTO: -#endif - -#ifdef EINPROGRESS - case EINPROGRESS: -#endif - -#ifdef EALREADY - case EALREADY: -#endif - return 1; - break; - - default: - break; - } - - return 0; -} - -int wc_BioSockShouldRetry(int i) -{ - if (!i || i == -1) { - int ret; -#ifdef USE_WINDOWS_API - ret = WSAGetLastError(); -#else -#ifdef HAVE_ERRNO_H - ret = errno; -#else - ret = -1; -#endif -#endif - return wc_BioSockNonFatalError(ret); - } - - return 0; -} - -/* end BIO Method socket */ - -#endif /* OPENSSL_EXTRA */ diff --git a/wolfcrypt/src/compat-wolfcrypt.c b/wolfcrypt/src/compat-wolfcrypt.c deleted file mode 100644 index 737ca7975..000000000 --- a/wolfcrypt/src/compat-wolfcrypt.c +++ /dev/null @@ -1,1592 +0,0 @@ -/* compat-wolfcrypt.c - * - * Copyright (C) 2006-2015 wolfSSL Inc. - * - * This file is part of wolfSSL. (formerly known as CyaSSL) - * - * wolfSSL is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (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 - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include - -#include -#include - -#ifdef NO_INLINE -#include -#else -#define WOLFSSL_MISC_INCLUDED -#include -#endif - - -#include - - -#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) - -#ifndef NO_MD5 - -#ifndef WOLFSSL_HAVE_MIN -#define WOLFSSL_HAVE_MIN - -static INLINE word32 min(word32 a, word32 b) -{ - return a > b ? b : a; -} -#endif /* WOLFSSSL_HAVE_MIN */ - -void wc_MD5_Init(WOLFCRYPT_MD5_CTX* md5) -{ - typedef char md5_test[sizeof(WOLFCRYPT_MD5_CTX) >= sizeof(Md5) ? 1 : -1]; - (void)sizeof(md5_test); - - WOLFSSL_ENTER("MD5_Init"); - wc_InitMd5((Md5*)md5); -} - -void wc_MD5_Update(WOLFCRYPT_MD5_CTX* md5, const void* input, unsigned long sz) -{ - WOLFSSL_ENTER("wc_MD5_Update"); - wc_Md5Update((Md5*)md5, (const byte*)input, (word32)sz); -} - -void wc_MD5_Final(byte* input, WOLFCRYPT_MD5_CTX* md5) -{ - WOLFSSL_ENTER("MD5_Final"); - wc_Md5Final((Md5*)md5, input); -} -#endif /* NO_MD5 */ - - -#ifndef NO_SHA -void wc_SHA_Init(WOLFCRYPT_SHA_CTX* sha) -{ - typedef char sha_test[sizeof(WOLFCRYPT_SHA_CTX) >= sizeof(Sha) ? 1 : -1]; - (void)sizeof(sha_test); - - WOLFSSL_ENTER("SHA_Init"); - wc_InitSha((Sha*)sha); /* OpenSSL compat, no ret */ -} - -void wc_SHA_Update(WOLFCRYPT_SHA_CTX* sha, const void* input, unsigned long sz) -{ - WOLFSSL_ENTER("SHA_Update"); - wc_ShaUpdate((Sha*)sha, (const byte*)input, (word32)sz); -} - -void wc_SHA_Final(byte* input, WOLFCRYPT_SHA_CTX* sha) -{ - WOLFSSL_ENTER("SHA_Final"); - wc_ShaFinal((Sha*)sha, input); -} - -void wc_SHA1_Init(WOLFCRYPT_SHA_CTX* sha) -{ - WOLFSSL_ENTER("SHA1_Init"); - wc_SHA_Init(sha); -} - -void wc_SHA1_Update(WOLFCRYPT_SHA_CTX* sha, const void* input, unsigned long sz) -{ - WOLFSSL_ENTER("SHA1_Update"); - wc_SHA_Update(sha, input, sz); -} - -void wc_SHA1_Final(byte* input, WOLFCRYPT_SHA_CTX* sha) -{ - WOLFSSL_ENTER("SHA1_Final"); - wc_SHA_Final(input, sha); -} -#endif /* NO_SHA */ - - -void wc_SHA256_Init(WOLFCRYPT_SHA256_CTX* sha256) -{ - typedef char sha_test[sizeof(WOLFCRYPT_SHA256_CTX)>=sizeof(Sha256) ? 1:-1]; - (void)sizeof(sha_test); - - WOLFSSL_ENTER("SHA256_Init"); - wc_InitSha256((Sha256*)sha256); /* OpenSSL compat, no error */ -} - -void wc_SHA256_Update(WOLFCRYPT_SHA256_CTX* sha, const void* input, - unsigned long sz) -{ - WOLFSSL_ENTER("SHA256_Update"); - wc_Sha256Update((Sha256*)sha, (const byte*)input, (word32)sz); - /* OpenSSL compat, no error */ -} - -void wc_SHA256_Final(byte* input, WOLFCRYPT_SHA256_CTX* sha) -{ - WOLFSSL_ENTER("SHA256_Final"); - wc_Sha256Final((Sha256*)sha, input); - /* OpenSSL compat, no error */ -} - - -#ifdef WOLFSSL_SHA384 -void wc_SHA384_Init(WOLFCRYPT_SHA384_CTX* sha) -{ - typedef char sha_test[sizeof(WOLFCRYPT_SHA384_CTX)>=sizeof(Sha384) ? 1:-1]; - (void)sizeof(sha_test); - - WOLFSSL_ENTER("SHA384_Init"); - wc_InitSha384((Sha384*)sha); /* OpenSSL compat, no error */ -} - -void wc_SHA384_Update(WOLFCRYPT_SHA384_CTX* sha, const void* input, - unsigned long sz) -{ - WOLFSSL_ENTER("SHA384_Update"); - wc_Sha384Update((Sha384*)sha, (const byte*)input, (word32)sz); - /* OpenSSL compat, no error */ -} - -void wc_SHA384_Final(byte* input, WOLFCRYPT_SHA384_CTX* sha) -{ - WOLFSSL_ENTER("SHA384_Final"); - wc_Sha384Final((Sha384*)sha, input); - /* OpenSSL compat, no error */ -} -#endif /* WOLFSSL_SHA384 */ - - -#ifdef WOLFSSL_SHA512 -void wc_SHA512_Init(WOLFCRYPT_SHA512_CTX* sha) -{ - typedef char sha_test[sizeof(WOLFCRYPT_SHA512_CTX)>=sizeof(Sha512) ? 1:-1]; - (void)sizeof(sha_test); - - WOLFSSL_ENTER("SHA512_Init"); - wc_InitSha512((Sha512*)sha); /* OpenSSL compat, no error */ -} - -void wc_SHA512_Update(WOLFCRYPT_SHA512_CTX* sha, const void* input, - unsigned long sz) -{ - WOLFSSL_ENTER("SHA512_Update"); - wc_Sha512Update((Sha512*)sha, (const byte*)input, (word32)sz); - /* OpenSSL compat, no error */ -} - -void wc_SHA512_Final(byte* input, WOLFCRYPT_SHA512_CTX* sha) -{ - WOLFSSL_ENTER("SHA512_Final"); - wc_Sha512Final((Sha512*)sha, input); - /* OpenSSL compat, no error */ -} -#endif /* WOLFSSL_SHA512 */ - -void wc_HMAC_Init(WOLFCRYPT_HMAC_CTX* ctx, const void* key, int keylen, - const WOLFCRYPT_EVP_MD* type) -{ - WOLFSSL_MSG("wc_HMAC_Init"); - - if (ctx == NULL) { - WOLFSSL_MSG("no ctx on init"); - return; - } - - if (type) { - WOLFSSL_MSG("init has type"); - - if (XSTRNCMP(type, "MD5", 3) == 0) { - WOLFSSL_MSG("md5 hmac"); - ctx->type = MD5; - } - else if (XSTRNCMP(type, "SHA256", 6) == 0) { - WOLFSSL_MSG("sha256 hmac"); - ctx->type = SHA256; - } - - /* has to be last since would pick or 256, 384, or 512 too */ - else if (XSTRNCMP(type, "SHA", 3) == 0) { - WOLFSSL_MSG("sha hmac"); - ctx->type = SHA; - } - else { - WOLFSSL_MSG("bad init type"); - } - } - - if (key && keylen) { - WOLFSSL_MSG("keying hmac"); - wc_HmacSetKey(&ctx->hmac, ctx->type, (const byte*)key, (word32)keylen); - /* OpenSSL compat, no error */ - } -} - - -void wc_HMAC_Update(WOLFCRYPT_HMAC_CTX* ctx, const unsigned char* data, int len) -{ - WOLFSSL_MSG("wc_HMAC_Update"); - - if (ctx && data) { - WOLFSSL_MSG("updating hmac"); - wc_HmacUpdate(&ctx->hmac, data, (word32)len); - /* OpenSSL compat, no error */ - } -} - - -void wc_HMAC_Final(WOLFCRYPT_HMAC_CTX* ctx, unsigned char* hash, - unsigned int* len) -{ - WOLFSSL_MSG("wc_HMAC_Final"); - - if (ctx && hash) { - WOLFSSL_MSG("final hmac"); - wc_HmacFinal(&ctx->hmac, hash); - /* OpenSSL compat, no error */ - - if (len) { - WOLFSSL_MSG("setting output len"); - switch (ctx->type) { -#ifndef NO_MD5 - case MD5: - *len = MD5_DIGEST_SIZE; - break; -#endif -#ifndef NO_SHA - case SHA: - *len = SHA_DIGEST_SIZE; - break; -#endif - case SHA256: - *len = SHA256_DIGEST_SIZE; - break; - - default: - WOLFSSL_MSG("bad hmac type"); - } - } - } -} - - -void wc_HMAC_cleanup(WOLFCRYPT_HMAC_CTX* ctx) -{ - (void)ctx; - - WOLFSSL_MSG("wc_HMAC_cleanup"); -} - -unsigned char* wc_HMAC(const WOLFCRYPT_EVP_MD* evp_md, const void* key, - int key_len, const unsigned char* d, int n, - unsigned char* md, unsigned int* md_len) -{ - int type = -1; - unsigned char* ret = NULL; -#ifdef WOLFSSL_SMALL_STACK - Hmac* hmac = NULL; -#else - Hmac hmac[1]; -#endif - - WOLFSSL_ENTER("HMAC"); - if (!md) - return NULL; /* no static buffer support */ - -#ifndef NO_MD5 - if (XSTRNCMP(evp_md, "MD5", 3) == 0) - type = MD5; -#endif -#ifndef NO_SHA - else if (XSTRNCMP(evp_md, "SHA", 3) == 0) - type = SHA; -#endif - - if (type == -1) - return NULL; - -#ifdef WOLFSSL_SMALL_STACK - hmac = (Hmac*)XMALLOC(sizeof(Hmac), NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (hmac == NULL) - return NULL; -#endif - - if (wc_HmacSetKey(hmac, type, (const byte*)key, key_len) == 0) - if (wc_HmacUpdate(hmac, d, n) == 0) - if (wc_HmacFinal(hmac, md) == 0) { - if (md_len) { -#ifndef NO_MD5 - if (type == MD5) - *md_len = (int)MD5_DIGEST_SIZE; -#endif -#ifndef NO_SHA - if (type == SHA) - *md_len = (int)SHA_DIGEST_SIZE; -#endif - } - ret = md; - } - -#ifdef WOLFSSL_SMALL_STACK - XFREE(hmac, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - return ret; -} - -const char *EVP_AES_128_CBC = "AES-128-CBC"; -const char *EVP_AES_192_CBC = "AES-192-CBC"; -const char *EVP_AES_256_CBC = "AES-256-CBC"; - -#if defined(OPENSSL_EXTRA) -const char *EVP_AES_128_CTR = "AES-128-CTR"; -const char *EVP_AES_192_CTR = "AES-192-CTR"; -const char *EVP_AES_256_CTR = "AES-256-CTR"; -#endif /* OPENSSL_EXTRA */ - -const int EVP_AES_SIZE = 11; - -const char *EVP_DES_CBC = "DES-CBC"; -const int EVP_DES_SIZE = 7; - -const char *EVP_DES_EDE3_CBC = "DES-EDE3-CBC"; -const int EVP_DES_EDE3_SIZE = 12; - -#ifdef HAVE_IDEA -const char *EVP_IDEA_CBC = "IDEA-CBC"; -const int EVP_IDEA_SIZE = 8; -#endif /* HAVE_IDEA */ - -#ifndef NO_AES -const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_128_cbc(void) -{ - WOLFSSL_ENTER("wc_EVP_aes_128_cbc"); - return EVP_AES_128_CBC; -} - -const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_192_cbc(void) -{ - WOLFSSL_ENTER("wc_EVP_aes_192_cbc"); - return EVP_AES_192_CBC; -} - -const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_256_cbc(void) -{ - WOLFSSL_ENTER("wc_EVP_aes_256_cbc"); - return EVP_AES_256_CBC; -} - -#ifdef WOLFSSL_AES_COUNTER -const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_128_ctr(void) -{ - WOLFSSL_ENTER("wc_EVP_aes_128_ctr"); - return EVP_AES_128_CTR; -} - -const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_192_ctr(void) -{ - WOLFSSL_ENTER("wc_EVP_aes_192_ctr"); - return EVP_AES_192_CTR; -} - -const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_256_ctr(void) -{ - WOLFSSL_ENTER("wc_EVP_aes_256_ctr"); - return EVP_AES_256_CTR; -} -#endif /* WOLFSSL_AES_COUNTER */ -#endif /* NO_AES */ - -#ifndef NO_DES3 -const WOLFCRYPT_EVP_CIPHER* wc_EVP_des_cbc(void) -{ - WOLFSSL_ENTER("wc_EVP_des_cbc"); - return EVP_DES_CBC; -} - -const WOLFCRYPT_EVP_CIPHER* wc_EVP_des_ede3_cbc(void) -{ - WOLFSSL_ENTER("wc_EVP_des_ede3_cbc"); - return EVP_DES_EDE3_CBC; -} -#endif /* NO_DES3 */ - -const WOLFCRYPT_EVP_CIPHER* wc_EVP_rc4(void) -{ - static const char* type = "ARC4"; - WOLFSSL_ENTER("wc_EVP_rc4"); - return type; -} - -#ifdef HAVE_IDEA -const WOLFCRYPT_EVP_CIPHER* wc_EVP_idea_cbc(void) -{ - WOLFSSL_ENTER("wc_EVP_idea_cbc"); - return EVP_IDEA_CBC; -} -#endif /* HAVE_IDEA */ - -const WOLFCRYPT_EVP_CIPHER* wc_EVP_enc_null(void) -{ - static const char* type = "NULL"; - WOLFSSL_ENTER("wc_EVP_enc_null"); - return type; -} - -#ifndef NO_MD5 -int wc_EVP_BytesToKey(const WOLFCRYPT_EVP_CIPHER* type, - const WOLFCRYPT_EVP_MD* md, const byte* salt, - const byte* data, int sz, int count, byte* key, byte* iv) -{ - int keyLen = 0; - int ivLen = 0; - int j; - int keyLeft; - int ivLeft; - int keyOutput = 0; - byte digest[MD5_DIGEST_SIZE]; -#ifdef WOLFSSL_SMALL_STACK - Md5* md5 = NULL; -#else - Md5 md5[1]; -#endif - -#ifdef WOLFSSL_SMALL_STACK - md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (md5 == NULL) - return 0; -#endif - - WOLFSSL_ENTER("wc_EVP_BytesToKey"); - wc_InitMd5(md5); - - /* only support MD5 for now */ - if (XSTRNCMP(md, "MD5", 3) != 0) return 0; - - /* only support CBC DES and AES for now */ - if (XSTRNCMP(type, EVP_DES_CBC, EVP_DES_SIZE) == 0) { - keyLen = DES_KEY_SIZE; - ivLen = DES_IV_SIZE; - } - else if (XSTRNCMP(type, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0) { - keyLen = DES3_KEY_SIZE; - ivLen = DES_IV_SIZE; - } - else if (XSTRNCMP(type, EVP_AES_128_CBC, EVP_AES_SIZE) == 0) { - keyLen = AES_128_KEY_SIZE; - ivLen = AES_IV_SIZE; - } - else if (XSTRNCMP(type, EVP_AES_192_CBC, EVP_AES_SIZE) == 0) { - keyLen = AES_192_KEY_SIZE; - ivLen = AES_IV_SIZE; - } - else if (XSTRNCMP(type, EVP_AES_256_CBC, EVP_AES_SIZE) == 0) { - keyLen = AES_256_KEY_SIZE; - ivLen = AES_IV_SIZE; - } - else { -#ifdef WOLFSSL_SMALL_STACK - XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - return 0; - } - - keyLeft = keyLen; - ivLeft = ivLen; - - while (keyOutput < (keyLen + ivLen)) { - int digestLeft = MD5_DIGEST_SIZE; - /* D_(i - 1) */ - if (keyOutput) /* first time D_0 is empty */ - wc_Md5Update(md5, digest, MD5_DIGEST_SIZE); - /* data */ - wc_Md5Update(md5, data, sz); - /* salt */ - if (salt) - wc_Md5Update(md5, salt, EVP_SALT_SIZE); - wc_Md5Final(md5, digest); - /* count */ - for (j = 1; j < count; j++) { - wc_Md5Update(md5, digest, MD5_DIGEST_SIZE); - wc_Md5Final(md5, digest); - } - - if (keyLeft) { - int store = min(keyLeft, MD5_DIGEST_SIZE); - XMEMCPY(&key[keyLen - keyLeft], digest, store); - - keyOutput += store; - keyLeft -= store; - digestLeft -= store; - } - - if (ivLeft && digestLeft) { - int store = min(ivLeft, digestLeft); - if (iv != NULL) - XMEMCPY(&iv[ivLen - ivLeft], - &digest[MD5_DIGEST_SIZE - digestLeft], store); - keyOutput += store; - ivLeft -= store; - } - } - -#ifdef WOLFSSL_SMALL_STACK - XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif - - return keyOutput == (keyLen + ivLen) ? keyOutput : 0; -} -#endif /* NO_MD5 */ - -#endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */ - -#ifdef OPENSSL_EXTRA - -#ifndef NO_MD5 -const WOLFCRYPT_EVP_MD* wc_EVP_md5(void) -{ - static const char* type = "MD5"; - WOLFSSL_ENTER("EVP_md5"); - return type; -} -#endif /* NO_MD5 */ - -#ifndef NO_SHA -const WOLFCRYPT_EVP_MD* wc_EVP_sha1(void) -{ - static const char* type = "SHA"; - WOLFSSL_ENTER("EVP_sha1"); - return type; -} -#endif /* NO_SHA */ - -const WOLFCRYPT_EVP_MD* wc_EVP_sha256(void) -{ - static const char* type = "SHA256"; - WOLFSSL_ENTER("EVP_sha256"); - return type; -} - -#ifdef WOLFSSL_SHA384 -const WOLFCRYPT_EVP_MD* wc_EVP_sha384(void) -{ - static const char* type = "SHA384"; - WOLFSSL_ENTER("EVP_sha384"); - return type; -} -#endif /* WOLFSSL_SHA384 */ - -#ifdef WOLFSSL_SHA512 -const WOLFCRYPT_EVP_MD* wc_EVP_sha512(void) -{ - static const char* type = "SHA512"; - WOLFSSL_ENTER("EVP_sha512"); - return type; -} -#endif /* WOLFSSL_SHA512 */ - -void wc_EVP_MD_CTX_init(WOLFCRYPT_EVP_MD_CTX* ctx) -{ - WOLFSSL_ENTER("EVP_CIPHER_MD_CTX_init"); - if (ctx == NULL) { - WOLFSSL_MSG("Bad function argument"); - return; - } - - ctx->macSize = 0; - ctx->macType = 0xff; -} - -/* return 1 on ok, 0 on failure to match API compatibility */ -int wc_EVP_MD_CTX_copy(WOLFCRYPT_EVP_MD_CTX *out,const WOLFCRYPT_EVP_MD_CTX *in) -{ - WOLFSSL_ENTER("EVP_MD_CTX_copy"); - - if (in == NULL || out == NULL) { - WOLFSSL_MSG("Bad function argument"); - return 0; - } - - wc_EVP_MD_CTX_init(out); - XMEMCPY(out, in, sizeof(WOLFCRYPT_EVP_MD_CTX)); - - return 1; -} - -int wc_EVP_MD_CTX_cleanup(WOLFCRYPT_EVP_MD_CTX* ctx) -{ - WOLFSSL_ENTER("EVP_MD_CTX_cleanup"); - (void)ctx; - return 0; -} - -int wc_EVP_MD_size(const WOLFCRYPT_EVP_MD* type) -{ - WOLFSSL_MSG("wc_EVP_MD_size"); - - if (type == NULL) { - WOLFSSL_MSG("No md type arg"); - return BAD_FUNC_ARG; - } - - if (XSTRNCMP(type, "SHA256", 6) == 0) { - return SHA256_DIGEST_SIZE; - } -#ifndef NO_MD5 - else if (XSTRNCMP(type, "MD5", 3) == 0) { - return MD5_DIGEST_SIZE; - } -#endif -#ifdef WOLFSSL_SHA384 - else if (XSTRNCMP(type, "SHA384", 6) == 0) { - return SHA384_DIGEST_SIZE; - } -#endif -#ifdef WOLFSSL_SHA512 - else if (XSTRNCMP(type, "SHA512", 6) == 0) { - return SHA512_DIGEST_SIZE; - } -#endif -#ifndef NO_SHA - /* has to be last since would pick or 256, 384, or 512 too */ - else if (XSTRNCMP(type, "SHA", 3) == 0) { - return SHA_DIGEST_SIZE; - } -#endif - - return BAD_FUNC_ARG; -} - -#ifdef WOLFSSL_RIPEMD -const WOLFCRYPT_EVP_MD* wc_EVP_ripemd160(void) -{ - WOLFSSL_MSG("wc_ripemd160"); - - return NULL; -} -#endif - -/* 1 on ok */ -int wc_EVP_DigestInit(WOLFCRYPT_EVP_MD_CTX* ctx, const WOLFCRYPT_EVP_MD* type) -{ - WOLFSSL_ENTER("EVP_DigestInit"); - if (XSTRNCMP(type, "SHA256", 6) == 0) { - ctx->macType = SHA256; - ctx->macSize = SHA256_DIGEST_SIZE; - wc_SHA256_Init((WOLFCRYPT_SHA256_CTX*)&ctx->hash); - } -#ifdef WOLFSSL_SHA384 - else if (XSTRNCMP(type, "SHA384", 6) == 0) { - ctx->macType = SHA384; - ctx->macSize = SHA384_DIGEST_SIZE; - wc_SHA384_Init((WOLFCRYPT_SHA384_CTX*)&ctx->hash); - } -#endif -#ifdef WOLFSSL_SHA512 - else if (XSTRNCMP(type, "SHA512", 6) == 0) { - ctx->macType = SHA512; - ctx->macSize = SHA512_DIGEST_SIZE; - wc_SHA512_Init((WOLFCRYPT_SHA512_CTX*)&ctx->hash); - } -#endif -#ifndef NO_MD5 - else if (XSTRNCMP(type, "MD5", 3) == 0) { - ctx->macType = MD5; - ctx->macSize = MD5_DIGEST_SIZE; - wc_MD5_Init((WOLFCRYPT_MD5_CTX*)&ctx->hash); - } -#endif -#ifndef NO_SHA - /* has to be last since would pick or 256, 384, or 512 too */ - else if (XSTRNCMP(type, "SHA", 3) == 0) { - ctx->macType = SHA; - ctx->macSize = SHA_DIGEST_SIZE; - wc_SHA_Init((WOLFCRYPT_SHA_CTX*)&ctx->hash); - } -#endif /* NO_SHA */ - else - return BAD_FUNC_ARG; - - return 1; -} - - -/* 1 on ok */ -int wc_EVP_DigestUpdate(WOLFCRYPT_EVP_MD_CTX* ctx, const void* data, - unsigned long sz) -{ - WOLFSSL_ENTER("EVP_DigestUpdate"); - - switch (ctx->macType) { -#ifndef NO_MD5 - case MD5: - wc_MD5_Update((WOLFCRYPT_MD5_CTX*)&ctx->hash, data, - (unsigned long)sz); - break; -#endif -#ifndef NO_SHA - case SHA: - wc_SHA_Update((WOLFCRYPT_SHA_CTX*)&ctx->hash, data, - (unsigned long)sz); - break; -#endif -#ifndef NO_SHA256 - case SHA256: - wc_SHA256_Update((WOLFCRYPT_SHA256_CTX*)&ctx->hash, data, - (unsigned long)sz); - break; -#endif -#ifdef WOLFSSL_SHA384 - case SHA384: - wc_SHA384_Update((WOLFCRYPT_SHA384_CTX*)&ctx->hash, data, - (unsigned long)sz); - break; -#endif -#ifdef WOLFSSL_SHA512 - case SHA512: - wc_SHA512_Update((WOLFCRYPT_SHA512_CTX*)&ctx->hash, data, - (unsigned long)sz); - break; -#endif - default: - return BAD_FUNC_ARG; - } - - return 1; -} - - -/* 1 on ok */ -int wc_EVP_DigestFinal(WOLFCRYPT_EVP_MD_CTX* ctx, unsigned char* md, - unsigned int* s) -{ - WOLFSSL_ENTER("EVP_DigestFinal"); - switch (ctx->macType) { -#ifndef NO_MD5 - case MD5: - wc_MD5_Final(md, (WOLFCRYPT_MD5_CTX*)&ctx->hash); - if (s) *s = MD5_DIGEST_SIZE; - break; -#endif -#ifndef NO_SHA - case SHA: - wc_SHA_Final(md, (WOLFCRYPT_SHA_CTX*)&ctx->hash); - if (s) *s = SHA_DIGEST_SIZE; - break; -#endif -#ifndef NO_SHA256 - case SHA256: - wc_SHA256_Final(md, (WOLFCRYPT_SHA256_CTX*)&ctx->hash); - if (s) *s = SHA256_DIGEST_SIZE; - break; -#endif -#ifdef WOLFSSL_SHA384 - case SHA384: - wc_SHA384_Final(md, (WOLFCRYPT_SHA384_CTX*)&ctx->hash); - if (s) *s = SHA384_DIGEST_SIZE; - break; -#endif -#ifdef WOLFSSL_SHA512 - case SHA512: - wc_SHA512_Final(md, (WOLFCRYPT_SHA512_CTX*)&ctx->hash); - if (s) *s = SHA512_DIGEST_SIZE; - break; -#endif - default: - return BAD_FUNC_ARG; - } - - return 1; -} - - -/* 1 on ok */ -int wc_EVP_DigestFinal_ex(WOLFCRYPT_EVP_MD_CTX* ctx, unsigned char* md, - unsigned int* s) -{ - WOLFSSL_ENTER("EVP_DigestFinal_ex"); - return wc_EVP_DigestFinal(ctx, md, s); -} - -const WOLFCRYPT_EVP_MD* wc_EVP_get_digestbynid(int id) -{ - WOLFSSL_MSG("wc_get_digestbynid"); - - switch(id) { -#ifndef NO_MD5 - case NID_md5: - return wc_EVP_md5(); -#endif -#ifndef NO_SHA - case NID_sha1: - return wc_EVP_sha1(); -#endif - default: - WOLFSSL_MSG("Bad digest id value"); - } - - return NULL; -} - - -void wc_EVP_CIPHER_CTX_init(WOLFCRYPT_EVP_CIPHER_CTX* ctx) -{ - WOLFSSL_ENTER("EVP_CIPHER_CTX_init"); - if (ctx) { - ctx->cipherType = 0xff; /* no init */ - ctx->keyLen = 0; - ctx->enc = 1; /* start in encrypt mode */ - - ctx->ivUpdate = 0; - ctx->final_used = 0; - ctx->bufLen = 0; - ctx->blockSize = 0; - ctx->padding = 0; - - XMEMSET(ctx->iv, 0, sizeof(ctx->iv)); - XMEMSET(ctx->buf, 0, sizeof(ctx->buf)); - XMEMSET(ctx->final, 0, sizeof(ctx->final)); - } -} - -/* 1 on ok */ -int wc_EVP_CIPHER_CTX_cleanup(WOLFCRYPT_EVP_CIPHER_CTX* ctx) -{ - WOLFSSL_ENTER("EVP_CIPHER_CTX_cleanup"); - - /* reset to initial values */ - wc_EVP_CIPHER_CTX_init(ctx); - - return 1; -} - -/* return 1 on ok, 0 on failure to match API compatibility */ -int wc_EVP_CipherInit(WOLFCRYPT_EVP_CIPHER_CTX* ctx, - const WOLFCRYPT_EVP_CIPHER* type, byte* key, - byte* iv, int enc) -{ - int ret = -1; /* failure local, during function 0 means success - because internal functions work that way */ - (void)iv; - (void)enc; - - WOLFSSL_ENTER("wc_EVP_CipherInit"); - if (ctx == NULL) { - WOLFSSL_MSG("no ctx"); - return 0; /* failure */ - } - - if (type == NULL && ctx->cipherType == 0xff) { - WOLFSSL_MSG("no type set"); - return 0; /* failure */ - } - -#ifndef NO_AES - if (ctx->cipherType == AES_128_CBC_TYPE || - (type && XSTRNCMP(type, EVP_AES_128_CBC, EVP_AES_SIZE) == 0)) { - WOLFSSL_MSG(EVP_AES_128_CBC); - ctx->cipherType = AES_128_CBC_TYPE; - ctx->padding = 1; - ctx->ivUpdate = 1; - ctx->blockSize = AES_BLOCK_SIZE; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->keyLen = 16; - if (enc == 0 || enc == 1) - ctx->enc = enc ? 1 : 0; - if (key) { - ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv, - ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION); - if (ret != 0) - return ret; - } - if (iv && key == NULL) { - ret = wc_AesSetIV(&ctx->cipher.aes, iv); - if (ret != 0) - return ret; - } - } - else if (ctx->cipherType == AES_192_CBC_TYPE || - (type && XSTRNCMP(type, EVP_AES_192_CBC, EVP_AES_SIZE) == 0)) { - WOLFSSL_MSG(EVP_AES_192_CBC); - ctx->cipherType = AES_192_CBC_TYPE; - ctx->padding = 1; - ctx->ivUpdate = 1; - ctx->blockSize = AES_BLOCK_SIZE; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->keyLen = 24; - if (enc == 0 || enc == 1) - ctx->enc = enc ? 1 : 0; - if (key) { - ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv, - ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION); - if (ret != 0) - return ret; - } - if (iv && key == NULL) { - ret = wc_AesSetIV(&ctx->cipher.aes, iv); - if (ret != 0) - return ret; - } - } - else if (ctx->cipherType == AES_256_CBC_TYPE || - (type && XSTRNCMP(type, EVP_AES_256_CBC, EVP_AES_SIZE) == 0)) { - WOLFSSL_MSG(EVP_AES_256_CBC); - ctx->cipherType = AES_256_CBC_TYPE; - ctx->padding = 1; - ctx->ivUpdate = 1; - ctx->blockSize = AES_BLOCK_SIZE; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->keyLen = 32; - if (enc == 0 || enc == 1) - ctx->enc = enc ? 1 : 0; - if (key) { - ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv, - ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION); - if (ret != 0) - return ret; - } - if (iv && key == NULL) { - ret = wc_AesSetIV(&ctx->cipher.aes, iv); - if (ret != 0) - return ret; - } - } -#ifdef WOLFSSL_AES_COUNTER - else if (ctx->cipherType == AES_128_CTR_TYPE || - (type && XSTRNCMP(type, EVP_AES_128_CTR, EVP_AES_SIZE) == 0)) { - WOLFSSL_MSG(EVP_AES_128_CTR); - ctx->cipherType = AES_128_CTR_TYPE; - ctx->padding = 0; - ctx->ivUpdate = 0; - ctx->blockSize = AES_BLOCK_SIZE; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->keyLen = 16; - if (enc == 0 || enc == 1) - ctx->enc = enc ? 1 : 0; - if (key) { - ret = wc_AesSetKeyDirect(&ctx->cipher.aes, key, ctx->keyLen, iv, - AES_ENCRYPTION); - if (ret != 0) - return ret; - } - if (iv && key == NULL) { - ret = wc_AesSetIV(&ctx->cipher.aes, iv); - if (ret != 0) - return ret; - } - } - else if (ctx->cipherType == AES_192_CTR_TYPE || - (type && XSTRNCMP(type, EVP_AES_192_CTR, EVP_AES_SIZE) == 0)) { - WOLFSSL_MSG(EVP_AES_192_CTR); - ctx->cipherType = AES_192_CTR_TYPE; - ctx->padding = 0; - ctx->ivUpdate = 0; - ctx->blockSize = AES_BLOCK_SIZE; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->keyLen = 24; - if (enc == 0 || enc == 1) - ctx->enc = enc ? 1 : 0; - if (key) { - ret = wc_AesSetKeyDirect(&ctx->cipher.aes, key, ctx->keyLen, iv, - AES_ENCRYPTION); - if (ret != 0) - return ret; - } - if (iv && key == NULL) { - ret = wc_AesSetIV(&ctx->cipher.aes, iv); - if (ret != 0) - return ret; - } - } - else if (ctx->cipherType == AES_256_CTR_TYPE || - (type && XSTRNCMP(type, EVP_AES_256_CTR, EVP_AES_SIZE) == 0)) { - WOLFSSL_MSG(EVP_AES_256_CTR); - ctx->cipherType = AES_256_CTR_TYPE; - ctx->padding = 0; - ctx->ivUpdate = 0; - ctx->blockSize = AES_BLOCK_SIZE; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->keyLen = 32; - if (enc == 0 || enc == 1) - ctx->enc = enc ? 1 : 0; - if (key) { - ret = wc_AesSetKeyDirect(&ctx->cipher.aes, key, ctx->keyLen, iv, - AES_ENCRYPTION); - if (ret != 0) - return ret; - } - if (iv && key == NULL) { - ret = wc_AesSetIV(&ctx->cipher.aes, iv); - if (ret != 0) - return ret; - } - } -#endif /* WOLFSSL_AES_CTR */ -#endif /* NO_AES */ - -#ifndef NO_DES3 - if (ctx->cipherType == DES_CBC_TYPE || - (type && XSTRNCMP(type, EVP_DES_CBC, EVP_DES_SIZE) == 0)) { - WOLFSSL_MSG(EVP_DES_CBC); - ctx->cipherType = DES_CBC_TYPE; - ctx->padding = 1; - ctx->ivUpdate = 1; - ctx->blockSize = DES_BLOCK_SIZE; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->keyLen = 8; - if (enc == 0 || enc == 1) - ctx->enc = enc ? 1 : 0; - if (key) { - ret = wc_Des_SetKey(&ctx->cipher.des, key, iv, - ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION); - if (ret != 0) - return ret; - } - - if (iv && key == NULL) - wc_Des_SetIV(&ctx->cipher.des, iv); - } - else if (ctx->cipherType == DES_EDE3_CBC_TYPE || - (type && - XSTRNCMP(type, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0)) { - WOLFSSL_MSG(EVP_DES_EDE3_CBC); - ctx->cipherType = DES_EDE3_CBC_TYPE; - ctx->padding = 1; - ctx->ivUpdate = 1; - ctx->blockSize = DES_BLOCK_SIZE; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->keyLen = 24; - if (enc == 0 || enc == 1) - ctx->enc = enc ? 1 : 0; - if (key) { - ret = wc_Des3_SetKey(&ctx->cipher.des3, key, iv, - ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION); - if (ret != 0) - return ret; - } - - if (iv && key == NULL) { - ret = wc_Des3_SetIV(&ctx->cipher.des3, iv); - if (ret != 0) - return ret; - } - } -#endif /* NO_DES3 */ -#ifndef NO_RC4 - if (ctx->cipherType == ARC4_TYPE || (type && - XSTRNCMP(type, "ARC4", 4) == 0)) { - WOLFSSL_MSG("ARC4"); - ctx->cipherType = ARC4_TYPE; - ctx->blockSize = 1; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->padding = 0; - ctx->ivUpdate = 0; - if (ctx->keyLen == 0) /* user may have already set */ - ctx->keyLen = 16; /* default to 128 */ - if (key) - wc_Arc4SetKey(&ctx->cipher.arc4, key, ctx->keyLen); - ret = 0; /* success */ - } -#endif /* NO_RC4 */ -#ifdef HAVE_IDEA - if (ctx->cipherType == IDEA_CBC_TYPE || - (type && XSTRNCMP(type, EVP_IDEA_CBC, EVP_IDEA_SIZE) == 0)) { - WOLFSSL_MSG(EVP_IDEA_CBC); - ctx->cipherType = IDEA_CBC_TYPE; - ctx->padding = 1; - ctx->ivUpdate = 1; - ctx->blockSize = IDEA_BLOCK_SIZE; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->keyLen = IDEA_KEY_SIZE; - if (enc == 0 || enc == 1) - ctx->enc = enc ? 1 : 0; - if (key) { - ret = wc_IdeaSetKey(&ctx->cipher.idea, key, (word16)ctx->keyLen, - iv, ctx->enc ? IDEA_ENCRYPTION : - IDEA_DECRYPTION); - if (ret != 0) - return ret; - } - - if (iv && key == NULL) - wc_IdeaSetIV(&ctx->cipher.idea, iv); - } -#endif /* HAVE_IDEA */ - if (ctx->cipherType == NULL_CIPHER_TYPE || (type && - XSTRNCMP(type, "NULL", 4) == 0)) { - WOLFSSL_MSG("NULL cipher"); - ctx->cipherType = NULL_CIPHER_TYPE; - ctx->keyLen = 0; - ctx->blockSize = 1; - ctx->bufLen = 0; - ctx->final_used = 0; - ctx->padding = 0; - ctx->ivUpdate = 0; - ret = 0; /* success */ - } - - if (ret == 0) - return 1; - else - return 0; /* overall failure */ -} - -/* return 1 on ok, 0 on failure to match API compatibility */ -int wc_EVP_CipherUpdate(WOLFCRYPT_EVP_CIPHER_CTX *ctx, byte *dst, int *dstLen, - const byte *src, int len) -{ - int ret = 0, notEncLen = 0, fixLen = 0; - WOLFSSL_ENTER("wc_EVP_CipherUpdate"); - - *dstLen = 0; - - if (len <= 0) - return (len == 0); - - /* Push pending data for the decryption case */ - if (!ctx->enc && ctx->final_used) { - XMEMCPY(dst, ctx->final, ctx->blockSize); - dst += ctx->blockSize; - fixLen = 1; - } - - /* No pending data, src len is a multiple of blocksize */ - if (!ctx->bufLen && !(len & (ctx->blockSize-1))) { - ret = wc_EVP_Cipher(ctx, &dst[*dstLen], (byte*)src, len); - if (ret != 1) { - *dstLen = 0; - WOLFSSL_MSG("wc_EVP_Cipher failure"); - return 0; - } - else { - *dstLen = len; - - /* save new iv if required */ - if (ctx->ivUpdate) { - if (ctx->enc) - XMEMCPY(ctx->iv, &dst[*dstLen-ctx->blockSize], - ctx->blockSize); - else - XMEMCPY(ctx->iv, &src[len-ctx->blockSize], - ctx->blockSize); - ctx->ivUpdate = 2; - } - - /* extra operation for decrypt case */ - if (!ctx->enc) - goto decrypt; - else - return 1; - } - } - - /* Pending data */ - if (ctx->bufLen) { - /* pending data + src data less than a block - * keep data and return */ - if (ctx->bufLen + len < ctx->blockSize) { - XMEMCPY(&ctx->buf[ctx->bufLen], src, len); - ctx->bufLen += len; - *dstLen = 0; - return 1; - } - else { - /* complete pending buffer and encrypt/decrypt it */ - XMEMCPY(&ctx->buf[ctx->bufLen], src, - ctx->blockSize - ctx->bufLen); - ret = wc_EVP_Cipher(ctx, &dst[*dstLen], - ctx->buf, ctx->blockSize); - if (ret != 1) { - *dstLen = 0; - WOLFSSL_MSG("wc_EVP_Cipher failure"); - return 0; - } - - /* save new iv if required */ - if (ctx->ivUpdate) { - if (ctx->enc) - XMEMCPY(ctx->iv, dst, ctx->blockSize); - else - XMEMCPY(ctx->iv, ctx->buf, ctx->blockSize); - ctx->ivUpdate = 2; - } - - len -= (ctx->blockSize - ctx->bufLen); - src += (ctx->blockSize - ctx->bufLen); - *dstLen = ctx->blockSize; - } - } - /* src len not a multiple of block size */ - else - *dstLen = 0; - - /* encrypt/decrypt max blocks as possible */ - notEncLen = len & (ctx->blockSize - 1); - len -= notEncLen; - if (len > 0) { - ret = wc_EVP_Cipher(ctx, &dst[*dstLen], (byte*)src, len); - if (ret != 1) { - WOLFSSL_MSG("wc_EVP_Cipher failure"); - return 0; - } - *dstLen += len; - - /* save new iv if required */ - if (ctx->ivUpdate) { - if (ctx->enc) - XMEMCPY(ctx->iv, &dst[*dstLen-ctx->blockSize], - ctx->blockSize); - else - XMEMCPY(ctx->iv, &src[len-ctx->blockSize], ctx->blockSize); - ctx->ivUpdate = 2; - } - } - - /* save pending data */ - if (notEncLen) - XMEMCPY(ctx->buf, src+len, notEncLen); - ctx->bufLen = notEncLen; - -decrypt: - /* extra operation for decrypt case */ - if (!ctx->enc) { - /* keep last block for final step when decrypting - * multiple of block size */ - if (ctx->blockSize > 1 && !ctx->bufLen) { - *dstLen -= ctx->blockSize; - ctx->final_used = 1; - XMEMCPY(ctx->final, &dst[*dstLen], ctx->blockSize); - } - else - ctx->final_used = 0; - - if (fixLen) - *dstLen += ctx->blockSize; - } - - return 1; -} - -/* return 1 on ok, 0 on failure to match API compatibility */ -int wc_EVP_CipherFinal(WOLFCRYPT_EVP_CIPHER_CTX *ctx, byte *dst, int *dstLen) -{ - int ret; - - if (ctx->blockSize == 1) { - *dstLen = 0; - WOLFSSL_MSG("wc_EVP_CipherFinal: blocksize 1"); - return 1; - } - - if (ctx->enc) { - if (ctx->padding) { - /* add padding */ - XMEMSET(ctx->buf+ctx->bufLen, (byte)(ctx->blockSize-ctx->bufLen), - ctx->blockSize-ctx->bufLen); - - ret = wc_EVP_Cipher(ctx, dst, ctx->buf, ctx->blockSize); - if (ret != 1) { - WOLFSSL_MSG("wc_EVP_CipherFinal failure"); - return 0; - } - - *dstLen = ctx->blockSize; - } - else { - if (ctx->bufLen) { - ret = wc_EVP_Cipher(ctx, dst, ctx->buf, ctx->bufLen); - if (ret != 1) { - WOLFSSL_MSG("wc_EVP_CipherFinal failure"); - return 0; - } - - *dstLen = ctx->bufLen; - } - else { - WOLFSSL_MSG("wc_EVP_CipherFinal: Nothing to do"); - *dstLen = 0; - } - } - } - else { - int i, pad; - - /* decrypt pending data, case of stream cipher */ - if (ctx->bufLen && !ctx->final_used) { - ret = wc_EVP_Cipher(ctx, dst, ctx->buf, ctx->bufLen); - if (ret != 1) { - WOLFSSL_MSG("wc_EVP_CipherFinal failure"); - return 0; - } - - *dstLen = ctx->bufLen; - ctx->bufLen = 0; - - return 1; - } - else if (ctx->bufLen || !ctx->final_used) { - WOLFSSL_MSG("wc_EVP_CipherFinal: Wrong final block length"); - return 0; - } - - /* get padding */ - if (ctx->padding) { - pad = (int)ctx->final[ctx->blockSize-1]; - if (!pad || pad > (int)ctx->blockSize) { - WOLFSSL_MSG("wc_EVP_CipherFinal: Bad decrypt"); - return 0; - } - - /* check padding */ - for (i = 0; i < pad; i++) { - if (ctx->final[ctx->blockSize-1-i] != pad) { - WOLFSSL_MSG("wc_EVP_CipherFinal: Bad decrypt"); - return 0; - } - } - - /* return data without padding */ - *dstLen = ctx->blockSize-pad; - XMEMCPY(dst, ctx->final, *dstLen); - } - else { - /* return data */ - *dstLen = ctx->blockSize; - XMEMCPY(dst, ctx->final, *dstLen); - } - } - - return 1; -} - -/* return 1 on ok, 0 on failure to match API compatibility */ -int wc_EVP_CIPHER_CTX_copy(WOLFCRYPT_EVP_CIPHER_CTX *out, - const WOLFCRYPT_EVP_CIPHER_CTX *in) -{ - WOLFSSL_ENTER("wc_EVP_CIPHER_CTX_copy"); - - if (in == NULL || out == NULL) { - WOLFSSL_MSG("Bad function argument"); - return 0; - } - - wc_EVP_CIPHER_CTX_cleanup(out); - XMEMCPY(out, in, sizeof(WOLFCRYPT_EVP_CIPHER_CTX)); - - return 1; -} - -/* 1 on ok */ -int wc_EVP_CIPHER_CTX_key_length(WOLFCRYPT_EVP_CIPHER_CTX* ctx) -{ - WOLFSSL_ENTER("wc_EVP_CIPHER_CTX_key_length"); - if (ctx) - return ctx->keyLen; - - return 0; /* failure */ -} - - -/* 1 on ok */ -int wc_EVP_CIPHER_CTX_set_key_length(WOLFCRYPT_EVP_CIPHER_CTX* ctx, int keylen) -{ - WOLFSSL_ENTER("wc_EVP_CIPHER_CTX_set_key_length"); - if (ctx) - ctx->keyLen = keylen; - else - return 0; /* failure */ - - return 1; -} - - -/* 1 on ok */ -int wc_EVP_Cipher(WOLFCRYPT_EVP_CIPHER_CTX* ctx, byte* dst, byte* src, - word32 len) -{ - int ret = 0; - WOLFSSL_ENTER("wc_EVP_Cipher"); - - if (ctx == NULL || dst == NULL || src == NULL) { - WOLFSSL_MSG("Bad function argument"); - return 0; /* failure */ - } - - if (ctx->cipherType == 0xff) { - WOLFSSL_MSG("no init"); - return 0; /* failure */ - } - - switch (ctx->cipherType) { - -#ifndef NO_AES - case AES_128_CBC_TYPE : - case AES_192_CBC_TYPE : - case AES_256_CBC_TYPE : - WOLFSSL_MSG("AES CBC"); - if (ctx->ivUpdate > 1) { - ret = wc_AesSetIV(&ctx->cipher.aes, ctx->iv); - if (ret != 0) { - WOLFSSL_MSG("wc_EVP_Cipher failure"); - return 0; /* failure */ - } - } - - if (ctx->enc) - ret = wc_AesCbcEncrypt(&ctx->cipher.aes, dst, src, len); - else - ret = wc_AesCbcDecrypt(&ctx->cipher.aes, dst, src, len); - break; - -#ifdef WOLFSSL_AES_COUNTER - case AES_128_CTR_TYPE : - case AES_192_CTR_TYPE : - case AES_256_CTR_TYPE : - WOLFSSL_MSG("AES CTR"); - wc_AesCtrEncrypt(&ctx->cipher.aes, dst, src, len); - break; -#endif -#endif /* NO_AES */ - -#ifndef NO_DES3 - case DES_CBC_TYPE : - if (ctx->ivUpdate > 1) - wc_Des_SetIV(&ctx->cipher.des, ctx->iv); - - if (ctx->enc) - wc_Des_CbcEncrypt(&ctx->cipher.des, dst, src, len); - else - wc_Des_CbcDecrypt(&ctx->cipher.des, dst, src, len); - break; - - case DES_EDE3_CBC_TYPE : - if (ctx->ivUpdate > 1) { - ret = wc_Des3_SetIV(&ctx->cipher.des3, ctx->iv); - if (ret != 0) { - WOLFSSL_MSG("wc_EVP_Cipher failure"); - return 0; /* failure */ - } - } - - if (ctx->enc) - ret = wc_Des3_CbcEncrypt(&ctx->cipher.des3, dst, src, len); - else - ret = wc_Des3_CbcDecrypt(&ctx->cipher.des3, dst, src, len); - break; -#endif - -#ifndef NO_RC4 - case ARC4_TYPE : - wc_Arc4Process(&ctx->cipher.arc4, dst, src, len); - break; -#endif - -#ifdef HAVE_IDEA - case IDEA_CBC_TYPE : - if (ctx->ivUpdate > 1) { - ret = wc_IdeaSetIV(&ctx->cipher.idea, ctx->iv); - if (ret != 0) { - WOLFSSL_MSG("wc_EVP_Cipher failure"); - return 0; /* failure */ - } - } - - if (ctx->enc) - wc_IdeaCbcEncrypt(&ctx->cipher.idea, dst, src, len); - else - wc_IdeaCbcDecrypt(&ctx->cipher.idea, dst, src, len); - break; -#endif - case NULL_CIPHER_TYPE : - XMEMCPY(dst, src, len); - break; - - default: { - WOLFSSL_MSG("bad type"); - return 0; /* failure */ - } - } - - if (ret != 0) { - WOLFSSL_MSG("wc_EVP_Cipher failure"); - return 0; /* failuer */ - } - - WOLFSSL_MSG("wc_EVP_Cipher success"); - return 1; /* success */ -} - -int wc_EVP_CIPHER_CTX_iv_length(const WOLFCRYPT_EVP_CIPHER_CTX* ctx) -{ - WOLFSSL_MSG("wc_EVP_CIPHER_CTX_iv_length"); - - switch (ctx->cipherType) { -#ifndef NO_AES - case AES_128_CBC_TYPE : - case AES_192_CBC_TYPE : - case AES_256_CBC_TYPE : - WOLFSSL_MSG("AES CBC"); - return AES_BLOCK_SIZE; - -#ifdef WOLFSSL_AES_COUNTER - case AES_128_CTR_TYPE : - case AES_192_CTR_TYPE : - case AES_256_CTR_TYPE : - WOLFSSL_MSG("AES CTR"); - return AES_BLOCK_SIZE; -#endif -#endif /* NO_AES */ - -#ifndef NO_DES3 - case DES_CBC_TYPE : - WOLFSSL_MSG("DES CBC"); - return DES_BLOCK_SIZE; - - case DES_EDE3_CBC_TYPE : - WOLFSSL_MSG("DES EDE3 CBC"); - return DES_BLOCK_SIZE; -#endif - -#ifdef HAVE_IDEA - case IDEA_CBC_TYPE : - WOLFSSL_MSG("IDEA CBC"); - return IDEA_BLOCK_SIZE; -#endif - case ARC4_TYPE : - WOLFSSL_MSG("ARC4"); - return 0; - - case NULL_CIPHER_TYPE : - WOLFSSL_MSG("NULL"); - return 0; - - default: { - WOLFSSL_MSG("bad type"); - } - } - return 0; -} - -#endif /* OPENSSL_EXTRA */ diff --git a/wolfcrypt/src/error.c b/wolfcrypt/src/error.c index 3bc69440b..75b16b023 100644 --- a/wolfcrypt/src/error.c +++ b/wolfcrypt/src/error.c @@ -386,99 +386,6 @@ const char* wc_GetErrorString(int error) case ASN_COUNTRY_SIZE_E: return "Country code size error, either too small or large"; - case BIO_CALLBACK_E: - return "BIO callback function failed"; - - case BIO_CREATE_METHOD_E: - return "BIO method create function failed"; - - case BIO_FIND_TYPE_E: - return "BIO find type failed"; - - case BIO_CTRL_E: - return "BIO Control function failed"; - - case BIO_UNINITIALIZED_E: - return "BIO is not initialized"; - - case BIO_PUTS_E: - return "BIO puts failed"; - - case BIO_FILE_READ_E: - return "BIO file read failed"; - - case BIO_FILE_WRITE_E: - return "BIO file write failed"; - - case BIO_FILE_MODE_E: - return "BIO file bad open mode"; - - case BIO_FILE_OPEN_E: - return "BIO file open failed"; - - case BIO_FILE_GETS_E: - return "BIO file gets failed"; - - case BIO_MEM_WRITE_E: - return "BIO memory write failed"; - - case BIO_B64_ENCODE_E: - return "BIO filter base64 encode failed"; - - case BIO_B64_DECODE_E: - return "BIO filter base64 decode failed"; - - case BIO_DGST_INIT_E: - return "BIO filter digest init failed"; - - case BIO_DGST_UPDATE_E: - return "BIO filter digest update failed"; - - case BIO_DGST_FINAL_E: - return "BIO filter digest final failed"; - - case BIO_NO_HOSTNAME_E: - return "BIO connect, no hostname provided"; - - case BIO_NO_PORT_E: - return "BIO connect, no port provided"; - - case BIO_CREATE_SOCKET_E: - return "BIO connect, create socket failed"; - - case BIO_NBIO_E: - return "BIO connect, NBIO error"; - - case BIO_CONNECT_E: - return "BIO connect, connect failed"; - - case BIO_ADDR_AF_INET_E: - return "BIO connect, address not AF_INET type"; - - case BIO_SRV_PROTO_E: - return "BIO connect, bad server protocol"; - - case BIO_WSASTARTUP_E: - return "BIO connect, WSA Startup failed"; - - case BIO_BIND_SOCKET_E: - return "BIO connect, Bind socket failed"; - - case BIO_LISTEN_SOCKET_E: - return "BIO connect, Listen socket failed"; - - case BIO_ACCEPT_E: - return "BIO connect, Accept failed"; - - case BIO_KEEPALIVE_E: - return "BIO connect, Keep ALive failed"; - - case BIO_OPTIONS_E: - return "BIO connect, Options error"; - - case BIO_BAD_REF: - return "BIO, Bad reference number"; - default: return "unknown error number"; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index f9b4ae130..0bfdc7599 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -118,7 +118,6 @@ #include #include #include - #include #endif @@ -249,13 +248,6 @@ int certext_test(void); #ifdef HAVE_IDEA int idea_test(void); #endif -#ifdef OPENSSL_EXTRA -int evp_test(void); -int bio_b64_test(void); -int bio_md_test(void); -int bio_test(void); -int bio_connect_test(void); -#endif /* OPENSSL_EXTRA */ /* General big buffer size for many tests. */ #define FOURK_BUF 4096 @@ -643,33 +635,6 @@ int wolfcrypt_test(void* args) else printf( "PKCS7signed test passed!\n"); #endif - -#ifdef OPENSSL_EXTRA - if ( (ret = bio_md_test()) != 0) - return err_sys("BIO digest test failed !\n", ret); - else - printf( "BIO Digest test passed!\n"); - - if ( (ret = bio_b64_test()) != 0) - return err_sys("B64 test failed !\n", ret); - else - printf( "BIO B64 test passed!\n"); - - if ( (ret = bio_connect_test()) != 0) - return err_sys("BIO Connect test failed !\n", ret); - else - printf( "BIO Connect test passed!\n"); - - if ( (ret = evp_test()) != 0) - return err_sys("EVP test failed !\n", ret); - else - printf( "EVP test passed!\n"); - - if ( (ret = bio_test()) != 0) - return err_sys("BIO test failed !\n", ret); - else - printf( "BIO test passed!\n"); -#endif /* OPENSSL_EXTRA */ #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY) ShowMemoryTracker(); @@ -817,7 +782,7 @@ int md2_test() wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen); wc_Md2Final(&md2, hash); - if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) + if (memcmp(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) return -155 - i; } @@ -879,7 +844,7 @@ int md5_test(void) wc_Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen); wc_Md5Final(&md5, hash); - if (XMEMCMP(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0) + if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0) return -5 - i; } @@ -957,7 +922,7 @@ int md4_test(void) wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen); wc_Md4Final(&md4, hash); - if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0) + if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0) return -205 - i; } @@ -1018,7 +983,7 @@ int sha_test(void) wc_ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen); wc_ShaFinal(&sha, hash); - if (XMEMCMP(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0) + if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0) return -10 - i; } @@ -1074,7 +1039,7 @@ int ripemd_test(void) (word32)test_ripemd[i].inLen); wc_RipeMdFinal(&ripemd, hash); - if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0) + if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0) return -10 - i; } @@ -1147,7 +1112,7 @@ int blake2b_test(void) if (ret != 0) return -4004; - if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) { + if (memcmp(digest, blake2b_vec[i], 64) != 0) { return -300 - i; } } @@ -1197,7 +1162,7 @@ int sha256_test(void) if (ret != 0) return -4007; - if (XMEMCMP(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0) + if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0) return -10 - i; } @@ -1252,7 +1217,7 @@ int sha512_test(void) if (ret != 0) return -4011; - if (XMEMCMP(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0) + if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0) return -10 - i; } @@ -1305,7 +1270,7 @@ int sha384_test(void) if (ret != 0) return -4014; - if (XMEMCMP(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0) + if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0) return -10 - i; } @@ -1378,7 +1343,7 @@ int hmac_md5_test(void) if (ret != 0) return -4017; - if (XMEMCMP(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0) + if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0) return -20 - i; #ifdef HAVE_CAVIUM wc_HmacFreeCavium(&hmac); @@ -1455,7 +1420,7 @@ int hmac_sha_test(void) if (ret != 0) return -4020; - if (XMEMCMP(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0) + if (memcmp(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0) return -20 - i; #ifdef HAVE_CAVIUM wc_HmacFreeCavium(&hmac); @@ -1536,7 +1501,7 @@ int hmac_sha256_test(void) if (ret != 0) return -4023; - if (XMEMCMP(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0) + if (memcmp(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0) return -20 - i; #ifdef HAVE_CAVIUM wc_HmacFreeCavium(&hmac); @@ -1618,7 +1583,7 @@ int hmac_blake2b_test(void) if (ret != 0) return -4026; - if (XMEMCMP(hash, test_hmac[i].output, BLAKE2B_256) != 0) + if (memcmp(hash, test_hmac[i].output, BLAKE2B_256) != 0) return -20 - i; #ifdef HAVE_CAVIUM wc_HmacFreeCavium(&hmac); @@ -1698,7 +1663,7 @@ int hmac_sha384_test(void) if (ret != 0) return -4029; - if (XMEMCMP(hash, test_hmac[i].output, SHA384_DIGEST_SIZE) != 0) + if (memcmp(hash, test_hmac[i].output, SHA384_DIGEST_SIZE) != 0) return -20 - i; } @@ -1778,7 +1743,7 @@ int hmac_sha512_test(void) if (ret != 0) return -4032; - if (XMEMCMP(hash, test_hmac[i].output, SHA512_DIGEST_SIZE) != 0) + if (memcmp(hash, test_hmac[i].output, SHA512_DIGEST_SIZE) != 0) return -20 - i; } @@ -1852,10 +1817,10 @@ int arc4_test(void) (word32)test_arc4[i].outLen); wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen); - if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen)) + if (memcmp(plain, test_arc4[i].input, test_arc4[i].outLen)) return -20 - i; - if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen)) + if (memcmp(cipher, test_arc4[i].output, test_arc4[i].outLen)) return -20 - 5 - i; #ifdef HAVE_CAVIUM @@ -1927,14 +1892,14 @@ int hc128_test(void) HC128 dec; /* align keys/ivs in plain/cipher buffers */ - XMEMCPY(plain, keys[i], 16); - XMEMCPY(cipher, ivs[i], 16); + memcpy(plain, keys[i], 16); + memcpy(cipher, ivs[i], 16); wc_Hc128_SetKey(&enc, plain, cipher); wc_Hc128_SetKey(&dec, plain, cipher); /* align input */ - XMEMCPY(plain, test_hc128[i].input, test_hc128[i].outLen); + memcpy(plain, test_hc128[i].input, test_hc128[i].outLen); if (wc_Hc128_Process(&enc, cipher, plain, (word32)test_hc128[i].outLen) != 0) { return -110; @@ -1944,10 +1909,10 @@ int hc128_test(void) return -115; } - if (XMEMCMP(plain, test_hc128[i].input, test_hc128[i].outLen)) + if (memcmp(plain, test_hc128[i].input, test_hc128[i].outLen)) return -120 - i; - if (XMEMCMP(cipher, test_hc128[i].output, test_hc128[i].outLen)) + if (memcmp(cipher, test_hc128[i].output, test_hc128[i].outLen)) return -120 - 5 - i; } @@ -2006,9 +1971,9 @@ int rabbit_test(void) byte* iv; /* align keys/ivs in plain/cipher buffers */ - XMEMCPY(plain, keys[i], 16); + memcpy(plain, keys[i], 16); if (ivs[i]) { - XMEMCPY(cipher, ivs[i], 8); + memcpy(cipher, ivs[i], 8); iv = cipher; } else iv = NULL; @@ -2016,14 +1981,14 @@ int rabbit_test(void) wc_RabbitSetKey(&dec, plain, iv); /* align input */ - XMEMCPY(plain, test_rabbit[i].input, test_rabbit[i].outLen); + memcpy(plain, test_rabbit[i].input, test_rabbit[i].outLen); wc_RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen); wc_RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen); - if (XMEMCMP(plain, test_rabbit[i].input, test_rabbit[i].outLen)) + if (memcmp(plain, test_rabbit[i].input, test_rabbit[i].outLen)) return -130 - i; - if (XMEMCMP(cipher, test_rabbit[i].output, test_rabbit[i].outLen)) + if (memcmp(cipher, test_rabbit[i].output, test_rabbit[i].outLen)) return -130 - 5 - i; } @@ -2286,7 +2251,7 @@ int poly1305_test(void) if (ret != 0) return -60; - if (XMEMCMP(tag, tests[i], sizeof(tag))) + if (memcmp(tag, tests[i], sizeof(tag))) return -61; } @@ -2301,7 +2266,7 @@ int poly1305_test(void) if (ret != 0) return -63; - if (XMEMCMP(tag, correct4, sizeof(tag))) + if (memcmp(tag, correct4, sizeof(tag))) return -64; /* Check fail of TLS MAC function if altering additional data */ @@ -2312,7 +2277,7 @@ int poly1305_test(void) if (ret != 0) return -65; - if (XMEMCMP(tag, correct4, sizeof(tag)) == 0) + if (memcmp(tag, correct4, sizeof(tag)) == 0) return -66; @@ -2624,10 +2589,10 @@ int des_test(void) return -32; wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher)); - if (XMEMCMP(plain, vector, sizeof(plain))) + if (memcmp(plain, vector, sizeof(plain))) return -33; - if (XMEMCMP(cipher, verify, sizeof(cipher))) + if (memcmp(cipher, verify, sizeof(cipher))) return -34; return 0; @@ -2693,10 +2658,10 @@ int des3_test(void) if (ret != 0) return -34; - if (XMEMCMP(plain, vector, sizeof(plain))) + if (memcmp(plain, vector, sizeof(plain))) return -35; - if (XMEMCMP(cipher, verify3, sizeof(cipher))) + if (memcmp(cipher, verify3, sizeof(cipher))) return -36; #ifdef HAVE_CAVIUM @@ -2757,10 +2722,10 @@ int aes_test(void) if (ret != 0) return -1006; - if (XMEMCMP(plain, msg, AES_BLOCK_SIZE)) + if (memcmp(plain, msg, AES_BLOCK_SIZE)) return -60; #endif /* HAVE_AES_DECRYPT */ - if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE)) + if (memcmp(cipher, verify, AES_BLOCK_SIZE)) return -61; #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT) @@ -2828,8 +2793,8 @@ int aes_test(void) msgSz <= sizeof(bigMsg); msgSz += AES_BLOCK_SIZE) { - XMEMSET(bigCipher, 0, sizeof(bigCipher)); - XMEMSET(bigPlain, 0, sizeof(bigPlain)); + memset(bigCipher, 0, sizeof(bigCipher)); + memset(bigPlain, 0, sizeof(bigPlain)); ret = wc_AesSetKey(&enc, bigKey, keySz, iv, AES_ENCRYPTION); if (ret != 0) return -1030; @@ -2844,7 +2809,7 @@ int aes_test(void) if (ret != 0) return -1033; - if (XMEMCMP(bigPlain, bigMsg, msgSz)) + if (memcmp(bigPlain, bigMsg, msgSz)) return -1034; } } @@ -2909,10 +2874,10 @@ int aes_test(void) wc_AesCtrEncrypt(&enc, cipher, ctrPlain, AES_BLOCK_SIZE*4); wc_AesCtrEncrypt(&dec, plain, cipher, AES_BLOCK_SIZE*4); - if (XMEMCMP(plain, ctrPlain, AES_BLOCK_SIZE*4)) + if (memcmp(plain, ctrPlain, AES_BLOCK_SIZE*4)) return -66; - if (XMEMCMP(cipher, ctrCipher, AES_BLOCK_SIZE*4)) + if (memcmp(cipher, ctrCipher, AES_BLOCK_SIZE*4)) return -67; /* let's try with just 9 bytes, non block size test */ @@ -2923,20 +2888,20 @@ int aes_test(void) wc_AesCtrEncrypt(&enc, cipher, ctrPlain, 9); wc_AesCtrEncrypt(&dec, plain, cipher, 9); - if (XMEMCMP(plain, ctrPlain, 9)) + if (memcmp(plain, ctrPlain, 9)) return -68; - if (XMEMCMP(cipher, ctrCipher, 9)) + if (memcmp(cipher, ctrCipher, 9)) return -69; /* and an additional 9 bytes to reuse tmp left buffer */ wc_AesCtrEncrypt(&enc, cipher, ctrPlain, 9); wc_AesCtrEncrypt(&dec, plain, cipher, 9); - if (XMEMCMP(plain, ctrPlain, 9)) + if (memcmp(plain, ctrPlain, 9)) return -70; - if (XMEMCMP(cipher, oddCipher, 9)) + if (memcmp(cipher, oddCipher, 9)) return -71; } #endif /* WOLFSSL_AES_COUNTER */ @@ -3091,45 +3056,45 @@ int aesgcm_test(void) byte resultC[sizeof(p)]; int result; - XMEMSET(resultT, 0, sizeof(resultT)); - XMEMSET(resultC, 0, sizeof(resultC)); - XMEMSET(resultP, 0, sizeof(resultP)); + memset(resultT, 0, sizeof(resultT)); + memset(resultC, 0, sizeof(resultC)); + memset(resultP, 0, sizeof(resultP)); wc_AesGcmSetKey(&enc, k1, sizeof(k1)); /* AES-GCM encrypt and decrypt both use AES encrypt internally */ wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv1, sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a)); - if (XMEMCMP(c1, resultC, sizeof(resultC))) + if (memcmp(c1, resultC, sizeof(resultC))) return -68; - if (XMEMCMP(t1, resultT, sizeof(resultT))) + if (memcmp(t1, resultT, sizeof(resultT))) return -69; result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC), iv1, sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a)); if (result != 0) return -70; - if (XMEMCMP(p, resultP, sizeof(resultP))) + if (memcmp(p, resultP, sizeof(resultP))) return -71; #ifndef HAVE_FIPS - XMEMSET(resultT, 0, sizeof(resultT)); - XMEMSET(resultC, 0, sizeof(resultC)); - XMEMSET(resultP, 0, sizeof(resultP)); + memset(resultT, 0, sizeof(resultT)); + memset(resultC, 0, sizeof(resultC)); + memset(resultP, 0, sizeof(resultP)); wc_AesGcmSetKey(&enc, k2, sizeof(k2)); /* AES-GCM encrypt and decrypt both use AES encrypt internally */ wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv2, sizeof(iv2), resultT, sizeof(resultT), a, sizeof(a)); - if (XMEMCMP(c2, resultC, sizeof(resultC))) + if (memcmp(c2, resultC, sizeof(resultC))) return -230; - if (XMEMCMP(t2, resultT, sizeof(resultT))) + if (memcmp(t2, resultT, sizeof(resultT))) return -231; result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC), iv2, sizeof(iv2), resultT, sizeof(resultT), a, sizeof(a)); if (result != 0) return -232; - if (XMEMCMP(p, resultP, sizeof(resultP))) + if (memcmp(p, resultP, sizeof(resultP))) return -233; #endif /* HAVE_FIPS */ @@ -3204,22 +3169,22 @@ int gmac_test(void) byte tag[16]; - XMEMSET(tag, 0, sizeof(tag)); + memset(tag, 0, sizeof(tag)); wc_GmacSetKey(&gmac, k1, sizeof(k1)); wc_GmacUpdate(&gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1)); - if (XMEMCMP(t1, tag, sizeof(t1)) != 0) + if (memcmp(t1, tag, sizeof(t1)) != 0) return -126; - XMEMSET(tag, 0, sizeof(tag)); + memset(tag, 0, sizeof(tag)); wc_GmacSetKey(&gmac, k2, sizeof(k2)); wc_GmacUpdate(&gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2)); - if (XMEMCMP(t2, tag, sizeof(t2)) != 0) + if (memcmp(t2, tag, sizeof(t2)) != 0) return -127; - XMEMSET(tag, 0, sizeof(tag)); + memset(tag, 0, sizeof(tag)); wc_GmacSetKey(&gmac, k3, sizeof(k3)); wc_GmacUpdate(&gmac, iv3, sizeof(iv3), a3, sizeof(a3), tag, sizeof(t3)); - if (XMEMCMP(t3, tag, sizeof(t3)) != 0) + if (memcmp(t3, tag, sizeof(t3)) != 0) return -128; return 0; @@ -3276,9 +3241,9 @@ int aesccm_test(void) int result; - XMEMSET(t2, 0, sizeof(t2)); - XMEMSET(c2, 0, sizeof(c2)); - XMEMSET(p2, 0, sizeof(p2)); + memset(t2, 0, sizeof(t2)); + memset(c2, 0, sizeof(c2)); + memset(p2, 0, sizeof(p2)); wc_AesCcmSetKey(&enc, k, sizeof(k)); /* AES-CCM encrypt and decrypt both use AES encrypt internally */ @@ -3286,16 +3251,16 @@ int aesccm_test(void) t2, sizeof(t2), a, sizeof(a)); if (result != 0) return -106; - if (XMEMCMP(c, c2, sizeof(c2))) + if (memcmp(c, c2, sizeof(c2))) return -107; - if (XMEMCMP(t, t2, sizeof(t2))) + if (memcmp(t, t2, sizeof(t2))) return -108; result = wc_AesCcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv), t2, sizeof(t2), a, sizeof(a)); if (result != 0) return -109; - if (XMEMCMP(p, p2, sizeof(p2))) + if (memcmp(p, p2, sizeof(p2))) return -110; /* Test the authentication failure */ @@ -3307,8 +3272,8 @@ int aesccm_test(void) /* Clear c2 to compare against p2. p2 should be set to zero in case of * authentication fail. */ - XMEMSET(c2, 0, sizeof(c2)); - if (XMEMCMP(p2, c2, sizeof(p2))) + memset(c2, 0, sizeof(c2)); + if (memcmp(p2, c2, sizeof(p2))) return -112; return 0; @@ -3467,24 +3432,24 @@ int camellia_test(void) switch (testVectors[i].type) { case CAM_ECB_ENC: wc_CamelliaEncryptDirect(&cam, out, testVectors[i].plaintext); - if (XMEMCMP(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE)) + if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; case CAM_ECB_DEC: wc_CamelliaDecryptDirect(&cam, out, testVectors[i].ciphertext); - if (XMEMCMP(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE)) + if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; case CAM_CBC_ENC: wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE); - if (XMEMCMP(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE)) + if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; case CAM_CBC_DEC: wc_CamelliaCbcDecrypt(&cam, out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE); - if (XMEMCMP(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE)) + if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; default: @@ -3584,7 +3549,7 @@ int idea_test(void) for (i = 0; i < IDEA_NB_TESTS; i++) { /* Set encryption key */ - XMEMSET(&idea, 0, sizeof(Idea)); + memset(&idea, 0, sizeof(Idea)); ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION); if (ret != 0) { @@ -3600,7 +3565,7 @@ int idea_test(void) } /* Set decryption key */ - XMEMSET(&idea, 0, sizeof(Idea)); + memset(&idea, 0, sizeof(Idea)); ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION); if (ret != 0) { @@ -3616,7 +3581,7 @@ int idea_test(void) } /* Set encryption key */ - XMEMSET(&idea, 0, sizeof(Idea)); + memset(&idea, 0, sizeof(Idea)); ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE, v_key[i], IDEA_ENCRYPTION); if (ret != 0) { @@ -3624,7 +3589,7 @@ int idea_test(void) return -1; } - XMEMSET(msg_enc, 0, sizeof(msg_enc)); + memset(msg_enc, 0, sizeof(msg_enc)); ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message, (word32)strlen(message)+1); if (ret != 0) { @@ -3633,7 +3598,7 @@ int idea_test(void) } /* Set decryption key */ - XMEMSET(&idea, 0, sizeof(Idea)); + memset(&idea, 0, sizeof(Idea)); ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE, v_key[i], IDEA_DECRYPTION); if (ret != 0) { @@ -3641,7 +3606,7 @@ int idea_test(void) return -1; } - XMEMSET(msg_dec, 0, sizeof(msg_dec)); + memset(msg_dec, 0, sizeof(msg_dec)); ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc, (word32)strlen(message)+1); if (ret != 0) { @@ -3657,7 +3622,7 @@ int idea_test(void) for (i = 0; i < IDEA_NB_TESTS_EXTRA; i++) { /* Set encryption key */ - XMEMSET(&idea, 0, sizeof(Idea)); + memset(&idea, 0, sizeof(Idea)); ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION); if (ret != 0) { @@ -3716,7 +3681,7 @@ int idea_test(void) return -41; /* Set encryption key */ - XMEMSET(&idea, 0, sizeof(Idea)); + memset(&idea, 0, sizeof(Idea)); ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_ENCRYPTION); if (ret != 0) { printf("wc_IdeaSetKey (enc) failed\n"); @@ -3724,7 +3689,7 @@ int idea_test(void) } /* Data encryption */ - XMEMSET(enc, 0, sizeof(enc)); + memset(enc, 0, sizeof(enc)); ret = wc_IdeaCbcEncrypt(&idea, enc, rnd, sizeof(rnd)); if (ret != 0) { printf("wc_IdeaCbcEncrypt failed\n"); @@ -3732,7 +3697,7 @@ int idea_test(void) } /* Set decryption key */ - XMEMSET(&idea, 0, sizeof(Idea)); + memset(&idea, 0, sizeof(Idea)); ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_DECRYPTION); if (ret != 0) { printf("wc_IdeaSetKey (enc) failed\n"); @@ -3740,7 +3705,7 @@ int idea_test(void) } /* Data decryption */ - XMEMSET(dec, 0, sizeof(dec)); + memset(dec, 0, sizeof(dec)); ret = wc_IdeaCbcDecrypt(&idea, dec, enc, sizeof(enc)); if (ret != 0) { printf("wc_IdeaCbcDecrypt failed\n"); @@ -4264,7 +4229,7 @@ int rsa_test(void) TEST_XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER); return -44; } - if (XMEMCMP(plain, in, inLen)) { + if (memcmp(plain, in, inLen)) { TEST_XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER); return -45; } @@ -4273,13 +4238,13 @@ int rsa_test(void) TEST_XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER); return -46; } - XMEMSET(plain, 0, sizeof(plain)); + memset(plain, 0, sizeof(plain)); ret = wc_RsaSSL_Verify(out, ret, plain, sizeof(plain), &key); if (ret < 0) { TEST_XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER); return -47; } - if (XMEMCMP(plain, in, ret)) { + if (memcmp(plain, in, ret)) { TEST_XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); return -48; } @@ -5663,7 +5628,7 @@ int dh_test(void) if (ret != 0) return -55; - if (XMEMCMP(agree, agree2, agreeSz)) + if (memcmp(agree, agree2, agreeSz)) return -56; wc_FreeDhKey(&key); @@ -5992,7 +5957,7 @@ int openssl_test(void) EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen); EVP_DigestFinal(&md_ctx, hash, 0); - if (XMEMCMP(hash, a.output, MD5_DIGEST_SIZE) != 0) + if (memcmp(hash, a.output, MD5_DIGEST_SIZE) != 0) return -71; #endif /* NO_MD5 */ @@ -6013,7 +5978,7 @@ int openssl_test(void) EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen); EVP_DigestFinal(&md_ctx, hash, 0); - if (XMEMCMP(hash, b.output, SHA_DIGEST_SIZE) != 0) + if (memcmp(hash, b.output, SHA_DIGEST_SIZE) != 0) return -72; #endif /* NO_SHA */ @@ -6032,7 +5997,7 @@ int openssl_test(void) EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen); EVP_DigestFinal(&md_ctx, hash, 0); - if (XMEMCMP(hash, d.output, SHA256_DIGEST_SIZE) != 0) + if (memcmp(hash, d.output, SHA256_DIGEST_SIZE) != 0) return -78; #ifdef WOLFSSL_SHA384 @@ -6052,7 +6017,7 @@ int openssl_test(void) EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen); EVP_DigestFinal(&md_ctx, hash, 0); - if (XMEMCMP(hash, e.output, SHA384_DIGEST_SIZE) != 0) + if (memcmp(hash, e.output, SHA384_DIGEST_SIZE) != 0) return -79; #endif /* WOLFSSL_SHA384 */ @@ -6076,7 +6041,7 @@ int openssl_test(void) EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen); EVP_DigestFinal(&md_ctx, hash, 0); - if (XMEMCMP(hash, f.output, SHA512_DIGEST_SIZE) != 0) + if (memcmp(hash, f.output, SHA512_DIGEST_SIZE) != 0) return -80; #endif /* WOLFSSL_SHA512 */ @@ -6095,7 +6060,7 @@ int openssl_test(void) HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen, hash, 0); - if (XMEMCMP(hash, c.output, MD5_DIGEST_SIZE) != 0) + if (memcmp(hash, c.output, MD5_DIGEST_SIZE) != 0) return -74; #endif /* NO_MD5 */ @@ -6135,17 +6100,17 @@ int openssl_test(void) DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT); DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT); - if (XMEMCMP(plain, vector, sizeof(vector)) != 0) + if (memcmp(plain, vector, sizeof(vector)) != 0) return -75; - if (XMEMCMP(cipher, verify, sizeof(verify)) != 0) + if (memcmp(cipher, verify, sizeof(verify)) != 0) return -76; /* test changing iv */ DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT); DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT); - if (XMEMCMP(cipher, verify, sizeof(verify)) != 0) + if (memcmp(cipher, verify, sizeof(verify)) != 0) return -77; } /* end des test */ @@ -6183,7 +6148,7 @@ int openssl_test(void) if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0) return -82; - if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE)) + if (memcmp(cipher, verify, AES_BLOCK_SIZE)) return -83; EVP_CIPHER_CTX_init(&ctx); @@ -6193,7 +6158,7 @@ int openssl_test(void) if (EVP_Cipher(&ctx, plain, cipher, 16) == 0) return -85; - if (XMEMCMP(plain, msg, AES_BLOCK_SIZE)) + if (memcmp(plain, msg, AES_BLOCK_SIZE)) return -86; @@ -6204,790 +6169,6 @@ int openssl_test(void) return 0; } -static int evp_enc_test(const WOLFCRYPT_EVP_CIPHER* type) -{ - WOLFCRYPT_EVP_CIPHER_CTX ctx; - int i, ret, len = 0, loop, loop_max, outLen, rand_size, witLen; - -#ifdef BENCH_EMBEDDED - byte brandom[100], wit[112], out[112]; -#else - byte brandom[1025], wit[1040], out[1040]; -#endif - - byte key[] = {0x9d, 0xbf, 0xe2, 0x11, 0xf9, 0xea, 0x19, 0xf3, - 0x44, 0x2e, 0xae, 0x19, 0x54, 0x80, 0x87, 0x4c, - 0x9d, 0xbf, 0xe2, 0x11, 0xf9, 0xea, 0x19, 0xf3, - 0x44, 0x2e, 0xae, 0x19, 0x54, 0x80, 0x87, 0x4c}; - byte iv[] = {0xde, 0xf9, 0x6b, 0x91, 0xda, 0x51, 0x71, 0x80, - 0x47, 0xc9, 0x68, 0xe5, 0xd7, 0x07, 0x85, 0x15}; - - WC_RNG rng; - - i = wc_InitRng(&rng); - if (i != 0) - return -1198; - - i = wc_RNG_GenerateBlock(&rng, brandom, sizeof(brandom)); - if (i != 0) - return -1199; - - wc_FreeRng(&rng); - -#ifdef BENCH_EMBEDDED - loop_max = 25; -#else - loop_max = 1000; -#endif - - /* Encrypt/Decrypt with round bytes block Update */ - for (loop = 0; loop < loop_max; loop++) { - rand_size = sizeof(brandom) - loop; - - /* Encrypt */ - wc_EVP_CIPHER_CTX_init(&ctx); - - XMEMSET(out, 0, sizeof(out)); - len = 0; - - ret = wc_EVP_CipherInit(&ctx, type, key, iv, 1); - if (ret != SSL_SUCCESS) - return -1200; - - ret = wc_EVP_CipherUpdate(&ctx, out+len, &outLen, - brandom, rand_size); - if (ret != SSL_SUCCESS) - return -1201; - - len += outLen; - - outLen = 0; - ret = wc_EVP_CipherFinal(&ctx, out+len, &outLen); - if (ret != SSL_SUCCESS) - return -1202; - - /* update final length */ - outLen += len; - - /* Decrypt */ - wc_EVP_CIPHER_CTX_init(&ctx); - - XMEMSET(wit, 0, sizeof(wit)); - len = 0; - - ret = wc_EVP_CipherInit(&ctx, type, key, iv, 0); - if (ret != SSL_SUCCESS) - return -1203; - - for (i = 0; i < outLen; i++) - { - witLen = 0; - ret = wc_EVP_CipherUpdate(&ctx, wit+len, &witLen, out+i, 1); - if (ret != SSL_SUCCESS) - return -1204; - - len += witLen; - } - - witLen = 0; - - ret = wc_EVP_CipherFinal(&ctx, wit+len, &witLen); - if (ret != SSL_SUCCESS) - return -1205; - - witLen += len; - - if (witLen != rand_size) - return -1206; - - if (XMEMCMP(brandom, wit, witLen)) - return -1207; - } - - return 0; -} - -int evp_test(void) -{ - int ret; - -#ifndef NO_AES - ret = evp_enc_test(wc_EVP_aes_128_cbc()); - if (ret != 0) - return ret; - - ret = evp_enc_test(wc_EVP_aes_192_cbc()); - if (ret != 0) - return ret; - - ret = evp_enc_test(wc_EVP_aes_256_cbc()); - if (ret != 0) - return ret; - -#ifdef WOLFSSL_AES_COUNTER - ret = evp_enc_test(wc_EVP_aes_128_ctr()); - if (ret != 0) - return ret; - - ret = evp_enc_test(wc_EVP_aes_192_ctr()); - if (ret != 0) - return ret; - - ret = evp_enc_test(wc_EVP_aes_256_ctr()); - if (ret != 0) - return ret; -#endif /* WOLFSSL_AES_COUNTER */ -#endif /* NO_AES */ - -#ifndef NO_RC4 - ret = evp_enc_test(wc_EVP_rc4()); - if (ret != 0) - return ret; -#endif /* NO_RC4 */ - -#ifndef NO_DES3 - ret = evp_enc_test(wc_EVP_des_cbc()); - if (ret != 0) - return ret; - - ret = evp_enc_test(wc_EVP_des_ede3_cbc()); - if (ret != 0) - return ret; -#endif /* NO_DES3 */ - -#ifdef HAVE_IDEA - ret = evp_enc_test(wc_EVP_idea_cbc()); - if (ret != 0) - return ret; -#endif /* HAVE_IDEA */ - - return 0; -} - -int bio_md_test(void) -{ - WOLFCRYPT_BIO *bmd, *bnull; - int i, size, total, w; - char digest[64]; /* SHA512_DIGEST_SIZE */ - - struct bio_digest { - const WOLFCRYPT_EVP_MD *type; - const char *data; - const char *digest; - } hash_list[] = { -#ifndef NO_MD5 - { - wc_EVP_md5(), - "1234567890123456789012345678901234567890123456789012345678" - "9012345678901234567890", - "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6" - "\x7a" - }, -#endif -#ifndef NO_SHA - { - wc_EVP_sha1(), - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaa", - "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7" - "\x53\x99\x5E\x26\xA0" - }, -#endif -#ifdef WOLFSSL_SHA256 - { - wc_EVP_sha256(), - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60" - "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB" - "\x06\xC1" - }, -#endif -#ifdef WOLFSSL_SHA384 - { - wc_EVP_sha384(), - "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi" - "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b" - "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0" - "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91" - "\x74\x60\x39" - }, -#endif -#ifdef WOLFSSL_SHA512 - { - wc_EVP_sha512(), - "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi" - "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", - "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14" - "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88" - "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4" - "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b" - "\x87\x4b\xe9\x09" - }, -#endif - { NULL, NULL, NULL } - }; - - i = 0; - while (hash_list[i].type != NULL) - { - /* Create a digest filter BIO */ - bmd = wc_BioNew(wc_Bio_f_md()); - if (bmd == NULL) - return -1065; - - /* set digest algorithm */ - wc_BioSetMd(bmd, hash_list[i].type); - - /* create a null bio to chain with digest */ - bnull = wc_BioNew(wc_Bio_s_null()); - if (bmd == NULL) - return -1066; - - if (wc_BioPush(bmd, bnull) == NULL) - return -1067; - - size = (int)strlen(hash_list[i].data); - - /* This loop writes the data to the digest. - * It checks for errors as if the underlying file were non-blocking */ - for (total = 0; total < size; total += w) - { - w = wc_BioWrite(bmd, hash_list[i].data + total, - size - (int)total); - if (w <= 0) { - if (wc_BioShouldRetry(bmd)) { - w = 0; - continue; - } - break; - } - } - - if (total != size) - return -1068; - - /* Ensure all of our data is pushed all the way to the BIO */ - if (wc_BioFlush(bmd) < 0) - return -1069; - - /* get the digest */ - XMEMSET(digest, 0, sizeof(digest)); - size = wc_BioGets(bmd, digest, sizeof(digest)); - if (size <= 0) - return -1070; - - if (size != wc_EVP_MD_size(hash_list[i].type)) - return -1071; - - if (XMEMCMP(digest, hash_list[i].digest, size)) - return -1072; - - /* free BIO */ - wc_BioFreeAll(bmd); - - i++; - } - - return 0; -} - -int bio_b64_test(void) -{ - WOLFCRYPT_BIO *b64, *file; - int i, loop, rand_size; - int total, w; - byte brandom[9000], wit[9000]; - WC_RNG rng; - - i = wc_InitRng(&rng); - if (i != 0) - return -1198; - - i = wc_RNG_GenerateBlock(&rng, brandom, sizeof(brandom)); - if (i != 0) - return -1199; - - wc_FreeRng(&rng); - - /* Encode/Decode brandom data */ - for (loop = 1; loop < 1000; loop++) { - rand_size = sizeof(brandom) - loop; - - /* Create a buffered file BIO for writing */ - if (loop & 1) - file = wc_BioNewFile("test_b64", "w"); - else - file = wc_BioNewFile("test_b64_nonl", "w"); - if (file == NULL) - return -1062; - - /* Create a base64 encoding filter BIO */ - b64 = wc_BioNew(wc_Bio_f_base64()); - if (b64 == NULL) - return -1065; - - if (wc_BioPush(b64, file) == NULL) - return -1069; - - /* This loop writes the data to the file. - * It checks for errors as if the underlying file were non-blocking */ - for (total = 0; total < rand_size; total += w) - { - w = wc_BioWrite(b64, brandom + total, - rand_size - (int)total); - if (w <= 0) { - if (wc_BioShouldRetry(b64)) { - w = 0; - continue; - } - break; - } - } - - /* Ensure all of our data is pushed all the way to the file */ - if (wc_BioFlush(b64) < 0) - return -1070; - - /* free BIO chain cipher-file */ - wc_BioFreeAll(b64); - - /* Start read / decode phase */ - - /* Create a buffered file BIO for writing */ - if (loop & 1) - file = wc_BioNewFile("test_b64", "r"); - else - file = wc_BioNewFile("test_b64_nonl", "r"); - if (file == NULL) - return -1071; - - /* Create a base64 encoding filter BIO */ - b64 = wc_BioNew(wc_Bio_f_base64()); - if (b64 == NULL) - return -1073; - - if (wc_BioPush(b64, file) == NULL) - return -1074; - - XMEMSET(wit, 0, sizeof(wit)); - for (total = 0; ; total += w) - { - w = wc_BioRead(b64, wit+total, - (int)sizeof(wit) - (int)total); - if (w <= 0) { - if (wc_BioShouldRetry(b64)) { - w = 0; - continue; - } - break; - } - } - - /* free BIO chain b64-file */ - wc_BioFreeAll(b64); - - /* check decoded data */ - if (total != rand_size) - return -1079; - - if (XMEMCMP(brandom, wit, total)) - return -1080; - } - - remove("test_b64"); - remove("test_b64_nonl"); - return 0; -} - -static int bio_filter_test(const WOLFCRYPT_EVP_CIPHER* cipher_type) -{ - int total, i, w, rand_size; - WOLFCRYPT_BIO *cipher, *buff, *file, *b64; - byte key[] = { 0x63, 0x21, 0x5f, 0x9b, 0xdc, 0x74, 0xcc, 0x90, - 0x96, 0x43, 0xd1, 0xcc, 0x7a, 0xb2, 0x27, 0xc5 }; - byte iv[] = { 0x1a, 0x4a, 0xf8, 0xd5, 0xd0, 0x09, 0xff, 0x09, - 0xd6, 0xb3, 0x72, 0x3d, 0xc4, 0xed, 0x9c, 0x8e }; - - byte brandom[4200], wit[4200]; - - WC_RNG rng; - - w = wc_InitRng(&rng); - if (w != 0) - return 1060; - - w = wc_RNG_GenerateBlock(&rng, brandom, sizeof(brandom)); - if (w != 0) - return 1061; - - wc_FreeRng(&rng); - - for (i = 1; i < 300; i++) { - rand_size = sizeof(brandom) - i; - - /* Create a buffered file BIO for writing */ - if (i & 1) - file = wc_BioNewFile("test_assembling_nonl", "w"); - else - file = wc_BioNewFile("test_assembling", "w"); - if (file == NULL) - return -1062; - - /* Create a buffering filter BIO to buffer writes to the file */ - buff = wc_BioNew(wc_Bio_f_buffer()); - if (buff == NULL) - return -1063; - - /* Create a cipher filter BIO */ - cipher = wc_BioNew(wc_Bio_f_cipher()); - if (cipher == NULL) - return -1064; - - /* Create a base64 encoding filter BIO */ - b64 = wc_BioNew(wc_Bio_f_base64()); - if (b64 == NULL) - return -1065; - - if (i & 1) - wc_BioSetFlags(b64, BIO_FLAGS_BASE64_NO_NL); - - /* Start encrypt / write phase */ - - /* Set cipher key and encryption mode */ - wc_BioSetCipher(cipher, cipher_type, key, iv, 1); - - /* Assemble the BIO chain to be in the order cipher-base64-buffer-file */ - - if (wc_BioPush(cipher, b64) == NULL) - return -1066; - - if (wc_BioPush(b64, buff) == NULL) - return -1067; - - if (wc_BioPush(buff, file) == NULL) - return -1068; - - - /* This loop writes the data to the file. - * It checks for errors as if the underlying file were non-blocking */ - for (total = 0; total < rand_size; total += w) - { - w = wc_BioWrite(cipher, brandom + total, - rand_size - (int)total); - if (w <= 0) { - if (wc_BioShouldRetry(cipher)) { - w = 0; - continue; - } - break; - } - } - - /* Ensure all of our data is pushed all the way to the file */ - if (wc_BioFlush(cipher) < 0) - return - 1069; - - /* get b64 BIO to free it only at first (test purpose) */ - wc_BioPop(b64); - - /* free b64 BIO */ - wc_BioFree(b64); - - /* free BIO chain cipher-base64-buffer-file */ - wc_BioFreeAll(cipher); - - /* Start read / decrypt phase */ - - /* Create a buffered file BIO for writing */ - if (i & 1) - file = wc_BioNewFile("test_assembling_nonl", "r"); - else - file = wc_BioNewFile("test_assembling", "r"); - if (file == NULL) - return -1070; - - /* Create a buffering filter BIO to buffer writes to the file */ - buff = wc_BioNew(wc_Bio_f_buffer()); - if (buff == NULL) - return -1071; - - /* Create a cipher filter BIO */ - cipher = wc_BioNew(wc_Bio_f_cipher()); - if (cipher == NULL) - return -1072; - - /* Create a base64 encoding filter BIO */ - b64 = wc_BioNew(wc_Bio_f_base64()); - if (b64 == NULL) - return -1073; - - if (i & 1) - wc_BioSetFlags(b64, BIO_FLAGS_BASE64_NO_NL); - - /* Set cipher key and decryption mode */ - wc_BioSetCipher(cipher, cipher_type, key, iv, 0); - - if (wc_BioPush(cipher, b64) == NULL) - return -1074; - - if (wc_BioPush(b64, buff) == NULL) - return -1075; - - if (wc_BioPush(buff, file) == NULL) - return -1077; - - /* This loop read the data from the file. - * It checks for errors as if the underlying file were non-blocking */ - XMEMSET(wit, 0, sizeof(wit)); - - for (total = 0; ; total += w) - { - w = wc_BioRead(cipher, wit+total, - (int)sizeof(wit) - (int)total); - if (w <= 0) { - if (wc_BioShouldRetry(cipher)) { - w = 0; - continue; - } - break; - } - } - - if (!wc_BioGetCipherStatus(cipher)) - return -1078; - - /* get b64 BIO to free it only at first (test purpose) */ - wc_BioPop(b64); - - /* free b64 BIO */ - wc_BioFree(b64); - - /* free BIO chain cipher-buffer-file */ - wc_BioFreeAll(cipher); - - /* check decrypted data */ - if (total != rand_size) - return -1079; - - if (XMEMCMP(brandom, wit, total)) - return -1080; - } - - remove("test_assembling_nonl"); - remove("test_assembling"); - - return 0; -} - -int bio_connect_test(void) -{ - WOLFCRYPT_BIO *cbio; - int len; - char buf[1024]; - - cbio = wc_BioNewConnect("www.wolfssl.com:http"); - if (cbio == NULL) - return -3000; - - if (wc_BioDoConnect(cbio) <= 0) { - fprintf(stderr, "Error connecting to server\n"); - return -3002; - } - - wc_BioPuts(cbio, "GET / HTTP/1.0\r\n\r\n"); - for(;;) { - len = wc_BioRead(cbio, buf, sizeof(buf)); - if (len == 0) - break; - else if (len < 0) - return -3003; - } - - wc_BioFree(cbio); - - return 0; -} - -int bio_test(void) -{ - WOLFCRYPT_BIO *bio; - int ret; - - const char buf[] = "This is a test of the implementation of OpenSSL BIO"; - char buf_w[200]; - - /* BIO FILE TEST */ - - /* create file */ - bio = wc_BioNewFile("test_bio.txt", "w"); - if (bio == NULL) - return -1000; - - /* write */ - ret = wc_BioWrite(bio, buf, (int)strlen(buf)); - if (ret != (int)strlen(buf)) - return -1001; - - ret = wc_BioPuts(bio, "\n"); - if (ret != 1) - return -1002; - - /* close */ - wc_BioFree(bio); - - /* open */ - bio = wc_BioNewFile("test_bio.txt", "r"); - if (bio == NULL) - return -1003; - - /* read */ - ret = wc_BioRead(bio, buf_w, sizeof(buf_w)); - if (ret != (int)strlen(buf)+1) - return -1004; - - if (XMEMCMP(buf, buf_w, ret-1)) - return -1005; - - /* close */ - wc_BioFree(bio); - - /* append */ - bio = wc_BioNewFile("test_bio.txt", "a+"); - if (bio == NULL) - return -1006; - - ret = wc_BioTell(bio); - if (ret != (int)strlen(buf)+1) - return -1007; - - /* write */ - ret = wc_BioPrintf(bio, "%s\n", buf); - if (ret != (int)strlen(buf)+1) - return -1008; - - ret = wc_BioTell(bio); - if (ret != 2*((int)strlen(buf)+1)) - return -1009; - - /* reset, before reading */ - ret = wc_BioReset(bio); - if (ret < 0) - return -1010; - - /* read */ - ret = wc_BioRead(bio, buf_w, sizeof(buf_w)); - if (ret != 2*((int)strlen(buf)+1)) - return -1011; - - /* close */ - wc_BioFree(bio); - - if (XMEMCMP(buf, buf_w, (int)strlen(buf))) - return -1012; - - if (XMEMCMP(buf, buf_w+1+strlen(buf), (int)strlen(buf))) - return -1013; - - /* BIO MEMORY TEST */ - bio = wc_BioNew(wc_Bio_s_mem()); - if (bio == NULL) - return -1050; - - ret = wc_BioPuts(bio, buf); - if (ret != (int)strlen(buf)) - return -1051; - - ret = wc_BioGets(bio, buf_w, sizeof(buf_w)); - if (ret != (int)strlen(buf)) - return -1052; - - ret = wc_BioGets(bio, buf_w, sizeof(buf_w)); - if (ret != 0) - return -1053; - - ret = wc_BioReset(bio); - if (ret < 0) - return -1054; - - ret = wc_BioWrite(bio, buf, (int)strlen(buf)); - if (ret != (int)strlen(buf)) - return -1055; - - ret = wc_BioWrite(bio, buf, (int)strlen(buf)); - if (ret != (int)strlen(buf)) - return -1056; - - XMEMSET(buf_w, 0, sizeof(buf_w)); - - ret = wc_BioRead(bio, buf_w, 5); - ret += wc_BioRead(bio, buf_w+5, 9); - ret += wc_BioRead(bio, buf_w+14, 17); - ret += wc_BioRead(bio, buf_w+31, 7); - - if (ret != 2*(int)strlen(buf)-wc_BioPending(bio)) - return -1059; - - if (XMEMCMP(buf, buf_w, 2*(int)strlen(buf)-wc_BioPending(bio))) - return -1060; - - wc_BioFree(bio); - - /* assembling BIO test for all cipher */ -#ifndef NO_AES - ret = bio_filter_test(wc_EVP_aes_128_cbc()); - if (ret != 0) - return ret; - - ret = bio_filter_test(wc_EVP_aes_192_cbc()); - if (ret != 0) - return ret; - - ret = bio_filter_test(wc_EVP_aes_256_cbc()); - if (ret != 0) - return ret; - -#ifdef WOLFSSL_AES_COUNTER - ret = bio_filter_test(wc_EVP_aes_128_ctr()); - if (ret != 0) - return ret; - - ret = bio_filter_test(wc_EVP_aes_192_ctr()); - if (ret != 0) - return ret; - - ret = bio_filter_test(wc_EVP_aes_256_ctr()); - if (ret != 0) - return ret; -#endif /* WOLFSSL_AES_COUNTER */ -#endif /* NO_AES */ - -#ifndef NO_RC4 - ret = bio_filter_test(wc_EVP_rc4()); - if (ret != 0) - return ret; -#endif /* NO_RC4 */ - -#ifndef NO_DES3 - ret = bio_filter_test(wc_EVP_des_cbc()); - if (ret != 0) - return ret; - - ret = bio_filter_test(wc_EVP_des_ede3_cbc()); - if (ret != 0) - return ret; -#endif /* NO_DES3 */ - -#ifdef HAVE_IDEA - ret = bio_filter_test(wc_EVP_idea_cbc()); - if (ret != 0) - return ret; -#endif /* HAVE_IDEA */ - - return 0; -} - #endif /* OPENSSL_EXTRA */ @@ -7025,7 +6206,7 @@ int pkcs12_test(void) if (ret < 0) return -103; - if ( (ret = XMEMCMP(derived, verify, kLen)) != 0) + if ( (ret = memcmp(derived, verify, kLen)) != 0) return -104; iterations = 1000; @@ -7039,7 +6220,7 @@ int pkcs12_test(void) if (ret < 0) return -106; - if ( (ret = XMEMCMP(derived, verify2, 24)) != 0) + if ( (ret = memcmp(derived, verify2, 24)) != 0) return -107; return 0; @@ -7064,7 +6245,7 @@ int pbkdf2_test(void) if (ret != 0) return ret; - if (XMEMCMP(derived, verify, sizeof(verify)) != 0) + if (memcmp(derived, verify, sizeof(verify)) != 0) return -102; return 0; @@ -7088,7 +6269,7 @@ int pbkdf1_test(void) wc_PBKDF1(derived, (byte*)passwd, (int)strlen(passwd), salt, 8, iterations, kLen, SHA); - if (XMEMCMP(derived, verify, sizeof(verify)) != 0) + if (memcmp(derived, verify, sizeof(verify)) != 0) return -101; return 0; @@ -7160,7 +6341,7 @@ int hkdf_test(void) if (ret != 0) return -2001; - if (XMEMCMP(okm1, res1, L) != 0) + if (memcmp(okm1, res1, L) != 0) return -2002; #ifndef HAVE_FIPS @@ -7169,7 +6350,7 @@ int hkdf_test(void) if (ret != 0) return -2003; - if (XMEMCMP(okm1, res2, L) != 0) + if (memcmp(okm1, res2, L) != 0) return -2004; #endif /* HAVE_FIPS */ #endif /* NO_SHA */ @@ -7179,7 +6360,7 @@ int hkdf_test(void) if (ret != 0) return -2005; - if (XMEMCMP(okm1, res3, L) != 0) + if (memcmp(okm1, res3, L) != 0) return -2006; #ifndef HAVE_FIPS @@ -7188,7 +6369,7 @@ int hkdf_test(void) if (ret != 0) return -2007; - if (XMEMCMP(okm1, res4, L) != 0) + if (memcmp(okm1, res4, L) != 0) return -2007; #endif /* HAVE_FIPS */ #endif /* NO_SHA256 */ @@ -7231,7 +6412,7 @@ static int ecc_test_vector_item(const eccVector* vector) wc_ecc_init(&userA); - XMEMSET(sig, 0, sizeof(sig)); + memset(sig, 0, sizeof(sig)); x = sizeof(sig); ret = wc_ecc_import_raw(&userA, vector->Qx, vector->Qy, @@ -7548,7 +6729,7 @@ static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount, if (y != x) ERROR_OUT(-1004, done); - if (XMEMCMP(sharedA, sharedB, x)) + if (memcmp(sharedA, sharedB, x)) ERROR_OUT(-1005, done); #endif /* HAVE_ECC_DHE */ @@ -7569,7 +6750,7 @@ static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount, if (ret != 0) ERROR_OUT(-1008, done); - if (XMEMCMP(sharedA, sharedB, y)) + if (memcmp(sharedA, sharedB, y)) ERROR_OUT(-1009, done); #endif /* HAVE_ECC_DHE */ @@ -7592,7 +6773,7 @@ static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount, if (ret != 0) ERROR_OUT(-1012, done); - if (XMEMCMP(sharedA, sharedB, y)) + if (memcmp(sharedA, sharedB, y)) ERROR_OUT(-1013, done); #endif /* HAVE_ECC_DHE */ #endif /* HAVE_COMP_KEY */ @@ -7799,7 +6980,7 @@ int ecc_encrypt_test(void) if (ret != 0) return -3004; - if (XMEMCMP(plain, msg, sizeof(msg)) != 0) + if (memcmp(plain, msg, sizeof(msg)) != 0) return -3005; @@ -7818,12 +6999,12 @@ int ecc_encrypt_test(void) tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx); if (tmpSalt == NULL) return -3007; - XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ); + memcpy(cliSalt, tmpSalt, EXCHANGE_SALT_SZ); tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx); if (tmpSalt == NULL) return -3007; - XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ); + memcpy(srvSalt, tmpSalt, EXCHANGE_SALT_SZ); /* in actual use, we'd get the peer's salt over the transport */ ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt); @@ -7847,7 +7028,7 @@ int ecc_encrypt_test(void) if (ret != 0) return -3010; - if (XMEMCMP(plain, msg, sizeof(msg)) != 0) + if (memcmp(plain, msg, sizeof(msg)) != 0) return -3011; { @@ -7873,7 +7054,7 @@ int ecc_encrypt_test(void) if (ret != 0) return -3013; - if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) + if (memcmp(plain2, msg2, sizeof(msg2)) != 0) return -3014; } @@ -8797,7 +7978,7 @@ int compress_test(void) if (ret == 0 && wc_DeCompress(d, dSz, c, cSz) != (int)dSz) ret = -302; - if (ret == 0 && XMEMCMP(d, sample_text, dSz)) + if (ret == 0 && memcmp(d, sample_text, dSz)) ret = -303; if (c) TEST_XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); @@ -8896,7 +8077,7 @@ int pkcs7enveloped_test(void) } /* test decode result */ - if (XMEMCMP(decoded, data, sizeof(data)) != 0) { + if (memcmp(decoded, data, sizeof(data)) != 0) { TEST_XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); TEST_XFREE(privKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); return -205; diff --git a/wolfssl-ntru.vcproj b/wolfssl-ntru.vcproj index 5b7ba1f2f..b427e9c0a 100755 --- a/wolfssl-ntru.vcproj +++ b/wolfssl-ntru.vcproj @@ -165,11 +165,7 @@ - - - + @@ -179,12 +175,8 @@ > - - + RelativePath=".\wolfcrypt\src\coding.c" + > - - - - - - diff --git a/wolfssl/internal.h b/wolfssl/internal.h index e17e139b0..01301001a 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -27,8 +27,6 @@ #include #include -#include - #ifdef HAVE_CRL #include #endif @@ -973,6 +971,15 @@ enum Misc { MAX_REQUEST_SZ = 256, /* Maximum cert req len (no auth yet */ SESSION_FLUSH_COUNT = 256, /* Flush session cache unless user turns off */ + RC4_KEY_SIZE = 16, /* always 128bit */ + DES_KEY_SIZE = 8, /* des */ + DES3_KEY_SIZE = 24, /* 3 des ede */ + DES_IV_SIZE = DES_BLOCK_SIZE, + AES_256_KEY_SIZE = 32, /* for 256 bit */ + AES_192_KEY_SIZE = 24, /* for 192 bit */ + AES_IV_SIZE = 16, /* always block size */ + AES_128_KEY_SIZE = 16, /* for 128 bit */ + AEAD_SEQ_OFFSET = 4, /* Auth Data: Sequence number */ AEAD_TYPE_OFFSET = 8, /* Auth Data: Type */ AEAD_VMAJ_OFFSET = 9, /* Auth Data: Major Version */ @@ -1010,6 +1017,7 @@ enum Misc { RABBIT_KEY_SIZE = 16, /* 128 bits */ RABBIT_IV_SIZE = 8, /* 64 bits for iv */ + EVP_SALT_SIZE = 8, /* evp salt size 64 bits */ ECDHE_SIZE = 32, /* ECHDE server size defaults to 256 bit */ MAX_EXPORT_ECC_SZ = 256, /* Export ANS X9.62 max future size */ @@ -1216,6 +1224,35 @@ WOLFSSL_LOCAL ProtocolVersion MakeTLSv1_2(void); #endif #endif + +enum BIO_TYPE { + BIO_BUFFER = 1, + BIO_SOCKET = 2, + BIO_SSL = 3, + BIO_MEMORY = 4 +}; + + +/* wolfSSL BIO_METHOD type */ +struct WOLFSSL_BIO_METHOD { + byte type; /* method type */ +}; + + +/* wolfSSL BIO type */ +struct WOLFSSL_BIO { + byte type; /* method type */ + byte close; /* close flag */ + byte eof; /* eof flag */ + WOLFSSL* ssl; /* possible associated ssl */ + byte* mem; /* memory buffer */ + int memLen; /* memory buffer length */ + int fd; /* possible file descriptor */ + WOLFSSL_BIO* prev; /* previous in chain */ + WOLFSSL_BIO* next; /* next in chain */ +}; + + /* wolfSSL method type */ struct WOLFSSL_METHOD { ProtocolVersion version; @@ -2231,7 +2268,6 @@ WOLFSSL_LOCAL WOLFSSL_SESSION* GetSession(WOLFSSL*, byte*, byte); WOLFSSL_LOCAL int SetSession(WOLFSSL*, WOLFSSL_SESSION*); -WOLFSSL_LOCAL int DupSession(WOLFSSL* ssl, WOLFSSL* ossl); typedef int (*hmacfp) (WOLFSSL*, byte*, const byte*, word32, int, int); @@ -2673,8 +2709,8 @@ struct WOLFSSL { Keys keys; Options options; #ifdef OPENSSL_EXTRA - WOLFCRYPT_BIO* biord; /* socket bio read to free/close */ - WOLFCRYPT_BIO* biowr; /* socket bio write to free/close */ + WOLFSSL_BIO* biord; /* socket bio read to free/close */ + WOLFSSL_BIO* biowr; /* socket bio write to free/close */ #endif #ifndef NO_RSA RsaKey* peerRsaKey; @@ -2823,8 +2859,6 @@ void FreeSSL(WOLFSSL*, void* heap); WOLFSSL_API void SSL_ResourceFree(WOLFSSL*); /* Micrium uses */ -WOLFSSL_LOCAL int DupSSL(WOLFSSL* ssl, WOLFSSL* ossl); - enum { IV_SZ = 32, /* max iv sz */ NAME_SZ = 80 /* max one line */ diff --git a/wolfssl/openssl/evp.h b/wolfssl/openssl/evp.h index 14b5089d4..f972d2594 100644 --- a/wolfssl/openssl/evp.h +++ b/wolfssl/openssl/evp.h @@ -21,7 +21,7 @@ -/* evp.h defines mini evp openssl compatibility layer +/* evp.h defines mini evp openssl compatibility layer * */ @@ -31,10 +31,6 @@ #include -#ifdef __cplusplus - extern "C" { -#endif - #ifdef WOLFSSL_PREFIX #include "prefix_evp.h" #endif @@ -48,86 +44,223 @@ #include #include -#include #include #include #include -#include +#ifdef __cplusplus + extern "C" { +#endif + +typedef char WOLFSSL_EVP_MD; +typedef char WOLFSSL_EVP_CIPHER; + +#ifndef NO_MD5 + WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_md5(void); +#endif +WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_sha1(void); +WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_sha256(void); +WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_sha384(void); +WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_sha512(void); +WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_ripemd160(void); + +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_cbc(void); +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_cbc(void); +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_cbc(void); +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ctr(void); +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_ctr(void); +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_ctr(void); +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_cbc(void); +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ede3_cbc(void); +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_rc4(void); +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_idea_cbc(void); +WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_enc_null(void); + + +typedef union { + #ifndef NO_MD5 + WOLFSSL_MD5_CTX md5; + #endif + WOLFSSL_SHA_CTX sha; + WOLFSSL_SHA256_CTX sha256; + #ifdef WOLFSSL_SHA384 + WOLFSSL_SHA384_CTX sha384; + #endif + #ifdef WOLFSSL_SHA512 + WOLFSSL_SHA512_CTX sha512; + #endif + #ifdef WOLFSSL_RIPEMD + WOLFSSL_RIPEMD_CTX ripemd; + #endif +} WOLFSSL_Hasher; + + +typedef struct WOLFSSL_EVP_MD_CTX { + unsigned char macType; + WOLFSSL_Hasher hash; +} WOLFSSL_EVP_MD_CTX; + + +typedef union { +#ifndef NO_AES + Aes aes; +#endif +#ifndef NO_DES3 + Des des; + Des3 des3; +#endif + Arc4 arc4; +#ifdef HAVE_IDEA + Idea idea; +#endif +} WOLFSSL_Cipher; + + +enum { + AES_128_CBC_TYPE = 1, + AES_192_CBC_TYPE = 2, + AES_256_CBC_TYPE = 3, + AES_128_CTR_TYPE = 4, + AES_192_CTR_TYPE = 5, + AES_256_CTR_TYPE = 6, + DES_CBC_TYPE = 7, + DES_EDE3_CBC_TYPE = 8, + ARC4_TYPE = 9, + NULL_CIPHER_TYPE = 10, + EVP_PKEY_RSA = 11, + EVP_PKEY_DSA = 12, + EVP_PKEY_EC = 13, + IDEA_CBC_TYPE = 14, + NID_sha1 = 64, + NID_md2 = 3, + NID_md5 = 4 +}; + + +typedef struct WOLFSSL_EVP_CIPHER_CTX { + int keyLen; /* user may set for variable */ + unsigned char enc; /* if encrypt side, then true */ + unsigned char cipherType; +#ifndef NO_AES + unsigned char iv[AES_BLOCK_SIZE]; /* working iv pointer into cipher */ +#elif !defined(NO_DES3) + unsigned char iv[DES_BLOCK_SIZE]; /* working iv pointer into cipher */ +#endif + WOLFSSL_Cipher cipher; +} WOLFSSL_EVP_CIPHER_CTX; + + +WOLFSSL_API int wolfSSL_EVP_MD_size(const WOLFSSL_EVP_MD* md); +WOLFSSL_API void wolfSSL_EVP_MD_CTX_init(WOLFSSL_EVP_MD_CTX* ctx); +WOLFSSL_API int wolfSSL_EVP_MD_CTX_cleanup(WOLFSSL_EVP_MD_CTX* ctx); + +WOLFSSL_API int wolfSSL_EVP_DigestInit(WOLFSSL_EVP_MD_CTX* ctx, + const WOLFSSL_EVP_MD* type); +WOLFSSL_API int wolfSSL_EVP_DigestUpdate(WOLFSSL_EVP_MD_CTX* ctx, const void* data, + unsigned long sz); +WOLFSSL_API int wolfSSL_EVP_DigestFinal(WOLFSSL_EVP_MD_CTX* ctx, unsigned char* md, + unsigned int* s); +WOLFSSL_API int wolfSSL_EVP_DigestFinal_ex(WOLFSSL_EVP_MD_CTX* ctx, + unsigned char* md, unsigned int* s); +#ifndef NO_MD5 +WOLFSSL_API int wolfSSL_EVP_BytesToKey(const WOLFSSL_EVP_CIPHER*, + const WOLFSSL_EVP_MD*, const unsigned char*, + const unsigned char*, int, int, unsigned char*, + unsigned char*); +#endif + +WOLFSSL_API void wolfSSL_EVP_CIPHER_CTX_init(WOLFSSL_EVP_CIPHER_CTX* ctx); +WOLFSSL_API int wolfSSL_EVP_CIPHER_CTX_cleanup(WOLFSSL_EVP_CIPHER_CTX* ctx); + +WOLFSSL_API int wolfSSL_EVP_CIPHER_CTX_iv_length(const WOLFSSL_EVP_CIPHER_CTX*); + + +WOLFSSL_API int wolfSSL_EVP_CipherInit(WOLFSSL_EVP_CIPHER_CTX* ctx, + const WOLFSSL_EVP_CIPHER* type, + unsigned char* key, unsigned char* iv, + int enc); +WOLFSSL_API int wolfSSL_EVP_CIPHER_CTX_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx); +WOLFSSL_API int wolfSSL_EVP_CIPHER_CTX_set_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx, + int keylen); +WOLFSSL_API int wolfSSL_EVP_Cipher(WOLFSSL_EVP_CIPHER_CTX* ctx, + unsigned char* dst, unsigned char* src, + unsigned int len); + +WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_EVP_get_digestbynid(int); + WOLFSSL_API WOLFSSL_RSA* wolfSSL_EVP_PKEY_get1_RSA(WOLFSSL_EVP_PKEY*); WOLFSSL_API WOLFSSL_DSA* wolfSSL_EVP_PKEY_get1_DSA(WOLFSSL_EVP_PKEY*); WOLFSSL_API WOLFSSL_EC_KEY *wolfSSL_EVP_PKEY_get1_EC_KEY(WOLFSSL_EVP_PKEY *key); /* these next ones don't need real OpenSSL type, for OpenSSH compat only */ -WOLFSSL_API void* wolfSSL_EVP_X_STATE(const WOLFCRYPT_EVP_CIPHER_CTX* ctx); -WOLFSSL_API int wolfSSL_EVP_X_STATE_LEN(const WOLFCRYPT_EVP_CIPHER_CTX* ctx); -WOLFSSL_API void wolfSSL_3des_iv(WOLFCRYPT_EVP_CIPHER_CTX* ctx, int doset, +WOLFSSL_API void* wolfSSL_EVP_X_STATE(const WOLFSSL_EVP_CIPHER_CTX* ctx); +WOLFSSL_API int wolfSSL_EVP_X_STATE_LEN(const WOLFSSL_EVP_CIPHER_CTX* ctx); + +WOLFSSL_API void wolfSSL_3des_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset, unsigned char* iv, int len); -WOLFSSL_API void wolfSSL_aes_ctr_iv(WOLFCRYPT_EVP_CIPHER_CTX* ctx, int doset, +WOLFSSL_API void wolfSSL_aes_ctr_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset, unsigned char* iv, int len); -WOLFSSL_API int wolfSSL_StoreExternalIV(WOLFCRYPT_EVP_CIPHER_CTX* ctx); -WOLFSSL_API int wolfSSL_SetInternalIV(WOLFCRYPT_EVP_CIPHER_CTX* ctx); + +WOLFSSL_API int wolfSSL_StoreExternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx); +WOLFSSL_API int wolfSSL_SetInternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx); + + /* end OpenSSH compat */ +typedef WOLFSSL_EVP_MD EVP_MD; +typedef WOLFSSL_EVP_CIPHER EVP_CIPHER; +typedef WOLFSSL_EVP_MD_CTX EVP_MD_CTX; +typedef WOLFSSL_EVP_CIPHER_CTX EVP_CIPHER_CTX; -/* OpenSSL compat */ -typedef WOLFCRYPT_EVP_MD EVP_MD; -typedef WOLFCRYPT_EVP_CIPHER EVP_CIPHER; -typedef WOLFCRYPT_EVP_MD_CTX EVP_MD_CTX; -typedef WOLFCRYPT_EVP_CIPHER_CTX EVP_CIPHER_CTX; +#ifndef NO_MD5 + #define EVP_md5 wolfSSL_EVP_md5 +#endif +#define EVP_sha1 wolfSSL_EVP_sha1 +#define EVP_sha256 wolfSSL_EVP_sha256 +#define EVP_sha384 wolfSSL_EVP_sha384 +#define EVP_sha512 wolfSSL_EVP_sha512 +#define EVP_ripemd160 wolfSSL_EVP_ripemd160 -#define EVP_md5 wc_EVP_md5 -#define EVP_sha1 wc_EVP_sha1 -#define EVP_sha256 wc_EVP_sha256 -#define EVP_sha384 wc_EVP_sha384 -#define EVP_sha512 wc_EVP_sha512 -#define EVP_ripemd160 wc_EVP_ripemd160 +#define EVP_aes_128_cbc wolfSSL_EVP_aes_128_cbc +#define EVP_aes_192_cbc wolfSSL_EVP_aes_192_cbc +#define EVP_aes_256_cbc wolfSSL_EVP_aes_256_cbc +#define EVP_aes_128_ctr wolfSSL_EVP_aes_128_ctr +#define EVP_aes_192_ctr wolfSSL_EVP_aes_192_ctr +#define EVP_aes_256_ctr wolfSSL_EVP_aes_256_ctr +#define EVP_des_cbc wolfSSL_EVP_des_cbc +#define EVP_des_ede3_cbc wolfSSL_EVP_des_ede3_cbc +#define EVP_rc4 wolfSSL_EVP_rc4 +#define EVP_idea_cbc wolfSSL_EVP_idea_cbc +#define EVP_enc_null wolfSSL_EVP_enc_null -#define EVP_aes_128_cbc wc_EVP_aes_128_cbc -#define EVP_aes_192_cbc wc_EVP_aes_192_cbc -#define EVP_aes_256_cbc wc_EVP_aes_256_cbc -#define EVP_aes_128_ctr wc_EVP_aes_128_ctr -#define EVP_aes_192_ctr wc_EVP_aes_192_ctr -#define EVP_aes_256_ctr wc_EVP_aes_256_ctr -#define EVP_des_cbc wc_EVP_des_cbc -#define EVP_des_ede3_cbc wc_EVP_des_ede3_cbc -#define EVP_rc4 wc_EVP_rc4 -#define EVP_idea_cbc wc_EVP_idea_cbc -#define EVP_enc_null wc_EVP_enc_null +#define EVP_MD_size wolfSSL_EVP_MD_size +#define EVP_MD_CTX_init wolfSSL_EVP_MD_CTX_init +#define EVP_MD_CTX_cleanup wolfSSL_EVP_MD_CTX_cleanup +#define EVP_DigestInit wolfSSL_EVP_DigestInit +#define EVP_DigestUpdate wolfSSL_EVP_DigestUpdate +#define EVP_DigestFinal wolfSSL_EVP_DigestFinal +#define EVP_DigestFinal_ex wolfSSL_EVP_DigestFinal_ex +#define EVP_BytesToKey wolfSSL_EVP_BytesToKey -#define EVP_MD_CTX_init wc_EVP_MD_CTX_init -#define EVP_MD_CTX_cleanup wc_EVP_MD_CTX_cleanup -#define EVP_MD_CTX_copy wc_EVP_MD_CTX_copy -#define EVP_MD_size wc_EVP_MD_size +#define EVP_CIPHER_CTX_init wolfSSL_EVP_CIPHER_CTX_init +#define EVP_CIPHER_CTX_cleanup wolfSSL_EVP_CIPHER_CTX_cleanup +#define EVP_CIPHER_CTX_iv_length wolfSSL_EVP_CIPHER_CTX_iv_length +#define EVP_CIPHER_CTX_key_length wolfSSL_EVP_CIPHER_CTX_key_length +#define EVP_CIPHER_CTX_set_key_length wolfSSL_EVP_CIPHER_CTX_set_key_length +#define EVP_CipherInit wolfSSL_EVP_CipherInit +#define EVP_Cipher wolfSSL_EVP_Cipher -#define EVP_DigestInit wc_EVP_DigestInit -#define EVP_DigestUpdate wc_EVP_DigestUpdate -#define EVP_DigestFinal wc_EVP_DigestFinal -#define EVP_DigestFinal_ex wc_EVP_DigestFinal_ex - -#define EVP_CIPHER_CTX_init wc_EVP_CIPHER_CTX_init -#define EVP_CIPHER_CTX_cleanup wc_EVP_CIPHER_CTX_cleanup -#define EVP_CIPHER_CTX_iv_length wc_EVP_CIPHER_CTX_iv_length -#define EVP_CIPHER_CTX_key_length wc_EVP_CIPHER_CTX_key_length -#define EVP_CIPHER_CTX_copy wc_EVP_CIPHER_CTX_copy -#define EVP_CIPHER_CTX_set_key_length wc_EVP_CIPHER_CTX_set_key_length - -#define EVP_CipherInit wc_EVP_CipherInit -#define EVP_CipherUpdate wc_EVP_CipherUpdate -#define EVP_CipherFinal wc_EVP_CipherFinal -#define EVP_Cipher wc_EVP_Cipher - -#define EVP_get_digestbynid wc_EVP_get_digestbynid +#define EVP_get_digestbynid wolfSSL_EVP_get_digestbynid #define EVP_PKEY_get1_RSA wolfSSL_EVP_PKEY_get1_RSA #define EVP_PKEY_get1_DSA wolfSSL_EVP_PKEY_get1_DSA #define EVP_PKEY_get1_EC_KEY wolfSSL_EVP_PKEY_get1_EC_KEY -#ifndef NO_MD5 -#define EVP_BytesToKey wc_EVP_BytesToKey -#endif +#ifndef EVP_MAX_MD_SIZE + #define EVP_MAX_MD_SIZE 64 /* sha512 */ +#endif #ifdef __cplusplus } /* extern "C" */ diff --git a/wolfssl/openssl/hmac.h b/wolfssl/openssl/hmac.h index a8598a177..76d2481bf 100644 --- a/wolfssl/openssl/hmac.h +++ b/wolfssl/openssl/hmac.h @@ -21,7 +21,7 @@ -/* hmac.h defines mini hamc openssl compatibility layer +/* hmac.h defines mini hamc openssl compatibility layer * */ @@ -35,20 +35,44 @@ #include "prefix_hmac.h" #endif -#include +#include +#include #ifdef __cplusplus extern "C" { #endif - //typedef struct WOLFCRYPT_HMAC_CTX HMAC_CTX; -#define HMAC(a,b,c,d,e,f,g) wc_HMAC((a),(b),(c),(d),(e),(f),(g)) +WOLFSSL_API unsigned char* wolfSSL_HMAC(const WOLFSSL_EVP_MD* evp_md, + const void* key, int key_len, + const unsigned char* d, int n, unsigned char* md, + unsigned int* md_len); + + +typedef struct WOLFSSL_HMAC_CTX { + Hmac hmac; + int type; +} WOLFSSL_HMAC_CTX; + + +WOLFSSL_API void wolfSSL_HMAC_Init(WOLFSSL_HMAC_CTX* ctx, const void* key, + int keylen, const EVP_MD* type); +WOLFSSL_API void wolfSSL_HMAC_Update(WOLFSSL_HMAC_CTX* ctx, + const unsigned char* data, int len); +WOLFSSL_API void wolfSSL_HMAC_Final(WOLFSSL_HMAC_CTX* ctx, unsigned char* hash, + unsigned int* len); +WOLFSSL_API void wolfSSL_HMAC_cleanup(WOLFSSL_HMAC_CTX* ctx); + + +typedef struct WOLFSSL_HMAC_CTX HMAC_CTX; + +#define HMAC(a,b,c,d,e,f,g) wolfSSL_HMAC((a),(b),(c),(d),(e),(f),(g)) + +#define HMAC_Init wolfSSL_HMAC_Init +#define HMAC_Update wolfSSL_HMAC_Update +#define HMAC_Final wolfSSL_HMAC_Final +#define HMAC_cleanup wolfSSL_HMAC_cleanup -#define HMAC_Init wc_HMAC_Init -#define HMAC_Update wc_HMAC_Update -#define HMAC_Final wc_HMAC_Final -#define HMAC_cleanup wc_HMAC_cleanup #ifdef __cplusplus } /* extern "C" */ diff --git a/wolfssl/openssl/md5.h b/wolfssl/openssl/md5.h index f145cad9c..bdcda5b98 100644 --- a/wolfssl/openssl/md5.h +++ b/wolfssl/openssl/md5.h @@ -12,23 +12,31 @@ #include "prefix_md5.h" #endif -#include - #ifdef __cplusplus extern "C" { #endif -typedef WOLFCRYPT_MD5_CTX WOLFSSL_MD5_CTX; -typedef WOLFCRYPT_MD5_CTX MD5_CTX; -#define MD5_Init wc_MD5_Init -#define MD5_Update wc_MD5_Update -#define MD5_Final wc_MD5_Final +typedef struct WOLFSSL_MD5_CTX { + int holder[24]; /* big enough to hold wolfcrypt md5, but check on init */ +} WOLFSSL_MD5_CTX; + +WOLFSSL_API void wolfSSL_MD5_Init(WOLFSSL_MD5_CTX*); +WOLFSSL_API void wolfSSL_MD5_Update(WOLFSSL_MD5_CTX*, const void*, unsigned long); +WOLFSSL_API void wolfSSL_MD5_Final(unsigned char*, WOLFSSL_MD5_CTX*); + + +typedef WOLFSSL_MD5_CTX MD5_CTX; + +#define MD5_Init wolfSSL_MD5_Init +#define MD5_Update wolfSSL_MD5_Update +#define MD5_Final wolfSSL_MD5_Final #ifdef __cplusplus - } /* extern "C" */ + } /* extern "C" */ #endif #endif /* NO_MD5 */ #endif /* WOLFSSL_MD5_H_ */ + diff --git a/wolfssl/openssl/pem.h b/wolfssl/openssl/pem.h index a6d80bff9..76a391f54 100644 --- a/wolfssl/openssl/pem.h +++ b/wolfssl/openssl/pem.h @@ -4,7 +4,7 @@ #ifndef WOLFSSL_PEM_H_ #define WOLFSSL_PEM_H_ -#include +#include #include #include #include @@ -16,19 +16,18 @@ /* RSA */ WOLFSSL_API -int wolfSSL_PEM_write_bio_RSAPrivateKey(WOLFCRYPT_BIO* bio, WOLFSSL_RSA* rsa, - const WOLFCRYPT_EVP_CIPHER* cipher, +int wolfSSL_PEM_write_bio_RSAPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_RSA* rsa, + const EVP_CIPHER* cipher, unsigned char* passwd, int len, pem_password_cb cb, void* arg); WOLFSSL_API -int wolfSSL_PEM_write_mem_RSAPrivateKey(RSA* rsa, - const WOLFCRYPT_EVP_CIPHER* cipher, +int wolfSSL_PEM_write_mem_RSAPrivateKey(RSA* rsa, const EVP_CIPHER* cipher, unsigned char* passwd, int len, unsigned char **pem, int *plen); #if !defined(NO_FILESYSTEM) WOLFSSL_API int wolfSSL_PEM_write_RSAPrivateKey(FILE *fp, WOLFSSL_RSA *rsa, - const WOLFCRYPT_EVP_CIPHER *enc, + const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); WOLFSSL_API @@ -43,20 +42,20 @@ int wolfSSL_PEM_write_RSA_PUBKEY(FILE *fp, WOLFSSL_RSA *x); /* DSA */ WOLFSSL_API -int wolfSSL_PEM_write_bio_DSAPrivateKey(WOLFCRYPT_BIO* bio, +int wolfSSL_PEM_write_bio_DSAPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_DSA* dsa, - const WOLFCRYPT_EVP_CIPHER* cipher, + const EVP_CIPHER* cipher, unsigned char* passwd, int len, pem_password_cb cb, void* arg); WOLFSSL_API int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa, - const WOLFCRYPT_EVP_CIPHER* cipher, + const EVP_CIPHER* cipher, unsigned char* passwd, int len, unsigned char **pem, int *plen); #if !defined(NO_FILESYSTEM) WOLFSSL_API int wolfSSL_PEM_write_DSAPrivateKey(FILE *fp, WOLFSSL_DSA *dsa, - const WOLFCRYPT_EVP_CIPHER *enc, + const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); WOLFSSL_API @@ -65,19 +64,19 @@ int wolfSSL_PEM_write_DSA_PUBKEY(FILE *fp, WOLFSSL_DSA *x); /* ECC */ WOLFSSL_API -int wolfSSL_PEM_write_bio_ECPrivateKey(WOLFCRYPT_BIO* bio, WOLFSSL_EC_KEY* ec, - const WOLFCRYPT_EVP_CIPHER* cipher, +int wolfSSL_PEM_write_bio_ECPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EC_KEY* ec, + const EVP_CIPHER* cipher, unsigned char* passwd, int len, pem_password_cb cb, void* arg); WOLFSSL_API int wolfSSL_PEM_write_mem_ECPrivateKey(WOLFSSL_EC_KEY* key, - const WOLFCRYPT_EVP_CIPHER* cipher, + const EVP_CIPHER* cipher, unsigned char* passwd, int len, unsigned char **pem, int *plen); #if !defined(NO_FILESYSTEM) WOLFSSL_API int wolfSSL_PEM_write_ECPrivateKey(FILE *fp, WOLFSSL_EC_KEY *key, - const WOLFCRYPT_EVP_CIPHER *enc, + const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); WOLFSSL_API @@ -86,7 +85,7 @@ int wolfSSL_PEM_write_EC_PUBKEY(FILE *fp, WOLFSSL_EC_KEY *key); /* EVP_KEY */ WOLFSSL_API -WOLFSSL_EVP_PKEY* wolfSSL_PEM_read_bio_PrivateKey(WOLFCRYPT_BIO* bio, +WOLFSSL_EVP_PKEY* wolfSSL_PEM_read_bio_PrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EVP_PKEY**, pem_password_cb cb, void* arg); @@ -119,7 +118,7 @@ WOLFSSL_EVP_PKEY *wolfSSL_PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, #define EVP_PKEY_type wolfSSL_EVP_PKEY_type #ifdef __cplusplus - } /* extern "C" */ + } /* extern "C" */ #endif #endif /* WOLFSSL_PEM_H_ */ diff --git a/wolfssl/openssl/ripemd.h b/wolfssl/openssl/ripemd.h index 62b1dc105..ef1578e91 100644 --- a/wolfssl/openssl/ripemd.h +++ b/wolfssl/openssl/ripemd.h @@ -10,15 +10,23 @@ extern "C" { #endif -#ifdef WOLFSSL_RIPEMD -typedef WOLFCRYPT_RIPEMD_CTX RIPEMD_CTX; +typedef struct WOLFSSL_RIPEMD_CTX { + int holder[32]; /* big enough to hold wolfcrypt, but check on init */ +} WOLFSSL_RIPEMD_CTX; + +WOLFSSL_API void wolfSSL_RIPEMD_Init(WOLFSSL_RIPEMD_CTX*); +WOLFSSL_API void wolfSSL_RIPEMD_Update(WOLFSSL_RIPEMD_CTX*, const void*, + unsigned long); +WOLFSSL_API void wolfSSL_RIPEMD_Final(unsigned char*, WOLFSSL_RIPEMD_CTX*); + + +typedef WOLFSSL_RIPEMD_CTX RIPEMD_CTX; + +#define RIPEMD_Init wolfSSL_RIPEMD_Init +#define RIPEMD_Update wolfSSL_RIPEMD_Update +#define RIPEMD_Final wolfSSL_RIPEMD_Final -#define RIPEMD_Init wc_RIPEMD_Init -#define RIPEMD_Update wc_RIPEMD_Update -#define RIPEMD_Final wc_RIPEMD_Final - -#endif /* WOLFSSL_RIPEMD */ #ifdef __cplusplus } /* extern "C" */ diff --git a/wolfssl/openssl/sha.h b/wolfssl/openssl/sha.h index ca8b70a94..7f8b0ebd6 100644 --- a/wolfssl/openssl/sha.h +++ b/wolfssl/openssl/sha.h @@ -10,48 +10,116 @@ #include "prefix_sha.h" #endif -#include - #ifdef __cplusplus extern "C" { #endif -typedef WOLFCRYPT_SHA_CTX SHA_CTX; -#define SHA_Init wc_SHA_Init -#define SHA_Update wc_SHA_Update -#define SHA_Final wc_SHA_Final +typedef struct WOLFSSL_SHA_CTX { + int holder[24]; /* big enough to hold wolfcrypt sha, but check on init */ +} WOLFSSL_SHA_CTX; -#define SHA1_Init wc_SHA1_Init -#define SHA1_Update wc_SHA1_Update -#define SHA1_Final wc_SHA1_Final +WOLFSSL_API void wolfSSL_SHA_Init(WOLFSSL_SHA_CTX*); +WOLFSSL_API void wolfSSL_SHA_Update(WOLFSSL_SHA_CTX*, const void*, unsigned long); +WOLFSSL_API void wolfSSL_SHA_Final(unsigned char*, WOLFSSL_SHA_CTX*); -typedef WOLFCRYPT_SHA256_CTX SHA256_CTX; +/* SHA1 points to above, shouldn't use SHA0 ever */ +WOLFSSL_API void wolfSSL_SHA1_Init(WOLFSSL_SHA_CTX*); +WOLFSSL_API void wolfSSL_SHA1_Update(WOLFSSL_SHA_CTX*, const void*, unsigned long); +WOLFSSL_API void wolfSSL_SHA1_Final(unsigned char*, WOLFSSL_SHA_CTX*); -#define SHA256_Init wc_SHA256_Init -#define SHA256_Update wc_SHA256_Update -#define SHA256_Final wc_SHA256_Final +enum { + SHA_DIGEST_LENGTH = 20 +}; + + +typedef WOLFSSL_SHA_CTX SHA_CTX; + +#define SHA_Init wolfSSL_SHA_Init +#define SHA_Update wolfSSL_SHA_Update +#define SHA_Final wolfSSL_SHA_Final + +#define SHA1_Init wolfSSL_SHA1_Init +#define SHA1_Update wolfSSL_SHA1_Update +#define SHA1_Final wolfSSL_SHA1_Final + + +typedef struct WOLFSSL_SHA256_CTX { + int holder[28]; /* big enough to hold wolfcrypt sha, but check on init */ +} WOLFSSL_SHA256_CTX; + +WOLFSSL_API void wolfSSL_SHA256_Init(WOLFSSL_SHA256_CTX*); +WOLFSSL_API void wolfSSL_SHA256_Update(WOLFSSL_SHA256_CTX*, const void*, + unsigned long); +WOLFSSL_API void wolfSSL_SHA256_Final(unsigned char*, WOLFSSL_SHA256_CTX*); + +enum { + SHA256_DIGEST_LENGTH = 32 +}; + + +typedef WOLFSSL_SHA256_CTX SHA256_CTX; + +#define SHA256_Init wolfSSL_SHA256_Init +#define SHA256_Update wolfSSL_SHA256_Update +#define SHA256_Final wolfSSL_SHA256_Final #ifdef WOLFSSL_SHA384 -typedef WOLFCRYPT_SHA384_CTX SHA384_CTX; -#define SHA384_Init wc_SHA384_Init -#define SHA384_Update wc_SHA384_Update -#define SHA384_Final wc_SHA384_Final +typedef struct WOLFSSL_SHA384_CTX { + long long holder[32]; /* big enough, but check on init */ +} WOLFSSL_SHA384_CTX; + +WOLFSSL_API void wolfSSL_SHA384_Init(WOLFSSL_SHA384_CTX*); +WOLFSSL_API void wolfSSL_SHA384_Update(WOLFSSL_SHA384_CTX*, const void*, + unsigned long); +WOLFSSL_API void wolfSSL_SHA384_Final(unsigned char*, WOLFSSL_SHA384_CTX*); + +enum { + SHA384_DIGEST_LENGTH = 48 +}; + + +typedef WOLFSSL_SHA384_CTX SHA384_CTX; + +#define SHA384_Init wolfSSL_SHA384_Init +#define SHA384_Update wolfSSL_SHA384_Update +#define SHA384_Final wolfSSL_SHA384_Final + #endif /* WOLFSSL_SHA384 */ #ifdef WOLFSSL_SHA512 -typedef WOLFCRYPT_SHA512_CTX SHA512_CTX; -#define SHA512_Init wc_SHA512_Init -#define SHA512_Update wc_SHA512_Update -#define SHA512_Final wc_SHA512_Final +typedef struct WOLFSSL_SHA512_CTX { + long long holder[36]; /* big enough, but check on init */ +} WOLFSSL_SHA512_CTX; + +WOLFSSL_API void wolfSSL_SHA512_Init(WOLFSSL_SHA512_CTX*); +WOLFSSL_API void wolfSSL_SHA512_Update(WOLFSSL_SHA512_CTX*, const void*, + unsigned long); +WOLFSSL_API void wolfSSL_SHA512_Final(unsigned char*, WOLFSSL_SHA512_CTX*); + +enum { + SHA512_DIGEST_LENGTH = 64 +}; + + +typedef WOLFSSL_SHA512_CTX SHA512_CTX; + +#define SHA512_Init wolfSSL_SHA512_Init +#define SHA512_Update wolfSSL_SHA512_Update +#define SHA512_Final wolfSSL_SHA512_Final + #endif /* WOLFSSL_SHA512 */ + + + #ifdef __cplusplus - } /* extern "C" */ + } /* extern "C" */ #endif #endif /* WOLFSSL_SHA_H_ */ + diff --git a/wolfssl/openssl/ssl.h b/wolfssl/openssl/ssl.h index 8c417e635..c4672ef0b 100644 --- a/wolfssl/openssl/ssl.h +++ b/wolfssl/openssl/ssl.h @@ -60,6 +60,7 @@ typedef WOLFSSL_X509_CHAIN X509_CHAIN; /* redeclare guard */ #define WOLFSSL_TYPES_DEFINED + typedef WOLFSSL_EVP_PKEY EVP_PKEY; typedef WOLFSSL_RSA RSA; typedef WOLFSSL_DSA DSA; @@ -67,8 +68,8 @@ typedef WOLFSSL_EC_KEY EC_KEY; typedef WOLFSSL_EC_GROUP EC_GROUP; typedef WOLFSSL_EC_POINT EC_POINT; typedef WOLFSSL_ECDSA_SIG ECDSA_SIG; -typedef WOLFCRYPT_BIO BIO; -typedef WOLFCRYPT_BIO_METHOD BIO_METHOD; +typedef WOLFSSL_BIO BIO; +typedef WOLFSSL_BIO_METHOD BIO_METHOD; typedef WOLFSSL_CIPHER SSL_CIPHER; typedef WOLFSSL_X509_LOOKUP X509_LOOKUP; typedef WOLFSSL_X509_LOOKUP_METHOD X509_LOOKUP_METHOD; @@ -208,6 +209,31 @@ typedef WOLFSSL_X509_STORE_CTX X509_STORE_CTX; #define MD4_Update wolfSSL_MD4_Update #define MD4_Final wolfSSL_MD4_Final +#define BIO_new wolfSSL_BIO_new +#define BIO_free wolfSSL_BIO_free +#define BIO_free_all wolfSSL_BIO_free_all +#define BIO_read wolfSSL_BIO_read +#define BIO_write wolfSSL_BIO_write +#define BIO_push wolfSSL_BIO_push +#define BIO_pop wolfSSL_BIO_pop +#define BIO_flush wolfSSL_BIO_flush +#define BIO_pending wolfSSL_BIO_pending + +#define BIO_get_mem_data wolfSSL_BIO_get_mem_data +#define BIO_new_mem_buf wolfSSL_BIO_new_mem_buf + +#define BIO_f_buffer wolfSSL_BIO_f_buffer +#define BIO_set_write_buffer_size wolfSSL_BIO_set_write_buffer_size +#define BIO_f_ssl wolfSSL_BIO_f_ssl +#define BIO_new_socket wolfSSL_BIO_new_socket +#define SSL_set_bio wolfSSL_set_bio +#define BIO_eof wolfSSL_BIO_eof +#define BIO_set_ss wolfSSL_BIO_set_ss + +#define BIO_s_mem wolfSSL_BIO_s_mem +#define BIO_f_base64 wolfSSL_BIO_f_base64 +#define BIO_set_flags wolfSSL_BIO_set_flags + #define OpenSSL_add_all_algorithms wolfSSL_add_all_algorithms #define SSLeay_add_ssl_algorithms wolfSSL_add_all_algorithms #define SSLeay_add_all_algorithms wolfSSL_add_all_algorithms @@ -361,6 +387,7 @@ typedef WOLFSSL_X509_STORE_CTX X509_STORE_CTX; #define SSL_want_read wolfSSL_want_read #define SSL_want_write wolfSSL_want_write +#define BIO_prf wolfSSL_BIO_prf #define ASN1_UTCTIME_pr wolfSSL_ASN1_UTCTIME_pr #define sk_num wolfSSL_sk_num @@ -391,6 +418,8 @@ typedef WOLFSSL_X509_NAME_ENTRY X509_NAME_ENTRY; #define X509_NAME_free wolfSSL_X509_NAME_free #define SSL_CTX_use_certificate wolfSSL_CTX_use_certificate #define SSL_CTX_use_PrivateKey wolfSSL_CTX_use_PrivateKey +#define BIO_read_filename wolfSSL_BIO_read_filename +#define BIO_s_file wolfSSL_BIO_s_file #define OBJ_nid2sn wolf_OBJ_nid2sn #define OBJ_obj2nid wolf_OBJ_obj2nid #define OBJ_sn2nid wolf_OBJ_sn2nid @@ -421,6 +450,7 @@ typedef WOLFSSL_X509_NAME_ENTRY X509_NAME_ENTRY; #define PEM_read_bio_DHparams wolfSSL_PEM_read_bio_DHparams #define PEM_write_bio_X509 PEM_write_bio_WOLFSSL_X509 #define SSL_CTX_set_tmp_dh wolfSSL_CTX_set_tmp_dh +#define BIO_new_file wolfSSL_BIO_new_file #endif /* HAVE_STUNNEL || HAVE_LIGHTY || WOLFSSL_MYSQL_COMPATIBLE */ @@ -486,198 +516,6 @@ typedef WOLFSSL_ASN1_BIT_STRING ASN1_BIT_STRING; #endif /* HAVE_STUNNEL */ -#ifdef OPENSSL_EXTRA - -#define BIO_method_name wc_BioMethodName -#define BIO_method_type wc_BioMethodType - -#define BIO_clear_flags wc_BioClearFlags -#define BIO_set_flags wc_BioSetFlags -#define BIO_test_flags wc_BioTestFlags - -#define BIO_get_flags wc_BioGetFlags -#define BIO_set_retry_special wc_BioSetRetrySpecial -#define BIO_set_retry_read wc_BioSetRetryRead -#define BIO_set_retry_write wc_BioSetRetryWrite - -#define BIO_clear_retry_flags wc_BioClearRetryFlags -#define BIO_get_retry_flags wc_BioGetRetryFlags - -#define BIO_should_read wc_BioShouldRead -#define BIO_should_write wc_BioShouldWrite -#define BIO_should_io_special wc_BioShouldIoSpecial -#define BIO_retry_type wc_BioRetryType -#define BIO_should_retry_type wc_BioShouldRetry - -#define BIO_get_callback wc_BioGetCallback -#define BIO_set_callback wc_BioSetCallback -#define BIO_get_callback_arg wc_BioSetCallbackArg -#define BIO_set_callback_arg wc_BioGetCallbackArg - -#define BIO_new wc_BioNew -#define BIO_free wc_BioFree -#define BIO_vfree wc_BioFree -#define BIO_free_all wc_BioFreeAll -#define BIO_set wc_BioSet -#define BIO_read wc_BioRead -#define BIO_wrie wc_BioWrite -#define BIO_puts wc_BioPuts -#define BIO_gets wc_BioGets -#define BIO_indent wc_BioIndent -#define BIO_ctrl wc_BioCtrl -#define BIO_callback_ctrl wc_BioCallbackCtrl -#define BIO_push wc_BioPush -#define BIO_pop wc_BioPop -#define BIO_next wc_BioNext -#define BIO_find_type wc_BioFindType -#define BIO_dup_chain wc_BioDupChain -#define BIO_dup_state wc_BioDupState - -#define BIO_get_retry_BIO wc_BioGetRetryBio -#define BIO_get_retry_reason wc_BioGetRetryReason - -#define BIO_int_ctrl wc_BioIntCtrl -#define BIO_ptr_ctrl wc_BioPtrCtrl -#define BIO_ctrl_pending wc_BioCtrlPending -#define BIO_ctrl_wpending wc_BioCtrlWpending - -#define BIO_number_read wc_BioNumberRead -#define BIO_number_written wc_BioNumberWritten - -#define BIO_reset wc_BioReset -#define BIO_eof wc_BioEof -#define BIO_set_close wc_BioSetClose -#define BIO_get_close wc_BioGetClose -#define BIO_pending wc_BioPending -#define BIO_wpending wc_BioWpending -#define BIO_flush wc_BioFlush - -#define BIO_set_info_callback wc_BioSetInfoCallback -#define BIO_get_info_callback wc_BioGetInfoCallback - -#define BIO_copy_next_retry wc_BioCopyNextRetry -#define BIO_printf wc_BioPrintf - -/* BIO file */ -#define BIO_s_file wc_Bio_s_file -#define BIO_new_fp wc_BioNewFp -#define BIO_new_file wc_BioNewFile -#define BIO_set_fp wc_BioSetFp -#define BIO_get_fp wc_BioGetFp -#define BIO_seek wc_BioSeek -#define BIO_tell wc_BioTell -#define BIO_read_filename wc_BioReadFilename -#define BIO_write_filename wc_BioWriteFilename -#define BIO_append_filename wc_BioAppendFilename -#define BIO_rw_filename wc_BioRwFilename - -/* BIO memory */ -#define BIO_s_mem wc_Bio_s_mem -#define BIO_new_mem_buf wc_BioNewMemBuf - -/* BIO fd */ -#define BIO_s_fd wc_Bio_s_fd -#define BIO_new_fd wc_BioNewFd - -/* BIO null */ -#define BIO_s_null wc_Bio_s_null - -/* BIO socket */ -#define BIO_s_socket wc_Bio_s_socket -#define BIO_new_socket wc_BioNewSocket -#define BIO_sock_should_retry wc_BioSockShouldRetry -#define BIO_sock_non_fatal_error wc_BioSockNonFatalError -#define BIO_set_fd wc_BioSetFd -#define BIO_get_fd wc_BioGetFd - -/* BIO connect */ -#define BIO_s_connect wc_Bio_s_connect -#define BIO_new_connect wc_BioNewConnect -#define BIO_set_conn_hostname wc_BioSetConnHostname -#define BIO_set_conn_port wc_BioSetConnPort -#define BIO_set_conn_ip wc_BioSetConnIp(bio, ip) -#define BIO_set_conn_int_port wc_BioSetConnIntPort -#define BIO_get_conn_hostname wc_BioGetConnHostname -#define BIO_get_conn_port wc_BioGetConnPort -#define BIO_get_conn_ip wc_BioGetConnIp -#define BIO_get_conn_int_port wc_BioGetConnIntPort -#define BIO_set_nbio wc_BioSetNbio -#define BIO_do_handshake wc_BioDoHandshake -#define BIO_do_connect wc_BioDoConnect -#define BIO_do_accept wc_BioDoAccept - -/* BIO accept */ -#define BIO_s_accept wc_Bio_s_accept -#define BIO_new_accept wc_BioNewAccept -#define BIO_set_accept_port wc_BioSetAcceptPort -#define BIO_get_accept_port wc_BioGetAcceptPort -#define BIO_set_nbio_accept wc_BioSetNbioAccept -#define BIO_set_accept_bios wc_BioSetAcceptBios -#define BIO_set_bind_mode wc_BioSetBindMode -#define BIO_get_bind_mode wc_BioGetBindMode - -/* BIO datagram */ -#define BIO_s_datagram wc_Bio_s_datagram -#define BIO_new_dgram wc_BioNewDgram - -/* BIO filter buffer */ -#define BIO_f_buffer wc_Bio_f_buffer -#define BIO_get_mem_data wc_BioGetMemData -#define BIO_set_mem_buf wc_BioSetMemBuf -#define BIO_get_mem_ptr wc_BioGetMemPtr -#define BIO_set_mem_eof_return wc_BioSetMemEofReturn -#define BIO_get_buffer_num_lines wc_BioGetBufferNumLines -#define BIO_set_buffer_size wc_BioSetBufferSize -#define BIO_set_read_buffer_size wc_BioSetReadBufferSize -#define BIO_set_write_buffer_size wc_BioSetWriteBufferSize -#define BIO_set_buffer_read_data wc_BioSetBufferReadData - -/* BIO filter cipher */ -#define BIO_f_cipher wc_Bio_f_cipher -#define BIO_set_cipher wc_BioSetCipher -#define BIO_get_cipher_status wc_BioGetCipherStatus -#define BIO_get_cipher_ctx wc_BioGetCipherCtx - -/* BIO filter base64 */ -#define BIO_f_base64 wc_Bio_f_base64 - -/* BIO filter digest */ -#define BIO_f_md wc_Bio_f_md -#define BIO_set_md wc_BioSetMd -#define BIO_get_md wc_BioGetMd -#define BIO_get_md_ctx wc_BioGetmdCtx -#define BIO_set_md_ctx wc_BioSetMdCtx - -/* BIO filter SSL */ -WOLFSSL_API WOLFCRYPT_BIO *wolfSSL_BioNewBufferSSLConnect(WOLFSSL_CTX *ctx); -WOLFSSL_API WOLFCRYPT_BIO *wolfSSL_BioNewSSLConnect(WOLFSSL_CTX *ctx); -WOLFSSL_API WOLFCRYPT_BIO *wolfSSL_BioNewSSL(WOLFSSL_CTX *ctx, int client); - -#define BIO_f_ssl wc_Bio_f_ssl -#define BIO_new_buffer_ssl_connect wolfSSL_BioNewBufferSSLConnect -#define BIO_new_ssl_connect wolfSSL_BioNewSSLConnect -#define BIO_new_ssl wolfSSL_BioNewSSL -#define BIO_ssl_shutdown wc_BioSSLShutdown -#define BIO_set_ssl wc_BioSetSSL -#define BIO_get_ssl wc_BioGetSSL -#define BIO_set_ssl_mode wc_BIOSetSSLMode -#define BIO_set_ssl_renegotiate_bytes wc_BIOSetSSLRenegotiateBytes -#define BIO_get_num_renegotiates wc_BIOGetNumRenegotiates -#define BIO_set_ssl_renegotiate_timeout wc_BIOSetSSLRenegotiateTimeout - -/* BIO socket internal functions */ -#define BIO_get_host_ip wc_BioGetHostIp -#define BIO_get_port wc_BioGetPort -#define BIO_sock_error wc_BioSockError -#define BIO_sock_init wc_BioSockInit -#define BIO_sock_cleanup wc_BioSockCleanup -#define BIO_get_accept_socket wc_BioGetAcceptSocket -#define BIO_accept wc_BioAccept -#define BIO_set_tcp_ndelay wc_BioSetTcpNdelay -#define BIO_socket_nbio wc_BioSocketNbio - -#endif /* OPENSSL_EXTRA */ - #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index 0bfcf1317..b62341269 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -92,8 +92,8 @@ typedef struct WOLFSSL_CIPHER WOLFSSL_CIPHER; typedef struct WOLFSSL_X509_LOOKUP WOLFSSL_X509_LOOKUP; typedef struct WOLFSSL_X509_LOOKUP_METHOD WOLFSSL_X509_LOOKUP_METHOD; typedef struct WOLFSSL_X509_CRL WOLFSSL_X509_CRL; -typedef struct WOLFCRYPT_BIO WOLFCRYPT_BIO; -typedef struct WOLFCRYPT_BIO_METHOD WOLFCRYPT_BIO_METHOD; +typedef struct WOLFSSL_BIO WOLFSSL_BIO; +typedef struct WOLFSSL_BIO_METHOD WOLFSSL_BIO_METHOD; typedef struct WOLFSSL_X509_EXTENSION WOLFSSL_X509_EXTENSION; typedef struct WOLFSSL_ASN1_TIME WOLFSSL_ASN1_TIME; typedef struct WOLFSSL_ASN1_INTEGER WOLFSSL_ASN1_INTEGER; @@ -310,7 +310,6 @@ WOLFSSL_API int wolfSSL_use_RSAPrivateKey_file(WOLFSSL*, const char*, int); WOLFSSL_API WOLFSSL_CTX* wolfSSL_CTX_new(WOLFSSL_METHOD*); WOLFSSL_API WOLFSSL* wolfSSL_new(WOLFSSL_CTX*); -WOLFSSL_API WOLFSSL* wolfSSL_dup(WOLFSSL*); WOLFSSL_API int wolfSSL_set_fd (WOLFSSL*, int); WOLFSSL_API char* wolfSSL_get_cipher_list(int priority); WOLFSSL_API int wolfSSL_get_ciphers(char*, int); @@ -448,6 +447,34 @@ WOLFSSL_API void wolfSSL_MD4_Init(WOLFSSL_MD4_CTX*); WOLFSSL_API void wolfSSL_MD4_Update(WOLFSSL_MD4_CTX*, const void*, unsigned long); WOLFSSL_API void wolfSSL_MD4_Final(unsigned char*, WOLFSSL_MD4_CTX*); + +WOLFSSL_API WOLFSSL_BIO* wolfSSL_BIO_new(WOLFSSL_BIO_METHOD*); +WOLFSSL_API int wolfSSL_BIO_free(WOLFSSL_BIO*); +WOLFSSL_API int wolfSSL_BIO_free_all(WOLFSSL_BIO*); +WOLFSSL_API int wolfSSL_BIO_read(WOLFSSL_BIO*, void*, int); +WOLFSSL_API int wolfSSL_BIO_write(WOLFSSL_BIO*, const void*, int); +WOLFSSL_API WOLFSSL_BIO* wolfSSL_BIO_push(WOLFSSL_BIO*, WOLFSSL_BIO* append); +WOLFSSL_API WOLFSSL_BIO* wolfSSL_BIO_pop(WOLFSSL_BIO*); +WOLFSSL_API int wolfSSL_BIO_flush(WOLFSSL_BIO*); +WOLFSSL_API int wolfSSL_BIO_pending(WOLFSSL_BIO*); + +WOLFSSL_API WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_buffer(void); +WOLFSSL_API long wolfSSL_BIO_set_write_buffer_size(WOLFSSL_BIO*, long size); +WOLFSSL_API WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_ssl(void); +WOLFSSL_API WOLFSSL_BIO* wolfSSL_BIO_new_socket(int sfd, int flag); +WOLFSSL_API int wolfSSL_BIO_eof(WOLFSSL_BIO*); + +WOLFSSL_API WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_mem(void); +WOLFSSL_API WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_base64(void); +WOLFSSL_API void wolfSSL_BIO_set_flags(WOLFSSL_BIO*, int); + +WOLFSSL_API int wolfSSL_BIO_get_mem_data(WOLFSSL_BIO* bio,const unsigned char** p); +WOLFSSL_API WOLFSSL_BIO* wolfSSL_BIO_new_mem_buf(void* buf, int len); + + +WOLFSSL_API long wolfSSL_BIO_set_ssl(WOLFSSL_BIO*, WOLFSSL*, int flag); +WOLFSSL_API void wolfSSL_set_bio(WOLFSSL*, WOLFSSL_BIO* rd, WOLFSSL_BIO* wr); + WOLFSSL_API int wolfSSL_add_all_algorithms(void); WOLFSSL_API void wolfSSL_RAND_screen(void); @@ -544,7 +571,7 @@ WOLFSSL_API WOLFSSL_X509_REVOKED* wolfSSL_sk_X509_REVOKED_value( WOLFSSL_X509_REVOKED*,int); WOLFSSL_API WOLFSSL_ASN1_INTEGER* wolfSSL_X509_get_serialNumber(WOLFSSL_X509*); -WOLFSSL_API int wolfSSL_ASN1_TIME_print(WOLFCRYPT_BIO*, const WOLFSSL_ASN1_TIME*); +WOLFSSL_API int wolfSSL_ASN1_TIME_print(WOLFSSL_BIO*, const WOLFSSL_ASN1_TIME*); WOLFSSL_API int wolfSSL_ASN1_INTEGER_cmp(const WOLFSSL_ASN1_INTEGER*, const WOLFSSL_ASN1_INTEGER*); @@ -664,6 +691,10 @@ enum { SSL_MODE_ENABLE_PARTIAL_WRITE = 2, + BIO_FLAGS_BASE64_NO_NL = 1, + BIO_CLOSE = 1, + BIO_NOCLOSE = 0, + NID_undef = 0, X509_FILETYPE_PEM = 8, @@ -830,7 +861,8 @@ WOLFSSL_API WOLFSSL_X509* wolfSSL_get_peer_certificate(WOLFSSL* ssl); WOLFSSL_API int wolfSSL_want_read(WOLFSSL*); WOLFSSL_API int wolfSSL_want_write(WOLFSSL*); -WOLFSSL_API int wolfSSL_ASN1_UTCTIME_print(WOLFCRYPT_BIO*, +WOLFSSL_API int wolfSSL_BIO_printf(WOLFSSL_BIO*, const char*, ...); +WOLFSSL_API int wolfSSL_ASN1_UTCTIME_print(WOLFSSL_BIO*, const WOLFSSL_ASN1_UTCTIME*); WOLFSSL_API int wolfSSL_sk_num(WOLFSSL_X509_REVOKED*); WOLFSSL_API void* wolfSSL_sk_value(WOLFSSL_X509_REVOKED*, int); @@ -1704,11 +1736,13 @@ struct WOLFSSL_X509_NAME_ENTRY { WOLFSSL_API void wolfSSL_X509_NAME_free(WOLFSSL_X509_NAME *name); WOLFSSL_API char wolfSSL_CTX_use_certificate(WOLFSSL_CTX *ctx, WOLFSSL_X509 *x); WOLFSSL_API int wolfSSL_CTX_use_PrivateKey(WOLFSSL_CTX *ctx, WOLFSSL_EVP_PKEY *pkey); +WOLFSSL_API int wolfSSL_BIO_read_filename(WOLFSSL_BIO *b, const char *name); +WOLFSSL_API WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_file(void); /* These are to be merged shortly */ WOLFSSL_API const char * wolf_OBJ_nid2sn(int n); WOLFSSL_API int wolf_OBJ_obj2nid(const WOLFSSL_ASN1_OBJECT *o); WOLFSSL_API int wolf_OBJ_sn2nid(const char *sn); -WOLFSSL_API WOLFSSL_X509 *PEM_read_bio_WOLFSSL_X509(WOLFCRYPT_BIO *bp, WOLFSSL_X509 **x, pem_password_cb *cb, void *u); +WOLFSSL_API WOLFSSL_X509 *PEM_read_bio_WOLFSSL_X509(WOLFSSL_BIO *bp, WOLFSSL_X509 **x, pem_password_cb *cb, void *u); WOLFSSL_API void wolfSSL_CTX_set_verify_depth(WOLFSSL_CTX *ctx,int depth); WOLFSSL_API void* wolfSSL_get_app_data( const WOLFSSL *ssl); WOLFSSL_API void wolfSSL_set_app_data(WOLFSSL *ssl, void *arg); @@ -1728,10 +1762,11 @@ WOLFSSL_API STACK_OF(WOLFSSL_X509_NAME) *wolfSSL_dup_CA_list( STACK_OF(WOLFSSL_X WOLFSSL_API char * wolf_OBJ_nid2ln(int n); WOLFSSL_API int wolf_OBJ_txt2nid(const char *sn); +WOLFSSL_API WOLFSSL_BIO* wolfSSL_BIO_new_file(const char *filename, const char *mode); WOLFSSL_API long wolfSSL_CTX_set_tmp_dh(WOLFSSL_CTX*, WOLFSSL_DH*); -WOLFSSL_API WOLFSSL_DH *wolfSSL_PEM_read_bio_DHparams(WOLFCRYPT_BIO *bp, +WOLFSSL_API WOLFSSL_DH *wolfSSL_PEM_read_bio_DHparams(WOLFSSL_BIO *bp, WOLFSSL_DH **x, pem_password_cb *cb, void *u); -WOLFSSL_API int PEM_write_bio_WOLFSSL_X509(WOLFCRYPT_BIO *bp, WOLFSSL_X509 *x); +WOLFSSL_API int PEM_write_bio_WOLFSSL_X509(WOLFSSL_BIO *bp, WOLFSSL_X509 *x); #endif /* HAVE_STUNNEL || HAVE_LIGHTY */ @@ -1769,7 +1804,7 @@ WOLFSSL_API int wolfSSL_sk_X509_NAME_num(const STACK_OF(WOLFSSL_X509_NAME) *s); WOLFSSL_API int wolfSSL_sk_X509_num(const STACK_OF(WOLFSSL_X509) *s); -WOLFSSL_API int wolfSSL_X509_NAME_print_ex(WOLFCRYPT_BIO*,WOLFSSL_X509_NAME*,int, +WOLFSSL_API int wolfSSL_X509_NAME_print_ex(WOLFSSL_BIO*,WOLFSSL_X509_NAME*,int, unsigned long); WOLFSSL_API WOLFSSL_ASN1_BIT_STRING* wolfSSL_X509_get0_pubkey_bitstr( diff --git a/wolfssl/test.h b/wolfssl/test.h index 0e4b07a13..3608052a0 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -858,61 +858,6 @@ static INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, *clientfd = *sockfd; } -static INLINE void tcp_set_ready(func_args* args, word16 port, int ready_file) -{ - tcp_ready* ready = NULL; - - (void) ready; /* Account for case when "ready" is not used */ - -#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) - /* signal ready to tcp_accept */ - if (args) - ready = args->signal; - if (ready) { - pthread_mutex_lock(&ready->mutex); - ready->ready = 1; - ready->port = port; - pthread_cond_signal(&ready->cond); - pthread_mutex_unlock(&ready->mutex); - } -#elif defined (WOLFSSL_TIRTOS) - /* Need mutex? */ - if (args) - ready = args->signal; - if (ready) { - ready->ready = 1; - ready->port = port; - } -#endif - - if (ready_file) { -#ifndef NO_FILESYSTEM - FILE* srf = NULL; - if (args) - ready = args->signal; - - if (ready) { - srf = fopen(ready->srfName, "w"); - - if (srf) { - /* let's write port sever is listening on to ready file - external monitor can then do ephemeral ports by passing - -p 0 to server on supported platforms with -R ready_file - client can then wait for existence of ready_file and see - which port the server is listening on. */ - fprintf(srf, "%d\n", (int)port); - fclose(srf); - } - } -#endif - } - - (void)port; - (void)ready_file; - (void)args; -} - - static INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, func_args* args, word16 port, int useAnyAddr, int udp, int ready_file, int do_listen) @@ -930,7 +875,49 @@ static INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, if(do_listen) { tcp_listen(sockfd, &port, useAnyAddr, udp); - tcp_set_ready(args, port, ready_file); + + #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__) + /* signal ready to tcp_accept */ + if (args) + ready = args->signal; + if (ready) { + pthread_mutex_lock(&ready->mutex); + ready->ready = 1; + ready->port = port; + pthread_cond_signal(&ready->cond); + pthread_mutex_unlock(&ready->mutex); + } + #elif defined (WOLFSSL_TIRTOS) + /* Need mutex? */ + if (args) + ready = args->signal; + if (ready) { + ready->ready = 1; + ready->port = port; + } + #endif + + if (ready_file) { + #ifndef NO_FILESYSTEM + FILE* srf = NULL; + if (args) + ready = args->signal; + + if (ready) { + srf = fopen(ready->srfName, "w"); + + if (srf) { + /* let's write port sever is listening on to ready file + external monitor can then do ephemeral ports by passing + -p 0 to server on supported platforms with -R ready_file + client can then wait for existence of ready_file and see + which port the server is listening on. */ + fprintf(srf, "%d\n", (int)port); + fclose(srf); + } + } + #endif + } } *clientfd = accept(*sockfd, (struct sockaddr*)&client, @@ -940,6 +927,7 @@ static INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd, } } + static INLINE void tcp_set_nonblocking(SOCKET_T* sockfd) { #ifdef USE_WINDOWS_API diff --git a/wolfssl/wolfcrypt/bio.h b/wolfssl/wolfcrypt/bio.h deleted file mode 100644 index 5399748cb..000000000 --- a/wolfssl/wolfcrypt/bio.h +++ /dev/null @@ -1,571 +0,0 @@ -#ifndef WOLF_CRYPT_BIO_H -#define WOLF_CRYPT_BIO_H - -#include -#include -#include - -#ifdef OPENSSL_EXTRA - -#ifdef __cplusplus -extern "C" { -#endif - -/* BIO types */ -enum WS_BIO_TYPE { - BIO_TYPE_NONE = 0, - - BIO_TYPE_SSL = 1, - BIO_TYPE_MD = 2, /* passive */ - BIO_TYPE_BUFFER = 3, - BIO_TYPE_CIPHER = 4, - BIO_TYPE_BASE64 = 5, - BIO_TYPE_LINEBUFFER = 6, - BIO_TYPE_ASN1 = 7, - BIO_TYPE_COMP = 8, - BIO_TYPE_PROXY_CLIENT = 9, /* client proxy BIO */ - BIO_TYPE_PROXY_SERVER = 10, /* server proxy BIO */ - BIO_TYPE_NULL_FILTER = 11, - BIO_TYPE_BER = 12, /* BER -> bin filter */ - - BIO_TYPE_MEM = 13, - BIO_TYPE_FILE = 14, - BIO_TYPE_NULL = 15, - BIO_TYPE_BIO = 16, /* (half a) BIO pair */ - - /* socket, fd, connect or accept */ - BIO_TYPE_DESCRIPTOR = 0x100, - - BIO_TYPE_FD = 17|BIO_TYPE_DESCRIPTOR, - BIO_TYPE_SOCKET = 18|BIO_TYPE_DESCRIPTOR, - /* socket - connect */ - BIO_TYPE_CONNECT = 19|BIO_TYPE_DESCRIPTOR, - /* socket for accept */ - BIO_TYPE_ACCEPT = 20|BIO_TYPE_DESCRIPTOR, - BIO_TYPE_DGRAM = 21|BIO_TYPE_DESCRIPTOR, -}; - -/* - * BIO_FILENAME_READ|BIO_CLOSE to open or close on free. - * BIO_set_fp(in,stdin,BIO_NOCLOSE); - */ -#define BIO_CLOSE 1 -#define BIO_NOCLOSE 0 - -/* - * These are used in the following macros and are passed to BIO_ctrl() - */ -enum WS_BIO_CTRL { - BIO_CTRL_RESET = 1, /* opt - rewind/zero etc */ - BIO_CTRL_EOF = 2, /* opt - are we at the eof */ - BIO_CTRL_INFO = 3, /* opt - extra tit-bits */ - BIO_CTRL_SET = 4, /* man - set the 'IO' type */ - BIO_CTRL_GET = 5, /* man - get the 'IO' type */ - BIO_CTRL_PUSH = 6, /* opt - internal, used to signify change */ - BIO_CTRL_POP = 7, /* opt - internal, used to signify change */ - BIO_CTRL_GET_CLOSE = 8, /* man - set the 'close' on free */ - BIO_CTRL_SET_CLOSE = 9, /* man - set the 'close' on free */ - BIO_CTRL_PENDING = 10, /* opt - is their more data buffered */ - BIO_CTRL_FLUSH = 11, /* opt - 'flush' buffered output */ - BIO_CTRL_DUP = 12, /* man - extra stuff for 'duped' BIO */ - BIO_CTRL_WPENDING = 13, /* opt - number of bytes still to write */ - - /* callback is int cb(BIO *bio,state,ret); */ - BIO_CTRL_SET_CALLBACK = 14, /* opt - set callback function */ - BIO_CTRL_GET_CALLBACK = 15, /* opt - set callback function */ - - BIO_CTRL_SET_FILENAME = 30, /* BIO_s_file special */ - - /* dgram BIO stuff */ - BIO_CTRL_DGRAM_CONNECT = 31, /* BIO dgram special */ - BIO_CTRL_DGRAM_SET_CONNECTED = 32, /* allow for an externally connected - * socket to be passed in */ - BIO_CTRL_DGRAM_SET_RECV_TIMEOUT = 33, /* setsockopt, essentially */ - BIO_CTRL_DGRAM_GET_RECV_TIMEOUT = 34, /* getsockopt, essentially */ - BIO_CTRL_DGRAM_SET_SEND_TIMEOUT = 35, /* setsockopt, essentially */ - BIO_CTRL_DGRAM_GET_SEND_TIMEOUT = 36, /* getsockopt, essentially */ - - BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP = 37, /* flag whether the last */ - BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP = 38, /* I/O operation tiemd out */ - - /* #ifdef IP_MTU_DISCOVER */ - BIO_CTRL_DGRAM_MTU_DISCOVER = 39, /* set DF bit on egress packets */ - /* #endif */ - - BIO_CTRL_DGRAM_QUERY_MTU = 40, /* as kernel for current MTU */ - BIO_CTRL_DGRAM_GET_FALLBACK_MTU = 47, - BIO_CTRL_DGRAM_GET_MTU = 41, /* get cached value for MTU */ - BIO_CTRL_DGRAM_SET_MTU = 42, /* set cached value for MTU. - * want to use this if asking - * the kernel fails */ - BIO_CTRL_DGRAM_MTU_EXCEEDED = 43, /* check whether the MTU was - * exceed in the previous write - * operation */ - BIO_CTRL_DGRAM_GET_PEER = 46, - BIO_CTRL_DGRAM_SET_PEER = 44, /* Destination for the data */ - - BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT = 45, /* Next DTLS handshake timeout - * to adjust socket timeouts */ - BIO_CTRL_DGRAM_SET_DONT_FRAG = 48, - BIO_CTRL_DGRAM_GET_MTU_OVERHEAD = 49, -}; - -/* modifiers */ -enum WS_BIO_MOD { - BIO_FP_READ = 0x02, - BIO_FP_WRITE = 0x04, - BIO_FP_APPEND = 0x08, - BIO_FP_TEXT = 0x10, -}; - - -/* flags */ -enum WS_BIO_FLAGS { - BIO_FLAGS_READ = 0x01, - BIO_FLAGS_WRITE = 0x02, - BIO_FLAGS_IO_SPECIAL = 0x04, - BIO_FLAGS_RWS = BIO_FLAGS_READ| - BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL, - BIO_FLAGS_SHOULD_RETRY = 0x08, - BIO_FLAGS_BASE64_NO_NL = 0x100, - /* Used with memory BIOs: shouldn't free up or change the data in any way. - */ - BIO_FLAGS_MEM_RDONLY = 0x200, -}; - -enum WS_BIO_RR { -/* Returned from the SSL bio when the certificate retrieval code had an error */ - BIO_RR_SSL_X509_LOOKUP = 0x01, -/* Returned from the connect BIO when a connect would have blocked */ - BIO_RR_CONNECT = 0x02, -/* Returned from the accept BIO when an accept would have blocked */ - BIO_RR_ACCEPT = 0x03, -}; - -/* These are passed by the BIO callback */ -enum WS_BIO_CB_FLAGS { - BIO_CB_FREE = 0x01, - BIO_CB_READ = 0x02, - BIO_CB_WRITE = 0x03, - BIO_CB_PUTS = 0x04, - BIO_CB_GETS = 0x05, - BIO_CB_CTRL = 0x06, - BIO_CB_RETURN = 0x80, -}; - -#define BIO_CB_return(a) ((a) | BIO_CB_RETURN) -#define BIO_cb_post(a) ((a) & BIO_CB_RETURN) -#define BIO_cb_pre(a) (!BIO_cb_post((a))) - - -typedef struct WOLFCRYPT_BIO WOLFCRYPT_BIO; -typedef struct WOLFCRYPT_BIO_METHOD WOLFCRYPT_BIO_METHOD; - -typedef void WOLFCRYPT_BIO_info_cb (WOLFCRYPT_BIO *, int, const char *, - int, long, long); - -/* Compatibility with previous name */ -#define WOLFSSL_BIO WOLFCRYPT_BIO - -/* wolfSSL BIO_METHOD type */ -struct WOLFCRYPT_BIO_METHOD { - int type; /* method type */ - const char *name; - int (*bwrite) (WOLFCRYPT_BIO *, const char *, int); - int (*bread) (WOLFCRYPT_BIO *, char *, int); - int (*bputs) (WOLFCRYPT_BIO *, const char *); - int (*bgets) (WOLFCRYPT_BIO *, char *, int); - long (*ctrl) (WOLFCRYPT_BIO *, int, long, void *); - int (*create) (WOLFCRYPT_BIO *); - int (*destroy) (WOLFCRYPT_BIO *); - long (*callback_ctrl) (WOLFCRYPT_BIO *, int, WOLFCRYPT_BIO_info_cb *); -}; - -struct WOLFCRYPT_BIO { - WOLFCRYPT_BIO_METHOD *method; - /* bio, mode, argp, argi, argl, ret */ - long (*callback) (WOLFCRYPT_BIO *, int, const char *, int, long, long); - char *cb_arg; /* first argument for the callback */ - int init; - int shutdown; - int flags; /* extra storage */ - int retry_reason; - int num; - void *ptr; - WOLFCRYPT_BIO *next_bio; /* used by filter BIOs */ - WOLFCRYPT_BIO *prev_bio; /* used by filter BIOs */ - int references; - wolfSSL_Mutex refMutex; /* to lock r/w on references */ - unsigned long num_read; - unsigned long num_write; -}; - -enum WS_BIO_C_FLAGS { - BIO_C_SET_CONNECT = 100, - BIO_C_DO_STATE_MACHINE = 101, - BIO_C_SET_NBIO = 102, - BIO_C_SET_PROXY_PARAM = 103, - BIO_C_SET_FD = 104, - BIO_C_GET_FD = 105, - BIO_C_SET_FILE_PTR = 106, - BIO_C_GET_FILE_PTR = 107, - BIO_C_SET_FILENAME = 108, - BIO_C_SET_SSL = 109, - BIO_C_GET_SSL = 110, - BIO_C_SET_MD = 111, - BIO_C_GET_MD = 112, - BIO_C_GET_CIPHER_STATUS = 113, - BIO_C_SET_BUF_MEM = 114, - BIO_C_GET_BUF_MEM_PTR = 115, - BIO_C_GET_BUFF_NUM_LINES = 116, - BIO_C_SET_BUFF_SIZE = 117, - BIO_C_SET_ACCEPT = 118, - BIO_C_SSL_MODE = 119, - BIO_C_GET_MD_CTX = 120, - BIO_C_GET_PROXY_PARAM = 121, - BIO_C_SET_BUFF_READ_DATA = 122, /* data to read first */ - BIO_C_GET_CONNECT = 123, - BIO_C_GET_ACCEPT = 124, - BIO_C_SET_SSL_RENEGOTIATE_BYTES = 125, - BIO_C_GET_SSL_NUM_RENEGOTIATES = 126, - BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT = 127, - BIO_C_FILE_SEEK = 128, - BIO_C_GET_CIPHER_CTX = 129, - BIO_C_SET_BUF_MEM_EOF_RETURN = 130, /* return end of input - * value */ - BIO_C_SET_BIND_MODE = 131, - BIO_C_GET_BIND_MODE = 132, - BIO_C_FILE_TELL = 133, - BIO_C_GET_SOCKS = 134, - BIO_C_SET_SOCKS = 135, - - BIO_C_SET_WRITE_BUF_SIZE = 136, /* for BIO_s_bio */ - BIO_C_GET_WRITE_BUF_SIZE = 137, - BIO_C_MAKE_BIO_PAIR = 138, - BIO_C_DESTROY_BIO_PAIR = 139, - BIO_C_GET_WRITE_GUARANTEE = 140, - BIO_C_GET_READ_REQUEST = 141, - BIO_C_SHUTDOWN_WR = 142, - BIO_C_NREAD0 = 143, - BIO_C_NREAD = 144, - BIO_C_NWRITE0 = 145, - BIO_C_NWRITE = 146, - BIO_C_RESET_READ_REQUEST = 147, - BIO_C_SET_MD_CTX = 148, - - BIO_C_SET_PREFIX = 149, - BIO_C_GET_PREFIX = 150, - BIO_C_SET_SUFFIX = 151, - BIO_C_GET_SUFFIX = 152, - - BIO_C_SET_EX_ARG = 153, - BIO_C_GET_EX_ARG = 154, -}; - -/* connect BIO */ -enum WS_BIO_CONN { - BIO_CONN_S_BEFORE = 1, - BIO_CONN_S_GET_IP = 2, - BIO_CONN_S_GET_PORT = 3, - BIO_CONN_S_CREATE_SOCKET = 4, - BIO_CONN_S_CONNECT = 5, - BIO_CONN_S_OK = 6, - BIO_CONN_S_BLOCKED_CONNECT = 7, - BIO_CONN_S_NBIO = 8, -}; - -enum WS_BIO_BIND { - BIO_BIND_NORMAL = 0, - BIO_BIND_REUSEADDR_IF_UNUSED = 1, - BIO_BIND_REUSEADDR = 2, -}; - -enum WC_BIO_OPT { - BIO_OPT_NONE = 0, - BIO_OPT_TCP_NO_DELAY = 1, - BIO_OPT_IGN_SIGPIPE = 2, -}; - -WOLFSSL_API WOLFCRYPT_BIO *wc_BioNew(WOLFCRYPT_BIO_METHOD *method); -WOLFSSL_API int wc_BioFree(WOLFCRYPT_BIO *bio); -WOLFSSL_API void wc_BioFreeAll(WOLFCRYPT_BIO *bio); - -WOLFSSL_API const char *wc_BioMethodName(const WOLFCRYPT_BIO *bio); -WOLFSSL_API int wc_BioMethodType(const WOLFCRYPT_BIO *bio); - -WOLFSSL_API int wc_BioSet(WOLFCRYPT_BIO *bio, - WOLFCRYPT_BIO_METHOD *method); -WOLFSSL_API void wc_BioClearFlags(WOLFCRYPT_BIO *bio, int flags); -WOLFSSL_API void wc_BioSetFlags(WOLFCRYPT_BIO *bio, int flags); -WOLFSSL_API int wc_BioTestFlags(const WOLFCRYPT_BIO *bio, int flags); - -#define wc_BioGetFlags(bio) wc_BioTestFlags(bio, ~(0x0)) -#define wc_BioSetRetrySpecial(bio) \ - wc_BioSetFlags(bio, (BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY)) -#define wc_BioSetRetryRead(bio) \ - wc_BioSetFlags(bio, (BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY)) -#define wc_BioSetRetryWrite(bio) \ - wc_BioSetFlags(bio, (BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY)) - - -#define wc_BioClearRetryFlags(bio) \ - wc_BioClearFlags(bio, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) -#define wc_BioGetRetryFlags(bio) \ - wc_BioTestFlags(bio, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) - -/* These should be used by the application to tell why we should retry */ -#define wc_BioShouldRead(bio) wc_BioTestFlags(bio, BIO_FLAGS_READ) -#define wc_BioShouldWrite(bio) wc_BioTestFlags(bio, BIO_FLAGS_WRITE) -#define wc_BioShouldIoSpecial(bio) wc_BioTestFlags(bio, BIO_FLAGS_IO_SPECIAL) -#define wc_BioRetryType(bio) wc_BioTestFlags(bio, BIO_FLAGS_RWS) -#define wc_BioShouldRetry(bio) wc_BioTestFlags(bio, BIO_FLAGS_SHOULD_RETRY) - -WOLFSSL_API long (*wc_BioGetCallback(const WOLFCRYPT_BIO *bio)) - (WOLFCRYPT_BIO *, int, const char *, int, long, long); - -WOLFSSL_API void wc_BioSetCallback(WOLFCRYPT_BIO *bio, - long (*cb) (WOLFCRYPT_BIO *, int, - const char *, int, long, long)); -WOLFSSL_API void wc_BioSetCallbackArg(WOLFCRYPT_BIO *bio, char *arg); -WOLFSSL_API char *wc_BioGetCallbackArg(const WOLFCRYPT_BIO *bio); - -WOLFSSL_API int wc_BioRead(WOLFCRYPT_BIO *bio, void *out, int outl); -WOLFSSL_API int wc_BioWrite(WOLFCRYPT_BIO *bio, const void *in, int inl); -WOLFSSL_API int wc_BioPuts(WOLFCRYPT_BIO *bio, const char *in); -WOLFSSL_API int wc_BioGets(WOLFCRYPT_BIO *bio, char *in, int inl); - -WOLFSSL_API WOLFCRYPT_BIO *wc_BioPush(WOLFCRYPT_BIO *top, - WOLFCRYPT_BIO *next); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioPop(WOLFCRYPT_BIO *bio); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioNext(WOLFCRYPT_BIO *bio); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioGetRetryBio(WOLFCRYPT_BIO *bio, - int *reason); -WOLFSSL_API int wc_BioGetRetryReason(WOLFCRYPT_BIO *bio); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioFindType(WOLFCRYPT_BIO *bio, int type); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioDupChain(WOLFCRYPT_BIO *bio); -#define wc_BioDupState(bio, ret) \ - wc_BioCtrl(bio, BIO_CTRL_DUP, 0, ret) - -WOLFSSL_API long wc_BioCtrl(WOLFCRYPT_BIO *bio, int cmd, - long larg, void *parg); -WOLFSSL_API long wc_BioCallbackCtrl(WOLFCRYPT_BIO *bio, int cmd, - void (*fp) (WOLFCRYPT_BIO *, int, - const char *, int, long, long)); -WOLFSSL_API long wc_BioIntCtrl(WOLFCRYPT_BIO *bio, int cmd, - long larg, int iarg); -WOLFSSL_API char *wc_BioPtrCtrl(WOLFCRYPT_BIO *bio, int cmd, long larg); -WOLFSSL_API size_t wc_BioCtrlPending(WOLFCRYPT_BIO *bio); -WOLFSSL_API size_t wc_BioCtrlWpending(WOLFCRYPT_BIO *bio); - -WOLFSSL_API int wc_BioIndent(WOLFCRYPT_BIO *bio, int indent, int max); - -WOLFSSL_API unsigned long wc_BioNumberRead(WOLFCRYPT_BIO *bio); -WOLFSSL_API unsigned long wc_BioNumberWritten(WOLFCRYPT_BIO *bio); - -#define wc_BioReset(bio) \ - (int)wc_BioCtrl(bio, BIO_CTRL_RESET, 0, NULL) -#define wc_BioEof(bio) \ - (int)wc_BioCtrl(bio, BIO_CTRL_EOF, 0, NULL) -#define wc_BioSetClose(bio,c) \ - (int)wc_BioCtrl(bio, BIO_CTRL_SET_CLOSE, (c), NULL) -#define wc_BioGetClose(bio) \ - (int)wc_BioCtrl(bio, BIO_CTRL_GET_CLOSE, 0, NULL) -#define wc_BioPending(bio) \ - (int)wc_BioCtrl(bio, BIO_CTRL_PENDING, 0, NULL) -#define wc_BioWpending(bio) \ - (int)wc_BioCtrl(bio, BIO_CTRL_WPENDING, 0, NULL) -#define wc_BioFlush(bio) \ - (int)wc_BioCtrl(bio, BIO_CTRL_FLUSH, 0, NULL) -#define wc_BioSetInfoCallback(bio, cb) \ - (int)wc_BioCallbackCtrl(bio, BIO_CTRL_SET_CALLBACK, cb) -#define wc_BioGetInfoCallback(bio, cb) \ - (int)wc_BioCallbackCtrl(bio, BIO_CTRL_GET_CALLBACK, 0, cb) - -WOLFSSL_API void wc_BioCopyNextRetry(WOLFCRYPT_BIO *b); - -#ifndef NO_STDIO_FILESYSTEM -WOLFSSL_API int wc_BioPrintf(WOLFCRYPT_BIO *bio, const char *format, ...); -#endif - -/* BIO file */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_s_file(void); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioNewFp(XFILE f, int close_flag); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioNewFile(const char *name, - const char *mode); -#define wc_BioSetFp(bio, f, c) \ - wc_BioCtrl(bio, BIO_C_SET_FILE_PTR, c, (char *)f) -#define wc_BioGetFp(bio, f) \ - wc_BioCtrl(bio, BIO_C_GET_FILE_PTR, 0, (char *)f) -#define wc_BioSeek(bio, ofs) \ - (int)wc_BioCtrl(bio, BIO_C_FILE_SEEK, ofs, NULL) -#define wc_BioTell(bio) \ - (int)wc_BioCtrl(bio, BIO_C_FILE_TELL, 0, NULL) -#define wc_BioReadFilename(bio, name) \ - wc_BioCtrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE|BIO_FP_READ, name) -#define wc_BioWriteFilename(bio, name) \ - wc_BioCtrl(bio,BIO_C_SET_FILENAME, BIO_CLOSE|BIO_FP_WRITE, name) -#define wc_BioAppendFilename(bio, name) \ - wc_BioCtrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE|BIO_FP_APPEND, name) -#define wc_BioRwFilename(bio, name) \ - wc_BioCtrl(bio, BIO_C_SET_FILENAME, \ - BIO_CLOSE|BIO_FP_READ|BIO_FP_WRITE,name) - -/* BIO memory */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_s_mem(void); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioNewMemBuf(void *data, int len); - -/* BIO fd */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_s_fd(void); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioNewFd(int fd, int close_flag); - -/* BIO null */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_s_null(void); - -/* BIO socket */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_s_socket(void); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioNewSocket(int fd, int close_flag); -WOLFSSL_API int wc_BioSockShouldRetry(int i); -WOLFSSL_API int wc_BioSockNonFatalError(int err); - -#define wc_BioSetFd(bio, fd, c) \ - wc_BioIntCtrl(bio, BIO_C_SET_FD, c, fd) -#define wc_BioGetFd(bio, c) \ - wc_BioCtrl(bio, BIO_C_GET_FD, 0, (char *)c) - -/* BIO connect */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_s_connect(void); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioNewConnect(const char *host_port); -#define wc_BioSetConnHostname(bio, hname) \ - wc_BioCtrl(bio, BIO_C_SET_CONNECT, 0, (char *)hname) -#define wc_BioSetConnPort(bio, port) \ - wc_BioCtrl(bio, BIO_C_SET_CONNECT, 1, (char *)port) -#define wc_BioSetConnIp(bio, ip) \ - wc_BioCtrl(bio, BIO_C_SET_CONNECT, 2, (char *)ip) -#define wc_BioSetConnIntPort(bio, port) \ - wc_BioCtrl(bio, BIO_C_SET_CONNECT, 3, (int *)port) -#define wc_BioGetConnHostname(bio) \ - wc_BioPtrCtrl(bio, BIO_C_GET_CONNECT, 0) -#define wc_BioGetConnPort(bio) \ - wc_BioPtrCtrl(bio, BIO_C_GET_CONNECT, 1) -#define wc_BioGetConnIp(bio) \ - wc_BioPtrCtrl(bio, BIO_C_GET_CONNECT, 2) -#define wc_BioGetConnIntPort(bio) \ - wc_BioIntCtrl(bio, BIO_C_GET_CONNECT, 3, 0) - -#define wc_BioSetNbio(bio, n) \ - wc_BioCtrl(bio, BIO_C_SET_NBIO, n, NULL) - -#define wc_BioDoHandshake(bio) \ - wc_BioCtrl(bio, BIO_C_DO_STATE_MACHINE, 0, NULL) -#define wc_BioDoConnect(bio) wc_BioDoHandshake(bio) -#define wc_BioDoAccept(bio) wc_BioDoHandshake(bio) - -/* BIO accept */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_s_accept(void); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioNewAccept(const char *str); -#define wc_BioSetAcceptPort(bio, name) \ - wc_BioCtrl(bio, BIO_C_SET_ACCEPT, 0, (char *)name) -#define wc_BioGetAcceptPort(bio) \ - wc_BioPtrCtrl(bio, BIO_C_GET_ACCEPT, 0) -#define wc_BioSetNbioAccept(bio, name) \ - wc_BioCtrl(bio, BIO_C_SET_ACCEPT, 1, name ? (void *)"a" : NULL) -#define wc_BioSetAcceptBios(bio, nbio) \ - wc_BioCtrl(bio, BIO_C_SET_ACCEPT, 2, nbio) -#define wc_BioSetBindMode(bio, mode) \ - wc_BioCtrl(bio, BIO_C_SET_BIND_MODE, mode, NULL) -#define wc_BioGetBindMode(bio, mode) \ - wc_BioCtrl(bio, BIO_C_GET_BIND_MODE, 0, NULL) -#define wc_BioSetSocketOptions(bio, opt) \ - wc_BioCtrl(bio, BIO_C_SET_EX_ARG, opt, NULL) - - -/* BIO datagram */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_s_datagram(void); -WOLFSSL_API WOLFCRYPT_BIO *wc_BioNewDgram(int fd, int close_flag); - -/* BIO filter buffer */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_f_buffer(void); - -#define wc_BioGetMemData(bio, data) \ - wc_BioCtrl(bio, BIO_CTRL_INFO, 0, data) -#define wc_BioSetMemBuf(bio, data, c) \ - wc_BioCtrl(bio, BIO_C_SET_BUF_MEM, c, data) -#define wc_BioGetMemPtr(b, ptr) \ - wc_BioCtrl(bio, BIO_C_GET_BUF_MEM_PTR,0, (char *)ptr) -#define wc_BioSetMemEofReturn(bio, v) \ - wc_BioCtrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, v, NULL) - -#define wc_BioGetBufferNumLines(bio) \ - wc_BioCtrl(bio, BIO_C_GET_BUFF_NUM_LINES, 0, NULL) -#define wc_BioSetBufferSize(bio, size) \ - wc_BioCtrl(bio, BIO_C_SET_BUFF_SIZE, size, NULL) -#define wc_BioSetReadBufferSize(bio, size) \ - wc_BioIntCtrl(bio, BIO_C_SET_BUFF_SIZE, size, 0) -#define wc_BioSetWriteBufferSize(bio, size) \ - wc_BioIntCtrl(bio, BIO_C_SET_BUFF_SIZE, size, 1) -#define wc_BioSetBufferReadData(bio, buf, num) \ - wc_BioCtrl(bio, BIO_C_SET_BUFF_READ_DATA, num, buf) - -/* BIO filter cipher */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_f_cipher(void); -WOLFSSL_API void wc_BioSetCipher(WOLFCRYPT_BIO *bio, - const WOLFCRYPT_EVP_CIPHER *cipher, - const unsigned char *key, - const unsigned char *iv, int enc); - -#define wc_BioGetCipherStatus(bio) \ - wc_BioCtrl(bio, BIO_C_GET_CIPHER_STATUS, 0, NULL) -#define wc_BioGetCipherCtx(bio, ctx) \ - wc_BioCtrl(bio, BIO_C_GET_CIPHER_CTX, 0, ctx) - -/* BIO filter base64 */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_f_base64(void); - -/* BIO filter digest */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_f_md(void); - -#define wc_BioSetMd(bio, md) \ - wc_BioCtrl(bio, BIO_C_SET_MD, 0, (WOLFCRYPT_EVP_MD *)md) -#define wc_BioGetMd(bio,md) \ - wc_BioCtrl(bio, BIO_C_GET_MD, 0, (WOLFCRYPT_EVP_MD *)md) -#define wc_BioGetmdCtx(bio,ctx) \ - wc_BioCtrl(bio, BIO_C_GET_MD_CTX, 0, (WOLFCRYPT_EVP_MD_CTX *)ctx) -#define wc_BioSetMdCtx(bio, ctx) \ - wc_BioCtrl(bio, BIO_C_SET_MD_CTX, 0, (WOLFCRYPT_EVP_MD_CTX *)ctx) - -/* BIO filter SSL */ -WOLFSSL_API WOLFCRYPT_BIO_METHOD *wc_Bio_f_ssl(void); -WOLFSSL_API void wc_BioSSLShutdown(WOLFCRYPT_BIO *bio); - -#define wc_BioSetSSL(bio, ssl, mode) \ - wc_BioCtrl(bio, BIO_C_SET_SSL, mode, ssl) -#define wc_BioGetSSL(bio, ssl) \ - wc_BioCtrl(bio, BIO_C_GET_SSL, 0, ssl) -#define wc_BIOSetSSLMode(bio, client) \ - wc_BioCtrl(bio, BIO_C_SSL_MODE, client, NULL) -#define wc_BIOSetSSLRenegotiateBytes(bio, num) \ - wc_BIOCtrl(bio, BIO_C_SET_SSL_RENEGOTIATE_BYTES, num, NULL); -#define wc_BIOGetNumRenegotiates(bio) \ - wc_BIOCtrl(bio, BIO_C_GET_SSL_NUM_RENEGOTIATES, 0, NULL); -#define wc_BIOSetSSLRenegotiateTimeout(bio, seconds) \ - wc_BIOCtrl(bio, BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT, seconds, NULL); - - -/* BIO socket internal functions */ -int wc_BioGetHostIp(const char *str, unsigned char *ip); -int wc_BioGetPort(const char *str, unsigned short *port_ptr); -int wc_BioSockError(int sock); -int wc_BioSockInit(void); -void wc_BioSockCleanup(void); -int wc_BioGetAcceptSocket(char *host, int bind_mode); -int wc_BioAccept(int sock, char **addr); -int wc_BioSetTcpNdelay(int s, int on); -int wc_BioSetTcpNsigpipe(int s, int on); -int wc_BioSocketNbio(int s, int mode); - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* OPENSSL_EXTRA */ -#endif /* WOLF_CRYPT_BIO_H */ diff --git a/wolfssl/wolfcrypt/compat-wolfcrypt.h b/wolfssl/wolfcrypt/compat-wolfcrypt.h deleted file mode 100644 index ed0e8e21b..000000000 --- a/wolfssl/wolfcrypt/compat-wolfcrypt.h +++ /dev/null @@ -1,352 +0,0 @@ -/* evp.h - * - * Copyright (C) 2015 wolfSSL Inc. - * - * This file is part of wolfSSL. (formerly known as CyaSSL) - * - * wolfSSL is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (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 - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#ifndef WOLF_CRYPT_COMPAT_WOLFSSL_H -#define WOLF_CRYPT_COMPAT_WOLFSSL_H - -#include - -#ifndef NO_MD5 -#include -#endif -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/* EVP digest */ -typedef char WOLFCRYPT_EVP_MD; -#ifndef NO_MD5 -WOLFSSL_API const WOLFCRYPT_EVP_MD* wc_EVP_md5(void); -#endif -#ifndef NO_SHA -WOLFSSL_API const WOLFCRYPT_EVP_MD* wc_EVP_sha1(void); -#endif -WOLFSSL_API const WOLFCRYPT_EVP_MD* wc_EVP_sha256(void); -#ifdef WOLFSSL_SHA384 -WOLFSSL_API const WOLFCRYPT_EVP_MD* wc_EVP_sha384(void); -#endif -#ifdef WOLFSSL_SHA512 -WOLFSSL_API const WOLFCRYPT_EVP_MD* wc_EVP_sha512(void); -#endif -#ifdef WOLFSSL_RIPEMD -WOLFSSL_API const WOLFCRYPT_EVP_MD* wc_EVP_ripemd160(void); -#endif - -/* EVP Cipher */ -typedef char WOLFCRYPT_EVP_CIPHER; -#ifndef NO_AES -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_128_cbc(void); -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_192_cbc(void); -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_256_cbc(void); -#ifdef WOLFSSL_AES_COUNTER -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_128_ctr(void); -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_192_ctr(void); -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_aes_256_ctr(void); -#endif /* WOLFSSL_AES_COUNTER */ -#endif /* NO_AES */ -#ifndef NO_DES3 -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_des_cbc(void); -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_des_ede3_cbc(void); -#endif -#ifdef HAVE_IDEA -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_idea_cbc(void); -#endif -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_rc4(void); -WOLFSSL_API const WOLFCRYPT_EVP_CIPHER* wc_EVP_enc_null(void); - -enum WC_Digest { - MD5_DIGEST_LENGTH = 16, - SHA_DIGEST_LENGTH = 20, - SHA256_DIGEST_LENGTH = 32, - SHA384_DIGEST_LENGTH = 48, - SHA512_DIGEST_LENGTH = 64 -}; - -#ifndef EVP_MAX_MD_SIZE -#define EVP_MAX_MD_SIZE 64 /* sha512 */ -#endif - -enum WC_Cipher { - RC4_KEY_SIZE = 16, /* always 128bit */ - DES_KEY_SIZE = 8, /* des */ - DES3_KEY_SIZE = 24, /* 3 des ede */ - DES_IV_SIZE = 8, /* des */ - AES_256_KEY_SIZE = 32, /* for 256 bit */ - AES_192_KEY_SIZE = 24, /* for 192 bit */ - AES_IV_SIZE = 16, /* always block size */ - AES_128_KEY_SIZE = 16, /* for 128 bit */ - EVP_SALT_SIZE = 8, /* evp salt size 64 bits */ -}; - -#ifndef NO_MD5 -typedef struct { - int holder[24]; /* big enough, but check on init */ -} WOLFCRYPT_MD5_CTX; - -WOLFSSL_API void wc_MD5_Init(WOLFCRYPT_MD5_CTX*); -WOLFSSL_API void wc_MD5_Update(WOLFCRYPT_MD5_CTX*, const void*, unsigned long); -WOLFSSL_API void wc_MD5_Final(unsigned char*, WOLFCRYPT_MD5_CTX*); -#endif /* NO_MD5 */ - -typedef struct { - int holder[24]; /* big enough, but check on init */ -} WOLFCRYPT_SHA_CTX; - -WOLFSSL_API void wc_SHA_Init(WOLFCRYPT_SHA_CTX*); -WOLFSSL_API void wc_SHA_Update(WOLFCRYPT_SHA_CTX*, const void*, unsigned long); -WOLFSSL_API void wc_SHA_Final(unsigned char*, WOLFCRYPT_SHA_CTX*); - -/* SHA1 points to above, shouldn't use SHA0 ever */ -WOLFSSL_API void wc_SHA1_Init(WOLFCRYPT_SHA_CTX*); -WOLFSSL_API void wc_SHA1_Update(WOLFCRYPT_SHA_CTX*, const void*, unsigned long); -WOLFSSL_API void wc_SHA1_Final(unsigned char*, WOLFCRYPT_SHA_CTX*); - -typedef struct { - int holder[28]; /* big enough, but check on init */ -} WOLFCRYPT_SHA256_CTX; - -WOLFSSL_API void wc_SHA256_Init(WOLFCRYPT_SHA256_CTX*); -WOLFSSL_API void wc_SHA256_Update(WOLFCRYPT_SHA256_CTX*, - const void*, unsigned long); -WOLFSSL_API void wc_SHA256_Final(unsigned char*, WOLFCRYPT_SHA256_CTX*); - -#ifdef WOLFSSL_SHA384 -typedef struct { - long long holder[32]; /* big enough, but check on init */ -} WOLFCRYPT_SHA384_CTX; - -WOLFSSL_API void wc_SHA384_Init(WOLFCRYPT_SHA384_CTX*); -WOLFSSL_API void wc_SHA384_Update(WOLFCRYPT_SHA384_CTX*, - const void*, unsigned long); -WOLFSSL_API void wc_SHA384_Final(unsigned char*, WOLFCRYPT_SHA384_CTX*); -#endif /* WOLFSSL_SHA384 */ - -#ifdef WOLFSSL_SHA512 -typedef struct { - long long holder[36]; /* big enough, but check on init */ -} WOLFCRYPT_SHA512_CTX; - -WOLFSSL_API void wc_SHA512_Init(WOLFCRYPT_SHA512_CTX*); -WOLFSSL_API void wc_SHA512_Update(WOLFCRYPT_SHA512_CTX*, - const void*, unsigned long); -WOLFSSL_API void wc_SHA512_Final(unsigned char*, WOLFCRYPT_SHA512_CTX*); -#endif /* WOLFSSL_SHA512 */ - - -#ifdef WOLFSSL_RIPEMD -typedef struct { - int holder[32]; /* big enough, but check on init */ -} WOLFCRYPT_RIPEMD_CTX; - -WOLFSSL_API void wc_RIPEMD_Init(WOLFCRYPT_RIPEMD_CTX*); -WOLFSSL_API void wc_RIPEMD_Update(WOLFCRYPT_RIPEMD_CTX*, - const void*, unsigned long); -WOLFSSL_API void wc_RIPEMD_Final(unsigned char*, WOLFCRYPT_RIPEMD_CTX*); -#endif /* WOLFSSL_RIPEMD */ - -typedef struct { - Hmac hmac; - int type; -} WOLFCRYPT_HMAC_CTX; - -WOLFSSL_API void wc_HMAC_Init(WOLFCRYPT_HMAC_CTX* ctx, const void* key, - int keylen, const WOLFCRYPT_EVP_MD* type); -WOLFSSL_API void wc_HMAC_Update(WOLFCRYPT_HMAC_CTX* ctx, - const unsigned char* data, int len); -WOLFSSL_API void wc_HMAC_Final(WOLFCRYPT_HMAC_CTX* ctx, unsigned char* hash, - unsigned int* len); -WOLFSSL_API void wc_HMAC_cleanup(WOLFCRYPT_HMAC_CTX* ctx); - -WOLFSSL_API unsigned char* wc_HMAC(const WOLFCRYPT_EVP_MD* evp_md, - const void* key, int key_len, - const unsigned char* d, int n, - unsigned char* md, unsigned int* md_len); - -typedef union { -#ifndef NO_MD5 - WOLFCRYPT_MD5_CTX md5; -#endif - WOLFCRYPT_SHA_CTX sha; - WOLFCRYPT_SHA256_CTX sha256; -#ifdef WOLFSSL_SHA384 - WOLFCRYPT_SHA384_CTX sha384; -#endif -#ifdef WOLFSSL_SHA512 - WOLFCRYPT_SHA512_CTX sha512; -#endif -#ifdef WOLFSSL_RIPEMD - WOLFCRYPT_RIPEMD_CTX ripemd; -#endif -} WOLFCRYPT_Hasher; - -typedef struct WOLFCRYPT_EVP_MD_CTX { - unsigned char macType; - int macSize; - const WOLFCRYPT_EVP_MD *digest; - WOLFCRYPT_Hasher hash; -} WOLFCRYPT_EVP_MD_CTX; - -typedef union { -#ifndef NO_AES - Aes aes; -#endif -#ifndef NO_DES3 - Des des; - Des3 des3; -#endif - Arc4 arc4; -#ifdef HAVE_IDEA - Idea idea; -#endif -} WOLFCRYPT_Cipher; - -enum { - AES_128_CBC_TYPE = 1, - AES_192_CBC_TYPE = 2, - AES_256_CBC_TYPE = 3, - AES_128_CTR_TYPE = 4, - AES_192_CTR_TYPE = 5, - AES_256_CTR_TYPE = 6, - DES_CBC_TYPE = 7, - DES_EDE3_CBC_TYPE = 8, - ARC4_TYPE = 9, - NULL_CIPHER_TYPE = 10, - EVP_PKEY_RSA = 11, - EVP_PKEY_DSA = 12, - EVP_PKEY_EC = 13, - IDEA_CBC_TYPE = 14, - NID_sha1 = 64, - NID_md2 = 3, - NID_md5 = 4 -}; - -typedef struct { - int keyLen; /* user may set for variable */ - int blockSize; - int bufLen; - unsigned char enc; /* if encrypt side, then true */ - unsigned char cipherType; - unsigned char final_used; - unsigned char ivUpdate; - unsigned char padding; - -#ifndef NO_AES - unsigned char iv[AES_BLOCK_SIZE]; /* working iv pointer into cipher */ - unsigned char buf[AES_BLOCK_SIZE]; - unsigned char final[AES_BLOCK_SIZE]; -#elif !defined(NO_DES3) || defined(HAVE_IDEA) - unsigned char iv[DES_BLOCK_SIZE]; /* working iv pointer into cipher */ - unsigned char buf[DES_BLOCK_SIZE]; - unsigned char final[DES_BLOCK_SIZE]; -#endif - WOLFCRYPT_Cipher cipher; -} WOLFCRYPT_EVP_CIPHER_CTX; - -#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) -extern const char *EVP_AES_128_CBC; -extern const char *EVP_AES_192_CBC; -extern const char *EVP_AES_256_CBC; -#if defined(OPENSSL_EXTRA) -extern const char *EVP_AES_128_CTR; -extern const char *EVP_AES_192_CTR; -extern const char *EVP_AES_256_CTR; -#endif -extern const int EVP_AES_SIZE; - -extern const char *EVP_DES_CBC; -extern const int EVP_DES_SIZE; - -extern const char *EVP_DES_EDE3_CBC; -extern const int EVP_DES_EDE3_SIZE; - -#ifdef HAVE_IDEA -extern const char *EVP_IDEA_CBC; -extern const int EVP_IDEA_SIZE; -#endif - -#endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */ - -WOLFSSL_API int wc_EVP_MD_size(const WOLFCRYPT_EVP_MD* md); -WOLFSSL_API void wc_EVP_MD_CTX_init(WOLFCRYPT_EVP_MD_CTX* ctx); -WOLFSSL_API int wc_EVP_MD_CTX_cleanup(WOLFCRYPT_EVP_MD_CTX* ctx); -WOLFSSL_API int wc_EVP_MD_CTX_copy(WOLFCRYPT_EVP_MD_CTX *out, - const WOLFCRYPT_EVP_MD_CTX *in); -WOLFSSL_API const WOLFCRYPT_EVP_MD* wc_EVP_get_digestbynid(int); - - -WOLFSSL_API int wc_EVP_DigestInit(WOLFCRYPT_EVP_MD_CTX* ctx, - const WOLFCRYPT_EVP_MD* type); -WOLFSSL_API int wc_EVP_DigestUpdate(WOLFCRYPT_EVP_MD_CTX* ctx, - const void* data, unsigned long sz); -WOLFSSL_API int wc_EVP_DigestFinal(WOLFCRYPT_EVP_MD_CTX* ctx, - unsigned char* md, unsigned int* s); -WOLFSSL_API int wc_EVP_DigestFinal_ex(WOLFCRYPT_EVP_MD_CTX* ctx, - unsigned char* md, unsigned int* s); -#ifndef NO_MD5 -WOLFSSL_API int wc_EVP_BytesToKey(const WOLFCRYPT_EVP_CIPHER*, - const WOLFCRYPT_EVP_MD*, - const unsigned char*, const unsigned char*, - int, int, unsigned char*, unsigned char*); -#endif - -WOLFSSL_API void wc_EVP_CIPHER_CTX_init(WOLFCRYPT_EVP_CIPHER_CTX* ctx); -WOLFSSL_API int wc_EVP_CIPHER_CTX_cleanup(WOLFCRYPT_EVP_CIPHER_CTX* ctx); -WOLFSSL_API int wc_EVP_CIPHER_CTX_iv_length(const WOLFCRYPT_EVP_CIPHER_CTX*); - -WOLFSSL_API int wc_EVP_CipherInit(WOLFCRYPT_EVP_CIPHER_CTX* ctx, - const WOLFCRYPT_EVP_CIPHER* type, - unsigned char* key, unsigned char* iv, - int enc); -WOLFSSL_API int wc_EVP_CipherUpdate(WOLFCRYPT_EVP_CIPHER_CTX *ctx, - unsigned char *dst, int *dstLen, - const unsigned char *src, int len); -WOLFSSL_API int wc_EVP_CipherFinal(WOLFCRYPT_EVP_CIPHER_CTX *ctx, - unsigned char *dst, int *dstLen); - -WOLFSSL_API int wc_EVP_CIPHER_CTX_key_length(WOLFCRYPT_EVP_CIPHER_CTX*); -WOLFSSL_API int wc_EVP_CIPHER_CTX_set_key_length(WOLFCRYPT_EVP_CIPHER_CTX*, - int); -WOLFSSL_API int wc_EVP_CIPHER_CTX_copy(WOLFCRYPT_EVP_CIPHER_CTX *out, - const WOLFCRYPT_EVP_CIPHER_CTX *in); - - -WOLFSSL_API int wc_EVP_Cipher(WOLFCRYPT_EVP_CIPHER_CTX* ctx, - unsigned char* dst, unsigned char* src, - unsigned int len); - -#ifndef EVP_MAX_MD_SIZE -#define EVP_MAX_MD_SIZE 64 /* sha512 */ -#endif - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* WOLF_CRYPT_COMPAT_WOLFSSL_H */ diff --git a/wolfssl/wolfcrypt/error-crypt.h b/wolfssl/wolfcrypt/error-crypt.h index 6d0a0a577..56c6ed8b9 100644 --- a/wolfssl/wolfcrypt/error-crypt.h +++ b/wolfssl/wolfcrypt/error-crypt.h @@ -174,39 +174,7 @@ enum { WC_KEY_SIZE_E = -234, /* Key size error, either too small or large */ ASN_COUNTRY_SIZE_E = -235, /* ASN Cert Gen, invalid country code size */ - BIO_CALLBACK_E = -250, /* BIO callback function failed */ - BIO_CREATE_METHOD_E = -251, /* BIO method create function failed */ - BIO_FIND_TYPE_E = -252, /* BIO find type failed */ - BIO_CTRL_E = -253, /* BIO Control function failed */ - BIO_UNINITIALIZED_E = -254, /* BIO is not initialized */ - BIO_PUTS_E = -255, /* BIO puts function failed */ - BIO_FILE_READ_E = -256, /* BIO file read failed */ - BIO_FILE_WRITE_E = -257, /* BIO file write failed */ - BIO_FILE_MODE_E = -258, /* BIO file bad open mode */ - BIO_FILE_OPEN_E = -259, /* BIO file open failed */ - BIO_FILE_GETS_E = -260, /* BIO file gets failed */ - BIO_MEM_WRITE_E = -261, /* BIO memory write failed */ - BIO_B64_ENCODE_E = -262, /* BIO filter base64 encode failed */ - BIO_B64_DECODE_E = -263, /* BIO filter base64 decode failed */ - BIO_DGST_INIT_E = -264, /* BIO filter digest init failed */ - BIO_DGST_UPDATE_E = -265, /* BIO filter digest update failed */ - BIO_DGST_FINAL_E = -266, /* BIO filter digest final failed */ - BIO_NO_HOSTNAME_E = -267, /* BIO connect, no hostname provided */ - BIO_NO_PORT_E = -268, /* BIO connect, no port provided */ - BIO_CREATE_SOCKET_E = -269, /* BIO connect, create socket failed */ - BIO_NBIO_E = -270, /* BIO connect, NBIO error */ - BIO_CONNECT_E = -271, /* BIO connect, connect failed */ - BIO_ADDR_AF_INET_E = -272, /* BIO connect, address not AF_INET type */ - BIO_SRV_PROTO_E = -273, /* BIO connect, bad server protocol */ - BIO_WSASTARTUP_E = -274, /* BIO connect, WSA Startup failed */ - BIO_BIND_SOCKET_E = -275, /* BIO connect, Bind socket failed */ - BIO_LISTEN_SOCKET_E = -276, /* BIO connect, Listen socket failed */ - BIO_ACCEPT_E = -277, /* BIO connect, Accept failed */ - BIO_KEEPALIVE_E = -278, /* BIO connect, Keep ALive failed */ - BIO_OPTIONS_E = -279, /* BIO connect, Options error */ - BIO_BAD_REF = -280, /* BIO, Bad reference number */ - - MIN_CODE_E = -300 /* errors -101 - -299 */ + MIN_CODE_E = -300 /* errors -101 - -299 */ /* add new companion error id strings for any new error codes wolfcrypt/src/error.c !!! */ diff --git a/wolfssl/wolfcrypt/include.am b/wolfssl/wolfcrypt/include.am index c57673a46..cdce81c61 100644 --- a/wolfssl/wolfcrypt/include.am +++ b/wolfssl/wolfcrypt/include.am @@ -15,8 +15,6 @@ nobase_include_HEADERS+= \ wolfssl/wolfcrypt/dh.h \ wolfssl/wolfcrypt/dsa.h \ wolfssl/wolfcrypt/ecc.h \ - wolfssl/wolfcrypt/bio.h \ - wolfssl/wolfcrypt/compat-wolfcrypt.h \ wolfssl/wolfcrypt/curve25519.h \ wolfssl/wolfcrypt/ed25519.h \ wolfssl/wolfcrypt/fe_operations.h \