regression: add case 8102

Adds regression case 8102 which uses the TAF
TA_CRYPT_CMD_MBED_CHECK_CERT to verify a certificate chain.

Acked-by: Jerome Forissier <jerome.forissier@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/host/xtest/CMakeLists.txt b/host/xtest/CMakeLists.txt
index b307d07..9cf5d69 100644
--- a/host/xtest/CMakeLists.txt
+++ b/host/xtest/CMakeLists.txt
@@ -12,6 +12,23 @@
 
 include(GNUInstallDirs)
 
+macro(EMBED_8100FILE prefix infile)
+	add_custom_command(
+		OUTPUT  regression_8100_${prefix}.h
+		COMMAND ../../scripts/file_to_c.py --inf ${infile}
+	--out ${CMAKE_CURRENT_BINARY_DIR}/regression_8100_${prefix}.h
+	--name regression_8100_${prefix}
+		DEPENDS ../../scripts/file_to_c.py ${infile}
+	)
+
+	set_property(SOURCE regression_8100.c APPEND PROPERTY OBJECT_DEPENDS
+		     ${CMAKE_CURRENT_BINARY_DIR}/regression_8100_${prefix}.h)
+endmacro(EMBED_8100FILE)
+
+EMBED_8100FILE(ca_crt ../../cert/ca.crt)
+EMBED_8100FILE(mid_crt ../../cert/mid.crt)
+EMBED_8100FILE(my_crt ../../cert/my.crt)
+
 set (SRC
 	adbg/src/adbg_case.c
 	adbg/src/adbg_enum.c
diff --git a/host/xtest/Makefile b/host/xtest/Makefile
index d25fcb6..6b666a7 100644
--- a/host/xtest/Makefile
+++ b/host/xtest/Makefile
@@ -75,9 +75,7 @@
 CFLAGS += -I./
 CFLAGS += -I./adbg/include
 CFLAGS += -I./xml/include
-ifeq ($(CFG_GCM_NIST_VECTORS),y)
 CFLAGS += -I$(out-dir)/xtest
-endif
 
 CFLAGS += -I$(OPTEE_CLIENT_EXPORT)/include
 CFLAGS += -I$(TA_DEV_KIT_DIR)/host_include
@@ -206,6 +204,19 @@
 	create-nist-gcm-vectors,$v,encrypt)))
 endif
 
+define embed-file
+cleanfiles += $(out-dir)/xtest/$(1).h
+
+$(out-dir)/xtest/$(1).h: $(2)
+	@echo '  GEN     $$@'
+	$(q)../../scripts/file_to_c.py --inf $$< --out $$@ --name $(1)
+
+$(CURDIR)/regression_8100.c: $(out-dir)/xtest/$(1).h
+endef
+
+$(eval $(call embed-file,regression_8100_ca_crt,../../cert/ca.crt))
+$(eval $(call embed-file,regression_8100_mid_crt,../../cert/mid.crt))
+$(eval $(call embed-file,regression_8100_my_crt,../../cert/my.crt))
 
 .PHONY: clean
 clean:
diff --git a/host/xtest/regression_8100.c b/host/xtest/regression_8100.c
index 001f037..52e6003 100644
--- a/host/xtest/regression_8100.c
+++ b/host/xtest/regression_8100.c
@@ -4,9 +4,19 @@
 #include "xtest_test.h"
 #include "xtest_helpers.h"
 
+#include <compiler.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <ta_crypt.h>
 #include <tee_api_types.h>
-#include <compiler.h>
+
+#include "regression_8100_ca_crt.h"
+#include "regression_8100_mid_crt.h"
+#include "regression_8100_my_crt.h"
+
+#ifdef CFG_TA_MBEDTLS
 
 static void test_8101(ADBG_Case_t *c __maybe_unused)
 {
@@ -27,3 +37,77 @@
 #endif
 }
 ADBG_CASE_DEFINE(regression, 8101, test_8101, "TA mbedTLS self tests");
+
+static int __printf(2, 3) myasprintf(char **strp, const char *fmt, ...)
+{
+	char *str = NULL;
+	int rc = 0;
+	va_list ap;
+
+	va_start(ap, fmt);
+	rc = vsnprintf(str, rc, fmt, ap);
+	if (rc <= 0)
+		goto out;
+
+	str = malloc(rc);
+	if (!str) {
+		rc = -1;
+		goto out;
+	}
+
+	rc = vsnprintf(str, rc, fmt, ap);
+	if (rc <= 0)
+		free(str);
+	else
+		*strp = str;
+
+out:
+	va_end(ap);
+	return rc;
+}
+
+static void test_8102(ADBG_Case_t *c)
+{
+	TEEC_Session session = { 0 };
+	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
+	uint32_t ret_orig;
+	char *chain = NULL;
+	int clen = 0;
+	char *trust = NULL;
+	int tlen;
+
+	if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
+					      &session, &crypt_user_ta_uuid,
+					      NULL, &ret_orig)))
+		return;
+
+	clen = myasprintf(&chain, "%*s\n%*s",
+			  (int)sizeof(regression_8100_my_crt),
+			  regression_8100_my_crt,
+			  (int)sizeof(regression_8100_mid_crt),
+			   regression_8100_mid_crt);
+	if (!ADBG_EXPECT_COMPARE_SIGNED(c, clen, !=, -1))
+		goto out;
+	tlen = myasprintf(&trust, "%*s", (int)sizeof(regression_8100_ca_crt),
+			  regression_8100_ca_crt);
+	if (!ADBG_EXPECT_COMPARE_SIGNED(c, tlen, !=, -1))
+		goto out;
+
+	op.params[0].tmpref.buffer = chain;
+	op.params[0].tmpref.size = clen;
+	op.params[1].tmpref.buffer = trust;
+	op.params[1].tmpref.size = tlen;
+	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
+					 TEEC_MEMREF_TEMP_INPUT,
+					 TEEC_NONE, TEEC_NONE);
+
+	ADBG_EXPECT_TEEC_SUCCESS(c,
+		TEEC_InvokeCommand(&session, TA_CRYPT_CMD_MBEDTLS_CHECK_CERT,
+				   &op, &ret_orig));
+out:
+	free(chain);
+	free(trust);
+	TEEC_CloseSession(&session);
+}
+ADBG_CASE_DEFINE(regression, 8102, test_8102, "TA mbedTLS test cert chain");
+#endif /*CFG_TA_MBEDTLS*/