Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 1 | //**********************************************************************; |
| 2 | // Copyright (c) 2015, Intel Corporation |
| 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 | |
| 28 | // |
| 29 | // NOTE: this file is used in two places: application SAPI (to |
| 30 | // communicate with RM) and RM calls to SAPI (to communicate with |
| 31 | // TPM simulator. |
| 32 | // |
| 33 | // There will be a few small differences between the two uses and |
| 34 | // these will be handled via #ifdef's and different header files. |
| 35 | // |
| 36 | |
| 37 | // |
| 38 | // NOTE: uncomment following if you think you need to see all |
| 39 | // socket communications. |
| 40 | // |
| 41 | //#define DEBUG_SOCKETS |
| 42 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> // Needed for _wtoi |
| 45 | |
Philip Tricca | c3dedc2 | 2016-01-15 13:47:22 -0800 | [diff] [blame] | 46 | #include <tss2/tpm20.h> |
Philip Tricca | 785e88d | 2016-02-13 12:02:44 -0800 | [diff] [blame] | 47 | #include <tcti/tcti_socket.h> |
Philip Tricca | c3dedc2 | 2016-01-15 13:47:22 -0800 | [diff] [blame] | 48 | #include "sysapi_util.h" |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 49 | #include "debug.h" |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 50 | #include "commonchecks.h" |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 51 | #include "logging.h" |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 52 | |
| 53 | #ifdef __cplusplus |
| 54 | extern "C" { |
| 55 | #endif |
| 56 | |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 57 | extern UINT8 simulator; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 58 | |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 59 | 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] | 60 | { |
| 61 | TSS2_RC result = 0; |
| 62 | result = recvBytes( sock, data, len); |
| 63 | if (result == SOCKET_ERROR) { |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 64 | 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] | 65 | return TSS2_TCTI_RC_IO_ERROR; |
| 66 | } |
| 67 | #ifdef DEBUG_SOCKETS |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 68 | 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^] | 69 | TCTI_LOG_BUFFER( tctiContext, NO_PREFIX, data, len ); |
Philip Tricca | a06f237 | 2016-03-30 17:13:11 -0700 | [diff] [blame] | 70 | #endif |
| 71 | |
| 72 | return TSS2_RC_SUCCESS; |
| 73 | } |
| 74 | |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 75 | 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] | 76 | { |
| 77 | TSS2_RC ret = TSS2_RC_SUCCESS; |
| 78 | |
| 79 | #ifdef DEBUG_SOCKETS |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 80 | 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^] | 81 | TCTI_LOG_BUFFER( tctiContext, NO_PREFIX, (UINT8 *)data, len ); |
Philip Tricca | 4ee284b | 2016-03-30 17:40:39 -0700 | [diff] [blame] | 82 | #endif |
| 83 | |
| 84 | ret = sendBytes( sock, data, len); |
| 85 | if (ret != TSS2_RC_SUCCESS) |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 86 | 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] | 87 | return ret; |
| 88 | } |
| 89 | |
Philip Tricca | d1bbedb | 2016-02-14 20:39:59 -0800 | [diff] [blame] | 90 | TSS2_RC SendSessionEndSocketTcti( |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 91 | TSS2_TCTI_CONTEXT *tctiContext, /* in */ |
| 92 | UINT8 tpmCmdServer ) |
| 93 | { |
| 94 | UINT32 tpmSendCommand = TPM_SESSION_END; // Value for "send command" to MS simulator. |
| 95 | SOCKET sock; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 96 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 97 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 98 | if( tpmCmdServer ) |
| 99 | { |
| 100 | sock = TCTI_CONTEXT_INTEL->tpmSock; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | sock = TCTI_CONTEXT_INTEL->otherSock; |
| 105 | } |
| 106 | |
| 107 | tpmSendCommand = CHANGE_ENDIAN_DWORD(tpmSendCommand); |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 108 | rval = tctiSendBytes( tctiContext, sock, (char unsigned *)&tpmSendCommand, 4 ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 109 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 110 | return( rval ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 113 | TSS2_RC SocketSendTpmCommand( |
| 114 | TSS2_TCTI_CONTEXT *tctiContext, /* in */ |
| 115 | size_t command_size, /* in */ |
| 116 | uint8_t *command_buffer /* in */ |
| 117 | ) |
| 118 | { |
| 119 | UINT32 tpmSendCommand = MS_SIM_TPM_SEND_COMMAND; // Value for "send command" to MS simulator. |
| 120 | UINT32 cnt, cnt1; |
| 121 | UINT8 locality; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 122 | TSS2_RC rval = TSS2_RC_SUCCESS; |
wcarthur1 | aee953e | 2016-04-01 15:13:24 -0700 | [diff] [blame] | 123 | |
| 124 | #ifdef DEBUG |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 125 | UINT32 commandCode ; |
wcarthur1 | aee953e | 2016-04-01 15:13:24 -0700 | [diff] [blame] | 126 | #endif |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 127 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 128 | #ifdef SAPI_CLIENT |
| 129 | UINT8 debugMsgLevel, statusBits; |
| 130 | #endif |
Will-nuc | 3ebfeb9 | 2015-10-29 15:53:27 -0400 | [diff] [blame] | 131 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 132 | rval = CommonSendChecks( tctiContext, command_buffer ); |
| 133 | if( rval != TSS2_RC_SUCCESS ) |
| 134 | { |
| 135 | goto returnFromSocketSendTpmCommand; |
| 136 | } |
Will-nuc | 3ebfeb9 | 2015-10-29 15:53:27 -0400 | [diff] [blame] | 137 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 138 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED ) |
| 139 | { |
| 140 | #ifdef DEBUG |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 141 | TCTI_LOG( tctiContext, NO_PREFIX, "\n" ); |
wcarthur | 0234001 | 2015-10-28 08:32:42 -0400 | [diff] [blame] | 142 | if( commandCode >= TPM_CC_NV_UndefineSpaceSpecial && commandCode <= TPM_CC_PolicyNvWritten ) |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 143 | TCTI_LOG( tctiContext, NO_PREFIX, "Cmd sent: %s\n", commandCodeStrings[ commandCode - TPM_CC_FIRST ] ); |
wcarthur | 0234001 | 2015-10-28 08:32:42 -0400 | [diff] [blame] | 144 | else |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 145 | TCTI_LOG( tctiContext, NO_PREFIX, "Cmd sent: 0x%4.4x\n", CHANGE_ENDIAN_DWORD(commandCode ) ); |
| 146 | #endif |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 147 | #ifdef DEBUG_SOCKETS |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 148 | TCTI_LOG( tctiContext, NO_PREFIX, "Command sent on socket #0x%x: %s\n", TCTI_CONTEXT_INTEL->tpmSock, commandCodeStrings[ commandCode - TPM_CC_FIRST ] ); |
| 149 | #endif |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 150 | } |
| 151 | // Size TPM 1.2 and TPM 2.0 headers overlap exactly, we can use |
| 152 | // either 1.2 or 2.0 header to get the size. |
| 153 | cnt = CHANGE_ENDIAN_DWORD(((TPM20_Header_In *) command_buffer)->commandSize); |
| 154 | |
| 155 | // Send TPM_SEND_COMMAND |
| 156 | tpmSendCommand = CHANGE_ENDIAN_DWORD(tpmSendCommand); |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 157 | rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&tpmSendCommand, 4 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 158 | if( rval != TSS2_RC_SUCCESS ) |
| 159 | goto returnFromSocketSendTpmCommand; |
| 160 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 161 | // Send the locality |
| 162 | locality = (UINT8)( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality; |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 163 | rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&locality, 1 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 164 | if( rval != TSS2_RC_SUCCESS ) |
| 165 | goto returnFromSocketSendTpmCommand; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 166 | |
| 167 | #ifdef SAPI_CLIENT |
| 168 | // Send the debug level |
| 169 | debugMsgLevel = (UINT8)( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.debugMsgLevel; |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 170 | rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&debugMsgLevel, 1 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 171 | if( rval != TSS2_RC_SUCCESS ) |
| 172 | goto returnFromSocketSendTpmCommand; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 173 | |
| 174 | // Send status bits |
| 175 | statusBits = (UINT8)( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent; |
| 176 | statusBits |= ( (UINT8)( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.rmDebugPrefix ) << 1; |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 177 | rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&statusBits, 1 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 178 | if( rval != TSS2_RC_SUCCESS ) |
| 179 | goto returnFromSocketSendTpmCommand; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 180 | #endif |
| 181 | |
| 182 | #ifdef DEBUG |
| 183 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED ) |
| 184 | { |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 185 | TCTI_LOG( tctiContext, NO_PREFIX, "Locality = %d", ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 186 | } |
| 187 | #endif |
| 188 | |
| 189 | // Send number of bytes. |
| 190 | cnt1 = cnt; |
| 191 | cnt = CHANGE_ENDIAN_DWORD(cnt); |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 192 | rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&cnt, 4 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 193 | if( rval != TSS2_RC_SUCCESS ) |
| 194 | goto returnFromSocketSendTpmCommand; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 195 | |
| 196 | // Send the TPM command buffer |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 197 | rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)command_buffer, cnt1 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 198 | if( rval != TSS2_RC_SUCCESS ) |
| 199 | goto returnFromSocketSendTpmCommand; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 200 | |
| 201 | #ifdef DEBUG |
| 202 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED ) |
| 203 | { |
Philip Tricca | 6960f1b | 2016-03-16 17:09:50 -0700 | [diff] [blame] | 204 | DEBUG_PRINT_BUFFER( NO_PREFIX, command_buffer, cnt1 ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 205 | } |
| 206 | #endif |
| 207 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 1; |
| 208 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 209 | returnFromSocketSendTpmCommand: |
| 210 | |
| 211 | if( rval == TSS2_RC_SUCCESS ) |
| 212 | { |
| 213 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_SEND_COMMAND; |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 214 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived = 0; |
| 215 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived = 0; |
| 216 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived = 0; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | return rval; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | TSS2_RC SocketCancel( |
| 223 | TSS2_TCTI_CONTEXT *tctiContext |
| 224 | ) |
| 225 | { |
| 226 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 227 | |
| 228 | if( tctiContext == 0 ) |
| 229 | { |
| 230 | rval = TSS2_TCTI_RC_BAD_REFERENCE; |
| 231 | } |
| 232 | else if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent != 1 ) |
| 233 | { |
| 234 | rval = TSS2_TCTI_RC_BAD_SEQUENCE; |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | rval = (TSS2_RC)PlatformCommand( tctiContext, MS_SIM_CANCEL_ON ); |
| 239 | #if 0 |
| 240 | if( rval == TSS2_RC_SUCCESS ) |
| 241 | { |
| 242 | rval = (TSS2_RC)PlatformCommand( tctiContext, MS_SIM_CANCEL_OFF ); |
| 243 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED ) |
| 244 | { |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 245 | TCTI_LOG( tctiContext, NO_PREFIX, "%s sent cancel ON command:\n", interfaceName ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | #endif |
| 249 | } |
| 250 | |
| 251 | return rval; |
| 252 | } |
| 253 | |
| 254 | TSS2_RC SocketSetLocality( |
| 255 | TSS2_TCTI_CONTEXT *tctiContext, /* in */ |
| 256 | uint8_t locality /* in */ |
| 257 | ) |
| 258 | { |
| 259 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 260 | |
| 261 | if( tctiContext == 0 ) |
| 262 | { |
| 263 | rval = TSS2_TCTI_RC_BAD_REFERENCE; |
| 264 | } |
| 265 | else if( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality != locality ) |
| 266 | { |
| 267 | if ( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent == 1 ) |
| 268 | { |
| 269 | rval = TSS2_TCTI_RC_BAD_SEQUENCE; |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality = locality; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return rval; |
| 278 | } |
| 279 | |
| 280 | void CloseSockets( SOCKET otherSock, SOCKET tpmSock) |
| 281 | { |
| 282 | closesocket(otherSock); |
| 283 | closesocket(tpmSock); |
| 284 | } |
| 285 | |
wcarthur | 35aa539 | 2016-03-22 16:52:44 -0400 | [diff] [blame] | 286 | void SocketFinalize( |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 287 | TSS2_TCTI_CONTEXT *tctiContext /* in */ |
| 288 | ) |
| 289 | { |
wcarthur | 35aa539 | 2016-03-22 16:52:44 -0400 | [diff] [blame] | 290 | if( tctiContext != NULL ) |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame] | 291 | { |
| 292 | // Send session end messages to servers. |
Philip Tricca | d1bbedb | 2016-02-14 20:39:59 -0800 | [diff] [blame] | 293 | SendSessionEndSocketTcti( tctiContext, 1 ); |
| 294 | SendSessionEndSocketTcti( tctiContext, 0 ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 295 | |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame] | 296 | CloseSockets( TCTI_CONTEXT_INTEL->otherSock, TCTI_CONTEXT_INTEL->tpmSock ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 297 | |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame] | 298 | free( tctiContext ); |
| 299 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 302 | TSS2_RC SocketReceiveTpmResponse( |
| 303 | TSS2_TCTI_CONTEXT *tctiContext, /* in */ |
| 304 | size_t *response_size, /* out */ |
| 305 | unsigned char *response_buffer, /* in */ |
| 306 | int32_t timeout |
| 307 | ) |
| 308 | { |
| 309 | UINT32 trash; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 310 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 311 | fd_set readFds; |
| 312 | struct timeval tv, *tvPtr; |
| 313 | int32_t timeoutMsecs = timeout % 1000; |
| 314 | int iResult; |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 315 | unsigned char responseSizeDelta = 0; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 316 | |
| 317 | rval = CommonReceiveChecks( tctiContext, response_size, response_buffer ); |
| 318 | if( rval != TSS2_RC_SUCCESS ) |
| 319 | { |
| 320 | goto retSocketReceiveTpmResponse; |
| 321 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 322 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 323 | if( timeout == TSS2_TCTI_TIMEOUT_BLOCK ) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 324 | { |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 325 | tvPtr = 0; |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | tv.tv_sec = timeout / 1000; |
| 330 | tv.tv_usec = timeoutMsecs * 1000; |
| 331 | tvPtr = &tv; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 332 | } |
| 333 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 334 | FD_ZERO( &readFds ); |
| 335 | FD_SET( TCTI_CONTEXT_INTEL->tpmSock, &readFds ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 336 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 337 | iResult = select( TCTI_CONTEXT_INTEL->tpmSock+1, &readFds, 0, 0, tvPtr ); |
| 338 | if( iResult == 0 ) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 339 | { |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 340 | TCTI_LOG( tctiContext, NO_PREFIX, "select failed due to timeout, socket #: 0x%x\n", TCTI_CONTEXT_INTEL->tpmSock ); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 341 | rval = TSS2_TCTI_RC_TRY_AGAIN; |
| 342 | goto retSocketReceiveTpmResponse; |
| 343 | } |
| 344 | else if( iResult == SOCKET_ERROR ) |
| 345 | { |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 346 | TCTI_LOG( tctiContext, NO_PREFIX, "select failed with socket error: %d\n", WSAGetLastError() ); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 347 | rval = TSS2_TCTI_RC_IO_ERROR; |
| 348 | goto retSocketReceiveTpmResponse; |
| 349 | } |
| 350 | else if ( iResult != 1 ) |
| 351 | { |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 352 | TCTI_LOG( tctiContext, NO_PREFIX, "select failed, read the wrong # of bytes: %d\n", iResult ); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 353 | rval = TSS2_TCTI_RC_IO_ERROR; |
| 354 | goto retSocketReceiveTpmResponse; |
| 355 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 356 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 357 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived != 1 ) |
| 358 | { |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 359 | // Receive the size of the response. |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 360 | rval = tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)& (((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize ), 4 ); |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 361 | if( rval != TSS2_RC_SUCCESS ) |
| 362 | goto retSocketReceiveTpmResponse; |
| 363 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 364 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize = CHANGE_ENDIAN_DWORD( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize ); |
| 365 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived = 1; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 366 | } |
| 367 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 368 | if( response_buffer == NULL ) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 369 | { |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 370 | // In this case, just return the size |
| 371 | *response_size = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize; |
| 372 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived = 1; |
| 373 | goto retSocketReceiveTpmResponse; |
| 374 | } |
| 375 | |
| 376 | if( *response_size < ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize ) |
| 377 | { |
| 378 | *response_size = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize; |
| 379 | rval = TSS2_TCTI_RC_INSUFFICIENT_BUFFER; |
| 380 | |
| 381 | |
| 382 | // If possible, receive tag from TPM. |
| 383 | if( *response_size >= sizeof( TPM_ST ) && ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived == 0 ) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 384 | { |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 385 | if( TSS2_RC_SUCCESS != tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->tag ), 2 ) ) |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 386 | { |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 387 | goto retSocketReceiveTpmResponse; |
wcarthur | 2efe291 | 2015-11-16 11:19:42 -0500 | [diff] [blame] | 388 | } |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 389 | else |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 390 | { |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 391 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived = 1; |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 392 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 393 | } |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 394 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 395 | // If possible, receive response size from TPM |
| 396 | if( *response_size >= ( sizeof( TPM_ST ) + sizeof( TPM_RC ) ) && ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived == 0 ) |
| 397 | { |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 398 | if( TSS2_RC_SUCCESS != tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->responseSize ), 4 ) ) |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 399 | { |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 400 | goto retSocketReceiveTpmResponse; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 401 | } |
| 402 | else |
| 403 | { |
| 404 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize = CHANGE_ENDIAN_DWORD( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize ); |
| 405 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived = 1; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | else |
| 410 | { |
Will-nuc | e0d3ec2 | 2015-12-09 15:09:49 -0500 | [diff] [blame] | 411 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED && |
| 412 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize > 0 ) |
| 413 | { |
| 414 | #ifdef DEBUG |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 415 | TCTI_LOG( tctiContext, NO_PREFIX, "Response Received: " ); |
Will-nuc | e0d3ec2 | 2015-12-09 15:09:49 -0500 | [diff] [blame] | 416 | #endif |
| 417 | #ifdef DEBUG_SOCKETS |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 418 | TCTI_LOG( tctiContext, NO_PREFIX, "from socket #0x%x:\n", TCTI_CONTEXT_INTEL->tpmSock ); |
Will-nuc | e0d3ec2 | 2015-12-09 15:09:49 -0500 | [diff] [blame] | 419 | #endif |
| 420 | } |
| 421 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 422 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived == 1 ) |
| 423 | { |
| 424 | *(TPM_ST *)response_buffer = ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->tag; |
| 425 | responseSizeDelta += sizeof( TPM_ST ); |
| 426 | response_buffer += sizeof( TPM_ST ); |
| 427 | } |
| 428 | |
| 429 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived == 1 ) |
| 430 | { |
| 431 | *(TPM_RC *)response_buffer = CHANGE_ENDIAN_DWORD( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->responseSize ); |
| 432 | responseSizeDelta += sizeof( TPM_RC ); |
| 433 | response_buffer += sizeof( TPM_RC ); |
| 434 | } |
| 435 | |
| 436 | // Receive the TPM response. |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 437 | rval = tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)response_buffer, ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize - responseSizeDelta ); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 438 | if( rval != TSS2_RC_SUCCESS ) |
| 439 | goto retSocketReceiveTpmResponse; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 440 | |
wcarthur | 2efe291 | 2015-11-16 11:19:42 -0500 | [diff] [blame] | 441 | #ifdef DEBUG |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 442 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED ) |
| 443 | { |
Philip Tricca | 6960f1b | 2016-03-16 17:09:50 -0700 | [diff] [blame] | 444 | DEBUG_PRINT_BUFFER( NO_PREFIX, response_buffer, ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize ); |
wcarthur | 0651af2 | 2015-11-25 15:01:22 -0500 | [diff] [blame] | 445 | } |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 446 | #endif |
wcarthur | 0651af2 | 2015-11-25 15:01:22 -0500 | [diff] [blame] | 447 | |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 448 | // Receive the appended four bytes of 0's |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 449 | rval = tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&trash, 4 ); |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 450 | if( rval != TSS2_RC_SUCCESS ) |
| 451 | goto retSocketReceiveTpmResponse; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 452 | } |
| 453 | |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 454 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize < *response_size ) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 455 | { |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 456 | *response_size = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 0; |
| 460 | |
| 461 | // Turn cancel off. |
wcarthur | 2efe291 | 2015-11-16 11:19:42 -0500 | [diff] [blame] | 462 | if( rval == TSS2_RC_SUCCESS ) |
| 463 | { |
| 464 | rval = (TSS2_RC)PlatformCommand( tctiContext, MS_SIM_CANCEL_OFF ); |
| 465 | } |
| 466 | else |
| 467 | { |
| 468 | // Ignore return value so earlier error code is preserved. |
| 469 | PlatformCommand( tctiContext, MS_SIM_CANCEL_OFF ); |
| 470 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 471 | |
| 472 | if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED ) |
| 473 | { |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 474 | // TCTI_LOG( tctiContext, NO_PREFIX, "%s sent cancel OFF command:\n", interfaceName ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | retSocketReceiveTpmResponse: |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 478 | if( rval == TSS2_RC_SUCCESS && |
| 479 | response_buffer != NULL ) |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 480 | { |
| 481 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_RECEIVE_RESPONSE; |
| 482 | } |
| 483 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 484 | return rval; |
| 485 | } |
| 486 | |
| 487 | #ifdef __cplusplus |
| 488 | } |
| 489 | #endif |
| 490 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 491 | #define HOSTNAME_LENGTH 200 |
| 492 | #define PORT_LENGTH 4 |
| 493 | |
Philip Tricca | d1bbedb | 2016-02-14 20:39:59 -0800 | [diff] [blame] | 494 | TSS2_RC InitSocketTcti ( |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 495 | TSS2_TCTI_CONTEXT *tctiContext, // OUT |
| 496 | size_t *contextSize, // IN/OUT |
Philip Tricca | 6feb976 | 2016-02-23 10:44:09 -0800 | [diff] [blame] | 497 | const TCTI_SOCKET_CONF *conf, // IN |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 498 | const uint8_t serverSockets |
| 499 | ) |
| 500 | { |
| 501 | TSS2_RC rval = TSS2_RC_SUCCESS; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 502 | SOCKET otherSock; |
| 503 | SOCKET tpmSock; |
| 504 | |
| 505 | if( tctiContext == NULL ) |
| 506 | { |
| 507 | *contextSize = sizeof( TSS2_TCTI_CONTEXT_INTEL ); |
| 508 | return TSS2_RC_SUCCESS; |
| 509 | } |
| 510 | else |
| 511 | { |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 512 | // Init TCTI context. |
Philip Tricca | 7484053 | 2016-03-25 19:14:27 -0700 | [diff] [blame] | 513 | ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->magic = TCTI_MAGIC; |
| 514 | ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->version = TCTI_VERSION; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 515 | ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->transmit = SocketSendTpmCommand; |
| 516 | ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->receive = SocketReceiveTpmResponse; |
| 517 | ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->finalize = SocketFinalize; |
| 518 | ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->cancel = SocketCancel; |
| 519 | ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->getPollHandles = 0; |
| 520 | ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->setLocality = SocketSetLocality; |
| 521 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality = 3; |
| 522 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 0; |
| 523 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.rmDebugPrefix = 0; |
| 524 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->currentTctiContext = 0; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 525 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_INITIALIZE; |
wcarthur | a05cdcc | 2015-12-04 18:07:16 -0500 | [diff] [blame] | 526 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived = 0; |
| 527 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived = 0; |
| 528 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived = 0; |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 529 | TCTI_LOG_CALLBACK( tctiContext ) = conf->logCallback; |
Philip Tricca | 4eb2f35 | 2016-03-31 05:55:35 -0700 | [diff] [blame^] | 530 | TCTI_LOG_BUFFER_CALLBACK( tctiContext ) = conf->logBufferCallback; |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 531 | TCTI_LOG_DATA( tctiContext ) = conf->logData; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 532 | |
Philip Tricca | bcc7303 | 2016-03-30 19:50:54 -0700 | [diff] [blame] | 533 | rval = (TSS2_RC) InitSockets( conf->hostname, conf->port, serverSockets, &otherSock, &tpmSock, TCTI_LOG_CALLBACK( tctiContext ), TCTI_LOG_DATA( tctiContext) ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 534 | if( rval == TSS2_RC_SUCCESS ) |
| 535 | { |
| 536 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->otherSock = otherSock; |
| 537 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->tpmSock = tpmSock; |
| 538 | } |
| 539 | else |
| 540 | { |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 541 | CloseSockets( otherSock, tpmSock); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 542 | } |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | return rval; |
| 546 | } |
| 547 | |
Philip Tricca | 6feb976 | 2016-02-23 10:44:09 -0800 | [diff] [blame] | 548 | TSS2_RC TeardownSocketTcti (TSS2_TCTI_CONTEXT *tctiContext) |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 549 | { |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 550 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->finalize( tctiContext ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 551 | |
| 552 | return TSS2_RC_SUCCESS; |
| 553 | } |
| 554 | |
| 555 | |