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