blob: 4afe2b710a241b15085af8f6acd2733fa2f8a7d4 [file] [log] [blame]
Will Arthur54e04e42015-07-15 11:29:25 -04001//**********************************************************************;
2// Copyright (c) 2015, Intel Corporation
3// All rights reserved.
Philip Tricca1ea84a52015-11-19 18:07:06 -08004//
5// Redistribution and use in source and binary forms, with or without
Will Arthur54e04e42015-07-15 11:29:25 -04006// modification, are permitted provided that the following conditions are met:
Philip Tricca1ea84a52015-11-19 18:07:06 -08007//
8// 1. Redistributions of source code must retain the above copyright notice,
Will Arthur54e04e42015-07-15 11:29:25 -04009// this list of conditions and the following disclaimer.
Philip Tricca1ea84a52015-11-19 18:07:06 -080010//
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 Arthur54e04e42015-07-15 11:29:25 -040013// and/or other materials provided with the distribution.
Philip Tricca1ea84a52015-11-19 18:07:06 -080014//
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 Arthur54e04e42015-07-15 11:29:25 -040025// 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 Arthur54e04e42015-07-15 11:29:25 -040043#include <stdio.h>
44#include <stdlib.h> // Needed for _wtoi
45
Philip Triccae03b8462016-07-08 19:51:23 -070046#include <sapi/tpm20.h>
Philip Tricca785e88d2016-02-13 12:02:44 -080047#include <tcti/tcti_socket.h>
Philip Triccac3dedc22016-01-15 13:47:22 -080048#include "sysapi_util.h"
Will Arthurca8e7f32015-08-03 15:35:19 -040049#include "debug.h"
wcarthureedecd62015-11-20 16:59:45 -050050#include "commonchecks.h"
Philip Triccabcc73032016-03-30 19:50:54 -070051#include "logging.h"
Will Arthur54e04e42015-07-15 11:29:25 -040052
53#ifdef __cplusplus
54extern "C" {
55#endif
56
Philip Triccabcc73032016-03-30 19:50:54 -070057static TSS2_RC tctiRecvBytes( TSS2_TCTI_CONTEXT *tctiContext, SOCKET sock, unsigned char *data, int len )
Philip Triccaa06f2372016-03-30 17:13:11 -070058{
59 TSS2_RC result = 0;
60 result = recvBytes( sock, data, len);
wcarthur1e2e04182016-04-11 07:37:55 -070061 if ( (INT32)result == SOCKET_ERROR) {
Philip Triccabcc73032016-03-30 19:50:54 -070062 TCTI_LOG( tctiContext, NO_PREFIX, "In recvBytes, recv failed (socket: 0x%x) with error: %d\n", sock, WSAGetLastError() );
Philip Triccaa06f2372016-03-30 17:13:11 -070063 return TSS2_TCTI_RC_IO_ERROR;
64 }
65#ifdef DEBUG_SOCKETS
Philip Triccabcc73032016-03-30 19:50:54 -070066 TCTI_LOG( tctiContext, NO_PREFIX, "Receive Bytes from socket #0x%x: \n", sock );
Philip Tricca4eb2f352016-03-31 05:55:35 -070067 TCTI_LOG_BUFFER( tctiContext, NO_PREFIX, data, len );
Philip Triccaa06f2372016-03-30 17:13:11 -070068#endif
69
70 return TSS2_RC_SUCCESS;
71}
72
Philip Triccabcc73032016-03-30 19:50:54 -070073static TSS2_RC tctiSendBytes( TSS2_TCTI_CONTEXT *tctiContext, SOCKET sock, const unsigned char *data, int len )
Philip Tricca4ee284b2016-03-30 17:40:39 -070074{
75 TSS2_RC ret = TSS2_RC_SUCCESS;
76
77#ifdef DEBUG_SOCKETS
Philip Triccabcc73032016-03-30 19:50:54 -070078 TCTI_LOG( tctiContext, NO_PREFIX, "Send Bytes to socket #0x%x: \n", sock );
Philip Tricca4eb2f352016-03-31 05:55:35 -070079 TCTI_LOG_BUFFER( tctiContext, NO_PREFIX, (UINT8 *)data, len );
Philip Tricca4ee284b2016-03-30 17:40:39 -070080#endif
81
82 ret = sendBytes( sock, data, len);
83 if (ret != TSS2_RC_SUCCESS)
Philip Triccabcc73032016-03-30 19:50:54 -070084 TCTI_LOG( tctiContext, NO_PREFIX, "In recvBytes, recv failed (socket: 0x%x) with error: %d\n", sock, WSAGetLastError() );
Philip Tricca4ee284b2016-03-30 17:40:39 -070085 return ret;
86}
87
Philip Triccad1bbedb2016-02-14 20:39:59 -080088TSS2_RC SendSessionEndSocketTcti(
Will Arthur54e04e42015-07-15 11:29:25 -040089 TSS2_TCTI_CONTEXT *tctiContext, /* in */
90 UINT8 tpmCmdServer )
91{
92 UINT32 tpmSendCommand = TPM_SESSION_END; // Value for "send command" to MS simulator.
93 SOCKET sock;
wcarthureedecd62015-11-20 16:59:45 -050094 TSS2_RC rval = TSS2_RC_SUCCESS;
Philip Triccadfa41a52016-07-20 17:43:57 -070095
Will Arthur54e04e42015-07-15 11:29:25 -040096 if( tpmCmdServer )
97 {
98 sock = TCTI_CONTEXT_INTEL->tpmSock;
99 }
100 else
101 {
102 sock = TCTI_CONTEXT_INTEL->otherSock;
103 }
Philip Triccadfa41a52016-07-20 17:43:57 -0700104
Will Arthur54e04e42015-07-15 11:29:25 -0400105 tpmSendCommand = CHANGE_ENDIAN_DWORD(tpmSendCommand);
Philip Triccabcc73032016-03-30 19:50:54 -0700106 rval = tctiSendBytes( tctiContext, sock, (char unsigned *)&tpmSendCommand, 4 );
Will Arthur54e04e42015-07-15 11:29:25 -0400107
wcarthureedecd62015-11-20 16:59:45 -0500108 return( rval );
Will Arthur54e04e42015-07-15 11:29:25 -0400109}
110
Will Arthur54e04e42015-07-15 11:29:25 -0400111TSS2_RC SocketSendTpmCommand(
112 TSS2_TCTI_CONTEXT *tctiContext, /* in */
113 size_t command_size, /* in */
114 uint8_t *command_buffer /* in */
115 )
116{
117 UINT32 tpmSendCommand = MS_SIM_TPM_SEND_COMMAND; // Value for "send command" to MS simulator.
118 UINT32 cnt, cnt1;
119 UINT8 locality;
wcarthureedecd62015-11-20 16:59:45 -0500120 TSS2_RC rval = TSS2_RC_SUCCESS;
Philip Triccadfa41a52016-07-20 17:43:57 -0700121
122#ifdef DEBUG
wcarthur1c827592016-04-27 11:07:51 -0400123 UINT32 commandCode;
124 printf_type rmPrefix;
wcarthur1aee953e2016-04-01 15:13:24 -0700125#endif
wcarthur1c827592016-04-27 11:07:51 -0400126
wcarthureedecd62015-11-20 16:59:45 -0500127 rval = CommonSendChecks( tctiContext, command_buffer );
128 if( rval != TSS2_RC_SUCCESS )
129 {
130 goto returnFromSocketSendTpmCommand;
131 }
Will-nuc3ebfeb92015-10-29 15:53:27 -0400132
Will Arthur54e04e42015-07-15 11:29:25 -0400133#ifdef DEBUG
wcarthur1c827592016-04-27 11:07:51 -0400134 if( ( ( TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.rmDebugPrefix == 1 )
135 rmPrefix = RM_PREFIX;
136 else
137 rmPrefix = NO_PREFIX;
138
139 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgEnabled == 1 )
140 {
141 TCTI_LOG( tctiContext, rmPrefix, "" );
wcarthur06befc02016-04-11 12:36:47 -0400142 commandCode = CHANGE_ENDIAN_DWORD( ( (TPM20_Header_In *)command_buffer )->commandCode );
Will Arthur54e04e42015-07-15 11:29:25 -0400143#ifdef DEBUG_SOCKETS
wcarthur06befc02016-04-11 12:36:47 -0400144 TCTI_LOG( tctiContext, NO_PREFIX, "Command sent on socket #0x%x: %s\n", TCTI_CONTEXT_INTEL->tpmSock, strTpmCommandCode( commandCode ) );
wcarthur1a10b0c92016-04-14 04:25:03 -0700145#else
146 TCTI_LOG( tctiContext, NO_PREFIX, "Cmd sent: %s\n", strTpmCommandCode( commandCode ) );
147#endif
Will Arthur54e04e42015-07-15 11:29:25 -0400148 }
wcarthur1c827592016-04-27 11:07:51 -0400149#endif
Will Arthur54e04e42015-07-15 11:29:25 -0400150 // Size TPM 1.2 and TPM 2.0 headers overlap exactly, we can use
151 // either 1.2 or 2.0 header to get the size.
152 cnt = CHANGE_ENDIAN_DWORD(((TPM20_Header_In *) command_buffer)->commandSize);
153
154 // Send TPM_SEND_COMMAND
155 tpmSendCommand = CHANGE_ENDIAN_DWORD(tpmSendCommand);
Philip Triccabcc73032016-03-30 19:50:54 -0700156 rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&tpmSendCommand, 4 );
wcarthureedecd62015-11-20 16:59:45 -0500157 if( rval != TSS2_RC_SUCCESS )
158 goto returnFromSocketSendTpmCommand;
Philip Triccadfa41a52016-07-20 17:43:57 -0700159
Will Arthur54e04e42015-07-15 11:29:25 -0400160 // Send the locality
161 locality = (UINT8)( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality;
Philip Triccabcc73032016-03-30 19:50:54 -0700162 rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&locality, 1 );
wcarthureedecd62015-11-20 16:59:45 -0500163 if( rval != TSS2_RC_SUCCESS )
164 goto returnFromSocketSendTpmCommand;
Will Arthur54e04e42015-07-15 11:29:25 -0400165
Will Arthur54e04e42015-07-15 11:29:25 -0400166#ifdef DEBUG
wcarthur1c827592016-04-27 11:07:51 -0400167 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgEnabled == 1 )
Will Arthur54e04e42015-07-15 11:29:25 -0400168 {
wcarthur1c827592016-04-27 11:07:51 -0400169 TCTI_LOG( tctiContext, rmPrefix, "Locality = %d", ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality );
Will Arthur54e04e42015-07-15 11:29:25 -0400170 }
171#endif
Philip Triccadfa41a52016-07-20 17:43:57 -0700172
Will Arthur54e04e42015-07-15 11:29:25 -0400173 // Send number of bytes.
174 cnt1 = cnt;
175 cnt = CHANGE_ENDIAN_DWORD(cnt);
Philip Triccabcc73032016-03-30 19:50:54 -0700176 rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&cnt, 4 );
wcarthureedecd62015-11-20 16:59:45 -0500177 if( rval != TSS2_RC_SUCCESS )
178 goto returnFromSocketSendTpmCommand;
Philip Triccadfa41a52016-07-20 17:43:57 -0700179
Will Arthur54e04e42015-07-15 11:29:25 -0400180 // Send the TPM command buffer
Philip Triccabcc73032016-03-30 19:50:54 -0700181 rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)command_buffer, cnt1 );
wcarthureedecd62015-11-20 16:59:45 -0500182 if( rval != TSS2_RC_SUCCESS )
183 goto returnFromSocketSendTpmCommand;
Philip Triccadfa41a52016-07-20 17:43:57 -0700184
Will Arthur54e04e42015-07-15 11:29:25 -0400185#ifdef DEBUG
wcarthur1c827592016-04-27 11:07:51 -0400186 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgEnabled == 1 )
Will Arthur54e04e42015-07-15 11:29:25 -0400187 {
wcarthur1c827592016-04-27 11:07:51 -0400188 DEBUG_PRINT_BUFFER( rmPrefix, command_buffer, cnt1 );
Will Arthur54e04e42015-07-15 11:29:25 -0400189 }
190#endif
191 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 1;
192
wcarthureedecd62015-11-20 16:59:45 -0500193returnFromSocketSendTpmCommand:
194
195 if( rval == TSS2_RC_SUCCESS )
196 {
197 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_SEND_COMMAND;
wcarthura05cdcc2015-12-04 18:07:16 -0500198 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived = 0;
199 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived = 0;
200 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived = 0;
wcarthureedecd62015-11-20 16:59:45 -0500201 }
202
203 return rval;
Will Arthur54e04e42015-07-15 11:29:25 -0400204}
205
206TSS2_RC SocketCancel(
207 TSS2_TCTI_CONTEXT *tctiContext
208 )
209{
210 TSS2_RC rval = TSS2_RC_SUCCESS;
211
212 if( tctiContext == 0 )
213 {
214 rval = TSS2_TCTI_RC_BAD_REFERENCE;
215 }
216 else if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent != 1 )
217 {
218 rval = TSS2_TCTI_RC_BAD_SEQUENCE;
219 }
220 else
221 {
222 rval = (TSS2_RC)PlatformCommand( tctiContext, MS_SIM_CANCEL_ON );
223#if 0
224 if( rval == TSS2_RC_SUCCESS )
225 {
226 rval = (TSS2_RC)PlatformCommand( tctiContext, MS_SIM_CANCEL_OFF );
227 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED )
228 {
Philip Triccabcc73032016-03-30 19:50:54 -0700229 TCTI_LOG( tctiContext, NO_PREFIX, "%s sent cancel ON command:\n", interfaceName );
Will Arthur54e04e42015-07-15 11:29:25 -0400230 }
231 }
Philip Triccadfa41a52016-07-20 17:43:57 -0700232#endif
Will Arthur54e04e42015-07-15 11:29:25 -0400233 }
234
235 return rval;
236}
237
238TSS2_RC SocketSetLocality(
239 TSS2_TCTI_CONTEXT *tctiContext, /* in */
240 uint8_t locality /* in */
241 )
242{
243 TSS2_RC rval = TSS2_RC_SUCCESS;
244
245 if( tctiContext == 0 )
246 {
247 rval = TSS2_TCTI_RC_BAD_REFERENCE;
248 }
249 else if( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality != locality )
250 {
251 if ( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent == 1 )
252 {
253 rval = TSS2_TCTI_RC_BAD_SEQUENCE;
254 }
255 else
256 {
257 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality = locality;
258 }
259 }
260
261 return rval;
262}
263
wcarthur35aa5392016-03-22 16:52:44 -0400264void SocketFinalize(
Will Arthur54e04e42015-07-15 11:29:25 -0400265 TSS2_TCTI_CONTEXT *tctiContext /* in */
266 )
267{
wcarthur35aa5392016-03-22 16:52:44 -0400268 if( tctiContext != NULL )
wcarthurf979e352015-11-24 17:34:44 -0500269 {
270 // Send session end messages to servers.
Philip Triccad1bbedb2016-02-14 20:39:59 -0800271 SendSessionEndSocketTcti( tctiContext, 1 );
272 SendSessionEndSocketTcti( tctiContext, 0 );
Will Arthur54e04e42015-07-15 11:29:25 -0400273
wcarthurf979e352015-11-24 17:34:44 -0500274 CloseSockets( TCTI_CONTEXT_INTEL->otherSock, TCTI_CONTEXT_INTEL->tpmSock );
wcarthurf979e352015-11-24 17:34:44 -0500275 }
Will Arthur54e04e42015-07-15 11:29:25 -0400276}
277
Will Arthur54e04e42015-07-15 11:29:25 -0400278TSS2_RC SocketReceiveTpmResponse(
279 TSS2_TCTI_CONTEXT *tctiContext, /* in */
280 size_t *response_size, /* out */
281 unsigned char *response_buffer, /* in */
282 int32_t timeout
283 )
284{
285 UINT32 trash;
Will Arthur54e04e42015-07-15 11:29:25 -0400286 TSS2_RC rval = TSS2_RC_SUCCESS;
287 fd_set readFds;
288 struct timeval tv, *tvPtr;
289 int32_t timeoutMsecs = timeout % 1000;
290 int iResult;
wcarthura05cdcc2015-12-04 18:07:16 -0500291 unsigned char responseSizeDelta = 0;
wcarthur1c827592016-04-27 11:07:51 -0400292 printf_type rmPrefix;
wcarthureedecd62015-11-20 16:59:45 -0500293
294 rval = CommonReceiveChecks( tctiContext, response_size, response_buffer );
295 if( rval != TSS2_RC_SUCCESS )
296 {
297 goto retSocketReceiveTpmResponse;
Philip Triccadfa41a52016-07-20 17:43:57 -0700298 }
299
wcarthur1c827592016-04-27 11:07:51 -0400300 if( ( ( TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.rmDebugPrefix == 1 )
301 rmPrefix = RM_PREFIX;
302 else
303 rmPrefix = NO_PREFIX;
304
wcarthur74a92a42015-12-07 16:02:12 -0500305 if( timeout == TSS2_TCTI_TIMEOUT_BLOCK )
Will Arthur54e04e42015-07-15 11:29:25 -0400306 {
wcarthur74a92a42015-12-07 16:02:12 -0500307 tvPtr = 0;
308 }
309 else
310 {
311 tv.tv_sec = timeout / 1000;
312 tv.tv_usec = timeoutMsecs * 1000;
313 tvPtr = &tv;
Will Arthur54e04e42015-07-15 11:29:25 -0400314 }
315
wcarthur74a92a42015-12-07 16:02:12 -0500316 FD_ZERO( &readFds );
317 FD_SET( TCTI_CONTEXT_INTEL->tpmSock, &readFds );
Will Arthur54e04e42015-07-15 11:29:25 -0400318
wcarthur74a92a42015-12-07 16:02:12 -0500319 iResult = select( TCTI_CONTEXT_INTEL->tpmSock+1, &readFds, 0, 0, tvPtr );
320 if( iResult == 0 )
Will Arthur54e04e42015-07-15 11:29:25 -0400321 {
wcarthur1c827592016-04-27 11:07:51 -0400322 TCTI_LOG( tctiContext, rmPrefix, "select failed due to timeout, socket #: 0x%x\n", TCTI_CONTEXT_INTEL->tpmSock );
wcarthur74a92a42015-12-07 16:02:12 -0500323 rval = TSS2_TCTI_RC_TRY_AGAIN;
324 goto retSocketReceiveTpmResponse;
325 }
326 else if( iResult == SOCKET_ERROR )
327 {
wcarthur1c827592016-04-27 11:07:51 -0400328 TCTI_LOG( tctiContext, rmPrefix, "select failed with socket error: %d\n", WSAGetLastError() );
wcarthur74a92a42015-12-07 16:02:12 -0500329 rval = TSS2_TCTI_RC_IO_ERROR;
330 goto retSocketReceiveTpmResponse;
331 }
332 else if ( iResult != 1 )
333 {
wcarthur1c827592016-04-27 11:07:51 -0400334 TCTI_LOG( tctiContext, rmPrefix, "select failed, read the wrong # of bytes: %d\n", iResult );
wcarthur74a92a42015-12-07 16:02:12 -0500335 rval = TSS2_TCTI_RC_IO_ERROR;
336 goto retSocketReceiveTpmResponse;
337 }
Will Arthur54e04e42015-07-15 11:29:25 -0400338
wcarthur74a92a42015-12-07 16:02:12 -0500339 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived != 1 )
Philip Triccadfa41a52016-07-20 17:43:57 -0700340 {
Will Arthur54e04e42015-07-15 11:29:25 -0400341 // Receive the size of the response.
Philip Triccabcc73032016-03-30 19:50:54 -0700342 rval = tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)& (((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize ), 4 );
wcarthureedecd62015-11-20 16:59:45 -0500343 if( rval != TSS2_RC_SUCCESS )
344 goto retSocketReceiveTpmResponse;
345
wcarthur74a92a42015-12-07 16:02:12 -0500346 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize = CHANGE_ENDIAN_DWORD( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize );
347 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived = 1;
Will Arthur54e04e42015-07-15 11:29:25 -0400348 }
349
wcarthur74a92a42015-12-07 16:02:12 -0500350 if( response_buffer == NULL )
Will Arthur54e04e42015-07-15 11:29:25 -0400351 {
wcarthur74a92a42015-12-07 16:02:12 -0500352 // In this case, just return the size
353 *response_size = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize;
354 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived = 1;
355 goto retSocketReceiveTpmResponse;
356 }
357
358 if( *response_size < ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize )
359 {
360 *response_size = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize;
Philip Triccadfa41a52016-07-20 17:43:57 -0700361 rval = TSS2_TCTI_RC_INSUFFICIENT_BUFFER;
wcarthur74a92a42015-12-07 16:02:12 -0500362
363
364 // If possible, receive tag from TPM.
365 if( *response_size >= sizeof( TPM_ST ) && ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived == 0 )
Will Arthur54e04e42015-07-15 11:29:25 -0400366 {
Philip Triccabcc73032016-03-30 19:50:54 -0700367 if( TSS2_RC_SUCCESS != tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->tag ), 2 ) )
wcarthur74a92a42015-12-07 16:02:12 -0500368 {
wcarthura05cdcc2015-12-04 18:07:16 -0500369 goto retSocketReceiveTpmResponse;
wcarthur2efe2912015-11-16 11:19:42 -0500370 }
wcarthur74a92a42015-12-07 16:02:12 -0500371 else
wcarthura05cdcc2015-12-04 18:07:16 -0500372 {
wcarthur74a92a42015-12-07 16:02:12 -0500373 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived = 1;
wcarthura05cdcc2015-12-04 18:07:16 -0500374 }
Will Arthur54e04e42015-07-15 11:29:25 -0400375 }
wcarthura05cdcc2015-12-04 18:07:16 -0500376
wcarthur74a92a42015-12-07 16:02:12 -0500377 // If possible, receive response size from TPM
378 if( *response_size >= ( sizeof( TPM_ST ) + sizeof( TPM_RC ) ) && ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived == 0 )
379 {
Philip Triccabcc73032016-03-30 19:50:54 -0700380 if( TSS2_RC_SUCCESS != tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->responseSize ), 4 ) )
wcarthura05cdcc2015-12-04 18:07:16 -0500381 {
wcarthureedecd62015-11-20 16:59:45 -0500382 goto retSocketReceiveTpmResponse;
wcarthur74a92a42015-12-07 16:02:12 -0500383 }
384 else
385 {
386 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize = CHANGE_ENDIAN_DWORD( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize );
387 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived = 1;
388 }
389 }
390 }
391 else
392 {
wcarthur1c827592016-04-27 11:07:51 -0400393 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgEnabled == 1 &&
Will-nuce0d3ec22015-12-09 15:09:49 -0500394 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize > 0 )
395 {
396#ifdef DEBUG
wcarthur1c827592016-04-27 11:07:51 -0400397 TCTI_LOG( tctiContext, rmPrefix, "Response Received: " );
Will-nuce0d3ec22015-12-09 15:09:49 -0500398#endif
399#ifdef DEBUG_SOCKETS
wcarthur1c827592016-04-27 11:07:51 -0400400 TCTI_LOG( tctiContext, rmPrefix, "from socket #0x%x:\n", TCTI_CONTEXT_INTEL->tpmSock );
Will-nuce0d3ec22015-12-09 15:09:49 -0500401#endif
402 }
Philip Triccadfa41a52016-07-20 17:43:57 -0700403
wcarthur74a92a42015-12-07 16:02:12 -0500404 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived == 1 )
405 {
406 *(TPM_ST *)response_buffer = ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->tag;
407 responseSizeDelta += sizeof( TPM_ST );
408 response_buffer += sizeof( TPM_ST );
409 }
410
411 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived == 1 )
412 {
413 *(TPM_RC *)response_buffer = CHANGE_ENDIAN_DWORD( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->responseSize );
414 responseSizeDelta += sizeof( TPM_RC );
415 response_buffer += sizeof( TPM_RC );
416 }
417
418 // Receive the TPM response.
Philip Triccabcc73032016-03-30 19:50:54 -0700419 rval = tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)response_buffer, ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize - responseSizeDelta );
wcarthur74a92a42015-12-07 16:02:12 -0500420 if( rval != TSS2_RC_SUCCESS )
421 goto retSocketReceiveTpmResponse;
wcarthureedecd62015-11-20 16:59:45 -0500422
wcarthur2efe2912015-11-16 11:19:42 -0500423#ifdef DEBUG
wcarthur1c827592016-04-27 11:07:51 -0400424 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgEnabled == 1 )
wcarthur74a92a42015-12-07 16:02:12 -0500425 {
wcarthur1c827592016-04-27 11:07:51 -0400426 DEBUG_PRINT_BUFFER( rmPrefix, response_buffer, ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize );
wcarthur0651af22015-11-25 15:01:22 -0500427 }
wcarthur74a92a42015-12-07 16:02:12 -0500428#endif
wcarthur0651af22015-11-25 15:01:22 -0500429
wcarthur74a92a42015-12-07 16:02:12 -0500430 // Receive the appended four bytes of 0's
Philip Triccabcc73032016-03-30 19:50:54 -0700431 rval = tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&trash, 4 );
wcarthur74a92a42015-12-07 16:02:12 -0500432 if( rval != TSS2_RC_SUCCESS )
433 goto retSocketReceiveTpmResponse;
Will Arthur54e04e42015-07-15 11:29:25 -0400434 }
435
wcarthura05cdcc2015-12-04 18:07:16 -0500436 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize < *response_size )
Will Arthur54e04e42015-07-15 11:29:25 -0400437 {
wcarthura05cdcc2015-12-04 18:07:16 -0500438 *response_size = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize;
Will Arthur54e04e42015-07-15 11:29:25 -0400439 }
Philip Triccadfa41a52016-07-20 17:43:57 -0700440
Will Arthur54e04e42015-07-15 11:29:25 -0400441 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 0;
442
443 // Turn cancel off.
wcarthur2efe2912015-11-16 11:19:42 -0500444 if( rval == TSS2_RC_SUCCESS )
445 {
446 rval = (TSS2_RC)PlatformCommand( tctiContext, MS_SIM_CANCEL_OFF );
447 }
448 else
449 {
450 // Ignore return value so earlier error code is preserved.
451 PlatformCommand( tctiContext, MS_SIM_CANCEL_OFF );
452 }
Will Arthur54e04e42015-07-15 11:29:25 -0400453
wcarthur1c827592016-04-27 11:07:51 -0400454 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgEnabled== 1 )
Will Arthur54e04e42015-07-15 11:29:25 -0400455 {
Philip Triccabcc73032016-03-30 19:50:54 -0700456// TCTI_LOG( tctiContext, NO_PREFIX, "%s sent cancel OFF command:\n", interfaceName );
Will Arthur54e04e42015-07-15 11:29:25 -0400457 }
458
459retSocketReceiveTpmResponse:
Philip Triccadfa41a52016-07-20 17:43:57 -0700460 if( rval == TSS2_RC_SUCCESS &&
wcarthura05cdcc2015-12-04 18:07:16 -0500461 response_buffer != NULL )
wcarthureedecd62015-11-20 16:59:45 -0500462 {
463 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_RECEIVE_RESPONSE;
464 }
Philip Triccadfa41a52016-07-20 17:43:57 -0700465
Will Arthur54e04e42015-07-15 11:29:25 -0400466 return rval;
467}
468
469#ifdef __cplusplus
470}
471#endif
472
Will Arthur54e04e42015-07-15 11:29:25 -0400473#define HOSTNAME_LENGTH 200
474#define PORT_LENGTH 4
475
Philip Triccad1bbedb2016-02-14 20:39:59 -0800476TSS2_RC InitSocketTcti (
Will Arthur54e04e42015-07-15 11:29:25 -0400477 TSS2_TCTI_CONTEXT *tctiContext, // OUT
478 size_t *contextSize, // IN/OUT
Philip Tricca6feb9762016-02-23 10:44:09 -0800479 const TCTI_SOCKET_CONF *conf, // IN
Will Arthur54e04e42015-07-15 11:29:25 -0400480 const uint8_t serverSockets
481 )
482{
483 TSS2_RC rval = TSS2_RC_SUCCESS;
Will Arthur54e04e42015-07-15 11:29:25 -0400484 SOCKET otherSock;
485 SOCKET tpmSock;
486
487 if( tctiContext == NULL )
488 {
489 *contextSize = sizeof( TSS2_TCTI_CONTEXT_INTEL );
490 return TSS2_RC_SUCCESS;
491 }
492 else
493 {
Will Arthur54e04e42015-07-15 11:29:25 -0400494 // Init TCTI context.
Philip Tricca7b4948e2016-04-18 20:43:59 -0700495 TSS2_TCTI_MAGIC( tctiContext ) = TCTI_MAGIC;
496 TSS2_TCTI_VERSION( tctiContext ) = TCTI_VERSION;
497 TSS2_TCTI_TRANSMIT( tctiContext ) = SocketSendTpmCommand;
498 TSS2_TCTI_RECEIVE( tctiContext ) = SocketReceiveTpmResponse;
499 TSS2_TCTI_FINALIZE( tctiContext ) = SocketFinalize;
500 TSS2_TCTI_CANCEL( tctiContext ) = SocketCancel;
501 TSS2_TCTI_GET_POLL_HANDLES( tctiContext ) = 0;
502 TSS2_TCTI_SET_LOCALITY( tctiContext ) = SocketSetLocality;
wcarthur1c827592016-04-27 11:07:51 -0400503 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.debugMsgEnabled = 0;
Will Arthur54e04e42015-07-15 11:29:25 -0400504 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality = 3;
505 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 0;
506 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.rmDebugPrefix = 0;
wcarthura05cdcc2015-12-04 18:07:16 -0500507 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived = 0;
508 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived = 0;
509 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived = 0;
wcarthur1c827592016-04-27 11:07:51 -0400510 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->currentTctiContext = 0;
511 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_INITIALIZE;
Philip Triccabcc73032016-03-30 19:50:54 -0700512 TCTI_LOG_CALLBACK( tctiContext ) = conf->logCallback;
Philip Tricca4eb2f352016-03-31 05:55:35 -0700513 TCTI_LOG_BUFFER_CALLBACK( tctiContext ) = conf->logBufferCallback;
Philip Triccabcc73032016-03-30 19:50:54 -0700514 TCTI_LOG_DATA( tctiContext ) = conf->logData;
Will Arthur54e04e42015-07-15 11:29:25 -0400515
Philip Triccabcc73032016-03-30 19:50:54 -0700516 rval = (TSS2_RC) InitSockets( conf->hostname, conf->port, serverSockets, &otherSock, &tpmSock, TCTI_LOG_CALLBACK( tctiContext ), TCTI_LOG_DATA( tctiContext) );
Will Arthur54e04e42015-07-15 11:29:25 -0400517 if( rval == TSS2_RC_SUCCESS )
518 {
519 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->otherSock = otherSock;
520 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->tpmSock = tpmSock;
521 }
522 else
523 {
Will Arthurca8e7f32015-08-03 15:35:19 -0400524 CloseSockets( otherSock, tpmSock);
Philip Triccadfa41a52016-07-20 17:43:57 -0700525 }
Will Arthur54e04e42015-07-15 11:29:25 -0400526 }
527
528 return rval;
529}