From f707580e27239c8bc3f9815a652aed77d115dec1 Mon Sep 17 00:00:00 2001 From: Ken Takayama Date: Fri, 20 Mar 2026 03:57:56 +0000 Subject: [PATCH 1/2] refactor: move Makefiles into each directory --- Makefile | 21 ++++- Makefile.client | 56 ------------- Makefile.cose | 73 ---------------- Makefile.cwt | 83 ------------------- Makefile.parser | 71 ---------------- Makefile.sign | 49 ----------- README.md | 14 ++++ client.Dockerfile | 2 +- examples/client/Makefile | 61 ++++++++++++++ examples/{ => client}/teep_http_client.c | 0 examples/{ => client}/teep_http_client_main.c | 0 examples/{ => common}/teep_examples_common.c | 0 .../{inc => common}/teep_examples_common.h | 0 examples/cose/Makefile | 80 ++++++++++++++++++ examples/{ => cose}/teep_cose_test_main.c | 0 examples/cose_parser_main.c | 39 --------- examples/parser/Makefile | 78 +++++++++++++++++ .../{ => parser}/teep_message_parser_main.c | 0 examples/sign/Makefile | 52 ++++++++++++ examples/{ => sign}/teep_sign_test_main.c | 0 ossl.Dockerfile | 12 +-- ossl3.Dockerfile | 12 +-- psa.Dockerfile | 12 +-- 23 files changed, 322 insertions(+), 393 deletions(-) delete mode 100644 Makefile.client delete mode 100644 Makefile.cose delete mode 100644 Makefile.cwt delete mode 100644 Makefile.parser delete mode 100644 Makefile.sign create mode 100644 examples/client/Makefile rename examples/{ => client}/teep_http_client.c (100%) rename examples/{ => client}/teep_http_client_main.c (100%) rename examples/{ => common}/teep_examples_common.c (100%) rename examples/{inc => common}/teep_examples_common.h (100%) create mode 100644 examples/cose/Makefile rename examples/{ => cose}/teep_cose_test_main.c (100%) delete mode 100644 examples/cose_parser_main.c create mode 100644 examples/parser/Makefile rename examples/{ => parser}/teep_message_parser_main.c (100%) create mode 100644 examples/sign/Makefile rename examples/{ => sign}/teep_sign_test_main.c (100%) diff --git a/Makefile b/Makefile index 0284468..f4c1d18 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ ifdef suit INC += -I ../libcsuit/inc -I ../libcsuit/examples/inc endif -.PHONY: all so install uninstall build_test test clean +.PHONY: all so install uninstall build_test test examples example-client example-cose example-parser example-sign example-cwt clean all: $(NAME).a @@ -84,11 +84,26 @@ build_test: test: build_test $(MAKE) -C test MBEDTLS=$(MBEDTLS) run +examples: example-client example-cose example-parser example-sign example-cwt + +example-client: + $(MAKE) -f examples/client/Makefile MBEDTLS=$(MBEDTLS) + +example-cose: + $(MAKE) -f examples/cose/Makefile MBEDTLS=$(MBEDTLS) + +example-parser: + $(MAKE) -f examples/parser/Makefile MBEDTLS=$(MBEDTLS) + +example-sign: + $(MAKE) -f examples/sign/Makefile MBEDTLS=$(MBEDTLS) + +example-cwt: + $(MAKE) -f examples/parser/Makefile.cwt MBEDTLS=$(MBEDTLS) + generate: $(MAKE) -C testfiles clean: $(RM) $(OBJS) $(NAME).a $(NAME).so $(MAKE) -C test clean - - diff --git a/Makefile.client b/Makefile.client deleted file mode 100644 index 4d3b213..0000000 --- a/Makefile.client +++ /dev/null @@ -1,56 +0,0 @@ -# -# Copyright (c) 2020 SECOM CO., LTD. All Rights reserved. -# -# SPDX-License-Identifier: BSD-2-Clause -# - -CFLAGS = -Wall -g -LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lcurl -lm -INC = $(CMD_INC) -I ./inc -I ./examples/inc -TARGET = ./bin/teep_http_client -SRCS := examples/teep_http_client_main.c -SRCS += examples/teep_examples_common.c -SRCS += src/teep_common.c -SRCS += src/teep_cose.c -SRCS += src/teep_message_print.c -SRCS += src/teep_message_decode.c -SRCS += src/teep_message_encode.c -SRCS += examples/teep_http_client.c - -OBJDIR = ./obj -OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(SRCS))) - -ifeq ($(MBEDTLS),1) - # use MbedTLS - CFLAGS += -DLIBTEEP_PSA_CRYPTO_C=1 - LDFLAGS +=-lmbedcrypto -else - # use OpenSSL - MBEDTLS=0 - LDFLAGS += -lcrypto -endif - -ifdef debug - CFLAGS += -DALLOW_CBOR_WITHOUT_SIGN1 -DSEND_CBOR_WITHOUT_SIGN1 -endif - -.PHONY: all debug clean - -all: $(TARGET) - -include Makefile.common - -$(TARGET): $(OBJS) - $(CC) -o $@ $^ $(LDFLAGS) - -$(OBJDIR)/%.o: %.c | $(OBJDIR) $(OBJDIR)/examples $(OBJDIR)/src - $(CC) $(CFLAGS) $(INC) -o $@ -c $< - - -run: $(TARGET) - sh -c "./testfiles/tam_server.sh > tam_stdout.txt &" - sleep 1 - sh -c "$(TARGET) > agent_stdout.txt" - -clean: - $(RM) $(OBJS) $(TARGET) diff --git a/Makefile.cose b/Makefile.cose deleted file mode 100644 index 88dcd9c..0000000 --- a/Makefile.cose +++ /dev/null @@ -1,73 +0,0 @@ -# -# Copyright (c) 2020-2023 SECOM CO., LTD. All Rights reserved. -# -# SPDX-License-Identifier: BSD-2-Clause -# - -CFLAGS = -Wall -g -LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lm -INC = $(CMD_INC) -I ./inc -I ./examples/inc -TAM = ./bin/teep_cose_tam -AGENT = ./bin/teep_cose_agent -SRCS = \ - examples/teep_examples_common.c \ - src/teep_common.c \ - src/teep_message_decode.c \ - src/teep_message_print.c \ - src/teep_cose.c -OBJDIR = ./obj -OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(SRCS))) -TAM_OBJ = obj/examples/teep_cose_tam.o -AGENT_OBJ = obj/examples/teep_cose_agent.o - -ifeq ($(MBEDTLS),1) - # use MbedTLS - CFLAGS += -DLIBTEEP_PSA_CRYPTO_C=1 - LDFLAGS += -lmbedcrypto -else - # use OpenSSL - MBEDTLS=0 - LDFLAGS += -lcrypto -endif - -.PHONY: all -all: $(TAM) $(AGENT) - -include Makefile.common - -$(OBJDIR)/%.o: %.c | $(OBJDIR) $(OBJDIR)/examples $(OBJDIR)/src - $(CC) $(CFLAGS) $(INC) -o $@ -c $< - -$(TAM_OBJ): examples/teep_cose_test_main.c | $(OBJDIR)/examples - $(CC) $(CFLAGS) $(INC) -DTEEP_ACTOR_TAM=1 -o $@ -c $< - -$(AGENT_OBJ): examples/teep_cose_test_main.c | $(OBJDIR)/examples - $(CC) $(CFLAGS) $(INC) -DTEEP_ACTOR_AGENT=1 -o $@ -c $< - -$(TAM): $(OBJS) $(TAM_OBJ) - $(CC) -o $@ $^ $(LDFLAGS) - -$(AGENT): $(OBJS) $(AGENT_OBJ) - $(CC) -o $@ $^ $(LDFLAGS) - -.PHONY: clean -clean: - $(RM) $(OBJS) $(TAM_OBJ) $(AGENT_OBJ) $(TAM) $(AGENT) - - -SOURCES := \ - query_request.cbor \ - query_response.cbor \ - update.cbor \ - teep_success.cbor \ - teep_error.cbor - -$(SOURCES): - $(MAKE) -C testfiles $@ - -run: $(TAM) $(AGENT) $(SOURCES) - $(TAM) ./testfiles/query_request.cbor ./testfiles/query_request_cose.cbor || exit 1 - $(AGENT) ./testfiles/query_response.cbor ./testfiles/query_response_cose.cbor || exit 1 - $(TAM) ./testfiles/update.cbor ./testfiles/update_cose.cbor || exit 1 - $(AGENT) ./testfiles/teep_success.cbor ./testfiles/teep_success_cose.cbor || exit 1 - $(AGENT) ./testfiles/teep_error.cbor ./testfiles/teep_error_cose.cbor || exit 1 diff --git a/Makefile.cwt b/Makefile.cwt deleted file mode 100644 index d02d298..0000000 --- a/Makefile.cwt +++ /dev/null @@ -1,83 +0,0 @@ -# -# Copyright (c) 2020-2023 SECOM CO., LTD. All Rights reserved. -# -# SPDX-License-Identifier: BSD-2-Clause -# - -CFLAGS = -Wall -g -LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lm -INC = $(CMD_INC) -I ./inc -I ./examples/inc -TARGET = ./bin/cose_parser -SRCS = \ - examples/cose_parser_main.c \ - examples/teep_examples_common.c \ - src/teep_common.c \ - src/teep_message_print.c -OBJDIR = ./obj -OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(SRCS))) - -ifeq ($(MBEDTLS),1) - # use MbedTLS - CFLAGS += -DLIBTEEP_PSA_CRYPTO_C=1 - #LDFLAGS += -lmbedtls -lmbedx509 - LDFLAGS += -lmbedcrypto -else - # use OpenSSL - MBEDTLS=0 - LDFLAGS += -lcrypto -endif - -.PHONY: all -all: $(TARGET) - -./obj/examples/: - mkdir -p $(dir $@) - -./obj/src/: - mkdir -p $(dir $@) - -$(OBJDIR)/%.o: %.c ./obj/examples/ ./obj/src/ - $(CC) $(CFLAGS) $(INC) -o $@ -c $< - -$(TARGET): $(OBJS) - $(CC) -o $@ $^ $(LDFLAGS) - -.PHONY: clean -clean: - $(RM) $(OBJS) $(TARGET) - -SOURCES := \ - es_dh_p256_a128gcm_a256kw.cose \ - es_dh_p256_a128gcm_a192kw.cose \ - es_dh_p256_a128gcm_a128kw.cose \ - es_dh_p256_a192gcm_a256kw.cose \ - es_dh_p256_a192gcm_a192kw.cose \ - es_dh_p256_a192gcm_a128kw.cose \ - es_dh_p256_a256gcm_a256kw.cose \ - es_dh_p256_a256gcm_a192kw.cose \ - es_dh_p256_a256gcm_a128kw.cose \ - es_dh_p384_a128gcm_a256kw.cose \ - es_dh_p384_a128gcm_a192kw.cose \ - es_dh_p384_a128gcm_a128kw.cose \ - es_dh_p384_a192gcm_a256kw.cose \ - es_dh_p384_a192gcm_a192kw.cose \ - es_dh_p384_a192gcm_a128kw.cose \ - es_dh_p384_a256gcm_a256kw.cose \ - es_dh_p384_a256gcm_a192kw.cose \ - es_dh_p384_a256gcm_a128kw.cose \ - es_dh_p521_a128gcm_a256kw.cose \ - es_dh_p521_a128gcm_a192kw.cose \ - es_dh_p521_a128gcm_a128kw.cose \ - es_dh_p521_a192gcm_a256kw.cose \ - es_dh_p521_a192gcm_a192kw.cose \ - es_dh_p521_a192gcm_a128kw.cose \ - es_dh_p521_a256gcm_a256kw.cose \ - es_dh_p521_a256gcm_a192kw.cose \ - es_dh_p521_a256gcm_a128kw.cose - -.PHONY: test -test: $(TARGET) $(SOURCES) - @for source in $(SOURCES); do\ - $(TARGET) $(source); \ - done - diff --git a/Makefile.parser b/Makefile.parser deleted file mode 100644 index bee01a7..0000000 --- a/Makefile.parser +++ /dev/null @@ -1,71 +0,0 @@ -# -# Copyright (c) 2020 SECOM CO., LTD. All Rights reserved. -# -# SPDX-License-Identifier: BSD-2-Clause -# - -CFLAGS = -Wall -g -LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lm -INC = $(CMD_INC) -I ./inc -I ./examples/inc -TAM = ./bin/teep_message_parser_tam -AGENT = ./bin/teep_message_parser_agent -SRCS = examples/teep_examples_common.c src/teep_common.c src/teep_message_decode.c src/teep_message_print.c src/teep_cose.c -TAM_OBJ = obj/examples/teep_message_parser_tam.o -AGENT_OBJ = obj/examples/teep_message_parser_agent.o -OBJDIR = ./obj -OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(SRCS))) -COSE_FLAG_TAM = .tam.cose -COSE_FLAG_AGENT = .agent.cose - -ifeq ($(suit),1) - CFLAGS += -DPARSE_SUIT - LDFLAGS := ../libcsuit/bin/libcsuit.a $(LDFLAGS) - INC += -I ../libcsuit/inc -I ../libcsuit/examples/inc -endif - -ifeq ($(debug),1) - CFLAGS += -DALLOW_CBOR_WITHOUT_SIGN1 - COSE_FLAG_TAM = .cbor - COSE_FLAG_AGENT = .cbor -endif - -ifeq ($(MBEDTLS),1) - # use MbedTLS - CFLAGS += -DLIBTEEP_PSA_CRYPTO_C=1 - LDFLAGS += -lmbedcrypto -else - # use OpenSSL - MBEDTLS=0 - LDFLAGS += -lcrypto -endif - -.PHONY: all run clean debug_test - -all: $(TAM) $(AGENT) $(VERIFIER) - -include Makefile.common - -$(OBJDIR)/%.o: %.c | $(OBJDIR) - $(CC) $(CFLAGS) $(INC) -o $@ -c $< - -$(TAM_OBJ): examples/teep_message_parser_main.c - $(CC) $(CFLAGS) $(INC) -DTEEP_ACTOR_TAM=1 -o $@ -c $< - -$(AGENT_OBJ): examples/teep_message_parser_main.c - $(CC) $(CFLAGS) $(INC) -DTEEP_ACTOR_AGENT=1 -o $@ -c $< - -$(TAM): $(OBJS) $(TAM_OBJ) - $(CC) -o $@ $^ $(LDFLAGS) - -$(AGENT): $(OBJS) $(AGENT_OBJ) - $(CC) -o $@ $^ $(LDFLAGS) - -run: $(TAM) $(AGENT) $(VERIFIER) - $(AGENT) ./testfiles/query_request$(COSE_FLAG_TAM) || exit 1 - $(TAM) ./testfiles/query_response$(COSE_FLAG_AGENT) || exit 1 - $(AGENT) ./testfiles/update$(COSE_FLAG_TAM) || exit 1 - $(TAM) ./testfiles/teep_success$(COSE_FLAG_AGENT) || exit 1 - $(TAM) ./testfiles/teep_error$(COSE_FLAG_AGENT) || exit 1 - -clean: - $(RM) $(OBJS) $(TAM_OBJ) $(AGENT_OBJ) $(TAM) $(AGENT) diff --git a/Makefile.sign b/Makefile.sign deleted file mode 100644 index a8ea9cd..0000000 --- a/Makefile.sign +++ /dev/null @@ -1,49 +0,0 @@ -# -# Copyright (c) 2020 SECOM CO., LTD. All Rights reserved. -# -# SPDX-License-Identifier: BSD-2-Clause -# - -CFLAGS = -Wall -g -LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lm -INC = $(CMD_INC) -I ./inc -I ./examples/inc -TARGET = ./bin/teep_sign_test -SRCS = \ - examples/teep_sign_test_main.c \ - examples/teep_examples_common.c \ - src/teep_common.c src/teep_message_encode.c \ - src/teep_message_decode.c \ - src/teep_message_print.c \ - src/teep_cose.c -OBJDIR = ./obj -OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(SRCS))) - -ifeq ($(MBEDTLS),1) - # use MbedTLS - CFLAGS += -DLIBTEEP_PSA_CRYPTO_C=1 - #LDFLAGS += -lmbedtls -lmbedx509 - LDFLAGS += -lmbedcrypto -else - # use OpenSSL - MBEDTLS=0 - LDFLAGS += -lcrypto -endif - -.PHONY: all clean run - -all: $(TARGET) - -$(OBJDIR)/%.o: %.c - mkdir -p $(dir $@) - $(CC) $(CFLAGS) $(INC) -o $@ -c $< - -$(TARGET): $(OBJS) - $(CC) -o $@ $(CFLAGS) $(INC) $^ $(LDFLAGS) - -clean: - $(RM) -f $(OBJS) $(TAM_OBJ) $(AGENT_OBJ) $(TAM) $(AGENT) - - -run: $(TARGET) - $< - diff --git a/README.md b/README.md index c55c804..4d67abe 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,20 @@ ## Getting started +### Example layout +- `examples/common/` contains shared example helper code. +- `examples/client/`, `examples/cose/`, `examples/parser/`, and `examples/sign/` contain example-specific sources. +- `examples/inc/` contains shared example headers and key material. + +Common example build entrypoints from the repository root: +``` +make example-client +make example-cose +make example-parser +make example-sign +make example-cwt +``` + ### Dockerfile policy - `ossl.Dockerfile`, `ossl3.Dockerfile`, and `psa.Dockerfile` are for CI latest checks. They intentionally track latest upstream dependencies and are expected to fail fast when upstream changes break compatibility. diff --git a/client.Dockerfile b/client.Dockerfile index c087974..b293111 100644 --- a/client.Dockerfile +++ b/client.Dockerfile @@ -39,7 +39,7 @@ RUN git clone --depth 1 https://github.com/kentakayama/libteep.git /tmp/libteep cd /tmp/libteep && \ git fetch --depth 1 origin "${LIBTEEP_REF}" && \ git checkout --detach "${LIBTEEP_REF}" && \ - make -f Makefile.client + make -f examples/client/Makefile FROM debian:12-slim diff --git a/examples/client/Makefile b/examples/client/Makefile new file mode 100644 index 0000000..1983377 --- /dev/null +++ b/examples/client/Makefile @@ -0,0 +1,61 @@ +# +# Copyright (c) 2020 SECOM CO., LTD. All Rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause +# + +THIS_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))) +ROOT_DIR := $(abspath $(THIS_DIR)/../..) + +CFLAGS = -Wall -g +LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lcurl -lm +INC = $(CMD_INC) -I $(ROOT_DIR)/inc -I $(ROOT_DIR)/examples/common -I $(ROOT_DIR)/examples/inc +TARGET = $(ROOT_DIR)/bin/teep_http_client +SRCS := $(ROOT_DIR)/examples/client/teep_http_client_main.c +SRCS += $(ROOT_DIR)/examples/common/teep_examples_common.c +SRCS += $(ROOT_DIR)/src/teep_common.c +SRCS += $(ROOT_DIR)/src/teep_cose.c +SRCS += $(ROOT_DIR)/src/teep_message_print.c +SRCS += $(ROOT_DIR)/src/teep_message_decode.c +SRCS += $(ROOT_DIR)/src/teep_message_encode.c +SRCS += $(ROOT_DIR)/examples/client/teep_http_client.c + +OBJDIR = $(THIS_DIR)/obj +REL_SRCS = $(patsubst $(ROOT_DIR)/%,%,$(SRCS)) +OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(REL_SRCS))) + +ifeq ($(MBEDTLS),1) + # use MbedTLS + CFLAGS += -DLIBTEEP_PSA_CRYPTO_C=1 + LDFLAGS +=-lmbedcrypto +else + # use OpenSSL + MBEDTLS=0 + LDFLAGS += -lcrypto +endif + +ifdef debug + CFLAGS += -DALLOW_CBOR_WITHOUT_SIGN1 -DSEND_CBOR_WITHOUT_SIGN1 +endif + +.PHONY: all debug clean + +all: $(TARGET) + +include $(ROOT_DIR)/Makefile.common + +$(TARGET): $(OBJS) + $(CC) -o $@ $^ $(LDFLAGS) + +$(OBJDIR)/%.o: $(ROOT_DIR)/%.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -o $@ -c $< + + +run: $(TARGET) + sh -c "$(ROOT_DIR)/testfiles/tam_server.sh > $(ROOT_DIR)/tam_stdout.txt &" + sleep 1 + sh -c "$(TARGET) > $(ROOT_DIR)/agent_stdout.txt" + +clean: + $(RM) $(OBJS) $(TARGET) diff --git a/examples/teep_http_client.c b/examples/client/teep_http_client.c similarity index 100% rename from examples/teep_http_client.c rename to examples/client/teep_http_client.c diff --git a/examples/teep_http_client_main.c b/examples/client/teep_http_client_main.c similarity index 100% rename from examples/teep_http_client_main.c rename to examples/client/teep_http_client_main.c diff --git a/examples/teep_examples_common.c b/examples/common/teep_examples_common.c similarity index 100% rename from examples/teep_examples_common.c rename to examples/common/teep_examples_common.c diff --git a/examples/inc/teep_examples_common.h b/examples/common/teep_examples_common.h similarity index 100% rename from examples/inc/teep_examples_common.h rename to examples/common/teep_examples_common.h diff --git a/examples/cose/Makefile b/examples/cose/Makefile new file mode 100644 index 0000000..5715276 --- /dev/null +++ b/examples/cose/Makefile @@ -0,0 +1,80 @@ +# +# Copyright (c) 2020-2023 SECOM CO., LTD. All Rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause +# + +THIS_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))) +ROOT_DIR := $(abspath $(THIS_DIR)/../..) + +CFLAGS = -Wall -g +LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lm +INC = $(CMD_INC) -I $(ROOT_DIR)/inc -I $(ROOT_DIR)/examples/common -I $(ROOT_DIR)/examples/inc +TAM = $(ROOT_DIR)/bin/teep_cose_tam +AGENT = $(ROOT_DIR)/bin/teep_cose_agent +SRCS = \ + $(ROOT_DIR)/examples/common/teep_examples_common.c \ + $(ROOT_DIR)/src/teep_common.c \ + $(ROOT_DIR)/src/teep_message_decode.c \ + $(ROOT_DIR)/src/teep_message_print.c \ + $(ROOT_DIR)/src/teep_cose.c +OBJDIR = $(THIS_DIR)/obj +REL_SRCS = $(patsubst $(ROOT_DIR)/%,%,$(SRCS)) +OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(REL_SRCS))) +TAM_OBJ = $(OBJDIR)/teep_cose_tam.o +AGENT_OBJ = $(OBJDIR)/teep_cose_agent.o + +ifeq ($(MBEDTLS),1) + # use MbedTLS + CFLAGS += -DLIBTEEP_PSA_CRYPTO_C=1 + LDFLAGS += -lmbedcrypto +else + # use OpenSSL + MBEDTLS=0 + LDFLAGS += -lcrypto +endif + +.PHONY: all +all: $(TAM) $(AGENT) + +include $(ROOT_DIR)/Makefile.common + +$(OBJDIR)/%.o: $(ROOT_DIR)/%.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -o $@ -c $< + +$(TAM_OBJ): $(ROOT_DIR)/examples/cose/teep_cose_test_main.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -DTEEP_ACTOR_TAM=1 -o $@ -c $< + +$(AGENT_OBJ): $(ROOT_DIR)/examples/cose/teep_cose_test_main.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -DTEEP_ACTOR_AGENT=1 -o $@ -c $< + +$(TAM): $(OBJS) $(TAM_OBJ) + $(CC) -o $@ $^ $(LDFLAGS) + +$(AGENT): $(OBJS) $(AGENT_OBJ) + $(CC) -o $@ $^ $(LDFLAGS) + +.PHONY: clean +clean: + $(RM) $(OBJS) $(TAM_OBJ) $(AGENT_OBJ) $(TAM) $(AGENT) + + +SOURCES := \ + query_request.cbor \ + query_response.cbor \ + update.cbor \ + teep_success.cbor \ + teep_error.cbor + +$(SOURCES): + $(MAKE) -C $(ROOT_DIR)/testfiles $@ + +run: $(TAM) $(AGENT) $(SOURCES) + $(TAM) $(ROOT_DIR)/testfiles/query_request.cbor $(ROOT_DIR)/testfiles/query_request_cose.cbor || exit 1 + $(AGENT) $(ROOT_DIR)/testfiles/query_response.cbor $(ROOT_DIR)/testfiles/query_response_cose.cbor || exit 1 + $(TAM) $(ROOT_DIR)/testfiles/update.cbor $(ROOT_DIR)/testfiles/update_cose.cbor || exit 1 + $(AGENT) $(ROOT_DIR)/testfiles/teep_success.cbor $(ROOT_DIR)/testfiles/teep_success_cose.cbor || exit 1 + $(AGENT) $(ROOT_DIR)/testfiles/teep_error.cbor $(ROOT_DIR)/testfiles/teep_error_cose.cbor || exit 1 diff --git a/examples/teep_cose_test_main.c b/examples/cose/teep_cose_test_main.c similarity index 100% rename from examples/teep_cose_test_main.c rename to examples/cose/teep_cose_test_main.c diff --git a/examples/cose_parser_main.c b/examples/cose_parser_main.c deleted file mode 100644 index 7720740..0000000 --- a/examples/cose_parser_main.c +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2020-2023 SECOM CO., LTD. All Rights reserved. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include "teep/teep_message_print.h" -#include "teep_examples_common.h" - -int main(int argc, const char *argv[]) -{ - const char *cose_file_name = NULL; - - if (argc < 2) { - printf("%s \n", argv[0]); - return EXIT_FAILURE; - } - cose_file_name = argv[1]; - - // Read cose file. - UsefulBuf_MAKE_STACK_UB(cose_buf, 1024); - - printf("main : Read CBOR file.\n"); - cose_buf.len = read_from_file(cose_file_name, cose_buf.ptr, 1024); - if (cose_buf.len == 0) { - printf("main : Failed to read CBOR file.\n"); - return EXIT_FAILURE; - } - teep_print_hex_within_max(cose_buf.ptr, cose_buf.len, 1024); - printf("\n"); - - // Print cose file. - printf("main : Print COSE file.\n"); - teep_print_cose_eat(UsefulBuf_Const(cose_buf), 4, 2); - - return EXIT_SUCCESS; -} diff --git a/examples/parser/Makefile b/examples/parser/Makefile new file mode 100644 index 0000000..5658107 --- /dev/null +++ b/examples/parser/Makefile @@ -0,0 +1,78 @@ +# +# Copyright (c) 2020 SECOM CO., LTD. All Rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause +# + +THIS_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))) +ROOT_DIR := $(abspath $(THIS_DIR)/../..) + +CFLAGS = -Wall -g +LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lm +INC = $(CMD_INC) -I $(ROOT_DIR)/inc -I $(ROOT_DIR)/examples/common -I $(ROOT_DIR)/examples/inc +TAM = $(ROOT_DIR)/bin/teep_message_parser_tam +AGENT = $(ROOT_DIR)/bin/teep_message_parser_agent +SRCS = $(ROOT_DIR)/examples/common/teep_examples_common.c $(ROOT_DIR)/src/teep_common.c $(ROOT_DIR)/src/teep_message_decode.c $(ROOT_DIR)/src/teep_message_print.c $(ROOT_DIR)/src/teep_cose.c +TAM_OBJ = $(THIS_DIR)/obj/teep_message_parser_tam.o +AGENT_OBJ = $(THIS_DIR)/obj/teep_message_parser_agent.o +OBJDIR = $(THIS_DIR)/obj +REL_SRCS = $(patsubst $(ROOT_DIR)/%,%,$(SRCS)) +OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(REL_SRCS))) +COSE_FLAG_TAM = .tam.cose +COSE_FLAG_AGENT = .agent.cose + +ifeq ($(suit),1) + CFLAGS += -DPARSE_SUIT + LDFLAGS := $(ROOT_DIR)/../libcsuit/bin/libcsuit.a $(LDFLAGS) + INC += -I $(ROOT_DIR)/../libcsuit/inc -I $(ROOT_DIR)/../libcsuit/examples/inc +endif + +ifeq ($(debug),1) + CFLAGS += -DALLOW_CBOR_WITHOUT_SIGN1 + COSE_FLAG_TAM = .cbor + COSE_FLAG_AGENT = .cbor +endif + +ifeq ($(MBEDTLS),1) + # use MbedTLS + CFLAGS += -DLIBTEEP_PSA_CRYPTO_C=1 + LDFLAGS += -lmbedcrypto +else + # use OpenSSL + MBEDTLS=0 + LDFLAGS += -lcrypto +endif + +.PHONY: all run clean debug_test + +all: $(TAM) $(AGENT) $(VERIFIER) + +include $(ROOT_DIR)/Makefile.common + +$(OBJDIR)/%.o: $(ROOT_DIR)/%.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -o $@ -c $< + +$(TAM_OBJ): $(ROOT_DIR)/examples/parser/teep_message_parser_main.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -DTEEP_ACTOR_TAM=1 -o $@ -c $< + +$(AGENT_OBJ): $(ROOT_DIR)/examples/parser/teep_message_parser_main.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -DTEEP_ACTOR_AGENT=1 -o $@ -c $< + +$(TAM): $(OBJS) $(TAM_OBJ) + $(CC) -o $@ $^ $(LDFLAGS) + +$(AGENT): $(OBJS) $(AGENT_OBJ) + $(CC) -o $@ $^ $(LDFLAGS) + +run: $(TAM) $(AGENT) $(VERIFIER) + $(AGENT) $(ROOT_DIR)/testfiles/query_request$(COSE_FLAG_TAM) || exit 1 + $(TAM) $(ROOT_DIR)/testfiles/query_response$(COSE_FLAG_AGENT) || exit 1 + $(AGENT) $(ROOT_DIR)/testfiles/update$(COSE_FLAG_TAM) || exit 1 + $(TAM) $(ROOT_DIR)/testfiles/teep_success$(COSE_FLAG_AGENT) || exit 1 + $(TAM) $(ROOT_DIR)/testfiles/teep_error$(COSE_FLAG_AGENT) || exit 1 + +clean: + $(RM) $(OBJS) $(TAM_OBJ) $(AGENT_OBJ) $(TAM) $(AGENT) diff --git a/examples/teep_message_parser_main.c b/examples/parser/teep_message_parser_main.c similarity index 100% rename from examples/teep_message_parser_main.c rename to examples/parser/teep_message_parser_main.c diff --git a/examples/sign/Makefile b/examples/sign/Makefile new file mode 100644 index 0000000..1cf5863 --- /dev/null +++ b/examples/sign/Makefile @@ -0,0 +1,52 @@ +# +# Copyright (c) 2020 SECOM CO., LTD. All Rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause +# + +THIS_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))) +ROOT_DIR := $(abspath $(THIS_DIR)/../..) + +CFLAGS = -Wall -g +LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lm +INC = $(CMD_INC) -I $(ROOT_DIR)/inc -I $(ROOT_DIR)/examples/common -I $(ROOT_DIR)/examples/inc +TARGET = $(ROOT_DIR)/bin/teep_sign_test +SRCS = \ + $(ROOT_DIR)/examples/sign/teep_sign_test_main.c \ + $(ROOT_DIR)/examples/common/teep_examples_common.c \ + $(ROOT_DIR)/src/teep_common.c $(ROOT_DIR)/src/teep_message_encode.c \ + $(ROOT_DIR)/src/teep_message_decode.c \ + $(ROOT_DIR)/src/teep_message_print.c \ + $(ROOT_DIR)/src/teep_cose.c +OBJDIR = $(THIS_DIR)/obj +REL_SRCS = $(patsubst $(ROOT_DIR)/%,%,$(SRCS)) +OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(REL_SRCS))) + +ifeq ($(MBEDTLS),1) + # use MbedTLS + CFLAGS += -DLIBTEEP_PSA_CRYPTO_C=1 + #LDFLAGS += -lmbedtls -lmbedx509 + LDFLAGS += -lmbedcrypto +else + # use OpenSSL + MBEDTLS=0 + LDFLAGS += -lcrypto +endif + +.PHONY: all clean run + +all: $(TARGET) + +$(OBJDIR)/%.o: $(ROOT_DIR)/%.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -o $@ -c $< + +$(TARGET): $(OBJS) + $(CC) -o $@ $(CFLAGS) $(INC) $^ $(LDFLAGS) + +clean: + $(RM) -f $(OBJS) $(TAM_OBJ) $(AGENT_OBJ) $(TAM) $(AGENT) + + +run: $(TARGET) + $< diff --git a/examples/teep_sign_test_main.c b/examples/sign/teep_sign_test_main.c similarity index 100% rename from examples/teep_sign_test_main.c rename to examples/sign/teep_sign_test_main.c diff --git a/ossl.Dockerfile b/ossl.Dockerfile index 97f89b7..6fdae15 100644 --- a/ossl.Dockerfile +++ b/ossl.Dockerfile @@ -33,11 +33,11 @@ WORKDIR /root COPY . ./libteep WORKDIR /root/libteep RUN make -RUN make -f Makefile.cose -RUN make -f Makefile.parser -RUN make -f Makefile.sign +RUN make -f examples/cose/Makefile +RUN make -f examples/parser/Makefile +RUN make -f examples/sign/Makefile CMD make test && \ - make -f Makefile.cose run && \ - make -f Makefile.parser run && \ - make -f Makefile.sign run + make -f examples/cose/Makefile run && \ + make -f examples/parser/Makefile run && \ + make -f examples/sign/Makefile run diff --git a/ossl3.Dockerfile b/ossl3.Dockerfile index 4258f9a..c8a2f0d 100644 --- a/ossl3.Dockerfile +++ b/ossl3.Dockerfile @@ -33,11 +33,11 @@ WORKDIR /root COPY . ./libteep WORKDIR /root/libteep RUN make -RUN make -f Makefile.cose -RUN make -f Makefile.parser -RUN make -f Makefile.sign +RUN make -f examples/cose/Makefile +RUN make -f examples/parser/Makefile +RUN make -f examples/sign/Makefile CMD make test && \ - make -f Makefile.cose run && \ - make -f Makefile.parser run && \ - make -f Makefile.sign run + make -f examples/cose/Makefile run && \ + make -f examples/parser/Makefile run && \ + make -f examples/sign/Makefile run diff --git a/psa.Dockerfile b/psa.Dockerfile index 2196fa6..17408f3 100644 --- a/psa.Dockerfile +++ b/psa.Dockerfile @@ -36,11 +36,11 @@ RUN make -f Makefile.psa libt_cose.a install COPY . /root/libteep WORKDIR /root/libteep RUN make MBEDTLS=1 -RUN make -f Makefile.cose MBEDTLS=1 -RUN make -f Makefile.parser MBEDTLS=1 -RUN make -f Makefile.sign MBEDTLS=1 +RUN make -f examples/cose/Makefile MBEDTLS=1 +RUN make -f examples/parser/Makefile MBEDTLS=1 +RUN make -f examples/sign/Makefile MBEDTLS=1 CMD make MBEDTLS=1 test && \ - make -f Makefile.cose MBEDTLS=1 run && \ - make -f Makefile.parser MBEDTLS=1 run && \ - make -f Makefile.sign MBEDTLS=1 run + make -f examples/cose/Makefile MBEDTLS=1 run && \ + make -f examples/parser/Makefile MBEDTLS=1 run && \ + make -f examples/sign/Makefile MBEDTLS=1 run From f4cfbc43656166e129ad7ed24e38c04de6ce3c60 Mon Sep 17 00:00:00 2001 From: Ken Takayama Date: Fri, 20 Mar 2026 05:47:00 +0000 Subject: [PATCH 2/2] refactor: move EAT parser to examples/eat_tool --- Makefile | 13 +- README.md | 6 +- bin/.gitignore | 7 - client.Dockerfile | 2 +- examples/cose/Makefile | 1 + examples/eat_tool/Makefile | 37 + examples/eat_tool/eat_parser.c | 36 + examples/inc/teep_http_client.h | 12 +- examples/parser/Makefile | 2 +- examples/parser/teep_message_parser_main.c | 5 - examples/sign/Makefile | 2 +- .../Makefile | 5 +- .../teep_http_client.c | 24 +- .../teep_http_client_main.c | 61 +- inc/teep/teep_message_print.h | 29 - src/cose_print_common.c | 753 +++++++++++++++++ src/cose_print_common.h | 18 + src/teep_message_print.c | 771 +----------------- testfiles/tam_server.sh | 17 +- 19 files changed, 932 insertions(+), 869 deletions(-) delete mode 100644 bin/.gitignore create mode 100644 examples/eat_tool/Makefile create mode 100644 examples/eat_tool/eat_parser.c rename examples/{client => teep_over_http_client}/Makefile (88%) rename examples/{client => teep_over_http_client}/teep_http_client.c (77%) rename examples/{client => teep_over_http_client}/teep_http_client_main.c (87%) create mode 100644 src/cose_print_common.c create mode 100644 src/cose_print_common.h diff --git a/Makefile b/Makefile index f4c1d18..d44ae5c 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ NAME = libteep CFLAGS = -Wall -g -fPIC INC = $(CMD_INC) -I ./inc SRCS = \ + src/cose_print_common.c \ src/teep_common.c \ src/teep_cose.c \ src/teep_message_decode.c \ @@ -35,7 +36,7 @@ ifdef suit INC += -I ../libcsuit/inc -I ../libcsuit/examples/inc endif -.PHONY: all so install uninstall build_test test examples example-client example-cose example-parser example-sign example-cwt clean +.PHONY: all so install uninstall build_test test examples example-teep-over-http-client example-cose example-parser example-sign example-eat-tool clean all: $(NAME).a @@ -84,10 +85,10 @@ build_test: test: build_test $(MAKE) -C test MBEDTLS=$(MBEDTLS) run -examples: example-client example-cose example-parser example-sign example-cwt +examples: example-teep-over-http-client example-cose example-parser example-sign example-eat-tool -example-client: - $(MAKE) -f examples/client/Makefile MBEDTLS=$(MBEDTLS) +example-teep-over-http-client: + $(MAKE) -f examples/teep_over_http_client/Makefile MBEDTLS=$(MBEDTLS) example-cose: $(MAKE) -f examples/cose/Makefile MBEDTLS=$(MBEDTLS) @@ -98,8 +99,8 @@ example-parser: example-sign: $(MAKE) -f examples/sign/Makefile MBEDTLS=$(MBEDTLS) -example-cwt: - $(MAKE) -f examples/parser/Makefile.cwt MBEDTLS=$(MBEDTLS) +example-eat-tool: + $(MAKE) -f examples/eat_tool/Makefile generate: $(MAKE) -C testfiles diff --git a/README.md b/README.md index 4d67abe..3331964 100644 --- a/README.md +++ b/README.md @@ -14,16 +14,16 @@ ### Example layout - `examples/common/` contains shared example helper code. -- `examples/client/`, `examples/cose/`, `examples/parser/`, and `examples/sign/` contain example-specific sources. +- `examples/teep_over_http_client/`, `examples/cose/`, `examples/parser/`, `examples/sign/`, and `examples/eat_tool/` contain example-specific sources. - `examples/inc/` contains shared example headers and key material. Common example build entrypoints from the repository root: ``` -make example-client +make example-teep-over-http-client make example-cose make example-parser make example-sign -make example-cwt +make example-eat-tool ``` ### Dockerfile policy diff --git a/bin/.gitignore b/bin/.gitignore deleted file mode 100644 index e15d9b7..0000000 --- a/bin/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -# Output files -teep_cose_tam -teep_cose_agent -teep_message_parser_tam -teep_message_parser_agent -teep_http_client -teep_sign_test diff --git a/client.Dockerfile b/client.Dockerfile index b293111..ffaecbe 100644 --- a/client.Dockerfile +++ b/client.Dockerfile @@ -39,7 +39,7 @@ RUN git clone --depth 1 https://github.com/kentakayama/libteep.git /tmp/libteep cd /tmp/libteep && \ git fetch --depth 1 origin "${LIBTEEP_REF}" && \ git checkout --detach "${LIBTEEP_REF}" && \ - make -f examples/client/Makefile + make -f examples/teep_over_http_client/Makefile FROM debian:12-slim diff --git a/examples/cose/Makefile b/examples/cose/Makefile index 5715276..4b9d6fc 100644 --- a/examples/cose/Makefile +++ b/examples/cose/Makefile @@ -15,6 +15,7 @@ AGENT = $(ROOT_DIR)/bin/teep_cose_agent SRCS = \ $(ROOT_DIR)/examples/common/teep_examples_common.c \ $(ROOT_DIR)/src/teep_common.c \ + $(ROOT_DIR)/src/cose_print_common.c \ $(ROOT_DIR)/src/teep_message_decode.c \ $(ROOT_DIR)/src/teep_message_print.c \ $(ROOT_DIR)/src/teep_cose.c diff --git a/examples/eat_tool/Makefile b/examples/eat_tool/Makefile new file mode 100644 index 0000000..bef18cb --- /dev/null +++ b/examples/eat_tool/Makefile @@ -0,0 +1,37 @@ +# +# Copyright (c) 2020 SECOM CO., LTD. All Rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause +# + +THIS_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))) +ROOT_DIR := $(abspath $(THIS_DIR)/../..) + +CFLAGS = -Wall -g +LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lm +INC = $(CMD_INC) -I $(ROOT_DIR)/inc -I $(ROOT_DIR)/src -I $(ROOT_DIR)/examples/common +TARGET = $(ROOT_DIR)/bin/eat_parser +SRCS = \ + $(ROOT_DIR)/examples/eat_tool/eat_parser.c \ + $(ROOT_DIR)/examples/common/teep_examples_common.c \ + $(ROOT_DIR)/src/cose_print_common.c +OBJDIR = $(THIS_DIR)/obj +REL_SRCS = $(patsubst $(ROOT_DIR)/%,%,$(SRCS)) +OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(REL_SRCS))) + +.PHONY: all clean run + +all: $(TARGET) + +$(OBJDIR)/%.o: $(ROOT_DIR)/%.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -o $@ -c $< + +$(TARGET): $(OBJS) + $(CC) -o $@ $^ $(LDFLAGS) + +clean: + $(RM) $(OBJS) $(TARGET) + +run: $(TARGET) + $< diff --git a/examples/eat_tool/eat_parser.c b/examples/eat_tool/eat_parser.c new file mode 100644 index 0000000..4233a70 --- /dev/null +++ b/examples/eat_tool/eat_parser.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2020-2023 SECOM CO., LTD. All Rights reserved. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include "teep/teep_common.h" +#include "teep_examples_common.h" +#include "cose_print_common.h" + +int main(int argc, const char *argv[]) +{ + const char *cose_file_name = NULL; + + if (argc < 2) { + printf("%s \n", argv[0]); + return EXIT_FAILURE; + } + cose_file_name = argv[1]; + + UsefulBuf_MAKE_STACK_UB(cose_buf, 1024); + + printf("main : Read CBOR file.\n"); + cose_buf.len = read_from_file(cose_file_name, cose_buf.ptr, 1024); + if (cose_buf.len == 0) { + printf("main : Failed to read CBOR file.\n"); + return EXIT_FAILURE; + } + teep_print_hex_within_max(cose_buf.ptr, cose_buf.len, 1024); + printf("\n"); + + printf("main : Print EAT-wrapped COSE file.\n"); + return teep_print_cose_usefulbufc(UsefulBuf_Const(cose_buf), 4, 2, teep_eat_claim_label_to_str); +} diff --git a/examples/inc/teep_http_client.h b/examples/inc/teep_http_client.h index ad67672..6239054 100644 --- a/examples/inc/teep_http_client.h +++ b/examples/inc/teep_http_client.h @@ -8,7 +8,17 @@ #define TEEP_HTTP_CLIENT_H #include +#include "teep/teep_common.h" -teep_err_t teep_send_http_post(const char *url, UsefulBufC send_buffer, UsefulBuf *recv_buffer); +typedef enum teep_http_post_result { + TEEP_HTTP_POST_OK = 0, + TEEP_HTTP_POST_NO_CONTENT, + TEEP_HTTP_POST_TRANSPORT_ERROR, + TEEP_HTTP_POST_RESPONSE_ERROR, +} teep_http_post_result_t; + +teep_http_post_result_t teep_send_http_post(const char *url, + UsefulBufC send_buffer, + UsefulBuf *recv_buffer); #endif // TEEP_HTTP_CLIENT_H diff --git a/examples/parser/Makefile b/examples/parser/Makefile index 5658107..badeb9c 100644 --- a/examples/parser/Makefile +++ b/examples/parser/Makefile @@ -12,7 +12,7 @@ LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lm INC = $(CMD_INC) -I $(ROOT_DIR)/inc -I $(ROOT_DIR)/examples/common -I $(ROOT_DIR)/examples/inc TAM = $(ROOT_DIR)/bin/teep_message_parser_tam AGENT = $(ROOT_DIR)/bin/teep_message_parser_agent -SRCS = $(ROOT_DIR)/examples/common/teep_examples_common.c $(ROOT_DIR)/src/teep_common.c $(ROOT_DIR)/src/teep_message_decode.c $(ROOT_DIR)/src/teep_message_print.c $(ROOT_DIR)/src/teep_cose.c +SRCS = $(ROOT_DIR)/examples/common/teep_examples_common.c $(ROOT_DIR)/src/teep_common.c $(ROOT_DIR)/src/cose_print_common.c $(ROOT_DIR)/src/teep_message_decode.c $(ROOT_DIR)/src/teep_message_print.c $(ROOT_DIR)/src/teep_cose.c TAM_OBJ = $(THIS_DIR)/obj/teep_message_parser_tam.o AGENT_OBJ = $(THIS_DIR)/obj/teep_message_parser_agent.o OBJDIR = $(THIS_DIR)/obj diff --git a/examples/parser/teep_message_parser_main.c b/examples/parser/teep_message_parser_main.c index 28f4a5b..ff0c5ef 100644 --- a/examples/parser/teep_message_parser_main.c +++ b/examples/parser/teep_message_parser_main.c @@ -103,7 +103,6 @@ int main(int argc, const char * argv[]) teep_print_hex_within_max(returned_payload.ptr, returned_payload.len, 1024); printf("\n"); -#if TEEP_ACTOR_VERIFIER != 1 printf("\nmain : TEEP message with COSE wrapper.\n"); teep_print_cose_teep_message(signed_cose, 0, 2); @@ -121,10 +120,6 @@ int main(int argc, const char * argv[]) printf("main : Failed to print CBOR as teep-message. %s(%d)\n", teep_err_to_str(result), result); return EXIT_FAILURE; } -#else - printf("\nmain : EAT data.\n"); - teep_print_cose_eat(signed_cose, 0, 2); -#endif teep_free_key(&mechanism.key); free(cbor_buf.ptr); diff --git a/examples/sign/Makefile b/examples/sign/Makefile index 1cf5863..b16175e 100644 --- a/examples/sign/Makefile +++ b/examples/sign/Makefile @@ -14,7 +14,7 @@ TARGET = $(ROOT_DIR)/bin/teep_sign_test SRCS = \ $(ROOT_DIR)/examples/sign/teep_sign_test_main.c \ $(ROOT_DIR)/examples/common/teep_examples_common.c \ - $(ROOT_DIR)/src/teep_common.c $(ROOT_DIR)/src/teep_message_encode.c \ + $(ROOT_DIR)/src/teep_common.c $(ROOT_DIR)/src/cose_print_common.c $(ROOT_DIR)/src/teep_message_encode.c \ $(ROOT_DIR)/src/teep_message_decode.c \ $(ROOT_DIR)/src/teep_message_print.c \ $(ROOT_DIR)/src/teep_cose.c diff --git a/examples/client/Makefile b/examples/teep_over_http_client/Makefile similarity index 88% rename from examples/client/Makefile rename to examples/teep_over_http_client/Makefile index 1983377..4c2e192 100644 --- a/examples/client/Makefile +++ b/examples/teep_over_http_client/Makefile @@ -11,14 +11,15 @@ CFLAGS = -Wall -g LDFLAGS = $(CMD_LD) -lt_cose -lqcbor -lcurl -lm INC = $(CMD_INC) -I $(ROOT_DIR)/inc -I $(ROOT_DIR)/examples/common -I $(ROOT_DIR)/examples/inc TARGET = $(ROOT_DIR)/bin/teep_http_client -SRCS := $(ROOT_DIR)/examples/client/teep_http_client_main.c +SRCS := $(ROOT_DIR)/examples/teep_over_http_client/teep_http_client_main.c SRCS += $(ROOT_DIR)/examples/common/teep_examples_common.c SRCS += $(ROOT_DIR)/src/teep_common.c +SRCS += $(ROOT_DIR)/src/cose_print_common.c SRCS += $(ROOT_DIR)/src/teep_cose.c SRCS += $(ROOT_DIR)/src/teep_message_print.c SRCS += $(ROOT_DIR)/src/teep_message_decode.c SRCS += $(ROOT_DIR)/src/teep_message_encode.c -SRCS += $(ROOT_DIR)/examples/client/teep_http_client.c +SRCS += $(ROOT_DIR)/examples/teep_over_http_client/teep_http_client.c OBJDIR = $(THIS_DIR)/obj REL_SRCS = $(patsubst $(ROOT_DIR)/%,%,$(SRCS)) diff --git a/examples/client/teep_http_client.c b/examples/teep_over_http_client/teep_http_client.c similarity index 77% rename from examples/client/teep_http_client.c rename to examples/teep_over_http_client/teep_http_client.c index 896dc00..700e1b2 100644 --- a/examples/client/teep_http_client.c +++ b/examples/teep_over_http_client/teep_http_client.c @@ -24,11 +24,11 @@ static size_t write_callback(void *recv_buffer_ptr, return recv_size; } -teep_err_t teep_send_http_post(const char *url, - UsefulBufC send_buffer, - UsefulBuf *recv_buffer) +teep_http_post_result_t teep_send_http_post(const char *url, + UsefulBufC send_buffer, + UsefulBuf *recv_buffer) { - teep_err_t result = TEEP_SUCCESS; + teep_http_post_result_t result = TEEP_HTTP_POST_OK; CURL *curl = NULL; CURLcode curl_result; struct curl_slist *curl_slist = NULL; @@ -38,7 +38,7 @@ teep_err_t teep_send_http_post(const char *url, curl = curl_easy_init(); if (curl == NULL) { printf("teep_send_post_request : curl_easy_init : Fail.\n"); - return TEEP_ERR_UNEXPECTED_ERROR; + return TEEP_HTTP_POST_TRANSPORT_ERROR; } curl_easy_setopt(curl, CURLOPT_URL, url); curl_slist = curl_slist_append(curl_slist, "Accept: application/teep+cbor"); @@ -62,15 +62,23 @@ teep_err_t teep_send_http_post(const char *url, curl_result = curl_easy_perform(curl); if (curl_result != CURLE_OK) { printf("teep_send_post_request : curl_easy_perform : Fail.\n"); - result = TEEP_ERR_ON_HTTP_POST; + result = TEEP_HTTP_POST_TRANSPORT_ERROR; goto out; } // Get status code. int64_t response_code = -1; curl_result = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); - if (curl_result != CURLE_OK || response_code < 0 || response_code != 200) { - result = TEEP_ERR_ABORT; + if (curl_result != CURLE_OK || response_code < 0) { + result = TEEP_HTTP_POST_RESPONSE_ERROR; + goto out; + } + if (response_code == 204) { + result = TEEP_HTTP_POST_NO_CONTENT; + goto out; + } + if (response_code != 200) { + result = TEEP_HTTP_POST_RESPONSE_ERROR; goto out; } diff --git a/examples/client/teep_http_client_main.c b/examples/teep_over_http_client/teep_http_client_main.c similarity index 87% rename from examples/client/teep_http_client_main.c rename to examples/teep_over_http_client/teep_http_client_main.c index 22b4153..52f2b24 100644 --- a/examples/client/teep_http_client_main.c +++ b/examples/teep_over_http_client/teep_http_client_main.c @@ -234,23 +234,35 @@ teep_err_t create_query_response_or_error(const teep_query_request_t *query_requ \param[out] recv_buf CBOR buffer to be used for received message. \param[out] message Pointer of returned struct. - \return This returns one of error codes defined by \ref teep_err_t; + \return This returns an example-local status and writes protocol errors to `teep_result`. */ -teep_err_t get_teep_message(const char *tam_url, - UsefulBufC send_buf, - teep_mechanism_t *verifying_key, - UsefulBuf recv_buf, - teep_message_t *message) +typedef enum teep_client_result { + TEEP_CLIENT_RESULT_OK = 0, + TEEP_CLIENT_RESULT_TAM_TERMINATED, + TEEP_CLIENT_RESULT_TRANSPORT_ERROR, + TEEP_CLIENT_RESULT_PROTOCOL_ERROR, +} teep_client_result_t; + +teep_client_result_t get_teep_message(const char *tam_url, + UsefulBufC send_buf, + teep_mechanism_t *verifying_key, + UsefulBuf recv_buf, + teep_message_t *message, + teep_err_t *teep_result) { teep_err_t result; + teep_http_post_result_t http_result; // Send TEEP/HTTP POST request. printf("main : Send TEEP/HTTP POST request.\n"); teep_print_hex(send_buf.ptr, send_buf.len); printf("\n"); - result = teep_send_http_post(tam_url, send_buf, &recv_buf); - if (result != TEEP_SUCCESS) { - return result; + http_result = teep_send_http_post(tam_url, send_buf, &recv_buf); + if (http_result == TEEP_HTTP_POST_NO_CONTENT) { + return TEEP_CLIENT_RESULT_TAM_TERMINATED; + } + if (http_result != TEEP_HTTP_POST_OK) { + return TEEP_CLIENT_RESULT_TRANSPORT_ERROR; } // Verify and print QueryRequest cose. @@ -262,16 +274,22 @@ teep_err_t get_teep_message(const char *tam_url, result = teep_verify_cose_sign(UsefulBuf_Const(recv_buf), verifying_key, 1, &payload); } if (result != TEEP_SUCCESS) { - printf("main : Failed to verify TEEP message. %s(%d)\n", teep_err_to_str(result), result); - return result; + *teep_result = result; + return TEEP_CLIENT_RESULT_PROTOCOL_ERROR; } - return teep_set_message_from_bytes(payload.ptr, payload.len, message); + result = teep_set_message_from_bytes(payload.ptr, payload.len, message); + if (result != TEEP_SUCCESS) { + *teep_result = result; + return TEEP_CLIENT_RESULT_PROTOCOL_ERROR; + } + return TEEP_CLIENT_RESULT_OK; } int main(int argc, const char * argv[]) { teep_err_t result; + teep_client_result_t client_result; typedef enum teep_agent_status { WAITING_QUERY_REQUEST, WAITING_UPDATE_OR_QUERY_REQUEST, @@ -312,20 +330,23 @@ int main(int argc, const char * argv[]) cose_send_buf.len = 0; while (1) { - result = get_teep_message(tam_url, UsefulBuf_Const(cose_send_buf), &mechanism_verify, cbor_recv_buf, &recv_message); - if (result != TEEP_SUCCESS) { - if (result == TEEP_ERR_ABORT) { - /* just the TAM terminated the connection */ - result = TEEP_SUCCESS; + client_result = get_teep_message(tam_url, UsefulBuf_Const(cose_send_buf), &mechanism_verify, cbor_recv_buf, &recv_message, &result); + if (client_result != TEEP_CLIENT_RESULT_OK) { + if (client_result == TEEP_CLIENT_RESULT_TAM_TERMINATED) { printf("main : The TAM terminated the connection.\n"); break; } - else if (result == TEEP_ERR_VERIFICATION_FAILED) { - /* could not authenticate the TAM's message, ignore */ + if (client_result == TEEP_CLIENT_RESULT_PROTOCOL_ERROR && + result == TEEP_ERR_VERIFICATION_FAILED) { printf("main : Could not authenticate the TAM's message.\n"); goto interval; } - printf("main : Failed to parse received message. %s(%d)\n", teep_err_to_str(result), result); + if (client_result == TEEP_CLIENT_RESULT_PROTOCOL_ERROR) { + printf("main : Failed to parse received message. %s(%d)\n", teep_err_to_str(result), result); + } + else { + printf("main : Failed to exchange a TEEP/HTTP message.\n"); + } return EXIT_FAILURE; } teep_print_message(&recv_message, 4, 2, NULL); diff --git a/inc/teep/teep_message_print.h b/inc/teep/teep_message_print.h index 8480bf4..16e58b5 100644 --- a/inc/teep/teep_message_print.h +++ b/inc/teep/teep_message_print.h @@ -25,35 +25,6 @@ teep_err_t teep_print_update(const teep_update_t *teep_update, uint32_t indent_s teep_err_t teep_print_success(const teep_success_t *success, uint32_t indent_space, uint32_t indent_delta); teep_err_t teep_print_error(const teep_error_t *error, uint32_t indent_space, uint32_t indent_delta); teep_err_t teep_print_message(const teep_message_t *msg, uint32_t indent_space, uint32_t indent_delta, const unsigned char *ta_public_key); - -typedef char* (*TeepLabelToStr)(const int64_t, const int64_t); -typedef char* (*TeepPrintValue)(void *val); - -char* teep_eat_claim_label_to_str(const int64_t label, const int64_t n); -char* teep_position_label_to_str(const int64_t label, const int64_t type); - -void teep_print_map(QCBORDecodeContext *context, - QCBORItem *item, - const uint32_t indent_space, - const uint32_t indent_delta, - TeepLabelToStr label_to_str, - const int64_t label, - bool in_header); -void teep_print_array(QCBORDecodeContext *context, - QCBORItem *item, - const uint32_t indent_space, - const uint32_t indent_delta, - TeepLabelToStr label_to_str, - const int64_t position, - bool in_hdeader); - -teep_err_t teep_print_cose_usefulbufc(UsefulBufC cose, - const uint32_t indent_space, - const uint32_t indent_delta, - TeepLabelToStr label_to_str); -teep_err_t teep_print_cose_eat(UsefulBufC cose_eat, - const uint32_t indent_space, - const uint32_t indent_delta); teep_err_t teep_print_cose_teep_message(UsefulBufC cose_teep_message, const uint32_t indent_space, const uint32_t indent_delta); diff --git a/src/cose_print_common.c b/src/cose_print_common.c new file mode 100644 index 0000000..18625f2 --- /dev/null +++ b/src/cose_print_common.c @@ -0,0 +1,753 @@ +/* + * Copyright (c) 2020 SECOM CO., LTD. All Rights reserved. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include "teep/teep_common.h" +#include "teep/teep_message_data.h" +#include "teep/teep_message_print.h" +#include "teep/claims.h" +#include "cose_print_common.h" + +#ifdef PARSE_SUIT +#include "csuit/csuit.h" +#endif + +typedef char* (*TeepPrintValue)(void *val); + +void teep_print_map(QCBORDecodeContext *context, + QCBORItem *item, + const uint32_t indent_space, + const uint32_t indent_delta, + TeepLabelToStr label_to_str, + const int64_t label, + bool in_header); +void teep_print_array(QCBORDecodeContext *context, + QCBORItem *item, + const uint32_t indent_space, + const uint32_t indent_delta, + TeepLabelToStr position_to_str, + const int64_t position, + bool in_header); + +bool teep_is_printable_char(const uint8_t c) +{ + return (' ' <= c && c <= '~'); +} + +bool teep_printable_hex_string(const char *array, const size_t size) +{ + size_t i; + for (i = 0; i < size; i++) { + if (!teep_is_printable_char(array[i])) { + return false; + } + } + return true; +} + +teep_err_t teep_print_text_body(const char *text, const size_t size) +{ + for (size_t i = 0; i < size; i++) { + if (text[i] == '\n') { + putchar('\\'); putchar('n'); + } + else { + putchar(text[i]); + } + } + return TEEP_SUCCESS; +} + +teep_err_t teep_print_text(const char *text, const size_t size) +{ + if (text == NULL) { + return TEEP_ERR_UNEXPECTED_ERROR; + } + + printf("\""); + teep_print_text_body(text, size); + printf("\""); + return TEEP_SUCCESS; +} + +teep_err_t teep_print_hex(const uint8_t *array, const size_t size) +{ + if (array == NULL) { + return TEEP_ERR_FATAL; + } + if (teep_printable_hex_string((const char *)array, size)) { + printf("'"); + teep_print_text_body((const char *)array, size); + printf("'"); + } + else { + printf("h'"); + for (size_t i = 0; i < size; i++) { + printf("%02x", (unsigned char)array[i]); + } + printf("'"); + } + return TEEP_SUCCESS; +} + +teep_err_t teep_print_hex_within_max(const uint8_t *array, + const size_t size, + const size_t size_max) +{ + teep_err_t result = TEEP_SUCCESS; + if (size <= size_max) { + result = teep_print_hex(array, size); + } + else { + result = teep_print_hex(array, size_max); + printf(".."); + } + return result; +} + +char *teep_cose_mechanism_key_to_str(int64_t cose_mechanism_key) +{ + switch (cose_mechanism_key) { + case CBOR_TAG_COSE_SIGN1: + return "COSE_Sign1"; + case CBOR_TAG_SIGN: + return "COSE_Sign"; + case CBOR_TAG_COSE_MAC0: + return "COSE_Mac0"; + case CBOR_TAG_MAC: + return "COSE_Mac"; + case CBOR_TAG_COSE_ENCRYPT0: + return "COSE_Encrypt0"; + case CBOR_TAG_ENCRYPT: + return "COSE_Encrypt"; + default: + return NULL; + } +} + +char *teep_cose_algs_key_to_str(int64_t cose_algs_key) +{ + switch (cose_algs_key) { + case TEEP_COSE_SHA256: + return "SHA-256"; + case TEEP_COSE_MAC_HMAC256: + return "HMAC-256"; + case TEEP_COSE_SIGN_ES256: + return "ES256"; + case TEEP_COSE_SIGN_ESP256: + return "ESP256"; + case TEEP_COSE_SIGN_EDDSA: + return "EdDSA"; + case TEEP_COSE_SIGN_ED25519: + return "Ed25519"; + case TEEP_COSE_SIGN_HSS_LMS: + return "HSS-LMS"; + case TEEP_COSE_A128KW: + return "A128KW"; + case TEEP_COSE_ECDHES_HKDF256: + return "ECDH-ES+HKDF-256"; + case TEEP_COSE_ECDHES_A128KW: + return "ECDH-ES+A128KW"; + case TEEP_COSE_ENCRYPT_A128_GCM: + return "AES-GCM-128"; + case TEEP_COSE_ENCRYPT_A192_GCM: + return "AES-GCM-192"; + case TEEP_COSE_ENCRYPT_A256_GCM: + return "AES-GCM-256"; + case TEEP_COSE_ENCRYPT_ACCM_16_64_128: + return "AES-CCM-16-64-128"; + case TEEP_COSE_ENCRYPT_CHACHA20_POLY1305: + return "ChaCha20/Poly1305"; + default: + return NULL; + } +} + +char* teep_position_label_to_str(const int64_t position, const int64_t type) +{ + switch (position) { + case 0: return "type"; + case 1: return "options"; + case 2: + if (type == TEEP_TYPE_QUERY_REQUEST) { + return "supported-teep-cipher-suites"; + } + else if (type == TEEP_TYPE_ERROR) { + return "err-code"; + } + break; + case 3: + if (type == TEEP_TYPE_QUERY_REQUEST) { + return "supported-suit-cose-profiles"; + } + break; + case 4: + if (type == TEEP_TYPE_QUERY_REQUEST) { + return "data-item-requested"; + } + } + return NULL; +} + +char* teep_position_label_to_str_sentinel(const int64_t position, const int64_t type) +{ + return NULL; +} + +char* teep_message_type_to_str(const int64_t type, const int64_t n) +{ + switch (type) { + case TEEP_TYPE_QUERY_REQUEST: return "TEEP-TYPE-QueryRequest"; + case TEEP_TYPE_QUERY_RESPONSE: return "TEEP-TYPE-QueryResponse"; + case TEEP_TYPE_UPDATE: return "TEEP-TYPE-Update"; + case TEEP_TYPE_SUCCESS: return "TEEP-TYPE-Success"; + case TEEP_TYPE_ERROR: return "TEEP-TYPE-Error"; + default: return NULL; + } +} + +char* teep_options_key_to_str(const int64_t label, const int64_t n) +{ + switch (label) { + case TEEP_OPTIONS_KEY_INVALID: return "invalid"; + case TEEP_OPTIONS_KEY_SUPPORTED_TEEP_CIPHER_SUITES: return "supported-teep-cipher-suites"; + case TEEP_OPTIONS_KEY_CHALLENGE: return "challenge"; + case TEEP_OPTIONS_KEY_VERSIONS: return "versions"; + case TEEP_OPTIONS_KEY_SUPPORTED_SUIT_COSE_PROFILES: return "supported-suit-cose-profiles"; + case TEEP_OPTIONS_KEY_SELECTED_VERSION: return "selected-version"; + case TEEP_OPTIONS_KEY_ATTESTATION_PAYLOAD: return "attestation-payload"; + case TEEP_OPTIONS_KEY_TC_LIST: return "tc-list"; + case TEEP_OPTIONS_KEY_EXT_LIST: return "ext-list"; + case TEEP_OPTIONS_KEY_MANIFEST_LIST: return "manifest-list"; + case TEEP_OPTIONS_KEY_MSG: return "msg"; + case TEEP_OPTIONS_KEY_ERR_MSG: return "err-msg"; + case TEEP_OPTIONS_KEY_ATTESTATION_PAYLOAD_FORMAT: return "attestation-payload-format"; + case TEEP_OPTIONS_KEY_REQUESTED_TC_LIST: return "requested-tc-list"; + case TEEP_OPTIONS_KEY_UNNEEDED_TC_LIST: return "unneeded-manifest-list"; + case TEEP_OPTIONS_KEY_COMPONENT_ID: return "component-id"; + case TEEP_OPTIONS_KEY_TC_MANIFEST_SEQUENCE_NUMBER: return "tc-manifest-sequence-number"; + case TEEP_OPTIONS_KEY_HAVE_BINARY: return "have-binary"; + case TEEP_OPTIONS_KEY_SUIT_REPORTS: return "suit-reports"; + case TEEP_OPTIONS_KEY_TOKEN: return "token"; + case TEEP_OPTIONS_KEY_SUPPORTED_FRESHNESS_MECHANISMS: return "supported-freshness-mechanisms"; + case TEEP_OPTIONS_KEY_ERR_LANG: return "err-lang"; + case TEEP_OPTIONS_KEY_ERR_CODE: return "err-code"; + default: return NULL; + } +} + +char* teep_cose_header_label_to_str(const int64_t label, const int64_t n) +{ + switch (label) { + case 1: return "alg"; + case 2: return "crit"; + case 3: return "content type"; + case 4: return "kid"; + case 5: return "IV"; + case 6: return "Partial IV"; + case 7: return "counter signature"; + default: return NULL; + } +} + +char* teep_eat_cnf_label_to_str(const int64_t label, const int64_t n) +{ + switch (label) { + case 1: return "COSE_Key"; + case 2: return "Encrypted_COSE_Key"; + case 3: return "kid"; + default: return NULL; + } +} + +char* teep_eat_claim_label_to_str(const int64_t label, const int64_t n) +{ + switch (label) { + case EAT_CLAIM_ISSUER: return "iss"; + case EAT_CLAIM_EXP: return "exp"; + case EAT_CLAIM_NOT_BEFORE: return "nbf"; + case EAT_CLAIM_TIMESTAMP: return "iat"; + case EAT_CLAIM_CONFIRMATION: return "cnf"; + case EAT_CLAIM_EAT_NONCE: return "eat_nonce"; + case EAT_CLAIM_SECURE_BOOT: return "secboot"; + case EAT_CLAIM_CHIP_VERSION: return "chip_version"; + case EAT_CLAIM_UEID: return "ueid"; + case EAT_CLAIM_SUEIDS: return "sueids"; + case EAT_CLAIM_OEMID: return "oemid"; + case EAT_CLAIM_HWMODEL: return "hwmodel"; + case EAT_CLAIM_HWVERSION: return "hwversion"; + case EAT_CLAIM_OEMBOOT: return "oemboot"; + case EAT_CLAIM_DBGSTAT: return "dbgstat"; + case EAT_CLAIM_LOCATION: return "location"; + case EAT_CLAIM_EAT_PROFILE: return "eat_profile"; + case EAT_CLAIM_SUBMODS: return "submods"; + case EAT_CLAIM_SWNAME: return "swname"; + case EAT_CLAIM_SWVERSION: return "swversion"; + case EAT_CLAIM_MANIFESTS: return "manifests"; + case EAT_CLAIM_MEASUREMENTS: return "measres"; + case EAT_CLAIM_VERIFIER_NONCE: return "verifier_nonce"; + default: return NULL; + } +} + +char* teep_eat_claim_label_to_str_sentinel(const int64_t label, const int64_t n) +{ + return NULL; +} + +char* teep_position_cipher_suite_items(const int64_t position, const int64_t n) +{ + switch (position) { + case 0: return "mechanism"; + case 1: return "algorithm-id"; + default: return NULL; + } +} + +char* teep_position_cipher_suite(const int64_t position, const int64_t n) +{ + return NULL; +} + +char* teep_position_cipher_suites(const int64_t position, const int64_t n) +{ + return NULL; +} + +char* teep_position_tc_list_to_str(const int64_t position, const int64_t n) +{ + return NULL; +} + +char* suit_system_property_claims_to_str(const int64_t label, const int64_t n) +{ + switch (label) { + case 0: return "system-component-id"; +#if PARSE_SUIT + default: return (char *)suit_parameter_key_to_str(label); +#else + default: return NULL; +#endif + } +} + +char* teep_position_requested_tc_list_to_str(const int64_t position, const int64_t n) +{ + return NULL; +} + +char* teep_debug_to_str(TeepLabelToStr from) +{ + if (teep_position_requested_tc_list_to_str == from) { + return "position_requested_tc_list"; + } + if (teep_eat_claim_label_to_str == from) { + return "eat_claim_label"; + } + if (teep_eat_claim_label_to_str_sentinel == from) { + return "eat_claim_label_sentinel"; + } + if (teep_cose_header_label_to_str == from) { + return "cose_header_label"; + } + if (teep_options_key_to_str == from) { + return "teep_options_key"; + } + if (teep_message_type_to_str == from) { + return "message_type"; + } + if (teep_position_label_to_str == from) { + return "position"; + } + return NULL; +} + +TeepLabelToStr teep_a_to_str(TeepLabelToStr from, bool is_map, const int64_t label) +{ + if (from == teep_eat_claim_label_to_str_sentinel) { + return teep_eat_claim_label_to_str; + } + else if (from == teep_position_label_to_str) { + if (is_map) { + return teep_options_key_to_str; + } + switch (label) { + case 2: return teep_position_cipher_suites; + case 3: return NULL; + } + } + else if (from == teep_eat_claim_label_to_str) { + switch (label) { + case EAT_CLAIM_CONFIRMATION: + return teep_eat_cnf_label_to_str; + } + } + else if (from == teep_position_tc_list_to_str) { + return suit_system_property_claims_to_str; + } + else if (from == teep_position_requested_tc_list_to_str) { + return teep_options_key_to_str; + } + else if (from == teep_position_label_to_str_sentinel) { + return teep_position_label_to_str; + } + else if (from == teep_options_key_to_str) { + switch (label) { + case TEEP_OPTIONS_KEY_TC_LIST: + return teep_position_tc_list_to_str; + case TEEP_OPTIONS_KEY_REQUESTED_TC_LIST: + return teep_position_requested_tc_list_to_str; + } + } + else if (from == teep_position_cipher_suite) { + return teep_position_cipher_suite_items; + } + else if (from == teep_position_cipher_suites) { + return teep_position_cipher_suite; + } + return NULL; +} + +char* teep_cose_tag_to_str(void *type) +{ + return teep_cose_mechanism_key_to_str(*(int64_t *)type); +} + +char* teep_algorithm_id_to_str(void *type) +{ + return teep_cose_algs_key_to_str(*(int64_t *)type); +} + +char* teep_message_type_value_to_str(void *type) +{ + return teep_message_type_to_str(*(int64_t *)type, 0); +} + +static char buf[32]; +char* teep_data_item_requested_to_str(void *type) +{ + size_t len = 0; + teep_data_item_requested_t r; + r.val = *(int64_t *)type; + + if (r.attestation) { + len += sprintf(&buf[len], "attestation|"); + } + if (r.trusted_components) { + len += sprintf(&buf[len], "trusted-components|"); + } + if (r.extensions) { + len += sprintf(&buf[len], "extensions"); + } + if (len > 0) { + buf[len - 1] = '\0'; + return buf; + } + return NULL; +} + +char* teep_alg_id_to_str(void *id) +{ + return teep_cose_algs_key_to_str(*(int64_t *)id); +} + +TeepPrintValue teep_value_to_str(TeepLabelToStr f, int64_t n) +{ + if (teep_position_label_to_str == f) { + switch (n) { + case 0: return teep_message_type_value_to_str; + case 4: return teep_data_item_requested_to_str; + } + } + else if (teep_cose_header_label_to_str == f) { + switch (n) { + case 1: return teep_alg_id_to_str; + } + } + else if (teep_position_cipher_suite_items == f) { + switch (n) { + case 0: return teep_cose_tag_to_str; + case 1: return teep_algorithm_id_to_str; + } + } + return NULL; +} + +void teep_print_value(QCBORDecodeContext *context, + QCBORItem *item, + const uint32_t indent_space, + const uint32_t indent_delta, + TeepLabelToStr label_to_str, + const int64_t label, + bool in_header) +{ + union teep_value_t { + int64_t int64; + uint64_t uint64; + UsefulBufC string; + bool boolean; + } val; + + switch (item->uDataType) { + case QCBOR_TYPE_INT64: + QCBORDecode_GetInt64(context, &val.int64); + printf("%ld", val.int64); + break; + case QCBOR_TYPE_UINT64: + QCBORDecode_GetUInt64(context, &val.uint64); + printf("%ld", val.uint64); + break; + case QCBOR_TYPE_ARRAY: + QCBORDecode_EnterArray(context, item); + teep_print_array(context, item, indent_space, indent_delta, label_to_str, label, in_header); + QCBORDecode_ExitArray(context); + break; + case QCBOR_TYPE_MAP: + QCBORDecode_EnterMap(context, item); + teep_print_map(context, item, indent_space, indent_delta, label_to_str, label, in_header); + QCBORDecode_ExitMap(context); + break; + case QCBOR_TYPE_BYTE_STRING: + QCBORDecode_GetByteString(context, &val.string); + teep_print_hex_within_max(val.string.ptr, val.string.len, TEEP_MAX_PRINT_BYTE_COUNT); + break; + case QCBOR_TYPE_TEXT_STRING: + QCBORDecode_GetTextString(context, &val.string); + teep_print_text(val.string.ptr, item->val.string.len); + break; + case QCBOR_TYPE_TRUE: + QCBORDecode_GetBool(context, &val.boolean); + printf("true"); + break; + case QCBOR_TYPE_FALSE: + QCBORDecode_GetBool(context, &val.boolean); + printf("false"); + break; + case QCBOR_TYPE_NULL: + QCBORDecode_GetNull(context); + printf("null"); + break; + default: + break; + } + + TeepPrintValue f = teep_value_to_str(label_to_str, label); + if (f != NULL && f(&val) != NULL) { + printf(" / %s /", f(&val)); + } +} + +void teep_print_map(QCBORDecodeContext *context, + QCBORItem *item, + const uint32_t indent_space, + const uint32_t indent_delta, + TeepLabelToStr label_to_str, + const int64_t label, + bool in_header) +{ + printf("{\n"); + size_t length = item->val.uCount; + for (size_t i = 0; i < length; i++) { + QCBORDecode_PeekNext(context, item); + TeepLabelToStr tmp_label_to_str = label_to_str; + if (item->uDataType == QCBOR_TYPE_ARRAY) { + tmp_label_to_str = teep_a_to_str(label_to_str, false, item->label.int64); + } + else if (item->uDataType == QCBOR_TYPE_MAP) { + tmp_label_to_str = teep_a_to_str(label_to_str, true, item->label.int64); + } + printf("%*s", indent_space + indent_delta, ""); + if (label_to_str != NULL) { + printf("/ %s / ", label_to_str(item->label.int64, item->label.int64)); + } + printf("%ld : ", item->label.int64); + + teep_print_value(context, item, indent_space + indent_delta, indent_delta, tmp_label_to_str, item->label.int64, in_header); + + if (i + 1 < length) { + printf(","); + } + printf("\n"); + } + printf("%*s}", indent_space, ""); +} + +void teep_print_array(QCBORDecodeContext *context, + QCBORItem *item, + const uint32_t indent_space, + const uint32_t indent_delta, + TeepLabelToStr position_to_str, + const int64_t position, + bool in_header) +{ + size_t length = item->val.uCount; + + int64_t type = 0; + bool is_teep_protocol = position_to_str == teep_position_label_to_str; + if (is_teep_protocol) { + QCBORDecode_PeekNext(context, item); + type = item->val.int64; + } + bool is_inline = (position_to_str == NULL); + bool has_label = (!is_inline && position_to_str(0, type) != NULL); + printf("[%c", is_inline ? ' ' : '\n'); + + int64_t prev_label = item->label.int64; + (void)position; + for (size_t i = 0; i < length; i++) { + QCBORDecode_PeekNext(context, item); + TeepLabelToStr tmp_position_to_str = position_to_str; + if (item->uDataType == QCBOR_TYPE_ARRAY) { + tmp_position_to_str = teep_a_to_str(position_to_str, false, i); + } + else if (item->uDataType == QCBOR_TYPE_MAP) { + tmp_position_to_str = teep_a_to_str(position_to_str, true, prev_label); + } + + if (!is_inline) { + printf("%*s", indent_space + indent_delta, ""); + } + if (has_label) { + printf("/ %s: / ", position_to_str(i, type)); + } + + teep_print_value(context, item, indent_space + indent_delta, indent_delta, tmp_position_to_str, i, in_header); + + if (i + 1 < length) { + printf(","); + printf("%c", (position_to_str == NULL) ? ' ' : '\n'); + } + } + + if (is_inline) { + printf(" ]"); + } + else { + printf("\n%*s]", indent_space, ""); + } +} + +teep_err_t teep_print_cose_header(QCBORDecodeContext *context, + const uint32_t indent_space, + const uint32_t indent_delta) +{ + QCBORItem item; + QCBORDecode_EnterMap(context, &item); + teep_print_map(context, &item, indent_space, indent_delta, teep_cose_header_label_to_str, false, true); + QCBORDecode_ExitMap(context); + return TEEP_SUCCESS; +} + +teep_err_t teep_print_cose(QCBORDecodeContext *context, + const uint32_t indent_space, + const uint32_t indent_delta, + TeepLabelToStr label_to_str) +{ + QCBORItem item; + + printf("%*s", indent_space, ""); + uint16_t cose_tag = 0; + item.uTags[0] = 0; + QCBORDecode_EnterArray(context, &item); + if (QCBORDecode_GetError(context) != QCBOR_SUCCESS) { + printf("context.uLastError = %d\n", QCBORDecode_GetError(context)); + return TEEP_ERR_FATAL; + } + if (item.uTags[0] != 0) { + cose_tag = item.uTags[0]; + printf("%d(", cose_tag); + } + if (item.val.uCount != 4) { + printf("item.val.uCount = %d\n", item.val.uCount); + return TEEP_ERR_FATAL; + } + + QCBORDecode_EnterBstrWrapped(context, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); + printf("[\n%*s/ protected: / << ", indent_space + indent_delta, ""); + teep_print_cose_header(context, indent_space + indent_delta, indent_delta); + printf(" >>,\n"); + QCBORDecode_ExitBstrWrapped(context); + printf("%*s/ unprotected: / ", indent_space + indent_delta, ""); + teep_print_cose_header(context, indent_space + indent_delta, indent_delta); + printf(",\n"); + + printf("%*s/ payload: / << ", indent_space + indent_delta, ""); + QCBORDecode_EnterBstrWrapped(context, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); + QCBORDecode_PeekNext(context, &item); + teep_print_value(context, &item, indent_space + indent_delta, indent_delta, label_to_str, 0, false); + QCBORDecode_ExitBstrWrapped(context); + printf(" >>,\n"); + + QCBORDecode_PeekNext(context, &item); + if (item.uDataType == QCBOR_TYPE_BYTE_STRING) { + UsefulBufC signature; + printf("%*s/ signature: / ", indent_space + indent_delta, ""); + QCBORDecode_GetByteString(context, &signature); + teep_print_hex(signature.ptr, signature.len); + } + else if (item.uDataType == QCBOR_TYPE_ARRAY) { + QCBORDecode_EnterArray(context, &item); + size_t array_len = item.val.uCount; + printf("%*s/ signatures: / [\n", indent_space + indent_delta, ""); + for (size_t i = 0; i < array_len; i++) { + UsefulBufC signature; + QCBORDecode_EnterArray(context, &item); + if (item.val.uCount != 3) { + return TEEP_ERR_INVALID_LENGTH; + } + printf("%*s[\n", indent_space + 2 * indent_delta, ""); + QCBORDecode_EnterBstrWrapped(context, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); + printf("%*s/ protected: / << ", indent_space + 3 * indent_delta, ""); + teep_print_cose_header(context, indent_space + 3 * indent_delta, indent_delta); + printf(" >>,\n"); + QCBORDecode_ExitBstrWrapped(context); + printf("%*s/ unprotected: / ", indent_space + 3 * indent_delta, ""); + teep_print_cose_header(context, indent_space + 3 * indent_delta, indent_delta); + printf(",\n"); + printf("%*s/ signature: / ", indent_space + 3 * indent_delta, ""); + QCBORDecode_GetByteString(context, &signature); + teep_print_hex(signature.ptr, signature.len); + printf("\n%*s]", indent_space + 2 * indent_delta, ""); + QCBORDecode_ExitArray(context); + if (i + 1 < array_len) { + printf(","); + } + printf("\n"); + } + printf("%*s]", indent_space + indent_delta, ""); + QCBORDecode_ExitArray(context); + } + else { + return TEEP_ERR_INVALID_TYPE_OF_VALUE; + } + + QCBORDecode_ExitArray(context); + printf("\n]"); + if (cose_tag != 0) { + printf(")"); + } + printf("\n"); + + return TEEP_SUCCESS; +} + +teep_err_t teep_print_cose_usefulbufc(UsefulBufC cose, + const uint32_t indent_space, + const uint32_t indent_delta, + TeepLabelToStr label_to_str) +{ + QCBORDecodeContext context; + QCBORDecode_Init(&context, cose, QCBOR_DECODE_MODE_NORMAL); + + teep_err_t result = teep_print_cose(&context, indent_space, indent_delta, label_to_str); + + QCBORError qcbor_err = QCBORDecode_Finish(&context); + if (qcbor_err != QCBOR_SUCCESS) { + printf("qcbor_err = %u\n", qcbor_err); + return TEEP_ERR_FATAL; + } + return result; +} diff --git a/src/cose_print_common.h b/src/cose_print_common.h new file mode 100644 index 0000000..b6145fd --- /dev/null +++ b/src/cose_print_common.h @@ -0,0 +1,18 @@ +#ifndef COSE_PRINT_COMMON_H +#define COSE_PRINT_COMMON_H + +#include +#include "teep/teep_common.h" + +typedef char* (*TeepLabelToStr)(const int64_t, const int64_t); + +char *teep_cose_mechanism_key_to_str(int64_t cose_mechanism_key); +char *teep_position_label_to_str(const int64_t label, const int64_t type); +char *teep_eat_claim_label_to_str(const int64_t label, const int64_t n); + +teep_err_t teep_print_cose_usefulbufc(UsefulBufC cose, + const uint32_t indent_space, + const uint32_t indent_delta, + TeepLabelToStr label_to_str); + +#endif /* COSE_PRINT_COMMON_H */ diff --git a/src/teep_message_print.c b/src/teep_message_print.c index e248967..cbab146 100644 --- a/src/teep_message_print.c +++ b/src/teep_message_print.c @@ -7,7 +7,7 @@ #include "teep/teep_common.h" #include "teep/teep_message_data.h" #include "teep/teep_message_print.h" -#include "teep/claims.h" +#include "cose_print_common.h" #ifdef PARSE_SUIT #include "csuit/csuit.h" @@ -72,47 +72,6 @@ teep_err_t teep_print_hex_string(const uint8_t *array, const int size) return TEEP_SUCCESS; } -bool teep_is_printable_char(const uint8_t c) -{ - return (' ' <= c && c <= '~'); -} - -bool teep_printable_hex_string(const char *array, const size_t size) -{ - size_t i; - for (i = 0; i < size; i++) { - if (!teep_is_printable_char(array[i])) { - return false; - } - } - return true; -} - -teep_err_t teep_print_text_body(const char *text, const size_t size) -{ - for (size_t i = 0; i < size; i++) { - if (text[i] == '\n') { - putchar('\\'); putchar('n'); - } - else { - putchar(text[i]); - } - } - return TEEP_SUCCESS; -} - -teep_err_t teep_print_text(const char *text, const size_t size) -{ - if (text == NULL) { - return TEEP_ERR_UNEXPECTED_ERROR; - } - - printf("\""); - teep_print_text_body(text, size); - printf("\""); - return TEEP_SUCCESS; -} - teep_err_t teep_print_text_within_max(const char *text, const size_t size, const size_t size_max) @@ -133,41 +92,6 @@ teep_err_t teep_print_string(const teep_buf_t *string) return teep_print_text_within_max((const char *)string->ptr, string->len, TEEP_MAX_PRINT_TEXT_COUNT); } -teep_err_t teep_print_hex(const uint8_t *array, const size_t size) -{ - if (array == NULL) { - return TEEP_ERR_FATAL; - } - if (teep_printable_hex_string((const char *)array, size)) { - printf("'"); - teep_print_text_body((const char *)array, size); - printf("'"); - } - else { - printf("h'"); - for (size_t i = 0; i < size; i++) { - printf("%02x", (unsigned char)array[i]); - } - printf("'"); - } - return TEEP_SUCCESS; -} - -teep_err_t teep_print_hex_within_max(const uint8_t *array, - const size_t size, - const size_t size_max) -{ - teep_err_t result = TEEP_SUCCESS; - if (size <= size_max) { - result = teep_print_hex(array, size); - } - else { - result = teep_print_hex(array, size_max); - printf(".."); - } - return result; -} - char *teep_err_code_to_str(int32_t err_code) { switch (err_code) { @@ -198,78 +122,6 @@ char *teep_err_code_to_str(int32_t err_code) } } -char *teep_cose_mechanism_key_to_str(int64_t cose_mechanism_key) -{ - switch (cose_mechanism_key) { - case CBOR_TAG_COSE_SIGN1: - return "COSE_Sign1"; - case CBOR_TAG_SIGN: - return "COSE_Sign"; - case CBOR_TAG_COSE_MAC0: - return "COSE_Mac0"; - case CBOR_TAG_MAC: - return "COSE_Mac"; - case CBOR_TAG_COSE_ENCRYPT0: - return "COSE_Encrypt0"; - case CBOR_TAG_ENCRYPT: - return "COSE_Encrypt"; - default: - return NULL; - } -} - -char *teep_cose_algs_key_to_str(int64_t cose_algs_key) -{ - switch (cose_algs_key) { - /* hash */ - case TEEP_COSE_SHA256: - return "SHA-256"; - - /* authentication algorithms */ - case TEEP_COSE_MAC_HMAC256: - return "HMAC-256"; - case TEEP_COSE_SIGN_ES256: - return "ES256"; - case TEEP_COSE_SIGN_ESP256: - return "ESP256"; - case TEEP_COSE_SIGN_EDDSA: - return "EdDSA"; - case TEEP_COSE_SIGN_ED25519: - return "Ed25519"; - case TEEP_COSE_SIGN_HSS_LMS: - return "HSS-LMS"; - - /* key_exchange algorithms */ - case TEEP_COSE_A128KW: - return "A128KW"; - case TEEP_COSE_ECDHES_HKDF256: - return "ECDH-ES+HKDF-256"; - case TEEP_COSE_ECDHES_A128KW: - return "ECDH-ES+A128KW"; - - /* encryption algorithms */ - case TEEP_COSE_ENCRYPT_A128_GCM: - return "AES-GCM-128"; - case TEEP_COSE_ENCRYPT_A192_GCM: - return "AES-GCM-192"; - case TEEP_COSE_ENCRYPT_A256_GCM: - return "AES-GCM-256"; - case TEEP_COSE_ENCRYPT_ACCM_16_64_128: - return "AES-CCM-16-64-128"; - case TEEP_COSE_ENCRYPT_CHACHA20_POLY1305: - return "ChaCha20/Poly1305"; - case TEEP_COSE_ENCRYPT_A128CTR: - return "A128CTR"; - case TEEP_COSE_ENCRYPT_A192CTR: - return "A192CTR"; - case TEEP_COSE_ENCRYPT_A256CTR: - return "A256CTR"; - - default: - return NULL; - } -} - void teep_debug_print(QCBORDecodeContext *message, QCBORItem *item, QCBORError *error, @@ -978,627 +830,6 @@ teep_err_t teep_print_message(const teep_message_t *msg, return result; } -char* teep_position_label_to_str(const int64_t position, const int64_t type) -{ - switch (position) { - case 0: return "type"; - case 1: return "options"; - case 2: - if (type == TEEP_TYPE_QUERY_REQUEST) { - return "supported-teep-cipher-suites"; - } - else if (type == TEEP_TYPE_ERROR) { - return "err-code"; - } - break; - case 3: - if (type == TEEP_TYPE_QUERY_REQUEST) { - return "supported-suit-cose-profiles"; - } - break; - case 4: - if (type == TEEP_TYPE_QUERY_REQUEST) { - return "data-item-requested"; - } - } - return NULL; -} - -char* teep_position_label_to_str_sentinel(const int64_t position, const int64_t type) -{ - return NULL; -} - -char* teep_message_type_to_str(const int64_t type, const int64_t n) -{ - switch (type) { - case TEEP_TYPE_QUERY_REQUEST: return "TEEP-TYPE-QueryRequest"; - case TEEP_TYPE_QUERY_RESPONSE: return "TEEP-TYPE-QueryResponse"; - case TEEP_TYPE_UPDATE: return "TEEP-TYPE-Update"; - case TEEP_TYPE_SUCCESS: return "TEEP-TYPE-Success"; - case TEEP_TYPE_ERROR: return "TEEP-TYPE-Error"; - default: return NULL; - } -} - -char* teep_options_key_to_str(const int64_t label, const int64_t n) -{ - switch (label) { - case TEEP_OPTIONS_KEY_INVALID: return "invalid"; - case TEEP_OPTIONS_KEY_SUPPORTED_TEEP_CIPHER_SUITES: return "supported-teep-cipher-suites"; - case TEEP_OPTIONS_KEY_CHALLENGE: return "challenge"; - case TEEP_OPTIONS_KEY_VERSIONS: return "versions"; - case TEEP_OPTIONS_KEY_SUPPORTED_SUIT_COSE_PROFILES: return "supported-suit-cose-profiles"; - case TEEP_OPTIONS_KEY_SELECTED_VERSION: return "selected-version"; - case TEEP_OPTIONS_KEY_ATTESTATION_PAYLOAD: return "attestation-payload"; - case TEEP_OPTIONS_KEY_TC_LIST: return "tc-list"; - case TEEP_OPTIONS_KEY_EXT_LIST: return "ext-list"; - case TEEP_OPTIONS_KEY_MANIFEST_LIST: return "manifest-list"; - case TEEP_OPTIONS_KEY_MSG: return "msg"; - case TEEP_OPTIONS_KEY_ERR_MSG: return "err-msg"; - case TEEP_OPTIONS_KEY_ATTESTATION_PAYLOAD_FORMAT: return "attestation-payload-format"; - case TEEP_OPTIONS_KEY_REQUESTED_TC_LIST: return "requested-tc-list"; - case TEEP_OPTIONS_KEY_UNNEEDED_TC_LIST: return "unneeded-manifest-list"; - case TEEP_OPTIONS_KEY_COMPONENT_ID: return "component-id"; - case TEEP_OPTIONS_KEY_TC_MANIFEST_SEQUENCE_NUMBER: return "tc-manifest-sequence-number"; - case TEEP_OPTIONS_KEY_HAVE_BINARY: return "have-binary"; - case TEEP_OPTIONS_KEY_SUIT_REPORTS: return "suit-reports"; - case TEEP_OPTIONS_KEY_TOKEN: return "token"; - case TEEP_OPTIONS_KEY_SUPPORTED_FRESHNESS_MECHANISMS: return "supported-freshness-mechanisms"; - case TEEP_OPTIONS_KEY_ERR_LANG: return "err-lang"; - case TEEP_OPTIONS_KEY_ERR_CODE: return "err-code"; - default: return NULL; - } -} - -char* teep_cose_header_label_to_str(const int64_t label, const int64_t n) -{ - switch (label) { - case 1: return "alg"; - case 2: return "crit"; - case 3: return "content type"; - case 4: return "kid"; - case 5: return "IV"; - case 6: return "Partial IV"; - case 7: return "counter signature"; - default: return NULL; - } -} - - -char* teep_eat_cnf_label_to_str(const int64_t label, const int64_t n) -{ - switch (label) { - case 1: return "COSE_Key"; - case 2: return "Encrypted_COSE_Key"; - case 3: return "kid"; - default: return NULL; - } -} - -char* teep_eat_claim_label_to_str(const int64_t label, const int64_t n) -{ - switch (label) { - case EAT_CLAIM_ISSUER: return "iss"; - case EAT_CLAIM_EXP: return "exp"; - case EAT_CLAIM_NOT_BEFORE: return "nbf"; - case EAT_CLAIM_TIMESTAMP: return "iat"; - case EAT_CLAIM_CONFIRMATION: return "cnf"; - case EAT_CLAIM_EAT_NONCE: return "eat_nonce"; - case EAT_CLAIM_SECURE_BOOT: return "secboot"; - case EAT_CLAIM_CHIP_VERSION: return "chip_version"; - case EAT_CLAIM_UEID: return "ueid"; - case EAT_CLAIM_SUEIDS: return "sueids"; - case EAT_CLAIM_OEMID: return "oemid"; - case EAT_CLAIM_HWMODEL: return "hwmodel"; - case EAT_CLAIM_HWVERSION: return "hwversion"; - case EAT_CLAIM_OEMBOOT: return "oemboot"; - case EAT_CLAIM_DBGSTAT: return "dbgstat"; - case EAT_CLAIM_LOCATION: return "location"; - case EAT_CLAIM_EAT_PROFILE: return "eat_profile"; - case EAT_CLAIM_SUBMODS: return "submods"; - case EAT_CLAIM_SWNAME: return "swname"; - case EAT_CLAIM_SWVERSION: return "swversion"; - case EAT_CLAIM_MANIFESTS: return "manifests"; - case EAT_CLAIM_MEASUREMENTS: return "measres"; - case EAT_CLAIM_VERIFIER_NONCE: return "verifier_nonce"; - default: return NULL; - } -} - -char* teep_eat_claim_label_to_str_sentinel(const int64_t label, const int64_t n) -{ - return NULL; -} - -char* teep_position_cipher_suite_items(const int64_t position, const int64_t n) -{ - switch (position) { - case 0: return "mechanism"; - case 1: return "algorithm-id"; - default: return NULL; - } -} - -char* teep_position_cipher_suite(const int64_t position, const int64_t n) -{ - return NULL; -} - -char* teep_position_cipher_suites(const int64_t position, const int64_t n) -{ - return NULL; -} - -char* teep_position_tc_list_to_str(const int64_t position, const int64_t n) -{ - return NULL; -} - -char* suit_system_property_claims_to_str(const int64_t label, const int64_t n) -{ - switch (label) { - case 0: return "system-component-id"; -#if PARSE_SUIT - default: return (char *)suit_parameter_key_to_str(label); -#else - default: return NULL; -#endif - } -} - -char* teep_position_requested_tc_list_to_str(const int64_t position, const int64_t n) -{ - return NULL; -} - -char* teep_debug_to_str(TeepLabelToStr from) -{ - if (teep_position_requested_tc_list_to_str == from) { - return "position_requested_tc_list"; - } - if (teep_eat_claim_label_to_str == from) { - return "eat_claim_label"; - } - if (teep_eat_claim_label_to_str_sentinel == from) { - return "eat_claim_label_sentinel"; - } - if (teep_cose_header_label_to_str == from) { - return "cose_header_label"; - } - if (teep_options_key_to_str == from) { - return "teep_options_key"; - } - if (teep_message_type_to_str == from) { - return "message_type"; - } - if (teep_position_label_to_str == from) { - return "position"; - } - return NULL; -} - -TeepLabelToStr teep_a_to_str(TeepLabelToStr from, bool is_map, const int64_t label) -{ - if (from == teep_eat_claim_label_to_str_sentinel) { - return teep_eat_claim_label_to_str; - } - else if (from == teep_position_label_to_str) { - if (is_map) { - return teep_options_key_to_str; - } - else { - switch (label) { - case 2: return teep_position_cipher_suites; - case 3: return NULL; /* TODO: teep_position_profiles */ - } - } - } - else if (from == teep_eat_claim_label_to_str) { - switch (label) { - case EAT_CLAIM_CONFIRMATION: - return teep_eat_cnf_label_to_str; - } - } - else if (from == teep_position_label_to_str) { - return teep_options_key_to_str; - } - else if (from == teep_position_tc_list_to_str) { - return suit_system_property_claims_to_str; - } - else if (from == teep_position_requested_tc_list_to_str) { - return teep_options_key_to_str; - } - else if (from == teep_position_label_to_str_sentinel) { - return teep_position_label_to_str; - } - else if (from == teep_options_key_to_str) { - switch (label) { - case TEEP_OPTIONS_KEY_TC_LIST: - return teep_position_tc_list_to_str; - case TEEP_OPTIONS_KEY_REQUESTED_TC_LIST: - return teep_position_requested_tc_list_to_str; - } - } - else if (from == teep_position_cipher_suite) { - return teep_position_cipher_suite_items; - } - else if (from == teep_position_cipher_suites) { - return teep_position_cipher_suite; - } - return NULL; -} - -char* teep_cose_tag_to_str(void *type) -{ - return teep_cose_mechanism_key_to_str(*(int64_t *)type); -} - -char* teep_algorithm_id_to_str(void *type) -{ - return teep_cose_algs_key_to_str(*(int64_t *)type); -} - -char* teep_message_type_value_to_str(void *type) -{ - return teep_message_type_to_str(*(int64_t *)type, 0); -} - -static char buf[32]; -char* teep_data_item_requested_to_str(void *type) -{ - size_t len = 0; - teep_data_item_requested_t r; - r.val = *(int64_t *)type; - - if (r.attestation) { - len += sprintf(&buf[len], "attestation|"); - } - if (r.trusted_components) { - len += sprintf(&buf[len], "trusted-components|"); - } - if (r.extensions) { - len += sprintf(&buf[len], "extensions"); - } - if (len > 0) { - buf[len - 1] = '\0'; - return buf; - } - return NULL; -} - -char* teep_alg_id_to_str(void *id) -{ - return teep_cose_algs_key_to_str(*(int64_t *)id); -} - -TeepPrintValue teep_value_to_str(TeepLabelToStr f, int64_t n) -{ - if (teep_position_label_to_str == f) { - switch (n) { - case 0: return teep_message_type_value_to_str; - case 4: return teep_data_item_requested_to_str; - } - } - else if (teep_cose_header_label_to_str == f) { - switch (n) { - case 1: return teep_alg_id_to_str; - } - } - else if (teep_position_cipher_suite_items == f) { - switch (n) { - case 0: return teep_cose_tag_to_str; - case 1: return teep_algorithm_id_to_str; - } - } - - return NULL; -} - -char* teep_debug_to_print(TeepPrintValue from) -{ - if (teep_message_type_value_to_str == from) { - return "message_type"; - } - else if (teep_data_item_requested_to_str == from) { - return "data_item_requested"; - } - else if (teep_alg_id_to_str == from) { - return "alg_id"; - } - else if (teep_cose_tag_to_str == from) { - return "cose_tag"; - } - else if (teep_algorithm_id_to_str == from) { - return "algorithm_id"; - } - return NULL; -} - -void teep_print_value(QCBORDecodeContext *context, - QCBORItem *item, - const uint32_t indent_space, - const uint32_t indent_delta, - TeepLabelToStr label_to_str, - const int64_t label, - bool in_header) -{ - union { - int64_t int64; - uint64_t uint64; - UsefulBufC string; - bool boolean; - } val; - - switch (item->uDataType) { - case QCBOR_TYPE_INT64: - QCBORDecode_GetInt64(context, &val.int64); - printf("%ld", val.int64); - break; - case QCBOR_TYPE_UINT64: - QCBORDecode_GetUInt64(context, &val.uint64); - printf("%ld", val.uint64); - break; - case QCBOR_TYPE_ARRAY: - QCBORDecode_EnterArray(context, item); - teep_print_array(context, item, indent_space, indent_delta, label_to_str, label, in_header); - QCBORDecode_ExitArray(context); - break; - case QCBOR_TYPE_MAP: - QCBORDecode_EnterMap(context, item); - teep_print_map(context, item, indent_space, indent_delta, label_to_str, label, in_header); - QCBORDecode_ExitMap(context); - break; - case QCBOR_TYPE_BYTE_STRING: - QCBORDecode_GetByteString(context, &val.string); - teep_print_hex_within_max(val.string.ptr, val.string.len, TEEP_MAX_PRINT_BYTE_COUNT); - break; - case QCBOR_TYPE_TEXT_STRING: - QCBORDecode_GetTextString(context, &val.string); - teep_print_text(val.string.ptr, item->val.string.len); - break; - case QCBOR_TYPE_TRUE: - QCBORDecode_GetBool(context, &val.boolean); - printf("true"); - break; - case QCBOR_TYPE_FALSE: - QCBORDecode_GetBool(context, &val.boolean); - printf("false"); - break; - case QCBOR_TYPE_NULL: - QCBORDecode_GetNull(context); - printf("null"); - break; - } - - TeepPrintValue f = teep_value_to_str(label_to_str, label); - if (f != NULL && f(&val) != NULL) { - /* there is something to print for the value */ - printf(" / %s /", f(&val)); - } -} - -void teep_print_map(QCBORDecodeContext *context, - QCBORItem *item, - const uint32_t indent_space, - const uint32_t indent_delta, - TeepLabelToStr label_to_str, - const int64_t label, - bool in_header) -{ - printf("{\n"); - size_t length = item->val.uCount; - for (size_t i = 0; i < length; i++) { - QCBORDecode_PeekNext(context, item); - TeepLabelToStr tmp_label_to_str = label_to_str; - if (item->uDataType == QCBOR_TYPE_ARRAY) { - tmp_label_to_str = teep_a_to_str(label_to_str, false, item->label.int64); - } - else if (item->uDataType == QCBOR_TYPE_MAP) { - tmp_label_to_str = teep_a_to_str(label_to_str, true, item->label.int64); - } - printf("%*s", indent_space + indent_delta, ""); - if (label_to_str != NULL) { - printf("/ %s / ", label_to_str(item->label.int64, item->label.int64)); - } - printf("%ld : ", item->label.int64); - - teep_print_value(context, item, indent_space + indent_delta, indent_delta, tmp_label_to_str, item->label.int64, in_header); - - if (i + 1 < length) { - printf(","); - } - printf("\n"); - } - printf("%*s}", indent_space, ""); -} - -void teep_print_array(QCBORDecodeContext *context, - QCBORItem *item, - const uint32_t indent_space, - const uint32_t indent_delta, - TeepLabelToStr position_to_str, - const int64_t position, - bool in_header) -{ - size_t length = item->val.uCount; - - int64_t type = 0; - bool is_teep_protocol = position_to_str == teep_position_label_to_str; - if (is_teep_protocol) { - QCBORDecode_PeekNext(context, item); - type = item->val.int64; - } - bool is_inline = (position_to_str == NULL); - bool has_label = (is_inline) ? false : position_to_str(0, type) != NULL; - printf("[%c", is_inline ? ' ' : '\n'); - - int64_t prev_label = item->label.int64; - for (size_t i = 0; i < length; i++) { - QCBORDecode_PeekNext(context, item); - TeepLabelToStr tmp_position_to_str = position_to_str; - if (item->uDataType == QCBOR_TYPE_ARRAY) { - tmp_position_to_str = teep_a_to_str(position_to_str, false, i); - } - else if (item->uDataType == QCBOR_TYPE_MAP) { - tmp_position_to_str = teep_a_to_str(position_to_str, true, prev_label); - } - - if (!is_inline) { - printf("%*s", indent_space + indent_delta, ""); - } - if (has_label) { - printf("/ %s: / ", position_to_str(i, type)); - } - - teep_print_value(context, item, indent_space + indent_delta, indent_delta, tmp_position_to_str, i, in_header); - - if (i + 1 < length) { - printf(","); - printf("%c", (position_to_str == NULL) ? ' ' : '\n'); - } - } - - if (is_inline) { - printf(" ]"); - } - else { - printf("\n%*s]", indent_space, ""); - } -} - -teep_err_t teep_print_cose_header(QCBORDecodeContext *context, - const uint32_t indent_space, - const uint32_t indent_delta) -{ - QCBORItem item; - QCBORDecode_EnterMap(context, &item); - teep_print_map(context, &item, indent_space, indent_delta, teep_cose_header_label_to_str, false, true); - QCBORDecode_ExitMap(context); - return TEEP_SUCCESS; -} - -teep_err_t teep_print_cose(QCBORDecodeContext *context, - const uint32_t indent_space, - const uint32_t indent_delta, - TeepLabelToStr label_to_str) -{ - QCBORItem item; - - printf("%*s", indent_space, ""); - uint16_t cose_tag = 0; - item.uTags[0] = 0; - QCBORDecode_EnterArray(context, &item); - if (QCBORDecode_GetError(context) != QCBOR_SUCCESS) { - printf("context.uLastError = %d\n", QCBORDecode_GetError(context)); - return TEEP_ERR_FATAL; - } - if (item.uTags[0] != 0) { - cose_tag = item.uTags[0]; - printf("%d(", cose_tag); - } - if (item.val.uCount != 4) { - printf("item.val.uCount = %d\n", item.val.uCount); - return TEEP_ERR_FATAL; - } - - QCBORDecode_EnterBstrWrapped(context, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); - printf("[\n%*s/ protected: / << ", indent_space + indent_delta, ""); - teep_print_cose_header(context, indent_space + indent_delta, indent_delta); - printf(" >>,\n"); - QCBORDecode_ExitBstrWrapped(context); - printf("%*s/ unprotected: / ", indent_space + indent_delta, ""); - teep_print_cose_header(context, indent_space + indent_delta, indent_delta); - printf(",\n"); - - printf("%*s/ payload: / << ", indent_space + indent_delta, ""); - QCBORDecode_EnterBstrWrapped(context, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); - QCBORDecode_PeekNext(context, &item); - teep_print_value(context, &item, indent_space + indent_delta, indent_delta, label_to_str, 0, false); - QCBORDecode_ExitBstrWrapped(context); - printf(" >>,\n"); - - QCBORDecode_PeekNext(context, &item); - if (item.uDataType == QCBOR_TYPE_BYTE_STRING) { - /* COSE_Sign1 */ - printf("%*s/ signature: / ", indent_space + indent_delta, ""); - UsefulBufC signature; - QCBORDecode_GetByteString(context, &signature); - teep_print_hex(signature.ptr, signature.len); - } - else if (item.uDataType == QCBOR_TYPE_ARRAY) { - /* COSE_Sign */ - QCBORDecode_EnterArray(context, &item); - size_t array_len = item.val.uCount; - printf("%*s/ signatures: / [\n", indent_space + indent_delta, ""); - for (size_t i = 0; i < array_len; i++) { - QCBORDecode_EnterArray(context, &item); - if (item.val.uCount != 3) { - return TEEP_ERR_INVALID_LENGTH; - } - printf("%*s[\n", indent_space + 2 * indent_delta, ""); - QCBORDecode_EnterBstrWrapped(context, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL); - printf("%*s/ protected: / << ", indent_space + 3 * indent_delta, ""); - teep_print_cose_header(context, indent_space + 3 * indent_delta, indent_delta); - printf(" >>,\n"); - QCBORDecode_ExitBstrWrapped(context); - printf("%*s/ unprotected: / ", indent_space + 3 * indent_delta, ""); - teep_print_cose_header(context, indent_space + 3 * indent_delta, indent_delta); - printf(",\n"); - printf("%*s/ signature: / ", indent_space + 3 * indent_delta, ""); - UsefulBufC signature; - QCBORDecode_GetByteString(context, &signature); - teep_print_hex(signature.ptr, signature.len); - printf("\n%*s]", indent_space + 2 * indent_delta, ""); - QCBORDecode_ExitArray(context); - if (i + 1 < array_len) { - printf(","); - } - printf("\n"); - } - printf("%*s]", indent_space + indent_delta, ""); - QCBORDecode_ExitArray(context); - } - else { - return TEEP_ERR_INVALID_TYPE_OF_VALUE; - } - - QCBORDecode_ExitArray(context); - printf("\n]"); - - if (cose_tag != 0) { - printf(")"); - } - printf("\n"); - - return TEEP_SUCCESS; -} - -teep_err_t teep_print_cose_usefulbufc(UsefulBufC cose, - const uint32_t indent_space, - const uint32_t indent_delta, - TeepLabelToStr label_to_str) -{ - QCBORDecodeContext context; - QCBORDecode_Init(&context, cose, QCBOR_DECODE_MODE_NORMAL); - - teep_err_t result = teep_print_cose(&context, indent_space, indent_delta, label_to_str); - - QCBORError qcbor_err = QCBORDecode_Finish(&context); - if (qcbor_err != QCBOR_SUCCESS) { - printf("qcbor_err = %u\n", qcbor_err); - return TEEP_ERR_FATAL; - } - return result; -} - -teep_err_t teep_print_cose_eat(UsefulBufC cose_eat, - const uint32_t indent_space, - const uint32_t indent_delta) -{ - return teep_print_cose_usefulbufc(cose_eat, indent_space, indent_delta, teep_eat_claim_label_to_str); -} - teep_err_t teep_print_cose_teep_message(UsefulBufC cose_teep_message, const uint32_t indent_space, const uint32_t indent_delta) diff --git a/testfiles/tam_server.sh b/testfiles/tam_server.sh index 4ff5d26..6a28983 100755 --- a/testfiles/tam_server.sh +++ b/testfiles/tam_server.sh @@ -16,12 +16,8 @@ do esac done -QUERY_REQUEST_ERR0_FILE="${DIR}/query_request.cbor" -QUERY_REQUEST_ERR1_FILE="${DIR}/query_request_err1_cose.cbor" -QUERY_REQUEST_ERR2_FILE="${DIR}/query_request_err2_cose.cbor" -QUERY_REQUEST_ERR3_FILE="${DIR}/query_request_err3_cose.cbor" -QUERY_REQUEST_FILE="${DIR}/query_request_cose.cbor" -UPDATE_FILE="${DIR}/update_cose.cbor" +QUERY_REQUEST_FILE="${DIR}/query_request.tam.cose" +UPDATE_FILE="${DIR}/update.tam.cose" function send_teep_cbor { # send_teep_cbor PATH_TO_CBOR_FILE @@ -49,15 +45,6 @@ function send_teep_cbor { echo -ne "\nSend TEEP/HTTP Update with illegal timing.\n" send_teep_cbor $UPDATE_FILE -echo -ne "\nSend TEEP/HTTP QueryRequest without cose.\n" -send_teep_cbor $QUERY_REQUEST_ERR0_FILE - -echo -ne "\nSend TEEP/HTTP QueryRequest with versions = [1].\n" -send_teep_cbor $QUERY_REQUEST_ERR1_FILE - -echo -ne "\nSend TEEP/HTTP QueryRequest with ciphersuites = {enc: a256gcm}.\n" -send_teep_cbor $QUERY_REQUEST_ERR2_FILE - echo -ne "\nSend TEEP/HTTP QueryRequest.\n" send_teep_cbor $QUERY_REQUEST_FILE