Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 1 | //**********************************************************************; |
Philip Tricca | e0a93d1 | 2017-06-29 08:42:34 -0700 | [diff] [blame] | 2 | // Copyright (c) 2015, 2016, 2017 Intel Corporation |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 3 | // All rights reserved. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 6 | // modification, are permitted provided that the following conditions are met: |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 9 | // this list of conditions and the following disclaimer. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 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 |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 13 | // and/or other materials provided with the distribution. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 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 |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 25 | // THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | //**********************************************************************; |
| 27 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> // Needed for _wtoi |
Armin Kuster | ace89dd | 2017-04-01 14:49:06 -0700 | [diff] [blame] | 30 | #include <sys/time.h> |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 31 | |
Philip Tricca | dd3b03c | 2017-03-05 11:38:08 -0800 | [diff] [blame] | 32 | #include "sapi/tpm20.h" |
Tadeusz Struk | b21f9f9 | 2017-10-09 13:51:19 -0700 | [diff] [blame] | 33 | #include "sapi/tss2_mu.h" |
Philip Tricca | dd3b03c | 2017-03-05 11:38:08 -0800 | [diff] [blame] | 34 | #include "tcti/tcti_socket.h" |
Philip Tricca | c3dedc2 | 2016-01-15 13:47:22 -0800 | [diff] [blame] | 35 | #include "sysapi_util.h" |
Philip Tricca | dd3b03c | 2017-03-05 11:38:08 -0800 | [diff] [blame] | 36 | #include "common/debug.h" |
Philip Tricca | 473aebe | 2017-10-17 13:04:17 -0700 | [diff] [blame^] | 37 | #include "tcti.h" |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 38 | #include "logging.h" |
Philip Tricca | 330d3c7 | 2017-07-02 13:49:32 -0700 | [diff] [blame] | 39 | #include "sockets.h" |
Tadeusz Struk | 8203a37 | 2017-06-26 15:15:18 -0700 | [diff] [blame] | 40 | #include "tss2_endian.h" |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 41 | |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 42 | static TSS2_RC tctiRecvBytes( TSS2_TCTI_CONTEXT *tctiContext, SOCKET sock, unsigned char *data, int len ) |
Philip Tricca | a06f237 | 2016-03-30 17:13:11 -0700 | [diff] [blame] | 43 | { |
| 44 | TSS2_RC result = 0; |
| 45 | result = recvBytes( sock, data, len); |
wcarthur1 | e2e0418 | 2016-04-11 07:37:55 -0700 | [diff] [blame] | 46 | if ( (INT32)result == SOCKET_ERROR) { |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 47 | TCTI_LOG( tctiContext, NO_PREFIX, "In recvBytes, recv failed (socket: 0x%x) with error: %d\n", sock, WSAGetLastError() ); |
Philip Tricca | a06f237 | 2016-03-30 17:13:11 -0700 | [diff] [blame] | 48 | return TSS2_TCTI_RC_IO_ERROR; |
| 49 | } |
| 50 | #ifdef DEBUG_SOCKETS |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 51 | TCTI_LOG( tctiContext, NO_PREFIX, "Receive Bytes from socket #0x%x: \n", sock ); |
Philip Tricca | 4eb2f35 | 2016-03-31 05:55:35 -0700 | [diff] [blame] | 52 | TCTI_LOG_BUFFER( tctiContext, NO_PREFIX, data, len ); |
Philip Tricca | a06f237 | 2016-03-30 17:13:11 -0700 | [diff] [blame] | 53 | #endif |
| 54 | |
| 55 | return TSS2_RC_SUCCESS; |
| 56 | } |
| 57 | |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 58 | static TSS2_RC tctiSendBytes( TSS2_TCTI_CONTEXT *tctiContext, SOCKET sock, const unsigned char *data, int len ) |
Philip Tricca | 4ee284b | 2016-03-30 17:40:39 -0700 | [diff] [blame] | 59 | { |
| 60 | TSS2_RC ret = TSS2_RC_SUCCESS; |
| 61 | |
| 62 | #ifdef DEBUG_SOCKETS |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 63 | TCTI_LOG( tctiContext, NO_PREFIX, "Send Bytes to socket #0x%x: \n", sock ); |
Philip Tricca | 4eb2f35 | 2016-03-31 05:55:35 -0700 | [diff] [blame] | 64 | TCTI_LOG_BUFFER( tctiContext, NO_PREFIX, (UINT8 *)data, len ); |
Philip Tricca | 4ee284b | 2016-03-30 17:40:39 -0700 | [diff] [blame] | 65 | #endif |
| 66 | |
| 67 | ret = sendBytes( sock, data, len); |
| 68 | if (ret != TSS2_RC_SUCCESS) |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 69 | TCTI_LOG( tctiContext, NO_PREFIX, "In recvBytes, recv failed (socket: 0x%x) with error: %d\n", sock, WSAGetLastError() ); |
Philip Tricca | 4ee284b | 2016-03-30 17:40:39 -0700 | [diff] [blame] | 70 | return ret; |
| 71 | } |
| 72 | |
Philip Tricca | d1bbedb | 2016-02-14 20:39:59 -0800 | [diff] [blame] | 73 | TSS2_RC SendSessionEndSocketTcti( |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 74 | TSS2_TCTI_CONTEXT *tctiContext, /* in */ |
| 75 | UINT8 tpmCmdServer ) |
| 76 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 77 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Philip Tricca | 6c45af3 | 2017-03-09 19:26:35 -0800 | [diff] [blame] | 78 | // Value for "send command" to MS simulator |
| 79 | uint8_t buffer [4] = { 0, }; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 80 | SOCKET sock; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 81 | TSS2_RC rval = TSS2_RC_SUCCESS; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 82 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 83 | if( tpmCmdServer ) |
| 84 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 85 | sock = tcti_intel->tpmSock; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 86 | } |
| 87 | else |
| 88 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 89 | sock = tcti_intel->otherSock; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 90 | } |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 91 | |
Tadeusz Struk | b21f9f9 | 2017-10-09 13:51:19 -0700 | [diff] [blame] | 92 | rval = Tss2_MU_UINT32_Marshal (TPM_SESSION_END, buffer, sizeof (buffer), NULL); |
Philip Tricca | 6c45af3 | 2017-03-09 19:26:35 -0800 | [diff] [blame] | 93 | if (rval == TSS2_RC_SUCCESS) { |
| 94 | return rval; |
| 95 | } |
| 96 | rval = tctiSendBytes( tctiContext, sock, (char unsigned *)buffer, 4 ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 97 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 98 | return( rval ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 101 | TSS2_RC SocketSendTpmCommand( |
| 102 | TSS2_TCTI_CONTEXT *tctiContext, /* in */ |
| 103 | size_t command_size, /* in */ |
| 104 | uint8_t *command_buffer /* in */ |
| 105 | ) |
| 106 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 107 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Philip Tricca | 6c45af3 | 2017-03-09 19:26:35 -0800 | [diff] [blame] | 108 | UINT32 tpmSendCommand; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 109 | UINT32 cnt, cnt1; |
| 110 | UINT8 locality; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 111 | TSS2_RC rval = TSS2_RC_SUCCESS; |
Philip Tricca | 6c45af3 | 2017-03-09 19:26:35 -0800 | [diff] [blame] | 112 | size_t offset; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 113 | |
| 114 | #ifdef DEBUG |
wcarthur | 1c82759 | 2016-04-27 11:07:51 -0400 | [diff] [blame] | 115 | UINT32 commandCode; |
| 116 | printf_type rmPrefix; |
wcarthur1 | aee953e | 2016-04-01 15:13:24 -0700 | [diff] [blame] | 117 | #endif |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 118 | rval = CommonSendChecks( tctiContext, command_buffer ); |
| 119 | if( rval != TSS2_RC_SUCCESS ) |
| 120 | { |
| 121 | goto returnFromSocketSendTpmCommand; |
| 122 | } |
Will-nuc | 3ebfeb9 | 2015-10-29 15:53:27 -0400 | [diff] [blame] | 123 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 124 | #ifdef DEBUG |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 125 | if (tcti_intel->status.rmDebugPrefix == 1) |
wcarthur | 1c82759 | 2016-04-27 11:07:51 -0400 | [diff] [blame] | 126 | rmPrefix = RM_PREFIX; |
| 127 | else |
| 128 | rmPrefix = NO_PREFIX; |
| 129 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 130 | if (tcti_intel->status.debugMsgEnabled == 1) |
wcarthur | 1c82759 | 2016-04-27 11:07:51 -0400 | [diff] [blame] | 131 | { |
| 132 | TCTI_LOG( tctiContext, rmPrefix, "" ); |
Philip Tricca | 6c45af3 | 2017-03-09 19:26:35 -0800 | [diff] [blame] | 133 | offset = sizeof (TPM_ST) + sizeof (UINT32); |
Tadeusz Struk | b21f9f9 | 2017-10-09 13:51:19 -0700 | [diff] [blame] | 134 | rval = Tss2_MU_TPM_CC_Unmarshal (command_buffer, |
Philip Tricca | 6c45af3 | 2017-03-09 19:26:35 -0800 | [diff] [blame] | 135 | command_size, |
| 136 | &offset, |
| 137 | &commandCode); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 138 | #ifdef DEBUG_SOCKETS |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 139 | TCTI_LOG(tctiContext, NO_PREFIX, "Command sent on socket #0x%x: %s\n", tcti_intel->tpmSock, strTpmCommandCode(commandCode)); |
wcarthur1 | a10b0c9 | 2016-04-14 04:25:03 -0700 | [diff] [blame] | 140 | #else |
Tadeusz Struk | 83d12be | 2017-10-05 14:58:30 -0700 | [diff] [blame] | 141 | TCTI_LOG(tctiContext, NO_PREFIX, "Cmd sent: %s\n", strTpmCommandCode(commandCode)); |
wcarthur1 | a10b0c9 | 2016-04-14 04:25:03 -0700 | [diff] [blame] | 142 | #endif |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 143 | } |
wcarthur | 1c82759 | 2016-04-27 11:07:51 -0400 | [diff] [blame] | 144 | #endif |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 145 | // Size TPM 1.2 and TPM 2.0 headers overlap exactly, we can use |
| 146 | // either 1.2 or 2.0 header to get the size. |
Philip Tricca | 6c45af3 | 2017-03-09 19:26:35 -0800 | [diff] [blame] | 147 | offset = sizeof (TPM_ST); |
Tadeusz Struk | b21f9f9 | 2017-10-09 13:51:19 -0700 | [diff] [blame] | 148 | rval = Tss2_MU_UINT32_Unmarshal (command_buffer, command_size, &offset, &cnt); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 149 | |
| 150 | // Send TPM_SEND_COMMAND |
Tadeusz Struk | b21f9f9 | 2017-10-09 13:51:19 -0700 | [diff] [blame] | 151 | rval = Tss2_MU_UINT32_Marshal (MS_SIM_TPM_SEND_COMMAND, |
Philip Tricca | 6c45af3 | 2017-03-09 19:26:35 -0800 | [diff] [blame] | 152 | (uint8_t*)&tpmSendCommand, |
| 153 | sizeof (tpmSendCommand), |
| 154 | NULL); // Value for "send command" to MS simulator. |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 155 | rval = tctiSendBytes( tctiContext, tcti_intel->tpmSock, (unsigned char *)&tpmSendCommand, 4 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 156 | if( rval != TSS2_RC_SUCCESS ) |
| 157 | goto returnFromSocketSendTpmCommand; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 158 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 159 | // Send the locality |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 160 | locality = (UINT8)tcti_intel->status.locality; |
| 161 | rval = tctiSendBytes( tctiContext, tcti_intel->tpmSock, (unsigned char *)&locality, 1 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 162 | if( rval != TSS2_RC_SUCCESS ) |
| 163 | goto returnFromSocketSendTpmCommand; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 164 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 165 | #ifdef DEBUG |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 166 | if (tcti_intel->status.debugMsgEnabled == 1) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 167 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 168 | TCTI_LOG( tctiContext, rmPrefix, "Locality = %d", tcti_intel->status.locality); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 169 | } |
| 170 | #endif |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 171 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 172 | // Send number of bytes. |
| 173 | cnt1 = cnt; |
Philip Tricca | 6c45af3 | 2017-03-09 19:26:35 -0800 | [diff] [blame] | 174 | cnt = HOST_TO_BE_32(cnt); |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 175 | rval = tctiSendBytes( tctiContext, tcti_intel->tpmSock, (unsigned char *)&cnt, 4 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 176 | if( rval != TSS2_RC_SUCCESS ) |
| 177 | goto returnFromSocketSendTpmCommand; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 178 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 179 | // Send the TPM command buffer |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 180 | rval = tctiSendBytes( tctiContext, tcti_intel->tpmSock, (unsigned char *)command_buffer, cnt1 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 181 | if( rval != TSS2_RC_SUCCESS ) |
| 182 | goto returnFromSocketSendTpmCommand; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 183 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 184 | #ifdef DEBUG |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 185 | if (tcti_intel->status.debugMsgEnabled == 1) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 186 | { |
wcarthur | 1c82759 | 2016-04-27 11:07:51 -0400 | [diff] [blame] | 187 | DEBUG_PRINT_BUFFER( rmPrefix, command_buffer, cnt1 ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 188 | } |
| 189 | #endif |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 190 | tcti_intel->status.commandSent = 1; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 191 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 192 | returnFromSocketSendTpmCommand: |
| 193 | |
| 194 | if( rval == TSS2_RC_SUCCESS ) |
| 195 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 196 | tcti_intel->previousStage = TCTI_STAGE_SEND_COMMAND; |
| 197 | tcti_intel->status.tagReceived = 0; |
| 198 | tcti_intel->status.responseSizeReceived = 0; |
| 199 | tcti_intel->status.protocolResponseSizeReceived = 0; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | return rval; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | TSS2_RC SocketCancel( |
| 206 | TSS2_TCTI_CONTEXT *tctiContext |
| 207 | ) |
| 208 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 209 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 210 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 211 | |
| 212 | if( tctiContext == 0 ) |
| 213 | { |
| 214 | rval = TSS2_TCTI_RC_BAD_REFERENCE; |
| 215 | } |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 216 | else if (tcti_intel->status.commandSent != 1) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 217 | { |
| 218 | rval = TSS2_TCTI_RC_BAD_SEQUENCE; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | rval = (TSS2_RC)PlatformCommand( tctiContext, MS_SIM_CANCEL_ON ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return rval; |
| 226 | } |
| 227 | |
| 228 | TSS2_RC SocketSetLocality( |
| 229 | TSS2_TCTI_CONTEXT *tctiContext, /* in */ |
| 230 | uint8_t locality /* in */ |
| 231 | ) |
| 232 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 233 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 234 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 235 | |
| 236 | if( tctiContext == 0 ) |
| 237 | { |
| 238 | rval = TSS2_TCTI_RC_BAD_REFERENCE; |
| 239 | } |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 240 | else if (tcti_intel->status.locality != locality) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 241 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 242 | if (tcti_intel->status.commandSent == 1) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 243 | { |
| 244 | rval = TSS2_TCTI_RC_BAD_SEQUENCE; |
| 245 | } |
| 246 | else |
| 247 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 248 | tcti_intel->status.locality = locality; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | return rval; |
| 253 | } |
| 254 | |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 255 | TSS2_RC SocketGetPollHandles( |
| 256 | TSS2_TCTI_CONTEXT *tctiContext, |
| 257 | TSS2_TCTI_POLL_HANDLE *handles, |
| 258 | size_t *num_handles) |
| 259 | { |
| 260 | return TSS2_TCTI_RC_NOT_IMPLEMENTED; |
| 261 | } |
| 262 | |
wcarthur | 35aa539 | 2016-03-22 16:52:44 -0400 | [diff] [blame] | 263 | void SocketFinalize( |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 264 | TSS2_TCTI_CONTEXT *tctiContext /* in */ |
| 265 | ) |
| 266 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 267 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
| 268 | |
wcarthur | 35aa539 | 2016-03-22 16:52:44 -0400 | [diff] [blame] | 269 | if( tctiContext != NULL ) |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame] | 270 | { |
| 271 | // Send session end messages to servers. |
Philip Tricca | d1bbedb | 2016-02-14 20:39:59 -0800 | [diff] [blame] | 272 | SendSessionEndSocketTcti( tctiContext, 1 ); |
| 273 | SendSessionEndSocketTcti( tctiContext, 0 ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 274 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 275 | CloseSockets(tcti_intel->otherSock, tcti_intel->tpmSock); |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame] | 276 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 279 | TSS2_RC SocketReceiveTpmResponse( |
| 280 | TSS2_TCTI_CONTEXT *tctiContext, /* in */ |
| 281 | size_t *response_size, /* out */ |
| 282 | unsigned char *response_buffer, /* in */ |
| 283 | int32_t timeout |
| 284 | ) |
| 285 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 286 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 287 | UINT32 trash; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 288 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 289 | fd_set readFds; |
| 290 | struct timeval tv, *tvPtr; |
| 291 | int32_t timeoutMsecs = timeout % 1000; |
| 292 | int iResult; |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 293 | unsigned char responseSizeDelta = 0; |
wcarthur | 1c82759 | 2016-04-27 11:07:51 -0400 | [diff] [blame] | 294 | printf_type rmPrefix; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 295 | |
| 296 | rval = CommonReceiveChecks( tctiContext, response_size, response_buffer ); |
| 297 | if( rval != TSS2_RC_SUCCESS ) |
| 298 | { |
| 299 | goto retSocketReceiveTpmResponse; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 302 | if (tcti_intel->status.rmDebugPrefix == 1) |
wcarthur | 1c82759 | 2016-04-27 11:07:51 -0400 | [diff] [blame] | 303 | rmPrefix = RM_PREFIX; |
| 304 | else |
| 305 | rmPrefix = NO_PREFIX; |
| 306 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 307 | if( timeout == TSS2_TCTI_TIMEOUT_BLOCK ) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 308 | { |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 309 | tvPtr = 0; |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | tv.tv_sec = timeout / 1000; |
| 314 | tv.tv_usec = timeoutMsecs * 1000; |
| 315 | tvPtr = &tv; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 316 | } |
| 317 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 318 | FD_ZERO( &readFds ); |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 319 | FD_SET(tcti_intel->tpmSock, &readFds); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 320 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 321 | iResult = select(tcti_intel->tpmSock + 1, &readFds, 0, 0, tvPtr); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 322 | if( iResult == 0 ) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 323 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 324 | TCTI_LOG(tctiContext, rmPrefix, "select failed due to timeout, socket #: 0x%x\n", tcti_intel->tpmSock); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 325 | rval = TSS2_TCTI_RC_TRY_AGAIN; |
| 326 | goto retSocketReceiveTpmResponse; |
| 327 | } |
| 328 | else if( iResult == SOCKET_ERROR ) |
| 329 | { |
wcarthur | 1c82759 | 2016-04-27 11:07:51 -0400 | [diff] [blame] | 330 | TCTI_LOG( tctiContext, rmPrefix, "select failed with socket error: %d\n", WSAGetLastError() ); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 331 | rval = TSS2_TCTI_RC_IO_ERROR; |
| 332 | goto retSocketReceiveTpmResponse; |
| 333 | } |
| 334 | else if ( iResult != 1 ) |
| 335 | { |
wcarthur | 1c82759 | 2016-04-27 11:07:51 -0400 | [diff] [blame] | 336 | TCTI_LOG( tctiContext, rmPrefix, "select failed, read the wrong # of bytes: %d\n", iResult ); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 337 | rval = TSS2_TCTI_RC_IO_ERROR; |
| 338 | goto retSocketReceiveTpmResponse; |
| 339 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 340 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 341 | if (tcti_intel->status.protocolResponseSizeReceived != 1) |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 342 | { |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 343 | // Receive the size of the response. |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 344 | rval = tctiRecvBytes(tctiContext, tcti_intel->tpmSock, (unsigned char *)&tcti_intel->responseSize, 4); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 345 | if( rval != TSS2_RC_SUCCESS ) |
| 346 | goto retSocketReceiveTpmResponse; |
| 347 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 348 | tcti_intel->responseSize = BE_TO_HOST_32(tcti_intel->responseSize); |
| 349 | tcti_intel->status.protocolResponseSizeReceived = 1; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 350 | } |
| 351 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 352 | if( response_buffer == NULL ) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 353 | { |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 354 | // In this case, just return the size |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 355 | *response_size = tcti_intel->responseSize; |
| 356 | tcti_intel->status.protocolResponseSizeReceived = 1; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 357 | goto retSocketReceiveTpmResponse; |
| 358 | } |
| 359 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 360 | if (*response_size < tcti_intel->responseSize) |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 361 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 362 | *response_size = tcti_intel->responseSize; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 363 | rval = TSS2_TCTI_RC_INSUFFICIENT_BUFFER; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 364 | |
| 365 | |
| 366 | // If possible, receive tag from TPM. |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 367 | if (*response_size >= sizeof( TPM_ST ) && tcti_intel->status.tagReceived == 0) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 368 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 369 | if(TSS2_RC_SUCCESS != tctiRecvBytes(tctiContext, tcti_intel->tpmSock, (unsigned char *)&tcti_intel->tag, 2)) |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 370 | { |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 371 | goto retSocketReceiveTpmResponse; |
wcarthur | 2efe291 | 2015-11-16 11:19:42 -0500 | [diff] [blame] | 372 | } |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 373 | else |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 374 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 375 | tcti_intel->status.tagReceived = 1; |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 376 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 377 | } |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 378 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 379 | // If possible, receive response size from TPM |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 380 | if (*response_size >= (sizeof(TPM_ST) + sizeof(TPM_RC)) && tcti_intel->status.responseSizeReceived == 0) |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 381 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 382 | if(TSS2_RC_SUCCESS != tctiRecvBytes(tctiContext, tcti_intel->tpmSock, (unsigned char *)&tcti_intel->responseSize, 4)) |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 383 | { |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 384 | goto retSocketReceiveTpmResponse; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 385 | } |
| 386 | else |
| 387 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 388 | tcti_intel->responseSize = BE_TO_HOST_32 (tcti_intel->responseSize); |
| 389 | tcti_intel->status.responseSizeReceived = 1; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | } |
| 393 | else |
| 394 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 395 | if (tcti_intel->status.debugMsgEnabled == 1 && |
| 396 | tcti_intel->responseSize > 0) |
Will-nuc | e0d3ec2 | 2015-12-09 15:09:49 -0500 | [diff] [blame] | 397 | { |
| 398 | #ifdef DEBUG |
wcarthur | 1c82759 | 2016-04-27 11:07:51 -0400 | [diff] [blame] | 399 | TCTI_LOG( tctiContext, rmPrefix, "Response Received: " ); |
Will-nuc | e0d3ec2 | 2015-12-09 15:09:49 -0500 | [diff] [blame] | 400 | #endif |
| 401 | #ifdef DEBUG_SOCKETS |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 402 | TCTI_LOG(tctiContext, rmPrefix, "from socket #0x%x:\n", tcti_intel->tpmSock); |
Will-nuc | e0d3ec2 | 2015-12-09 15:09:49 -0500 | [diff] [blame] | 403 | #endif |
| 404 | } |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 405 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 406 | if (tcti_intel->status.tagReceived == 1) |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 407 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 408 | *(TPM_ST *)response_buffer = tcti_intel->tag; |
Tadeusz Struk | 83d12be | 2017-10-05 14:58:30 -0700 | [diff] [blame] | 409 | responseSizeDelta += sizeof(TPM_ST); |
| 410 | response_buffer += sizeof(TPM_ST); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 411 | } |
| 412 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 413 | if (tcti_intel->status.responseSizeReceived == 1) |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 414 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 415 | *(TPM_RC *)response_buffer = HOST_TO_BE_32(tcti_intel->responseSize); |
Tadeusz Struk | 83d12be | 2017-10-05 14:58:30 -0700 | [diff] [blame] | 416 | responseSizeDelta += sizeof(TPM_RC); |
| 417 | response_buffer += sizeof(TPM_RC); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // Receive the TPM response. |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 421 | rval = tctiRecvBytes(tctiContext, tcti_intel->tpmSock, (unsigned char *)response_buffer, tcti_intel->responseSize - responseSizeDelta ); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 422 | if( rval != TSS2_RC_SUCCESS ) |
| 423 | goto retSocketReceiveTpmResponse; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 424 | |
wcarthur | 2efe291 | 2015-11-16 11:19:42 -0500 | [diff] [blame] | 425 | #ifdef DEBUG |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 426 | if (tcti_intel->status.debugMsgEnabled == 1) |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 427 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 428 | DEBUG_PRINT_BUFFER(rmPrefix, response_buffer, tcti_intel->responseSize); |
wcarthur | 0651af2 | 2015-11-25 15:01:22 -0500 | [diff] [blame] | 429 | } |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 430 | #endif |
wcarthur | 0651af2 | 2015-11-25 15:01:22 -0500 | [diff] [blame] | 431 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 432 | // Receive the appended four bytes of 0's |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 433 | rval = tctiRecvBytes(tctiContext, tcti_intel->tpmSock, (unsigned char *)&trash, 4); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 434 | if( rval != TSS2_RC_SUCCESS ) |
| 435 | goto retSocketReceiveTpmResponse; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 438 | if (tcti_intel->responseSize < *response_size) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 439 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 440 | *response_size = tcti_intel->responseSize; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 441 | } |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 442 | |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 443 | tcti_intel->status.commandSent = 0; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 444 | |
| 445 | // Turn cancel off. |
wcarthur | 2efe291 | 2015-11-16 11:19:42 -0500 | [diff] [blame] | 446 | if( rval == TSS2_RC_SUCCESS ) |
| 447 | { |
| 448 | rval = (TSS2_RC)PlatformCommand( tctiContext, MS_SIM_CANCEL_OFF ); |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | // Ignore return value so earlier error code is preserved. |
| 453 | PlatformCommand( tctiContext, MS_SIM_CANCEL_OFF ); |
| 454 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 455 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 456 | retSocketReceiveTpmResponse: |
Tadeusz Struk | 83d12be | 2017-10-05 14:58:30 -0700 | [diff] [blame] | 457 | if (rval == TSS2_RC_SUCCESS && response_buffer != NULL) |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 458 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 459 | tcti_intel->previousStage = TCTI_STAGE_RECEIVE_RESPONSE; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 460 | } |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 461 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 462 | return rval; |
| 463 | } |
| 464 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 465 | #define HOSTNAME_LENGTH 200 |
| 466 | #define PORT_LENGTH 4 |
| 467 | |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 468 | /** |
| 469 | * This function sends the Microsoft simulator the MS_SIM_POWER_ON and |
| 470 | * MS_SIM_NV_ON commands using the PlatformCommand mechanism. Without |
| 471 | * these the simulator will respond with zero sized buffer which causes |
| 472 | * the TSS to freak out. Sending this command more than once is harmelss |
| 473 | * so it's advisable to call this function as part of the TCTI context |
| 474 | * initialization just to be sure. |
| 475 | * |
| 476 | * NOTE: The caller will still need to call Tss2_Sys_Startup. If they |
| 477 | * don't, an error will be returned from each call till they do but |
| 478 | * the error will at least be meaningful (TPM_RC_INITIALIZE). |
| 479 | */ |
| 480 | static TSS2_RC InitializeMsTpm2Simulator( |
| 481 | TSS2_TCTI_CONTEXT *tctiContext |
| 482 | ) |
| 483 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 484 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 485 | TSS2_RC rval; |
| 486 | |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 487 | rval = PlatformCommand( tctiContext ,MS_SIM_POWER_ON ); |
| 488 | if( rval != TSS2_RC_SUCCESS ) { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 489 | CloseSockets( tcti_intel->otherSock, tcti_intel->tpmSock ); |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 490 | return rval; |
| 491 | } |
| 492 | rval = PlatformCommand( tctiContext, MS_SIM_NV_ON ); |
| 493 | if( rval != TSS2_RC_SUCCESS ) |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 494 | CloseSockets( tcti_intel->otherSock, tcti_intel->tpmSock ); |
Philip Tricca | 40e926d | 2016-09-22 20:37:28 -0700 | [diff] [blame] | 495 | |
| 496 | return rval; |
| 497 | } |
| 498 | |
Philip Tricca | d1bbedb | 2016-02-14 20:39:59 -0800 | [diff] [blame] | 499 | TSS2_RC InitSocketTcti ( |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 500 | TSS2_TCTI_CONTEXT *tctiContext, // OUT |
| 501 | size_t *contextSize, // IN/OUT |
Philip Tricca | 6feb976 | 2016-02-23 10:44:09 -0800 | [diff] [blame] | 502 | const TCTI_SOCKET_CONF *conf, // IN |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 503 | const uint8_t serverSockets |
| 504 | ) |
| 505 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 506 | TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 507 | TSS2_RC rval = TSS2_RC_SUCCESS; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 508 | SOCKET otherSock; |
| 509 | SOCKET tpmSock; |
| 510 | |
Philip Tricca | 4ccb532 | 2017-06-29 08:07:17 -0700 | [diff] [blame] | 511 | if( tctiContext == NULL && contextSize == NULL ) |
| 512 | { |
| 513 | return TSS2_TCTI_RC_BAD_VALUE; |
| 514 | } |
| 515 | else if( tctiContext == NULL ) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 516 | { |
| 517 | *contextSize = sizeof( TSS2_TCTI_CONTEXT_INTEL ); |
| 518 | return TSS2_RC_SUCCESS; |
| 519 | } |
Philip Tricca | 4ccb532 | 2017-06-29 08:07:17 -0700 | [diff] [blame] | 520 | else if( conf == NULL ) |
| 521 | { |
| 522 | return TSS2_TCTI_RC_BAD_VALUE; |
| 523 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 524 | else |
| 525 | { |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 526 | // Init TCTI context. |
Philip Tricca | 7b4948e | 2016-04-18 20:43:59 -0700 | [diff] [blame] | 527 | TSS2_TCTI_MAGIC( tctiContext ) = TCTI_MAGIC; |
| 528 | TSS2_TCTI_VERSION( tctiContext ) = TCTI_VERSION; |
| 529 | TSS2_TCTI_TRANSMIT( tctiContext ) = SocketSendTpmCommand; |
| 530 | TSS2_TCTI_RECEIVE( tctiContext ) = SocketReceiveTpmResponse; |
| 531 | TSS2_TCTI_FINALIZE( tctiContext ) = SocketFinalize; |
| 532 | TSS2_TCTI_CANCEL( tctiContext ) = SocketCancel; |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 533 | TSS2_TCTI_GET_POLL_HANDLES( tctiContext ) = SocketGetPollHandles; |
Philip Tricca | 7b4948e | 2016-04-18 20:43:59 -0700 | [diff] [blame] | 534 | TSS2_TCTI_SET_LOCALITY( tctiContext ) = SocketSetLocality; |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 535 | tcti_intel->status.debugMsgEnabled = 0; |
| 536 | tcti_intel->status.locality = 3; |
| 537 | tcti_intel->status.commandSent = 0; |
| 538 | tcti_intel->status.rmDebugPrefix = 0; |
| 539 | tcti_intel->status.tagReceived = 0; |
| 540 | tcti_intel->status.responseSizeReceived = 0; |
| 541 | tcti_intel->status.protocolResponseSizeReceived = 0; |
| 542 | tcti_intel->currentTctiContext = 0; |
| 543 | tcti_intel->previousStage = TCTI_STAGE_INITIALIZE; |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 544 | TCTI_LOG_CALLBACK( tctiContext ) = conf->logCallback; |
Philip Tricca | 4eb2f35 | 2016-03-31 05:55:35 -0700 | [diff] [blame] | 545 | TCTI_LOG_BUFFER_CALLBACK( tctiContext ) = conf->logBufferCallback; |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 546 | TCTI_LOG_DATA( tctiContext ) = conf->logData; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 547 | |
Philip Tricca | 14d9fbc | 2017-07-04 10:52:14 -0700 | [diff] [blame] | 548 | rval = (TSS2_RC) InitSockets( conf->hostname, conf->port, &otherSock, &tpmSock, TCTI_LOG_CALLBACK( tctiContext ), TCTI_LOG_DATA( tctiContext) ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 549 | if( rval == TSS2_RC_SUCCESS ) |
| 550 | { |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 551 | tcti_intel->otherSock = otherSock; |
| 552 | tcti_intel->tpmSock = tpmSock; |
Tadeusz Struk | 83d12be | 2017-10-05 14:58:30 -0700 | [diff] [blame] | 553 | rval = InitializeMsTpm2Simulator(tctiContext); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 554 | } |
| 555 | else |
| 556 | { |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 557 | CloseSockets( otherSock, tpmSock); |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 558 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | return rval; |
| 562 | } |