Philip Tricca | 21fa321 | 2018-02-19 06:43:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 - 2018 Intel Corporation |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 | * THE POSSIBILITY OF SUCH DAMAGE. |
Philip Tricca | 21fa321 | 2018-02-19 06:43:56 -0800 | [diff] [blame] | 26 | */ |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 27 | |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 28 | #include <inttypes.h> |
| 29 | #include <limits.h> |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 30 | #include <stdio.h> |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 31 | #include <stdlib.h> |
Armin Kuster | ace89dd | 2017-04-01 14:49:06 -0700 | [diff] [blame] | 32 | #include <sys/time.h> |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 33 | #include <inttypes.h> |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 34 | |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 35 | #include <uriparser/Uri.h> |
| 36 | |
Tadeusz Struk | b21f9f9 | 2017-10-09 13:51:19 -0700 | [diff] [blame] | 37 | #include "sapi/tss2_mu.h" |
Philip Tricca | dd3b03c | 2017-03-05 11:38:08 -0800 | [diff] [blame] | 38 | #include "tcti/tcti_socket.h" |
Philip Tricca | c3dedc2 | 2016-01-15 13:47:22 -0800 | [diff] [blame] | 39 | #include "sysapi_util.h" |
Philip Tricca | 473aebe | 2017-10-17 13:04:17 -0700 | [diff] [blame] | 40 | #include "tcti.h" |
Philip Tricca | 330d3c7 | 2017-07-02 13:49:32 -0700 | [diff] [blame] | 41 | #include "sockets.h" |
Tadeusz Struk | 8203a37 | 2017-06-26 15:15:18 -0700 | [diff] [blame] | 42 | #include "tss2_endian.h" |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 43 | #define LOGMODULE tcti |
| 44 | #include "log/log.h" |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 45 | |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 46 | #define TCTI_SOCKET_DEFAULT_CONF "tcp://127.0.0.1:2321" |
| 47 | #define TCTI_SOCKET_DEFAULT_PORT 2321 |
| 48 | |
Philip Tricca | 76ba600 | 2018-02-21 10:52:52 -0800 | [diff] [blame] | 49 | static TSS2_RC xmit_buf ( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 50 | SOCKET sock, |
Philip Tricca | 76ba600 | 2018-02-21 10:52:52 -0800 | [diff] [blame] | 51 | const void *buf, |
| 52 | size_t size) |
Philip Tricca | 4ee284b | 2016-03-30 17:40:39 -0700 | [diff] [blame] | 53 | { |
Philip Tricca | dff2686 | 2018-02-19 15:27:18 -0800 | [diff] [blame] | 54 | int ret; |
Philip Tricca | 4ee284b | 2016-03-30 17:40:39 -0700 | [diff] [blame] | 55 | |
Philip Tricca | 76ba600 | 2018-02-21 10:52:52 -0800 | [diff] [blame] | 56 | LOGBLOB_DEBUG (buf, size, "Writing %zu bytes to socket %d:", size, sock); |
| 57 | ret = write_all (sock, buf, size); |
| 58 | if (ret < size) { |
Philip Tricca | dff2686 | 2018-02-19 15:27:18 -0800 | [diff] [blame] | 59 | LOG_ERROR("Failed to write to fd %d: %d", sock, WSAGetLastError ()); |
| 60 | return TSS2_TCTI_RC_IO_ERROR; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 61 | } |
Philip Tricca | dff2686 | 2018-02-19 15:27:18 -0800 | [diff] [blame] | 62 | return TSS2_RC_SUCCESS; |
Philip Tricca | 4ee284b | 2016-03-30 17:40:39 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Philip Tricca | bdc6945 | 2018-02-21 12:45:33 -0800 | [diff] [blame] | 65 | TSS2_RC send_sim_session_end ( |
| 66 | SOCKET sock) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 67 | { |
Philip Tricca | bdc6945 | 2018-02-21 12:45:33 -0800 | [diff] [blame] | 68 | uint8_t buf [4] = { 0, }; |
| 69 | TSS2_RC rc; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 70 | |
Philip Tricca | bdc6945 | 2018-02-21 12:45:33 -0800 | [diff] [blame] | 71 | rc = Tss2_MU_UINT32_Marshal (TPM_SESSION_END, buf, sizeof (buf), NULL); |
| 72 | if (rc == TSS2_RC_SUCCESS) { |
| 73 | return rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 74 | } |
Philip Tricca | bdc6945 | 2018-02-21 12:45:33 -0800 | [diff] [blame] | 75 | return xmit_buf (sock, buf, sizeof (buf)); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Philip Tricca | 72fba0b | 2018-02-21 14:14:32 -0800 | [diff] [blame] | 78 | /* |
| 79 | * Utility to function to parse the first 10 bytes of a buffer and populate |
| 80 | * the 'header' structure with the results. The provided buffer is assumed to |
| 81 | * be at least 10 bytes long. |
| 82 | */ |
| 83 | TSS2_RC parse_header ( |
Philip Tricca | ddf42b2 | 2018-02-21 15:37:23 -0800 | [diff] [blame] | 84 | const uint8_t *buf, |
Philip Tricca | 72fba0b | 2018-02-21 14:14:32 -0800 | [diff] [blame] | 85 | tpm_header_t *header) |
| 86 | { |
| 87 | TSS2_RC rc; |
| 88 | size_t offset = 0; |
| 89 | |
| 90 | LOG_TRACE ("Parsing header from buffer: 0x%" PRIxPTR, (uintptr_t)buf); |
| 91 | rc = Tss2_MU_TPM2_ST_Unmarshal (buf, |
| 92 | TPM_HEADER_SIZE, |
| 93 | &offset, |
| 94 | &header->tag); |
| 95 | if (rc != TSS2_RC_SUCCESS) { |
| 96 | LOG_ERROR ("Failed to unmarshal tag."); |
| 97 | return rc; |
| 98 | } |
| 99 | rc = Tss2_MU_UINT32_Unmarshal (buf, |
| 100 | TPM_HEADER_SIZE, |
| 101 | &offset, |
| 102 | &header->size); |
| 103 | if (rc != TSS2_RC_SUCCESS) { |
| 104 | LOG_ERROR ("Failed to unmarshal command size."); |
| 105 | return rc; |
| 106 | } |
| 107 | rc = Tss2_MU_UINT32_Unmarshal (buf, |
| 108 | TPM_HEADER_SIZE, |
| 109 | &offset, |
| 110 | &header->code); |
| 111 | if (rc != TSS2_RC_SUCCESS) { |
| 112 | LOG_ERROR ("Failed to unmarshal command code."); |
| 113 | } |
| 114 | return rc; |
| 115 | } |
| 116 | |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 117 | /* |
| 118 | * This fucntion is used to send the simulator a sort of command message |
| 119 | * that tells it we're about to send it a TPM command. This requires that |
| 120 | * we first send it a 4 byte code that's defined by the simulator. Then |
| 121 | * another byte identifying the locality and finally the size of the TPM |
| 122 | * command buffer that we're about to send. After these 9 bytes are sent |
| 123 | * the simulator will accept a TPM command buffer. |
| 124 | */ |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 125 | #define SIM_CMD_SIZE (sizeof (UINT32) + sizeof (UINT8) + sizeof (UINT32)) |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 126 | TSS2_RC send_sim_cmd_setup ( |
| 127 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel, |
| 128 | UINT32 size) |
| 129 | { |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 130 | uint8_t buf [SIM_CMD_SIZE] = { 0 }; |
| 131 | size_t offset = 0; |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 132 | TSS2_RC rc; |
| 133 | |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 134 | rc = Tss2_MU_UINT32_Marshal (MS_SIM_TPM_SEND_COMMAND, |
| 135 | buf, |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 136 | sizeof (buf), |
| 137 | &offset); |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 138 | if (rc != TSS2_RC_SUCCESS) { |
| 139 | return rc; |
| 140 | } |
| 141 | |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 142 | rc = Tss2_MU_UINT8_Marshal (tcti_intel->status.locality, |
| 143 | buf, |
| 144 | sizeof (buf), |
| 145 | &offset); |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 146 | if (rc != TSS2_RC_SUCCESS) { |
| 147 | return rc; |
| 148 | } |
| 149 | |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 150 | rc = Tss2_MU_UINT32_Marshal (size, buf, sizeof (buf), &offset); |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 151 | if (rc != TSS2_RC_SUCCESS) { |
| 152 | return rc; |
| 153 | } |
Philip Tricca | 3a38292 | 2018-02-21 16:14:15 -0800 | [diff] [blame] | 154 | |
Philip Tricca | d42e723 | 2018-02-21 14:59:49 -0800 | [diff] [blame] | 155 | return xmit_buf (tcti_intel->tpmSock, buf, sizeof (buf)); |
| 156 | } |
| 157 | |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 158 | TSS2_RC tcti_socket_transmit ( |
| 159 | TSS2_TCTI_CONTEXT *tcti_ctx, |
| 160 | size_t size, |
| 161 | const uint8_t *cmd_buf) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 162 | { |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 163 | tpm_header_t header = { 0 }; |
| 164 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tcti_ctx); |
| 165 | TSS2_RC rc; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 166 | |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 167 | rc = tcti_send_checks (tcti_ctx, cmd_buf); |
| 168 | if (rc != TSS2_RC_SUCCESS) { |
| 169 | return rc; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 170 | } |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 171 | rc = parse_header (cmd_buf, &header); |
| 172 | if (rc != TSS2_RC_SUCCESS) { |
| 173 | return rc; |
Andreas Fuchs | f8a6662 | 2017-11-29 14:27:25 +0100 | [diff] [blame] | 174 | } |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 175 | if (header.size != size) { |
Philip Tricca | 72fba0b | 2018-02-21 14:14:32 -0800 | [diff] [blame] | 176 | LOG_ERROR ("Buffer size parameter: %zu, and TPM2 command header size " |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 177 | "field: %" PRIu32 " disagree.", size, header.size); |
Philip Tricca | 72fba0b | 2018-02-21 14:14:32 -0800 | [diff] [blame] | 178 | return TSS2_TCTI_RC_BAD_VALUE; |
| 179 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 180 | |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 181 | LOG_DEBUG ("Sending command with TPM_CC 0x%" PRIx32 " and size %" PRIu32, |
| 182 | header.code, header.size); |
| 183 | rc = send_sim_cmd_setup (tcti_intel, header.size); |
| 184 | if (rc != TSS2_RC_SUCCESS) { |
| 185 | return rc; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 186 | } |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 187 | rc = xmit_buf (tcti_intel->tpmSock, cmd_buf, size); |
| 188 | if (rc != TSS2_RC_SUCCESS) { |
| 189 | return rc; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 190 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 191 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 192 | tcti_intel->previousStage = TCTI_STAGE_SEND_COMMAND; |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 193 | tcti_intel->status.commandSent = 1; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 194 | tcti_intel->status.tagReceived = 0; |
| 195 | tcti_intel->status.responseSizeReceived = 0; |
| 196 | tcti_intel->status.protocolResponseSizeReceived = 0; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 197 | |
Philip Tricca | aca82c9 | 2018-02-21 15:47:40 -0800 | [diff] [blame] | 198 | return rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | TSS2_RC SocketCancel( |
| 202 | TSS2_TCTI_CONTEXT *tctiContext |
| 203 | ) |
| 204 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 205 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 206 | TSS2_RC rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 207 | |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 208 | rc = tcti_common_checks (tctiContext); |
| 209 | if (rc != TSS2_RC_SUCCESS) { |
| 210 | return rc; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 211 | } else if (tcti_intel->status.commandSent != 1) { |
| 212 | return TSS2_TCTI_RC_BAD_SEQUENCE; |
| 213 | } else { |
| 214 | return PlatformCommand (tctiContext, MS_SIM_CANCEL_ON); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 215 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | TSS2_RC SocketSetLocality( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 219 | TSS2_TCTI_CONTEXT *tctiContext, |
| 220 | uint8_t locality |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 221 | ) |
| 222 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 223 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 224 | TSS2_RC rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 225 | |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 226 | rc = tcti_common_checks (tctiContext); |
| 227 | if (rc != TSS2_RC_SUCCESS) { |
| 228 | return rc; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 229 | } |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 230 | if (tcti_intel->status.commandSent == 1) { |
| 231 | return TSS2_TCTI_RC_BAD_SEQUENCE; |
| 232 | } |
| 233 | |
| 234 | tcti_intel->status.locality = locality; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 235 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 236 | return TSS2_RC_SUCCESS; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 239 | TSS2_RC SocketGetPollHandles( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 240 | TSS2_TCTI_CONTEXT *tctiContext, |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 241 | TSS2_TCTI_POLL_HANDLE *handles, |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 242 | size_t *num_handles) |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 243 | { |
| 244 | return TSS2_TCTI_RC_NOT_IMPLEMENTED; |
| 245 | } |
| 246 | |
wcarthur | 35aa539 | 2016-03-22 16:52:44 -0400 | [diff] [blame] | 247 | void SocketFinalize( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 248 | TSS2_TCTI_CONTEXT *tctiContext |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 249 | ) |
| 250 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 251 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 252 | TSS2_RC rc; |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 253 | |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 254 | rc = tcti_common_checks (tctiContext); |
| 255 | if (rc != TSS2_RC_SUCCESS) { |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 256 | return; |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame] | 257 | } |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 258 | |
Philip Tricca | bdc6945 | 2018-02-21 12:45:33 -0800 | [diff] [blame] | 259 | send_sim_session_end (tcti_intel->otherSock); |
| 260 | send_sim_session_end (tcti_intel->tpmSock); |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 261 | |
Philip Tricca | 01b9a1e | 2018-03-04 21:47:58 -0800 | [diff] [blame] | 262 | socket_close (&tcti_intel->otherSock); |
| 263 | socket_close (&tcti_intel->tpmSock); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 264 | } |
| 265 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 266 | TSS2_RC SocketReceiveTpmResponse( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 267 | TSS2_TCTI_CONTEXT *tctiContext, |
| 268 | size_t *response_size, |
| 269 | unsigned char *response_buffer, |
| 270 | int32_t timeout |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 271 | ) |
| 272 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 273 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 274 | UINT32 trash; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 275 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 276 | fd_set readFds; |
| 277 | struct timeval tv, *tvPtr; |
| 278 | int32_t timeoutMsecs = timeout % 1000; |
| 279 | int iResult; |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 280 | unsigned char responseSizeDelta = 0; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 281 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 282 | rval = tcti_receive_checks (tctiContext, response_size, response_buffer); |
| 283 | if (rval != TSS2_RC_SUCCESS) { |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 284 | goto retSocketReceiveTpmResponse; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 287 | if (timeout == TSS2_TCTI_TIMEOUT_BLOCK) { |
| 288 | tvPtr = 0; |
| 289 | } else { |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 290 | tv.tv_sec = timeout / 1000; |
| 291 | tv.tv_usec = timeoutMsecs * 1000; |
| 292 | tvPtr = &tv; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 295 | FD_ZERO (&readFds); |
| 296 | FD_SET (tcti_intel->tpmSock, &readFds); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 297 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 298 | iResult = select (tcti_intel->tpmSock + 1, &readFds, 0, 0, tvPtr); |
| 299 | if (iResult == 0) { |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 300 | LOG_ERROR("select failed due to timeout, socket #: 0x%x", |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 301 | tcti_intel->tpmSock); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 302 | rval = TSS2_TCTI_RC_TRY_AGAIN; |
| 303 | goto retSocketReceiveTpmResponse; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 304 | } else if (iResult == SOCKET_ERROR) { |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 305 | LOG_ERROR("select failed with socket error: %d", |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 306 | WSAGetLastError ()); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 307 | rval = TSS2_TCTI_RC_IO_ERROR; |
| 308 | goto retSocketReceiveTpmResponse; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 309 | } else if (iResult != 1) { |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 310 | LOG_ERROR("select failed, read the wrong # of bytes: %d", |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 311 | iResult); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 312 | rval = TSS2_TCTI_RC_IO_ERROR; |
| 313 | goto retSocketReceiveTpmResponse; |
| 314 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 315 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 316 | if (tcti_intel->status.protocolResponseSizeReceived != 1) { |
| 317 | /* Receive the size of the response. */ |
Philip Tricca | 77042de | 2018-03-05 15:14:04 -0800 | [diff] [blame^] | 318 | iResult = socket_recv_buf (tcti_intel->tpmSock, |
| 319 | (unsigned char *)&tcti_intel->responseSize, |
| 320 | 4); |
| 321 | if (iResult != 4) { |
| 322 | rval = TSS2_TCTI_RC_IO_ERROR; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 323 | goto retSocketReceiveTpmResponse; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 324 | } |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 325 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 326 | tcti_intel->responseSize = BE_TO_HOST_32 (tcti_intel->responseSize); |
Philip Tricca | 77042de | 2018-03-05 15:14:04 -0800 | [diff] [blame^] | 327 | LOG_DEBUG ("response size: %" PRIu32, tcti_intel->responseSize); |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 328 | tcti_intel->status.protocolResponseSizeReceived = 1; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 329 | } |
| 330 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 331 | if (response_buffer == NULL) { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 332 | *response_size = tcti_intel->responseSize; |
| 333 | tcti_intel->status.protocolResponseSizeReceived = 1; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 334 | goto retSocketReceiveTpmResponse; |
| 335 | } |
| 336 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 337 | if (*response_size < tcti_intel->responseSize) { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 338 | *response_size = tcti_intel->responseSize; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 339 | rval = TSS2_TCTI_RC_INSUFFICIENT_BUFFER; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 340 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 341 | /* If possible, receive tag from TPM. */ |
Andreas Fuchs | be5899d | 2017-11-13 17:14:36 +0100 | [diff] [blame] | 342 | if (*response_size >= sizeof (TPM2_ST) && |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 343 | tcti_intel->status.tagReceived == 0) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 344 | { |
Philip Tricca | 77042de | 2018-03-05 15:14:04 -0800 | [diff] [blame^] | 345 | iResult = socket_recv_buf (tcti_intel->tpmSock, |
| 346 | (unsigned char *)&tcti_intel->tag, |
| 347 | 2); |
| 348 | if (iResult < 2) { |
| 349 | rval = TSS2_TCTI_RC_IO_ERROR; |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 350 | goto retSocketReceiveTpmResponse; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 351 | } else { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 352 | tcti_intel->status.tagReceived = 1; |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 353 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 354 | } |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 355 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 356 | /* If possible, receive response size from TPM */ |
Andreas Fuchs | be5899d | 2017-11-13 17:14:36 +0100 | [diff] [blame] | 357 | if (*response_size >= (sizeof (TPM2_ST) + sizeof (TPM2_RC)) && |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 358 | tcti_intel->status.responseSizeReceived == 0) |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 359 | { |
Philip Tricca | 77042de | 2018-03-05 15:14:04 -0800 | [diff] [blame^] | 360 | iResult = socket_recv_buf (tcti_intel->tpmSock, |
| 361 | (unsigned char *)&tcti_intel->responseSize, |
| 362 | 4); |
| 363 | if (iResult != 4) { |
| 364 | rval = TSS2_TCTI_RC_IO_ERROR; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 365 | goto retSocketReceiveTpmResponse; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 366 | } else { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 367 | tcti_intel->responseSize = BE_TO_HOST_32 (tcti_intel->responseSize); |
| 368 | tcti_intel->status.responseSizeReceived = 1; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 369 | } |
| 370 | } |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 371 | } else { |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 372 | if (tcti_intel->responseSize > 0) |
Will-nuc | e0d3ec2 | 2015-12-09 15:09:49 -0500 | [diff] [blame] | 373 | { |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 374 | LOG_DEBUG("Response Received: "); |
| 375 | LOG_DEBUG("from socket #0x%x:", |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 376 | tcti_intel->tpmSock); |
Will-nuc | e0d3ec2 | 2015-12-09 15:09:49 -0500 | [diff] [blame] | 377 | } |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 378 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 379 | if (tcti_intel->status.tagReceived == 1) { |
Andreas Fuchs | be5899d | 2017-11-13 17:14:36 +0100 | [diff] [blame] | 380 | *(TPM2_ST *)response_buffer = tcti_intel->tag; |
| 381 | responseSizeDelta += sizeof (TPM2_ST); |
| 382 | response_buffer += sizeof (TPM2_ST); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 383 | } |
| 384 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 385 | if (tcti_intel->status.responseSizeReceived == 1) { |
Andreas Fuchs | be5899d | 2017-11-13 17:14:36 +0100 | [diff] [blame] | 386 | *(TPM2_RC *)response_buffer = HOST_TO_BE_32 (tcti_intel->responseSize); |
| 387 | responseSizeDelta += sizeof (TPM2_RC); |
| 388 | response_buffer += sizeof (TPM2_RC); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 389 | } |
| 390 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 391 | /* Receive the TPM response. */ |
Philip Tricca | 77042de | 2018-03-05 15:14:04 -0800 | [diff] [blame^] | 392 | iResult = socket_recv_buf (tcti_intel->tpmSock, |
| 393 | (unsigned char *)response_buffer, |
| 394 | tcti_intel->responseSize - responseSizeDelta); |
| 395 | if (iResult < 0) { |
| 396 | rval = TSS2_TCTI_RC_IO_ERROR; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 397 | goto retSocketReceiveTpmResponse; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 398 | } |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 399 | LOGBLOB_DEBUG(response_buffer, tcti_intel->responseSize, |
| 400 | "Received response buffer="); |
wcarthur | 0651af2 | 2015-11-25 15:01:22 -0500 | [diff] [blame] | 401 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 402 | /* Receive the appended four bytes of 0's */ |
Philip Tricca | 77042de | 2018-03-05 15:14:04 -0800 | [diff] [blame^] | 403 | iResult = socket_recv_buf (tcti_intel->tpmSock, |
| 404 | (unsigned char *)&trash, |
| 405 | 4); |
| 406 | if (iResult != 4) { |
| 407 | rval = TSS2_TCTI_RC_IO_ERROR; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 408 | goto retSocketReceiveTpmResponse; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 409 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 412 | if (tcti_intel->responseSize < *response_size) { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 413 | *response_size = tcti_intel->responseSize; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 414 | } |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 415 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 416 | tcti_intel->status.commandSent = 0; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 417 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 418 | /* Turn cancel off. */ |
| 419 | if (rval == TSS2_RC_SUCCESS) { |
| 420 | rval = PlatformCommand (tctiContext, MS_SIM_CANCEL_OFF); |
| 421 | } else { |
| 422 | /* Ignore return value so earlier error code is preserved. */ |
| 423 | PlatformCommand (tctiContext, MS_SIM_CANCEL_OFF); |
wcarthur | 2efe291 | 2015-11-16 11:19:42 -0500 | [diff] [blame] | 424 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 425 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 426 | retSocketReceiveTpmResponse: |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 427 | if (rval == TSS2_RC_SUCCESS && response_buffer != NULL) { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 428 | tcti_intel->previousStage = TCTI_STAGE_RECEIVE_RESPONSE; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 429 | } |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 430 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 431 | return rval; |
| 432 | } |
| 433 | |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 434 | /** |
| 435 | * This function sends the Microsoft simulator the MS_SIM_POWER_ON and |
| 436 | * MS_SIM_NV_ON commands using the PlatformCommand mechanism. Without |
| 437 | * these the simulator will respond with zero sized buffer which causes |
| 438 | * the TSS to freak out. Sending this command more than once is harmelss |
| 439 | * so it's advisable to call this function as part of the TCTI context |
| 440 | * initialization just to be sure. |
| 441 | * |
| 442 | * NOTE: The caller will still need to call Tss2_Sys_Startup. If they |
| 443 | * 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] | 444 | * the error will at least be meaningful (TPM2_RC_INITIALIZE). |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 445 | */ |
Philip Tricca | dc4ff84 | 2018-03-04 21:59:48 -0800 | [diff] [blame] | 446 | static TSS2_RC |
| 447 | simulator_setup ( |
| 448 | TSS2_TCTI_CONTEXT *tctiContext) |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 449 | { |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 450 | TSS2_RC rval; |
| 451 | |
Philip Tricca | dc4ff84 | 2018-03-04 21:59:48 -0800 | [diff] [blame] | 452 | LOG_TRACE ("Initializing TCTI context 0x%" PRIxPTR, |
| 453 | (uintptr_t)tctiContext); |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 454 | rval = PlatformCommand (tctiContext ,MS_SIM_POWER_ON); |
| 455 | if (rval != TSS2_RC_SUCCESS) { |
Philip Tricca | dc4ff84 | 2018-03-04 21:59:48 -0800 | [diff] [blame] | 456 | LOG_WARNING ("Failed to send MS_SIM_POWER_ON platform command."); |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 457 | return rval; |
| 458 | } |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 459 | |
| 460 | rval = PlatformCommand (tctiContext, MS_SIM_NV_ON); |
| 461 | if (rval != TSS2_RC_SUCCESS) { |
Philip Tricca | dc4ff84 | 2018-03-04 21:59:48 -0800 | [diff] [blame] | 462 | LOG_WARNING ("Failed to send MS_SIM_NV_ON platform command."); |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 463 | } |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 464 | |
| 465 | return rval; |
| 466 | } |
| 467 | |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 468 | /* |
| 469 | * This is a utility function to extract a TCP port number from a string. |
| 470 | * The string must be 6 characters long. If the supplied string contains an |
| 471 | * invalid port number then 0 is returned. |
| 472 | */ |
| 473 | static uint16_t |
| 474 | string_to_port (char port_str[6]) |
| 475 | { |
| 476 | uint32_t port = 0; |
| 477 | |
| 478 | if (sscanf (port_str, "%" SCNu32, &port) == EOF || port > UINT16_MAX) { |
| 479 | return 0; |
| 480 | } |
| 481 | return port; |
| 482 | } |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 483 | /* |
| 484 | * This function extracts the hostname and port part of the provided conf |
| 485 | * string (which is really just a URI). The hostname parameter is an output |
| 486 | * buffer that must be large enough to hold the hostname. HOST_NAME_MAX is |
| 487 | * probably a good size. The 'port' parameter is an output parameter where |
| 488 | * we store the port from the URI after we convert it to a uint16. |
| 489 | * If the URI does not contain a port number then the contents of the 'port' |
| 490 | * parameter will not be changed. |
| 491 | * This function returns TSS2_RC_SUCCESS when the 'hostname' and 'port' have |
| 492 | * been populated successfully. On failure it will return |
| 493 | * TSS2_TCTI_RC_BAD_VALUE to indicate that the provided conf string contains |
| 494 | * values that we can't parse or are invalid. |
| 495 | */ |
| 496 | TSS2_RC |
| 497 | conf_str_to_host_port ( |
| 498 | const char *conf, |
| 499 | char *hostname, |
| 500 | uint16_t *port) |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 501 | { |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 502 | UriParserStateA state; |
| 503 | UriUriA uri; |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 504 | /* maximum 5 digits in uint16_t + 1 for \0 */ |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 505 | char port_str[6] = { 0 }; |
| 506 | size_t range; |
| 507 | TSS2_RC rc = TSS2_RC_SUCCESS; |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 508 | |
| 509 | state.uri = &uri; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 510 | if (uriParseUriA (&state, conf) != URI_SUCCESS) { |
| 511 | LOG_WARNING ("Failed to parse provided conf string: %s", conf); |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 512 | rc = TSS2_TCTI_RC_BAD_VALUE; |
| 513 | goto out; |
| 514 | } |
| 515 | |
| 516 | /* extract host & domain name / fqdn */ |
| 517 | range = uri.hostText.afterLast - uri.hostText.first; |
| 518 | if (range > HOST_NAME_MAX) { |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 519 | LOG_WARNING ("Provided conf string has hostname that exceeds " |
| 520 | "HOST_NAME_MAX."); |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 521 | rc = TSS2_TCTI_RC_BAD_VALUE; |
| 522 | goto out; |
| 523 | } |
| 524 | strncpy (hostname, uri.hostText.first, range); |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 525 | |
| 526 | /* extract port number */ |
| 527 | range = uri.portText.afterLast - uri.portText.first; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 528 | if (range > 5) { |
| 529 | LOG_WARNING ("conf string contains invalid port."); |
| 530 | rc = TSS2_TCTI_RC_BAD_VALUE; |
| 531 | goto out; |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 532 | } else if (range == 0) { |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 533 | LOG_INFO ("conf string does not contain a port."); |
| 534 | goto out; |
| 535 | } |
| 536 | |
| 537 | strncpy (port_str, uri.portText.first, range); |
| 538 | *port = string_to_port (port_str); |
| 539 | if (*port == 0) { |
| 540 | LOG_WARNING ("Provided conf string contains invalid port: 0"); |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 541 | rc = TSS2_TCTI_RC_BAD_VALUE; |
| 542 | goto out; |
| 543 | } |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 544 | out: |
| 545 | uriFreeUriMembersA (&uri); |
| 546 | return rc; |
| 547 | } |
| 548 | |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 549 | void |
| 550 | tcti_socket_init_context_data ( |
| 551 | TSS2_TCTI_CONTEXT *tcti_ctx) |
| 552 | { |
| 553 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tcti_ctx); |
| 554 | |
| 555 | TSS2_TCTI_MAGIC (tcti_ctx) = TCTI_MAGIC; |
| 556 | TSS2_TCTI_VERSION (tcti_ctx) = TCTI_VERSION; |
| 557 | TSS2_TCTI_TRANSMIT (tcti_ctx) = tcti_socket_transmit; |
| 558 | TSS2_TCTI_RECEIVE (tcti_ctx) = SocketReceiveTpmResponse; |
| 559 | TSS2_TCTI_FINALIZE (tcti_ctx) = SocketFinalize; |
| 560 | TSS2_TCTI_CANCEL (tcti_ctx) = SocketCancel; |
| 561 | TSS2_TCTI_GET_POLL_HANDLES (tcti_ctx) = SocketGetPollHandles; |
| 562 | TSS2_TCTI_SET_LOCALITY (tcti_ctx) = SocketSetLocality; |
| 563 | TSS2_TCTI_MAKE_STICKY (tcti_ctx) = tcti_make_sticky_not_implemented; |
| 564 | tcti_intel->status.locality = 3; |
| 565 | tcti_intel->status.commandSent = 0; |
| 566 | tcti_intel->status.tagReceived = 0; |
| 567 | tcti_intel->status.responseSizeReceived = 0; |
| 568 | tcti_intel->status.protocolResponseSizeReceived = 0; |
| 569 | tcti_intel->currentTctiContext = 0; |
| 570 | tcti_intel->previousStage = TCTI_STAGE_INITIALIZE; |
| 571 | } |
| 572 | /* |
| 573 | * This is an implementation of the standard TCTI initialization function for |
| 574 | * this module. |
| 575 | */ |
| 576 | TSS2_RC |
| 577 | Tss2_Tcti_Socket_Init ( |
| 578 | TSS2_TCTI_CONTEXT *tctiContext, |
| 579 | size_t *size, |
| 580 | const char *conf) |
| 581 | { |
| 582 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
| 583 | TSS2_RC rc; |
Philip Tricca | 2bec2a8 | 2018-03-04 21:27:30 -0800 | [diff] [blame] | 584 | SOCKET *tpmSock, *otherSock = NULL; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 585 | const char *uri_str = conf != NULL ? conf : TCTI_SOCKET_DEFAULT_CONF; |
| 586 | char hostname[HOST_NAME_MAX + 1] = { 0 }; |
| 587 | uint16_t port = TCTI_SOCKET_DEFAULT_PORT; |
| 588 | |
| 589 | if (tctiContext == NULL && size == NULL) { |
| 590 | return TSS2_TCTI_RC_BAD_VALUE; |
| 591 | } else if( tctiContext == NULL ) { |
| 592 | *size = sizeof (TSS2_TCTI_CONTEXT_INTEL); |
| 593 | return TSS2_RC_SUCCESS; |
| 594 | } else if( conf == NULL ) { |
| 595 | return TSS2_TCTI_RC_BAD_VALUE; |
| 596 | } |
| 597 | |
| 598 | rc = conf_str_to_host_port (uri_str, hostname, &port); |
| 599 | if (rc != TSS2_RC_SUCCESS) { |
| 600 | return rc; |
| 601 | } |
| 602 | |
Philip Tricca | 2bec2a8 | 2018-03-04 21:27:30 -0800 | [diff] [blame] | 603 | tpmSock = &tcti_intel->tpmSock; |
| 604 | otherSock = &tcti_intel->otherSock; |
| 605 | |
| 606 | rc = socket_connect (hostname, port, tpmSock); |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 607 | if (rc != TSS2_RC_SUCCESS) { |
Philip Tricca | 2bec2a8 | 2018-03-04 21:27:30 -0800 | [diff] [blame] | 608 | return rc; |
| 609 | } |
| 610 | |
| 611 | rc = socket_connect (hostname, port + 1, otherSock); |
| 612 | if (rc != TSS2_RC_SUCCESS) { |
| 613 | goto fail_out; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Philip Tricca | dc4ff84 | 2018-03-04 21:59:48 -0800 | [diff] [blame] | 616 | rc = simulator_setup (tctiContext); |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 617 | if (rc != TSS2_RC_SUCCESS) { |
Philip Tricca | 2bec2a8 | 2018-03-04 21:27:30 -0800 | [diff] [blame] | 618 | goto fail_out; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | tcti_socket_init_context_data (tctiContext); |
Philip Tricca | 2bec2a8 | 2018-03-04 21:27:30 -0800 | [diff] [blame] | 622 | |
| 623 | return TSS2_RC_SUCCESS; |
| 624 | |
| 625 | fail_out: |
Philip Tricca | 01b9a1e | 2018-03-04 21:47:58 -0800 | [diff] [blame] | 626 | socket_close (tpmSock); |
| 627 | socket_close (otherSock); |
Philip Tricca | 2bec2a8 | 2018-03-04 21:27:30 -0800 | [diff] [blame] | 628 | |
| 629 | return TSS2_TCTI_RC_IO_ERROR; |
Philip Tricca | c21dbf3 | 2018-03-03 09:07:48 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 632 | /* public info structure */ |
| 633 | const static TSS2_TCTI_INFO tss2_tcti_info = { |
Philip Tricca | dc11b4f | 2018-02-23 18:21:48 -0800 | [diff] [blame] | 634 | .version = { |
| 635 | .magic = TCTI_MAGIC, |
| 636 | .version = TCTI_VERSION, |
| 637 | }, |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 638 | .name = "tcti-socket", |
| 639 | .description = "TCTI module for communication with the Microsoft TPM2 Simulator.", |
Philip Tricca | 7362c44 | 2018-01-08 18:22:36 -0800 | [diff] [blame] | 640 | .config_help = "Connection URI in the form tcp://ip_address[:port]. " \ |
| 641 | "Default is: TCTI_SOCKET_DEFAULT.", |
Philip Tricca | 77683c7 | 2017-12-04 16:05:52 -0800 | [diff] [blame] | 642 | .init = Tss2_Tcti_Socket_Init, |
| 643 | }; |
| 644 | |
| 645 | const TSS2_TCTI_INFO* |
| 646 | Tss2_Tcti_Info (void) |
| 647 | { |
| 648 | return &tss2_tcti_info; |
| 649 | } |