Peter Huewe | d5a36f6 | 2018-06-12 00:59:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-2 */ |
Philip Tricca | 21fa321 | 2018-02-19 06:43:56 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 - 2018 Intel Corporation |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 4 | * All rights reserved. |
Philip Tricca | 21fa321 | 2018-02-19 06:43:56 -0800 | [diff] [blame] | 5 | */ |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 6 | |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 7 | #include <inttypes.h> |
| 8 | #include <limits.h> |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 9 | #include <stdio.h> |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 10 | #include <stdlib.h> |
Safayet Ahmed | 1716129 | 2018-06-18 13:22:16 -0400 | [diff] [blame] | 11 | #include <string.h> |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 12 | #include <inttypes.h> |
David J. Maria | 06db575 | 2018-06-19 14:52:37 -0700 | [diff] [blame] | 13 | |
| 14 | #ifndef _WIN32 |
| 15 | #include <sys/time.h> |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 16 | #include <unistd.h> |
David J. Maria | 06db575 | 2018-06-19 14:52:37 -0700 | [diff] [blame] | 17 | #endif |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 18 | |
Philip Tricca | 8ffd3c4 | 2018-03-09 16:27:24 -0800 | [diff] [blame] | 19 | #include "tss2_mu.h" |
Philip Tricca | 8ffd3c4 | 2018-03-09 16:27:24 -0800 | [diff] [blame] | 20 | #include "tss2_tcti_mssim.h" |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 21 | |
| 22 | #include "tcti-mssim.h" |
| 23 | #include "tcti-common.h" |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 24 | #include "util/key-value-parse.h" |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 25 | #define LOGMODULE tcti |
Philip Tricca | a7c51ce | 2018-03-10 18:28:25 -0800 | [diff] [blame] | 26 | #include "util/log.h" |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 27 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 28 | /* |
| 29 | * This function wraps the "up-cast" of the opaque TCTI context type to the |
| 30 | * type for the mssim TCTI context. The only safeguard we have to ensure this |
dantpm | 5358e37 | 2018-04-06 15:13:06 -0700 | [diff] [blame] | 31 | * operation is possible is the magic number in the mssim TCTI context. |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 32 | * If passed a NULL context, or the magic number check fails, this function |
dantpm | 5358e37 | 2018-04-06 15:13:06 -0700 | [diff] [blame] | 33 | * will return NULL. |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 34 | */ |
| 35 | TSS2_TCTI_MSSIM_CONTEXT* |
| 36 | tcti_mssim_context_cast (TSS2_TCTI_CONTEXT *tcti_ctx) |
| 37 | { |
| 38 | if (tcti_ctx != NULL && TSS2_TCTI_MAGIC (tcti_ctx) == TCTI_MSSIM_MAGIC) { |
| 39 | return (TSS2_TCTI_MSSIM_CONTEXT*)tcti_ctx; |
| 40 | } |
| 41 | return NULL; |
| 42 | } |
| 43 | /* |
| 44 | * This function down-casts the mssim TCTI context to the common context |
| 45 | * defined in the tcti-common module. |
| 46 | */ |
| 47 | TSS2_TCTI_COMMON_CONTEXT* |
| 48 | tcti_mssim_down_cast (TSS2_TCTI_MSSIM_CONTEXT *tcti_mssim) |
| 49 | { |
| 50 | if (tcti_mssim == NULL) { |
| 51 | return NULL; |
| 52 | } |
| 53 | return &tcti_mssim->common; |
| 54 | } |
| 55 | /* |
| 56 | * This function is for sending one of the MS_SIM_* platform commands to the |
dantpm | 5358e37 | 2018-04-06 15:13:06 -0700 | [diff] [blame] | 57 | * Microsoft TPM2 simulator. These are sent over the platform socket. |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 58 | */ |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 59 | TSS2_RC tcti_platform_command ( |
| 60 | TSS2_TCTI_CONTEXT *tctiContext, |
| 61 | UINT32 cmd) |
| 62 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 63 | TSS2_TCTI_MSSIM_CONTEXT *tcti_mssim = tcti_mssim_context_cast (tctiContext); |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 64 | uint8_t buf [sizeof (cmd)] = { 0 }; |
| 65 | UINT32 rsp = 0; |
| 66 | TSS2_RC rc = TSS2_RC_SUCCESS; |
| 67 | int ret; |
| 68 | ssize_t read_ret; |
| 69 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 70 | if (tcti_mssim == NULL) { |
| 71 | return TSS2_TCTI_RC_BAD_CONTEXT; |
| 72 | } |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 73 | rc = Tss2_MU_UINT32_Marshal (cmd, buf, sizeof (cmd), NULL); |
| 74 | if (rc != TSS2_RC_SUCCESS) { |
| 75 | LOG_ERROR ("Failed to marshal platform command %" PRIu32 ", rc: 0x%" |
| 76 | PRIx32, cmd, rc); |
| 77 | return rc; |
| 78 | } |
| 79 | |
| 80 | LOGBLOB_DEBUG(buf, sizeof (cmd), "Sending %zu bytes to socket %" PRIu32 |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 81 | ":", sizeof (cmd), tcti_mssim->platform_sock); |
| 82 | ret = write_all (tcti_mssim->platform_sock, buf, sizeof (cmd)); |
Andreas Fuchs | 450cf0b | 2018-03-29 15:48:27 +0200 | [diff] [blame] | 83 | if (ret < (ssize_t) sizeof (cmd)) { |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 84 | LOG_ERROR("Failed to send platform command %d with error: %d", |
| 85 | cmd, ret); |
| 86 | return TSS2_TCTI_RC_IO_ERROR; |
| 87 | } |
| 88 | |
David J. Maria | 06db575 | 2018-06-19 14:52:37 -0700 | [diff] [blame] | 89 | #ifdef _WIN32 |
| 90 | read_ret = recv (tcti_mssim->platform_sock, (char *) buf, sizeof (buf), 0); |
Andreas Fuchs | 450cf0b | 2018-03-29 15:48:27 +0200 | [diff] [blame] | 91 | if (read_ret < (ssize_t) sizeof (buf)) { |
David J. Maria | 06db575 | 2018-06-19 14:52:37 -0700 | [diff] [blame] | 92 | LOG_ERROR ("Failed to get response to platform command, errno %d: %s", |
| 93 | WSAGetLastError(), strerror (WSAGetLastError())); |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 94 | return TSS2_TCTI_RC_IO_ERROR; |
| 95 | } |
David J. Maria | 06db575 | 2018-06-19 14:52:37 -0700 | [diff] [blame] | 96 | #else |
| 97 | read_ret = read(tcti_mssim->platform_sock, buf, sizeof (buf)); |
| 98 | if (read_ret < (ssize_t) sizeof (buf)) { |
| 99 | LOG_ERROR ("Failed to get response to platform command, errno %d: %s", |
| 100 | errno, strerror (errno)); |
| 101 | return TSS2_TCTI_RC_IO_ERROR; |
| 102 | } |
| 103 | #endif |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 104 | LOGBLOB_DEBUG (buf, sizeof (buf), "Received %zu bytes from socket 0x%" |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 105 | PRIx32 ":", read_ret, tcti_mssim->platform_sock); |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 106 | rc = Tss2_MU_UINT32_Unmarshal (buf, sizeof (rsp), NULL, &rsp); |
| 107 | if (rc != TSS2_RC_SUCCESS) { |
| 108 | LOG_ERROR ("Failed to unmarshal response to platform command. rc: 0x%" |
| 109 | PRIx32, rc); |
| 110 | return rc; |
| 111 | } |
| 112 | if (rsp != 0) { |
| 113 | LOG_INFO ("Platform command failed with error: %" PRIu32, rsp); |
| 114 | return TSS2_TCTI_RC_IO_ERROR; |
| 115 | } |
| 116 | return rc; |
| 117 | } |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 118 | /* |
| 119 | * This function sends the special TPM_SESSION_END message over the provided |
| 120 | * socket. |
| 121 | */ |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 122 | TSS2_RC |
| 123 | send_sim_session_end ( |
Philip Tricca | bdc6945 | 2018-02-21 12:45:33 -0800 | [diff] [blame] | 124 | SOCKET sock) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 125 | { |
Philip Tricca | bdc6945 | 2018-02-21 12:45:33 -0800 | [diff] [blame] | 126 | uint8_t buf [4] = { 0, }; |
| 127 | TSS2_RC rc; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 128 | |
Philip Tricca | bdc6945 | 2018-02-21 12:45:33 -0800 | [diff] [blame] | 129 | rc = Tss2_MU_UINT32_Marshal (TPM_SESSION_END, buf, sizeof (buf), NULL); |
| 130 | if (rc == TSS2_RC_SUCCESS) { |
| 131 | return rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 132 | } |
Philip Tricca | 9bc025d | 2018-03-05 15:34:06 -0800 | [diff] [blame] | 133 | return socket_xmit_buf (sock, buf, sizeof (buf)); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Philip Tricca | 72fba0b | 2018-02-21 14:14:32 -0800 | [diff] [blame] | 136 | /* |
dantpm | 5358e37 | 2018-04-06 15:13:06 -0700 | [diff] [blame] | 137 | * This function is used to send the simulator a sort of command message |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 138 | * that tells it we're about to send it a TPM command. This requires that |
| 139 | * we first send it a 4 byte code that's defined by the simulator. Then |
| 140 | * another byte identifying the locality and finally the size of the TPM |
| 141 | * command buffer that we're about to send. After these 9 bytes are sent |
| 142 | * the simulator will accept a TPM command buffer. |
| 143 | */ |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 144 | #define SIM_CMD_SIZE (sizeof (UINT32) + sizeof (UINT8) + sizeof (UINT32)) |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 145 | TSS2_RC |
| 146 | send_sim_cmd_setup ( |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 147 | TSS2_TCTI_MSSIM_CONTEXT *tcti_mssim, |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 148 | UINT32 size) |
| 149 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 150 | TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_mssim_down_cast (tcti_mssim); |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 151 | uint8_t buf [SIM_CMD_SIZE] = { 0 }; |
| 152 | size_t offset = 0; |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 153 | TSS2_RC rc; |
| 154 | |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 155 | rc = Tss2_MU_UINT32_Marshal (MS_SIM_TPM_SEND_COMMAND, |
| 156 | buf, |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 157 | sizeof (buf), |
| 158 | &offset); |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 159 | if (rc != TSS2_RC_SUCCESS) { |
| 160 | return rc; |
| 161 | } |
| 162 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 163 | rc = Tss2_MU_UINT8_Marshal (tcti_common->locality, |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 164 | buf, |
| 165 | sizeof (buf), |
| 166 | &offset); |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 167 | if (rc != TSS2_RC_SUCCESS) { |
| 168 | return rc; |
| 169 | } |
| 170 | |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 171 | rc = Tss2_MU_UINT32_Marshal (size, buf, sizeof (buf), &offset); |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 172 | if (rc != TSS2_RC_SUCCESS) { |
| 173 | return rc; |
| 174 | } |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 175 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 176 | return socket_xmit_buf (tcti_mssim->tpm_sock, buf, sizeof (buf)); |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 179 | TSS2_RC |
Philip Tricca | addad78 | 2018-03-09 11:42:05 -0800 | [diff] [blame] | 180 | tcti_mssim_transmit ( |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 181 | TSS2_TCTI_CONTEXT *tcti_ctx, |
| 182 | size_t size, |
| 183 | const uint8_t *cmd_buf) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 184 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 185 | TSS2_TCTI_MSSIM_CONTEXT *tcti_mssim = tcti_mssim_context_cast (tcti_ctx); |
| 186 | TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_mssim_down_cast (tcti_mssim); |
Andreas Fuchs | 1c4a811 | 2018-03-29 16:58:07 +0200 | [diff] [blame] | 187 | tpm_header_t header; |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 188 | TSS2_RC rc; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 189 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 190 | if (tcti_mssim == NULL) { |
| 191 | return TSS2_TCTI_RC_BAD_CONTEXT; |
| 192 | } |
| 193 | rc = tcti_common_transmit_checks (tcti_common, cmd_buf); |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 194 | if (rc != TSS2_RC_SUCCESS) { |
| 195 | return rc; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 196 | } |
Philip Tricca | 9d4e41e | 2018-03-27 08:22:54 -0700 | [diff] [blame] | 197 | rc = header_unmarshal (cmd_buf, &header); |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 198 | if (rc != TSS2_RC_SUCCESS) { |
| 199 | return rc; |
Andreas Fuchs | f8a6662 | 2017-11-29 14:27:25 +0100 | [diff] [blame] | 200 | } |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 201 | if (header.size != size) { |
Philip Tricca | 72fba0b | 2018-02-21 14:14:32 -0800 | [diff] [blame] | 202 | LOG_ERROR ("Buffer size parameter: %zu, and TPM2 command header size " |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 203 | "field: %" PRIu32 " disagree.", size, header.size); |
Philip Tricca | 72fba0b | 2018-02-21 14:14:32 -0800 | [diff] [blame] | 204 | return TSS2_TCTI_RC_BAD_VALUE; |
| 205 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 206 | |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 207 | LOG_DEBUG ("Sending command with TPM_CC 0x%" PRIx32 " and size %" PRIu32, |
| 208 | header.code, header.size); |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 209 | rc = send_sim_cmd_setup (tcti_mssim, header.size); |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 210 | if (rc != TSS2_RC_SUCCESS) { |
| 211 | return rc; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 212 | } |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 213 | rc = socket_xmit_buf (tcti_mssim->tpm_sock, cmd_buf, size); |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 214 | if (rc != TSS2_RC_SUCCESS) { |
| 215 | return rc; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 216 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 217 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 218 | tcti_common->state = TCTI_STATE_RECEIVE; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 219 | |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 220 | return rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 223 | TSS2_RC |
Philip Tricca | addad78 | 2018-03-09 11:42:05 -0800 | [diff] [blame] | 224 | tcti_mssim_cancel ( |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 225 | TSS2_TCTI_CONTEXT *tctiContext) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 226 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 227 | TSS2_TCTI_MSSIM_CONTEXT *tcti_mssim = tcti_mssim_context_cast (tctiContext); |
| 228 | TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_mssim_down_cast (tcti_mssim); |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 229 | TSS2_RC rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 230 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 231 | if (tcti_mssim == NULL) { |
| 232 | return TSS2_TCTI_RC_BAD_CONTEXT; |
| 233 | } |
| 234 | rc = tcti_common_cancel_checks (tcti_common); |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 235 | if (rc != TSS2_RC_SUCCESS) { |
| 236 | return rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 237 | } |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 238 | rc = tcti_platform_command (tctiContext, MS_SIM_CANCEL_ON); |
Philip Tricca | fa14abe | 2018-03-05 21:14:54 -0800 | [diff] [blame] | 239 | if (rc != TSS2_RC_SUCCESS) { |
| 240 | return rc; |
| 241 | } |
| 242 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 243 | tcti_common->state = TCTI_STATE_TRANSMIT; |
| 244 | tcti_mssim->cancel = 1; |
Tadeusz Struk | 262c1ef | 2018-03-08 09:56:34 -0800 | [diff] [blame] | 245 | |
Philip Tricca | fa14abe | 2018-03-05 21:14:54 -0800 | [diff] [blame] | 246 | return rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 249 | TSS2_RC |
Philip Tricca | addad78 | 2018-03-09 11:42:05 -0800 | [diff] [blame] | 250 | tcti_mssim_set_locality ( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 251 | TSS2_TCTI_CONTEXT *tctiContext, |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 252 | uint8_t locality) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 253 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 254 | TSS2_TCTI_MSSIM_CONTEXT *tcti_mssim = tcti_mssim_context_cast (tctiContext); |
| 255 | TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_mssim_down_cast (tcti_mssim); |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 256 | TSS2_RC rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 257 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 258 | if (tcti_mssim == NULL) { |
| 259 | return TSS2_TCTI_RC_BAD_CONTEXT; |
| 260 | } |
| 261 | rc = tcti_common_set_locality_checks (tcti_common); |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 262 | if (rc != TSS2_RC_SUCCESS) { |
| 263 | return rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 264 | } |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 265 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 266 | tcti_common->locality = locality; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 267 | return TSS2_RC_SUCCESS; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 270 | TSS2_RC |
Philip Tricca | addad78 | 2018-03-09 11:42:05 -0800 | [diff] [blame] | 271 | tcti_mssim_get_poll_handles ( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 272 | TSS2_TCTI_CONTEXT *tctiContext, |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 273 | TSS2_TCTI_POLL_HANDLE *handles, |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 274 | size_t *num_handles) |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 275 | { |
Andreas Fuchs | baeb2be | 2018-03-29 15:39:50 +0200 | [diff] [blame] | 276 | (void)(tctiContext); |
| 277 | (void)(handles); |
| 278 | (void)(num_handles); |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 279 | return TSS2_TCTI_RC_NOT_IMPLEMENTED; |
| 280 | } |
| 281 | |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 282 | void |
Philip Tricca | addad78 | 2018-03-09 11:42:05 -0800 | [diff] [blame] | 283 | tcti_mssim_finalize( |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 284 | TSS2_TCTI_CONTEXT *tctiContext) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 285 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 286 | TSS2_TCTI_MSSIM_CONTEXT *tcti_mssim = tcti_mssim_context_cast (tctiContext); |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 287 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 288 | if (tcti_mssim == NULL) { |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 289 | return; |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame] | 290 | } |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 291 | send_sim_session_end (tcti_mssim->platform_sock); |
| 292 | send_sim_session_end (tcti_mssim->tpm_sock); |
| 293 | socket_close (&tcti_mssim->platform_sock); |
| 294 | socket_close (&tcti_mssim->tpm_sock); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 295 | } |
| 296 | |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 297 | TSS2_RC |
Philip Tricca | addad78 | 2018-03-09 11:42:05 -0800 | [diff] [blame] | 298 | tcti_mssim_receive ( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 299 | TSS2_TCTI_CONTEXT *tctiContext, |
| 300 | size_t *response_size, |
| 301 | unsigned char *response_buffer, |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 302 | int32_t timeout) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 303 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 304 | TSS2_TCTI_MSSIM_CONTEXT *tcti_mssim = tcti_mssim_context_cast (tctiContext); |
| 305 | TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_mssim_down_cast (tcti_mssim); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 306 | TSS2_RC rc; |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 307 | UINT32 trash; |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 308 | int ret; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 309 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 310 | if (tcti_mssim == NULL) { |
| 311 | return TSS2_TCTI_RC_BAD_CONTEXT; |
| 312 | } |
| 313 | rc = tcti_common_receive_checks (tcti_common, response_size); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 314 | if (rc != TSS2_RC_SUCCESS) { |
Philip Tricca | 5477c2a | 2018-03-07 15:39:28 -0800 | [diff] [blame] | 315 | return rc; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Philip Tricca | 9eb86d5 | 2018-03-05 15:50:05 -0800 | [diff] [blame] | 318 | if (timeout != TSS2_TCTI_TIMEOUT_BLOCK) { |
| 319 | LOG_WARNING ("Asynchronous I/O not implemented. The 'timeout' " |
| 320 | "parameter must be TSS2_TCTI_TIMEOUT_BLOCK."); |
| 321 | return TSS2_TCTI_RC_BAD_VALUE; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 322 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 323 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 324 | if (tcti_common->header.size == 0) { |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 325 | /* Receive the size of the response. */ |
Philip Tricca | 7ac3640 | 2018-03-05 16:21:28 -0800 | [diff] [blame] | 326 | uint8_t size_buf [sizeof (UINT32)]; |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 327 | ret = socket_recv_buf (tcti_mssim->tpm_sock, size_buf, sizeof (UINT32)); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 328 | if (ret != sizeof (UINT32)) { |
| 329 | rc = TSS2_TCTI_RC_IO_ERROR; |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 330 | goto out; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 331 | } |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 332 | |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 333 | rc = Tss2_MU_UINT32_Unmarshal (size_buf, |
| 334 | sizeof (size_buf), |
| 335 | 0, |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 336 | &tcti_common->header.size); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 337 | if (rc != TSS2_RC_SUCCESS) { |
Philip Tricca | 7ac3640 | 2018-03-05 16:21:28 -0800 | [diff] [blame] | 338 | LOG_WARNING ("Failed to unmarshal size from tpm2 simulator " |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 339 | "protocol: 0x%" PRIu32, rc); |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 340 | goto out; |
Philip Tricca | 7ac3640 | 2018-03-05 16:21:28 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 343 | LOG_DEBUG ("response size: %" PRIu32, tcti_common->header.size); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 346 | *response_size = tcti_common->header.size; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 347 | if (response_buffer == NULL) { |
Philip Tricca | 5477c2a | 2018-03-07 15:39:28 -0800 | [diff] [blame] | 348 | return TSS2_RC_SUCCESS; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 349 | } |
| 350 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 351 | if (*response_size < tcti_common->header.size) { |
| 352 | *response_size = tcti_common->header.size; |
Philip Tricca | 5477c2a | 2018-03-07 15:39:28 -0800 | [diff] [blame] | 353 | return TSS2_TCTI_RC_INSUFFICIENT_BUFFER; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 354 | } |
| 355 | |
Philip Tricca | 7ac3640 | 2018-03-05 16:21:28 -0800 | [diff] [blame] | 356 | /* Receive the TPM response. */ |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 357 | LOG_DEBUG ("Reading response of size %" PRIu32, tcti_common->header.size); |
| 358 | ret = socket_recv_buf (tcti_mssim->tpm_sock, |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 359 | (unsigned char *)response_buffer, |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 360 | tcti_common->header.size); |
Philip Tricca | 097cae6 | 2018-04-08 13:42:29 -0700 | [diff] [blame] | 361 | if (ret < (ssize_t)tcti_common->header.size) { |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 362 | rc = TSS2_TCTI_RC_IO_ERROR; |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 363 | goto out; |
Philip Tricca | 7ac3640 | 2018-03-05 16:21:28 -0800 | [diff] [blame] | 364 | } |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 365 | LOGBLOB_DEBUG(response_buffer, tcti_common->header.size, |
Philip Tricca | 10b8116 | 2018-03-06 14:59:56 -0800 | [diff] [blame] | 366 | "Response buffer received:"); |
Philip Tricca | 7ac3640 | 2018-03-05 16:21:28 -0800 | [diff] [blame] | 367 | |
| 368 | /* Receive the appended four bytes of 0's */ |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 369 | ret = socket_recv_buf (tcti_mssim->tpm_sock, |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 370 | (unsigned char *)&trash, |
| 371 | 4); |
| 372 | if (ret != 4) { |
| 373 | rc = TSS2_TCTI_RC_IO_ERROR; |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 374 | goto out; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 375 | } |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 376 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 377 | if (tcti_mssim->cancel) { |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 378 | rc = tcti_platform_command (tctiContext, MS_SIM_CANCEL_OFF); |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 379 | tcti_mssim->cancel = 0; |
Tadeusz Struk | 262c1ef | 2018-03-08 09:56:34 -0800 | [diff] [blame] | 380 | } |
Philip Tricca | 5477c2a | 2018-03-07 15:39:28 -0800 | [diff] [blame] | 381 | /* |
| 382 | * Executing code beyond this point transitions the state machine to |
| 383 | * TRANSMIT. Another call to this function will not be possible until |
| 384 | * another command is sent to the TPM. |
| 385 | */ |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 386 | out: |
| 387 | tcti_common->header.size = 0; |
| 388 | tcti_common->state = TCTI_STATE_TRANSMIT; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 389 | |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 390 | return rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 393 | /** |
| 394 | * This function sends the Microsoft simulator the MS_SIM_POWER_ON and |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 395 | * MS_SIM_NV_ON commands using the platform command mechanism. Without |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 396 | * these the simulator will respond with zero sized buffer which causes |
dantpm | 5358e37 | 2018-04-06 15:13:06 -0700 | [diff] [blame] | 397 | * the TSS to freak out. Sending this command more than once is harmless, |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 398 | * so it's advisable to call this function as part of the TCTI context |
| 399 | * initialization just to be sure. |
| 400 | * |
| 401 | * NOTE: The caller will still need to call Tss2_Sys_Startup. If they |
| 402 | * don't, an error will be returned from each call till they do but |
Andreas Fuchs | be5899d | 2017-11-13 17:14:36 +0100 | [diff] [blame] | 403 | * the error will at least be meaningful (TPM2_RC_INITIALIZE). |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 404 | */ |
Philip Tricca | dc4ff84 | 2018-03-04 21:59:48 -0800 | [diff] [blame] | 405 | static TSS2_RC |
| 406 | simulator_setup ( |
| 407 | TSS2_TCTI_CONTEXT *tctiContext) |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 408 | { |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 409 | TSS2_RC rc; |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 410 | |
Philip Tricca | dc4ff84 | 2018-03-04 21:59:48 -0800 | [diff] [blame] | 411 | LOG_TRACE ("Initializing TCTI context 0x%" PRIxPTR, |
| 412 | (uintptr_t)tctiContext); |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 413 | rc = tcti_platform_command (tctiContext, MS_SIM_POWER_ON); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 414 | if (rc != TSS2_RC_SUCCESS) { |
Philip Tricca | dc4ff84 | 2018-03-04 21:59:48 -0800 | [diff] [blame] | 415 | LOG_WARNING ("Failed to send MS_SIM_POWER_ON platform command."); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 416 | return rc; |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 417 | } |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 418 | |
Tadeusz Struk | 105c1a0 | 2018-03-09 11:10:36 -0800 | [diff] [blame] | 419 | rc = tcti_platform_command (tctiContext, MS_SIM_NV_ON); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 420 | if (rc != TSS2_RC_SUCCESS) { |
Philip Tricca | dc4ff84 | 2018-03-04 21:59:48 -0800 | [diff] [blame] | 421 | LOG_WARNING ("Failed to send MS_SIM_NV_ON platform command."); |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 422 | } |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 423 | |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 424 | return rc; |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 427 | /* |
| 428 | * This is a utility function to extract a TCP port number from a string. |
| 429 | * The string must be 6 characters long. If the supplied string contains an |
| 430 | * invalid port number then 0 is returned. |
| 431 | */ |
| 432 | static uint16_t |
| 433 | string_to_port (char port_str[6]) |
| 434 | { |
| 435 | uint32_t port = 0; |
| 436 | |
| 437 | if (sscanf (port_str, "%" SCNu32, &port) == EOF || port > UINT16_MAX) { |
| 438 | return 0; |
| 439 | } |
| 440 | return port; |
| 441 | } |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 442 | /* |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 443 | * This function is a callback conforming to the KeyValueFunc prototype. It |
| 444 | * is called by the key-value-parse module for each key / value pair extracted |
| 445 | * from the configuration string. Its sole purpose is to identify valid keys |
| 446 | * from the conf string and to store their corresponding values in the |
| 447 | * mssim_conf_t structure which is passed through the 'user_data' parameter. |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 448 | */ |
| 449 | TSS2_RC |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 450 | mssim_kv_callback (const key_value_t *key_value, |
| 451 | void *user_data) |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 452 | { |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 453 | mssim_conf_t *mssim_conf = (mssim_conf_t*)user_data; |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 454 | |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 455 | LOG_TRACE ("key_value: 0x%" PRIxPTR " and user_data: 0x%" PRIxPTR, |
| 456 | (uintptr_t)key_value, (uintptr_t)user_data); |
| 457 | if (key_value == NULL || user_data == NULL) { |
| 458 | LOG_WARNING ("%s passed NULL parameter", __func__); |
| 459 | return TSS2_TCTI_RC_GENERAL_FAILURE; |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 460 | } |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 461 | LOG_DEBUG ("key: %s / value: %s\n", key_value->key, key_value->value); |
| 462 | if (strcmp (key_value->key, "host") == 0) { |
| 463 | mssim_conf->host = key_value->value; |
| 464 | return TSS2_RC_SUCCESS; |
| 465 | } else if (strcmp (key_value->key, "port") == 0) { |
| 466 | mssim_conf->port = string_to_port (key_value->value); |
| 467 | if (mssim_conf->port == 0) { |
| 468 | return TSS2_TCTI_RC_BAD_VALUE; |
| 469 | } |
| 470 | return TSS2_RC_SUCCESS; |
| 471 | } else { |
| 472 | return TSS2_TCTI_RC_BAD_VALUE; |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 473 | } |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 474 | } |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 475 | void |
Philip Tricca | addad78 | 2018-03-09 11:42:05 -0800 | [diff] [blame] | 476 | tcti_mssim_init_context_data ( |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 477 | TSS2_TCTI_COMMON_CONTEXT *tcti_common) |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 478 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 479 | TSS2_TCTI_MAGIC (tcti_common) = TCTI_MSSIM_MAGIC; |
| 480 | TSS2_TCTI_VERSION (tcti_common) = TCTI_VERSION; |
| 481 | TSS2_TCTI_TRANSMIT (tcti_common) = tcti_mssim_transmit; |
| 482 | TSS2_TCTI_RECEIVE (tcti_common) = tcti_mssim_receive; |
| 483 | TSS2_TCTI_FINALIZE (tcti_common) = tcti_mssim_finalize; |
| 484 | TSS2_TCTI_CANCEL (tcti_common) = tcti_mssim_cancel; |
| 485 | TSS2_TCTI_GET_POLL_HANDLES (tcti_common) = tcti_mssim_get_poll_handles; |
| 486 | TSS2_TCTI_SET_LOCALITY (tcti_common) = tcti_mssim_set_locality; |
| 487 | TSS2_TCTI_MAKE_STICKY (tcti_common) = tcti_make_sticky_not_implemented; |
| 488 | tcti_common->state = TCTI_STATE_TRANSMIT; |
| 489 | tcti_common->locality = 3; |
| 490 | memset (&tcti_common->header, 0, sizeof (tcti_common->header)); |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 491 | } |
| 492 | /* |
| 493 | * This is an implementation of the standard TCTI initialization function for |
| 494 | * this module. |
| 495 | */ |
| 496 | TSS2_RC |
Philip Tricca | addad78 | 2018-03-09 11:42:05 -0800 | [diff] [blame] | 497 | Tss2_Tcti_Mssim_Init ( |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 498 | TSS2_TCTI_CONTEXT *tctiContext, |
| 499 | size_t *size, |
| 500 | const char *conf) |
| 501 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 502 | TSS2_TCTI_MSSIM_CONTEXT *tcti_mssim = (TSS2_TCTI_MSSIM_CONTEXT*)tctiContext; |
| 503 | TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_mssim_down_cast (tcti_mssim); |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 504 | TSS2_RC rc; |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 505 | char *conf_copy = NULL; |
| 506 | mssim_conf_t mssim_conf = MSSIM_CONF_DEFAULT_INIT; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 507 | |
Philip Tricca | ad8140e | 2018-11-11 15:10:19 -0800 | [diff] [blame] | 508 | if (conf == NULL) { |
Juergen Repp | 5409054 | 2018-09-12 16:25:43 +0200 | [diff] [blame] | 509 | LOG_TRACE ("tctiContext: 0x%" PRIxPTR ", size: 0x%" PRIxPTR "" |
| 510 | " default configuration will be used.", |
| 511 | (uintptr_t)tctiContext, (uintptr_t)size); |
Philip Tricca | ad8140e | 2018-11-11 15:10:19 -0800 | [diff] [blame] | 512 | } else { |
Juergen Repp | 5409054 | 2018-09-12 16:25:43 +0200 | [diff] [blame] | 513 | LOG_TRACE ("tctiContext: 0x%" PRIxPTR ", size: 0x%" PRIxPTR ", conf: %s", |
| 514 | (uintptr_t)tctiContext, (uintptr_t)size, conf); |
Philip Tricca | ad8140e | 2018-11-11 15:10:19 -0800 | [diff] [blame] | 515 | } |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 516 | if (size == NULL) { |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 517 | return TSS2_TCTI_RC_BAD_VALUE; |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 518 | } |
| 519 | if (tctiContext == NULL) { |
| 520 | *size = sizeof (TSS2_TCTI_MSSIM_CONTEXT); |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 521 | return TSS2_RC_SUCCESS; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 524 | if (conf != NULL) { |
| 525 | LOG_TRACE ("conf is not NULL"); |
| 526 | if (strlen (conf) > TCTI_MSSIM_CONF_MAX) { |
| 527 | LOG_WARNING ("Provided conf string exceeds maximum of %u", |
| 528 | TCTI_MSSIM_CONF_MAX); |
| 529 | return TSS2_TCTI_RC_BAD_VALUE; |
| 530 | } |
| 531 | conf_copy = strdup (conf); |
| 532 | if (conf_copy == NULL) { |
| 533 | LOG_ERROR ("Failed to allocate buffer: %s", strerror (errno)); |
| 534 | rc = TSS2_TCTI_RC_GENERAL_FAILURE; |
| 535 | goto fail_out; |
| 536 | } |
| 537 | LOG_DEBUG ("Dup'd conf string to: 0x%" PRIxPTR, |
| 538 | (uintptr_t)conf_copy); |
| 539 | rc = parse_key_value_string (conf_copy, |
| 540 | mssim_kv_callback, |
| 541 | &mssim_conf); |
| 542 | if (rc != TSS2_RC_SUCCESS) { |
| 543 | goto fail_out; |
| 544 | } |
| 545 | } |
| 546 | LOG_DEBUG ("Initializing mssim TCTI with host: %s, port: %" PRIu16, |
| 547 | mssim_conf.host, mssim_conf.port); |
| 548 | |
Tadeusz Struk | c8887d5 | 2018-04-27 11:30:09 -0700 | [diff] [blame] | 549 | tcti_mssim->tpm_sock = -1; |
| 550 | tcti_mssim->platform_sock = -1; |
| 551 | |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 552 | rc = socket_connect (mssim_conf.host, |
| 553 | mssim_conf.port, |
| 554 | &tcti_mssim->tpm_sock); |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 555 | if (rc != TSS2_RC_SUCCESS) { |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 556 | goto fail_out; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 559 | rc = socket_connect (mssim_conf.host, |
| 560 | mssim_conf.port + 1, |
| 561 | &tcti_mssim->platform_sock); |
Philip Tricca | 2bec2a8 | 2018-03-04 21:27:30 -0800 | [diff] [blame] | 562 | if (rc != TSS2_RC_SUCCESS) { |
| 563 | goto fail_out; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 566 | tcti_mssim_init_context_data (tcti_common); |
Philip Tricca | dc4ff84 | 2018-03-04 21:59:48 -0800 | [diff] [blame] | 567 | rc = simulator_setup (tctiContext); |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 568 | if (rc != TSS2_RC_SUCCESS) { |
Philip Tricca | 2bec2a8 | 2018-03-04 21:27:30 -0800 | [diff] [blame] | 569 | goto fail_out; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 570 | } |
| 571 | |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 572 | if (conf_copy != NULL) { |
| 573 | free (conf_copy); |
| 574 | } |
Philip Tricca | 2bec2a8 | 2018-03-04 21:27:30 -0800 | [diff] [blame] | 575 | return TSS2_RC_SUCCESS; |
| 576 | |
| 577 | fail_out: |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 578 | if (conf_copy != NULL) { |
| 579 | free (conf_copy); |
| 580 | } |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 581 | socket_close (&tcti_mssim->tpm_sock); |
| 582 | socket_close (&tcti_mssim->platform_sock); |
Philip Tricca | 2bec2a8 | 2018-03-04 21:27:30 -0800 | [diff] [blame] | 583 | |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 584 | return rc; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 587 | /* public info structure */ |
Andreas Fuchs | 37b45c6 | 2018-03-29 16:54:33 +0200 | [diff] [blame] | 588 | const TSS2_TCTI_INFO tss2_tcti_info = { |
Philip Tricca | df1410e | 2018-04-23 13:07:17 -0700 | [diff] [blame] | 589 | .version = TCTI_VERSION, |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 590 | .name = "tcti-socket", |
| 591 | .description = "TCTI module for communication with the Microsoft TPM2 Simulator.", |
Philip Tricca | e394cff | 2018-04-18 08:36:06 -0700 | [diff] [blame] | 592 | .config_help = "Key / value string in the form \"host=localhost,port=2321\".", |
Philip Tricca | addad78 | 2018-03-09 11:42:05 -0800 | [diff] [blame] | 593 | .init = Tss2_Tcti_Mssim_Init, |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 594 | }; |
| 595 | |
| 596 | const TSS2_TCTI_INFO* |
| 597 | Tss2_Tcti_Info (void) |
| 598 | { |
| 599 | return &tss2_tcti_info; |
| 600 | } |