@@ -599,6 +599,248 @@ static int testDevId = WOLFSSL_CAAM_DEVID;
599599static int testDevId = INVALID_DEVID;
600600#endif
601601
602+ #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
603+ !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
604+
605+ /* This set of memio functions allows for more fine tuned control of the TLS
606+ * connection operations. For new tests, try to use ssl_memio first. */
607+
608+ /* To dump the memory in gdb use
609+ * dump memory client.bin test_ctx.c_buff test_ctx.c_buff+test_ctx.c_len
610+ * dump memory server.bin test_ctx.s_buff test_ctx.s_buff+test_ctx.s_len
611+ * This can be imported into Wireshark by transforming the file with
612+ * od -Ax -tx1 -v client.bin > client.bin.hex
613+ * od -Ax -tx1 -v server.bin > server.bin.hex
614+ * And then loading test_output.dump.hex into Wireshark using the
615+ * "Import from Hex Dump..." option ion and selecting the TCP
616+ * encapsulation option.
617+ */
618+
619+ #define HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES
620+
621+ static WC_INLINE int test_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
622+ void *ctx)
623+ {
624+ struct test_memio_ctx *test_ctx;
625+ byte *buf;
626+ int *len;
627+
628+ test_ctx = (struct test_memio_ctx*)ctx;
629+
630+ if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
631+ buf = test_ctx->c_buff;
632+ len = &test_ctx->c_len;
633+ }
634+ else {
635+ buf = test_ctx->s_buff;
636+ len = &test_ctx->s_len;
637+ }
638+
639+ if ((unsigned)(*len + sz) > TEST_MEMIO_BUF_SZ)
640+ return WOLFSSL_CBIO_ERR_WANT_WRITE;
641+
642+ #ifdef WOLFSSL_DUMP_MEMIO_STREAM
643+ {
644+ char dump_file_name[64];
645+ WOLFSSL_BIO *dump_file;
646+ sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
647+ dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
648+ if (dump_file != NULL) {
649+ (void)wolfSSL_BIO_write(dump_file, data, sz);
650+ wolfSSL_BIO_free(dump_file);
651+ }
652+ }
653+ #endif
654+ XMEMCPY(buf + *len, data, (size_t)sz);
655+ *len += sz;
656+
657+ return sz;
658+ }
659+
660+ static WC_INLINE int test_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
661+ void *ctx)
662+ {
663+ struct test_memio_ctx *test_ctx;
664+ int read_sz;
665+ byte *buf;
666+ int *len;
667+
668+ test_ctx = (struct test_memio_ctx*)ctx;
669+
670+ if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
671+ buf = test_ctx->s_buff;
672+ len = &test_ctx->s_len;
673+ }
674+ else {
675+ buf = test_ctx->c_buff;
676+ len = &test_ctx->c_len;
677+ }
678+
679+ if (*len == 0)
680+ return WOLFSSL_CBIO_ERR_WANT_READ;
681+
682+ read_sz = sz < *len ? sz : *len;
683+
684+ XMEMCPY(data, buf, (size_t)read_sz);
685+ XMEMMOVE(buf, buf + read_sz,(size_t) (*len - read_sz));
686+
687+ *len -= read_sz;
688+
689+ return read_sz;
690+ }
691+
692+ int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
693+ int max_rounds, int *rounds)
694+ {
695+ byte handshake_complete = 0, hs_c = 0, hs_s = 0;
696+ int ret, err;
697+
698+ if (rounds != NULL)
699+ *rounds = 0;
700+ while (!handshake_complete && max_rounds > 0) {
701+ if (!hs_c) {
702+ wolfSSL_SetLoggingPrefix("client");
703+ ret = wolfSSL_connect(ssl_c);
704+ wolfSSL_SetLoggingPrefix(NULL);
705+ if (ret == WOLFSSL_SUCCESS) {
706+ hs_c = 1;
707+ }
708+ else {
709+ err = wolfSSL_get_error(ssl_c, ret);
710+ if (err != WOLFSSL_ERROR_WANT_READ &&
711+ err != WOLFSSL_ERROR_WANT_WRITE)
712+ return -1;
713+ }
714+ }
715+ if (!hs_s) {
716+ wolfSSL_SetLoggingPrefix("server");
717+ ret = wolfSSL_accept(ssl_s);
718+ wolfSSL_SetLoggingPrefix(NULL);
719+ if (ret == WOLFSSL_SUCCESS) {
720+ hs_s = 1;
721+ }
722+ else {
723+ err = wolfSSL_get_error(ssl_s, ret);
724+ if (err != WOLFSSL_ERROR_WANT_READ &&
725+ err != WOLFSSL_ERROR_WANT_WRITE)
726+ return -1;
727+ }
728+ }
729+ handshake_complete = hs_c && hs_s;
730+ max_rounds--;
731+ if (rounds != NULL)
732+ *rounds = *rounds + 1;
733+ }
734+
735+ if (!handshake_complete)
736+ return -1;
737+
738+ return 0;
739+ }
740+
741+ int test_memio_setup_ex(struct test_memio_ctx *ctx,
742+ WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
743+ method_provider method_c, method_provider method_s,
744+ byte *caCert, int caCertSz, byte *serverCert, int serverCertSz,
745+ byte *serverKey, int serverKeySz)
746+ {
747+ int ret;
748+ (void)caCert;
749+ (void)caCertSz;
750+ (void)serverCert;
751+ (void)serverCertSz;
752+ (void)serverKey;
753+ (void)serverKeySz;
754+
755+ if (ctx_c != NULL && *ctx_c == NULL) {
756+ *ctx_c = wolfSSL_CTX_new(method_c());
757+ if (*ctx_c == NULL)
758+ return -1;
759+ #ifndef NO_CERTS
760+ if (caCert == NULL) {
761+ ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
762+ }
763+ else {
764+ ret = wolfSSL_CTX_load_verify_buffer(*ctx_c, caCert, (long)caCertSz,
765+ WOLFSSL_FILETYPE_ASN1);
766+ }
767+ if (ret != WOLFSSL_SUCCESS)
768+ return -1;
769+ #endif /* NO_CERTS */
770+ wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
771+ wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
772+ if (ctx->c_ciphers != NULL) {
773+ ret = wolfSSL_CTX_set_cipher_list(*ctx_c, ctx->c_ciphers);
774+ if (ret != WOLFSSL_SUCCESS)
775+ return -1;
776+ }
777+ }
778+
779+ if (ctx_s != NULL && *ctx_s == NULL) {
780+ *ctx_s = wolfSSL_CTX_new(method_s());
781+ if (*ctx_s == NULL)
782+ return -1;
783+ #ifndef NO_CERTS
784+ if (serverKey == NULL) {
785+ ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, svrKeyFile,
786+ WOLFSSL_FILETYPE_PEM);
787+ }
788+ else {
789+ ret = wolfSSL_CTX_use_PrivateKey_buffer(*ctx_s, serverKey,
790+ (long)serverKeySz, WOLFSSL_FILETYPE_ASN1);
791+ }
792+ if (ret != WOLFSSL_SUCCESS)
793+ return- -1;
794+
795+ if (serverCert == NULL) {
796+ ret = wolfSSL_CTX_use_certificate_file(*ctx_s, svrCertFile,
797+ WOLFSSL_FILETYPE_PEM);
798+ }
799+ else {
800+ ret = wolfSSL_CTX_use_certificate_chain_buffer_format(*ctx_s,
801+ serverCert, (long)serverCertSz, WOLFSSL_FILETYPE_ASN1);
802+ }
803+ if (ret != WOLFSSL_SUCCESS)
804+ return -1;
805+ #endif /* NO_CERTS */
806+ wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
807+ wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
808+ if (ctx->s_ciphers != NULL) {
809+ ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
810+ if (ret != WOLFSSL_SUCCESS)
811+ return -1;
812+ }
813+ }
814+
815+ if (ctx_c != NULL && ssl_c != NULL) {
816+ *ssl_c = wolfSSL_new(*ctx_c);
817+ if (*ssl_c == NULL)
818+ return -1;
819+ wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
820+ wolfSSL_SetIOReadCtx(*ssl_c, ctx);
821+ }
822+ if (ctx_s != NULL && ssl_s != NULL) {
823+ *ssl_s = wolfSSL_new(*ctx_s);
824+ if (*ssl_s == NULL)
825+ return -1;
826+ wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
827+ wolfSSL_SetIOReadCtx(*ssl_s, ctx);
828+ #if !defined(NO_DH)
829+ SetDH(*ssl_s);
830+ #endif
831+ }
832+
833+ return 0;
834+ }
835+
836+ int test_memio_setup(struct test_memio_ctx *ctx,
837+ WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
838+ method_provider method_c, method_provider method_s)
839+ {
840+ return test_memio_setup_ex(ctx, ctx_c, ctx_s, ssl_c, ssl_s, method_c,
841+ method_s, NULL, 0, NULL, 0, NULL, 0);
842+ }
843+ #endif
602844
603845/*----------------------------------------------------------------------------*
604846 | BIO with fixed read/write size
0 commit comments