Javier Almansa Sobrino | cddc000 | 2020-02-10 13:35:37 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (c) 2020, ARM Limited. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #include <inttypes.h> |
| 7 | #include <pta_system.h> |
| 8 | #include <string.h> |
| 9 | #include <ta_tpm_log.h> |
| 10 | #include <tee_internal_api.h> |
| 11 | |
| 12 | #define LOG_SIZE 1024 |
| 13 | |
| 14 | static TEE_Result invoke_system_pta(uint32_t cmd_id, uint32_t param_types, |
| 15 | TEE_Param params[TEE_NUM_PARAMS]) |
| 16 | { |
| 17 | const TEE_UUID system_uuid = PTA_SYSTEM_UUID; |
| 18 | TEE_TASessionHandle session = TEE_HANDLE_NULL; |
| 19 | TEE_Result res = TEE_ERROR_GENERIC; |
| 20 | uint32_t ret_origin = 0; |
| 21 | |
| 22 | res = TEE_OpenTASession(&system_uuid, TEE_TIMEOUT_INFINITE, |
| 23 | 0, NULL, &session, &ret_origin); |
| 24 | if (res != TEE_SUCCESS) |
| 25 | return res; |
| 26 | |
| 27 | res = TEE_InvokeTACommand(session, TEE_TIMEOUT_INFINITE, |
| 28 | cmd_id, param_types, params, &ret_origin); |
| 29 | |
| 30 | TEE_CloseTASession(session); |
| 31 | |
| 32 | return res; |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Test case for the use of right parameters. |
| 37 | */ |
| 38 | static TEE_Result test_with_right_parameters(void) |
| 39 | { |
| 40 | unsigned char buf[LOG_SIZE] = { 0 }; |
| 41 | const uint32_t param_types = TEE_PARAM_TYPES( |
| 42 | TEE_PARAM_TYPE_MEMREF_OUTPUT, |
| 43 | TEE_PARAM_TYPE_NONE, |
| 44 | TEE_PARAM_TYPE_NONE, |
| 45 | TEE_PARAM_TYPE_NONE); |
| 46 | TEE_Param params[TEE_NUM_PARAMS] = { 0 }; |
| 47 | uint32_t field = 0; |
| 48 | unsigned char *digestptr = NULL; |
| 49 | unsigned int i = 0; |
| 50 | |
| 51 | params[0].memref.buffer = (void *)buf; |
| 52 | params[0].memref.size = LOG_SIZE; |
| 53 | |
| 54 | if (invoke_system_pta(PTA_SYSTEM_GET_TPM_EVENT_LOG, |
| 55 | param_types, params) == TEE_SUCCESS) { |
| 56 | DMSG("Received %i bytes of event log", params[0].memref.size); |
| 57 | DMSG("Parsing the event log header:"); |
| 58 | |
| 59 | memcpy(&field, &buf[0], sizeof(uint32_t)); |
| 60 | DMSG("\tPCRIndex = 0x%" PRIx32, field); |
| 61 | |
| 62 | /* |
| 63 | * PCR Index must be 0 on the header. |
| 64 | * Ref. Section 5.3 of TCG EFI Protocol Specification. |
| 65 | * Family 2.0 Level 00 Revision 00.13. March 30, 2016 |
| 66 | */ |
| 67 | if (field != 0) { |
| 68 | EMSG("PCRIndex must be 0"); |
| 69 | return TEE_ERROR_GENERIC; |
| 70 | } |
| 71 | |
| 72 | memcpy(&field, &buf[4], sizeof(uint32_t)); |
| 73 | DMSG("\tEventType = 0x%" PRIx32, field); |
| 74 | |
| 75 | /* |
| 76 | * Event type must be EV_NO_ACTION on the header. |
| 77 | * Ref. Section 5.3 of TCG EFI Protocol Specification. |
| 78 | * Family 2.0 Level 00 Revision 00.13. March 30, 2016 |
| 79 | */ |
| 80 | if (field != 3) { |
| 81 | EMSG("EventType must be 3 (EV_NO_ACTION)"); |
| 82 | return TEE_ERROR_GENERIC; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Header digest must be formed of 20 bytes set to 0. |
| 87 | * Ref. Section 5.3 of TCG EFI Protocol Specification. |
| 88 | * Family 2.0 Level 00 Revision 00.12. March 30, 2016 |
| 89 | */ |
| 90 | digestptr = &buf[8]; |
| 91 | for (i = 0; i < 20; i++) { |
| 92 | if (*(digestptr + i) != 0) { |
| 93 | EMSG("Digest byte %i must be 0", i); |
| 94 | return TEE_ERROR_GENERIC; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | DMSG("--> Digest value passed"); |
| 99 | return TEE_SUCCESS; |
| 100 | } |
| 101 | return TEE_ERROR_ACCESS_DENIED; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Test case for short buffer. |
| 106 | */ |
| 107 | static TEE_Result test_short_buffer(void) |
| 108 | { |
| 109 | unsigned char buf[LOG_SIZE] = { 0 }; |
| 110 | const uint32_t param_types = TEE_PARAM_TYPES( |
| 111 | TEE_PARAM_TYPE_MEMREF_OUTPUT, |
| 112 | TEE_PARAM_TYPE_NONE, |
| 113 | TEE_PARAM_TYPE_NONE, |
| 114 | TEE_PARAM_TYPE_NONE); |
| 115 | TEE_Param params[TEE_NUM_PARAMS] = { 0 }; |
| 116 | unsigned int i = 0; |
| 117 | |
| 118 | params[0].memref.buffer = (void *)buf; |
| 119 | params[0].memref.size = 1; |
| 120 | |
| 121 | if (invoke_system_pta(PTA_SYSTEM_GET_TPM_EVENT_LOG, param_types, |
| 122 | params) != TEE_ERROR_SHORT_BUFFER) { |
| 123 | return TEE_ERROR_GENERIC; |
| 124 | } |
| 125 | |
| 126 | for (i = 0; i < LOG_SIZE; i++) { |
| 127 | if (buf[i] != 0) { |
| 128 | EMSG("Buf is expected to be all zeroed"); |
| 129 | return TEE_ERROR_GENERIC; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return TEE_SUCCESS; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Trusted Application Entry Points |
| 138 | */ |
| 139 | |
| 140 | /* Called each time a new instance is created */ |
| 141 | TEE_Result TA_CreateEntryPoint(void) |
| 142 | { |
| 143 | return TEE_SUCCESS; |
| 144 | } |
| 145 | |
| 146 | /* Called each time an instance is destroyed */ |
| 147 | void TA_DestroyEntryPoint(void) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | /* Called each time a session is opened */ |
| 152 | TEE_Result TA_OpenSessionEntryPoint(uint32_t nParamTypes __unused, |
| 153 | TEE_Param pParams[4] __unused, |
| 154 | void **ppSessionContext __unused) |
| 155 | { |
| 156 | return TEE_SUCCESS; |
| 157 | } |
| 158 | |
| 159 | /* Called each time a session is closed */ |
| 160 | void TA_CloseSessionEntryPoint(void *pSessionContext __unused) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | /* Called when a command is invoked */ |
| 165 | TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext __unused, |
| 166 | uint32_t nCommandID, |
| 167 | uint32_t nParamTypes __unused, |
| 168 | TEE_Param pParams[4] __unused) |
| 169 | { |
| 170 | switch (nCommandID) { |
| 171 | case TA_TPM_TEST_GET_LOG: |
| 172 | return test_with_right_parameters(); |
| 173 | |
| 174 | case TA_TPM_TEST_SHORT_BUF: |
| 175 | return test_short_buffer(); |
| 176 | |
| 177 | default: |
| 178 | return TEE_ERROR_BAD_PARAMETERS; |
| 179 | } |
| 180 | } |