Tamas Ban | afc4f3c | 2024-08-13 11:46:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2024, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Tamas Ban | 628192c | 2024-08-13 13:35:58 +0200 | [diff] [blame] | 8 | #include <argp.h> |
Tamas Ban | afc4f3c | 2024-08-13 11:46:58 +0200 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | #include "dice_protection_environment.h" |
| 13 | #include "tfm_sp_log.h" |
| 14 | |
Tamas Ban | 545bb5c | 2024-08-13 12:00:52 +0200 | [diff] [blame] | 15 | #include "extra_s_tests.h" |
| 16 | #include "test_framework.h" |
| 17 | |
Tamas Ban | afc4f3c | 2024-08-13 11:46:58 +0200 | [diff] [blame] | 18 | #include "cmd.h" |
| 19 | |
Tamas Ban | 545bb5c | 2024-08-13 12:00:52 +0200 | [diff] [blame] | 20 | static struct test_suite_t test_suites[] = { |
| 21 | {®ister_testsuite_extra_s_interface, 0, 0, 0}, |
| 22 | /* End of test suites */ |
| 23 | {0, 0, 0, 0} |
| 24 | }; |
| 25 | |
Tamas Ban | 628192c | 2024-08-13 13:35:58 +0200 | [diff] [blame] | 26 | static char doc[] = "\nDICE Protection Environment (DPE): \n" \ |
| 27 | " - Without any argument it executes the regression test suite\n" \ |
| 28 | " - With 2 arguments it executes a DPE command as below"; |
| 29 | static char args_doc[] = "<ARG1> <ARG2>"; |
| 30 | static struct argp_option options[] = { |
| 31 | { "cbor", 'c', "CBOR_INPUT", 0, "Execute an already CBOR encoded command."}, |
| 32 | { "derive-context", 'd', "RAW_INPUT", 0, "Execute a DeriveContext command."}, |
| 33 | { "certify-key", 'k', "RAW_INPUT", 0, "Execute a CertifyKey command."}, |
| 34 | { "get-cert-chain", 'g', "RAW_INPUT", 0, "Execute a GetCertificateChain command."}, |
| 35 | { "rnd-dpe-cmd", 'r', "RAW_INPUT", 0, "Execute a random supported command."}, |
| 36 | { 0 } |
| 37 | }; |
| 38 | |
| 39 | struct arguments { |
| 40 | enum cmd cmd; |
| 41 | }; |
| 42 | |
| 43 | static error_t parse_opt(int key, char *arg, struct argp_state *state) { |
| 44 | struct arguments *arguments = state->input; |
| 45 | |
| 46 | switch (key) { |
| 47 | case 'c': arguments->cmd = CBOR; break; |
| 48 | case 'd': arguments->cmd = DC; break; |
| 49 | case 'k': arguments->cmd = CK; break; |
| 50 | case 'g': arguments->cmd = GCC; break; |
| 51 | case 'r': arguments->cmd = RND; break; |
| 52 | case ARGP_KEY_ARG: |
| 53 | argp_usage(state); |
| 54 | break; |
| 55 | default: return ARGP_ERR_UNKNOWN; |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
Tamas Ban | 545bb5c | 2024-08-13 12:00:52 +0200 | [diff] [blame] | 60 | |
Tamas Ban | afc4f3c | 2024-08-13 11:46:58 +0200 | [diff] [blame] | 61 | static int read_cmd(const char path[], char *cmd_buf, size_t *cmd_buf_size) |
| 62 | { |
| 63 | FILE *fd; |
| 64 | size_t cmd_size; |
| 65 | |
| 66 | if ((fd = fopen(path, "r")) == NULL) { |
| 67 | printf("ERROR: File (%s) cannot be opened.\n", path); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | fseek(fd, 0, SEEK_END); |
| 72 | cmd_size = ftell(fd); |
| 73 | rewind(fd); |
| 74 | |
| 75 | if (*cmd_buf_size < cmd_size) { |
| 76 | printf("ERROR: cmd_buf is too small\n"); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | for (size_t i = 0; i < cmd_size; ++i) { |
| 81 | cmd_buf[i] = fgetc(fd); |
| 82 | } |
| 83 | *cmd_buf_size = cmd_size; |
| 84 | |
| 85 | fclose(fd); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int main(int argc, char **argv) |
| 91 | { |
| 92 | int context_handle; |
| 93 | int ret; |
| 94 | char cmd_in_buf[4096] = {0}; |
| 95 | size_t cmd_in_size = sizeof(cmd_in_buf); |
| 96 | dpe_error_t err; |
Tamas Ban | 628192c | 2024-08-13 13:35:58 +0200 | [diff] [blame] | 97 | struct arguments arguments; |
| 98 | struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 }; |
Tamas Ban | afc4f3c | 2024-08-13 11:46:58 +0200 | [diff] [blame] | 99 | |
| 100 | dpe_lib_init(&context_handle); |
Tamas Ban | 628192c | 2024-08-13 13:35:58 +0200 | [diff] [blame] | 101 | argp_parse(&argp, argc, argv, 0, 0, &arguments); |
Tamas Ban | afc4f3c | 2024-08-13 11:46:58 +0200 | [diff] [blame] | 102 | |
Tamas Ban | 545bb5c | 2024-08-13 12:00:52 +0200 | [diff] [blame] | 103 | if (argc == 1) { |
| 104 | /*************** Invoked without any command line parameter ************/ |
| 105 | printf("Execute DPE regression test\n"); |
| 106 | /* Regression test prints the result to the console */ |
| 107 | (void)run_test("DPE Regression", test_suites); |
| 108 | |
Tamas Ban | 628192c | 2024-08-13 13:35:58 +0200 | [diff] [blame] | 109 | } else if (argc == 3) { |
| 110 | /****************** Input params are provided *************************/ |
| 111 | LOG_INFFMT("Execute DPE API test (%s %s)\n", argv[1], argv[2]); |
| 112 | ret = read_cmd(argv[2], cmd_in_buf, &cmd_in_size); |
Tamas Ban | afc4f3c | 2024-08-13 11:46:58 +0200 | [diff] [blame] | 113 | if (ret < 0) { |
| 114 | exit(1); |
| 115 | } |
| 116 | |
Tamas Ban | 628192c | 2024-08-13 13:35:58 +0200 | [diff] [blame] | 117 | err = exec_dpe_cmd(arguments.cmd, cmd_in_buf, cmd_in_size, &context_handle); |
Tamas Ban | afc4f3c | 2024-08-13 11:46:58 +0200 | [diff] [blame] | 118 | if (err != DPE_NO_ERROR) { |
| 119 | printf("DPE command decode/execution failed (%d)\n", ret); |
| 120 | exit(1); |
| 121 | } |
Tamas Ban | afc4f3c | 2024-08-13 11:46:58 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | exit(0); |
| 125 | } |