blob: 6a773a18705dcfb17ed98fd249ae347d2f5e6e10 [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 Triccac3dedc22016-01-15 13:47:22 -080046#include <tss2/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
Will Arthurca8e7f32015-08-03 15:35:19 -040057extern UINT8 simulator;
Will Arthur54e04e42015-07-15 11:29:25 -040058
Philip Triccabcc73032016-03-30 19:50:54 -070059static TSS2_RC tctiRecvBytes( TSS2_TCTI_CONTEXT *tctiContext, SOCKET sock, unsigned char *data, int len )
Philip Triccaa06f2372016-03-30 17:13:11 -070060{
61 TSS2_RC result = 0;
62 result = recvBytes( sock, data, len);
63 if (result == SOCKET_ERROR) {
Philip Triccabcc73032016-03-30 19:50:54 -070064 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 -070065 return TSS2_TCTI_RC_IO_ERROR;
66 }
67#ifdef DEBUG_SOCKETS
Philip Triccabcc73032016-03-30 19:50:54 -070068 TCTI_LOG( tctiContext, NO_PREFIX, "Receive Bytes from socket #0x%x: \n", sock );
Philip Tricca4eb2f352016-03-31 05:55:35 -070069 TCTI_LOG_BUFFER( tctiContext, NO_PREFIX, data, len );
Philip Triccaa06f2372016-03-30 17:13:11 -070070#endif
71
72 return TSS2_RC_SUCCESS;
73}
74
Philip Triccabcc73032016-03-30 19:50:54 -070075static TSS2_RC tctiSendBytes( TSS2_TCTI_CONTEXT *tctiContext, SOCKET sock, const unsigned char *data, int len )
Philip Tricca4ee284b2016-03-30 17:40:39 -070076{
77 TSS2_RC ret = TSS2_RC_SUCCESS;
78
79#ifdef DEBUG_SOCKETS
Philip Triccabcc73032016-03-30 19:50:54 -070080 TCTI_LOG( tctiContext, NO_PREFIX, "Send Bytes to socket #0x%x: \n", sock );
Philip Tricca4eb2f352016-03-31 05:55:35 -070081 TCTI_LOG_BUFFER( tctiContext, NO_PREFIX, (UINT8 *)data, len );
Philip Tricca4ee284b2016-03-30 17:40:39 -070082#endif
83
84 ret = sendBytes( sock, data, len);
85 if (ret != TSS2_RC_SUCCESS)
Philip Triccabcc73032016-03-30 19:50:54 -070086 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 -070087 return ret;
88}
89
Philip Triccad1bbedb2016-02-14 20:39:59 -080090TSS2_RC SendSessionEndSocketTcti(
Will Arthur54e04e42015-07-15 11:29:25 -040091 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;
wcarthureedecd62015-11-20 16:59:45 -050096 TSS2_RC rval = TSS2_RC_SUCCESS;
97
Will Arthur54e04e42015-07-15 11:29:25 -040098 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 Triccabcc73032016-03-30 19:50:54 -0700108 rval = tctiSendBytes( tctiContext, sock, (char unsigned *)&tpmSendCommand, 4 );
Will Arthur54e04e42015-07-15 11:29:25 -0400109
wcarthureedecd62015-11-20 16:59:45 -0500110 return( rval );
Will Arthur54e04e42015-07-15 11:29:25 -0400111}
112
Will Arthur54e04e42015-07-15 11:29:25 -0400113TSS2_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;
wcarthureedecd62015-11-20 16:59:45 -0500122 TSS2_RC rval = TSS2_RC_SUCCESS;
wcarthur1aee953e2016-04-01 15:13:24 -0700123
124#ifdef DEBUG
wcarthureedecd62015-11-20 16:59:45 -0500125 UINT32 commandCode ;
wcarthur1aee953e2016-04-01 15:13:24 -0700126#endif
wcarthureedecd62015-11-20 16:59:45 -0500127
Will Arthur54e04e42015-07-15 11:29:25 -0400128#ifdef SAPI_CLIENT
129 UINT8 debugMsgLevel, statusBits;
130#endif
Will-nuc3ebfeb92015-10-29 15:53:27 -0400131
wcarthureedecd62015-11-20 16:59:45 -0500132 rval = CommonSendChecks( tctiContext, command_buffer );
133 if( rval != TSS2_RC_SUCCESS )
134 {
135 goto returnFromSocketSendTpmCommand;
136 }
Will-nuc3ebfeb92015-10-29 15:53:27 -0400137
Will Arthur54e04e42015-07-15 11:29:25 -0400138 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED )
139 {
140#ifdef DEBUG
Philip Triccabcc73032016-03-30 19:50:54 -0700141 TCTI_LOG( tctiContext, NO_PREFIX, "\n" );
wcarthur02340012015-10-28 08:32:42 -0400142 if( commandCode >= TPM_CC_NV_UndefineSpaceSpecial && commandCode <= TPM_CC_PolicyNvWritten )
Philip Triccabcc73032016-03-30 19:50:54 -0700143 TCTI_LOG( tctiContext, NO_PREFIX, "Cmd sent: %s\n", commandCodeStrings[ commandCode - TPM_CC_FIRST ] );
wcarthur02340012015-10-28 08:32:42 -0400144 else
Philip Triccabcc73032016-03-30 19:50:54 -0700145 TCTI_LOG( tctiContext, NO_PREFIX, "Cmd sent: 0x%4.4x\n", CHANGE_ENDIAN_DWORD(commandCode ) );
146#endif
Will Arthur54e04e42015-07-15 11:29:25 -0400147#ifdef DEBUG_SOCKETS
Philip Triccabcc73032016-03-30 19:50:54 -0700148 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 Arthur54e04e42015-07-15 11:29:25 -0400150 }
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 Triccabcc73032016-03-30 19:50:54 -0700157 rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&tpmSendCommand, 4 );
wcarthureedecd62015-11-20 16:59:45 -0500158 if( rval != TSS2_RC_SUCCESS )
159 goto returnFromSocketSendTpmCommand;
160
Will Arthur54e04e42015-07-15 11:29:25 -0400161 // Send the locality
162 locality = (UINT8)( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality;
Philip Triccabcc73032016-03-30 19:50:54 -0700163 rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&locality, 1 );
wcarthureedecd62015-11-20 16:59:45 -0500164 if( rval != TSS2_RC_SUCCESS )
165 goto returnFromSocketSendTpmCommand;
Will Arthur54e04e42015-07-15 11:29:25 -0400166
167#ifdef SAPI_CLIENT
168 // Send the debug level
169 debugMsgLevel = (UINT8)( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.debugMsgLevel;
Philip Triccabcc73032016-03-30 19:50:54 -0700170 rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&debugMsgLevel, 1 );
wcarthureedecd62015-11-20 16:59:45 -0500171 if( rval != TSS2_RC_SUCCESS )
172 goto returnFromSocketSendTpmCommand;
Will Arthur54e04e42015-07-15 11:29:25 -0400173
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 Triccabcc73032016-03-30 19:50:54 -0700177 rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&statusBits, 1 );
wcarthureedecd62015-11-20 16:59:45 -0500178 if( rval != TSS2_RC_SUCCESS )
179 goto returnFromSocketSendTpmCommand;
Will Arthur54e04e42015-07-15 11:29:25 -0400180#endif
181
182#ifdef DEBUG
183 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED )
184 {
Philip Triccabcc73032016-03-30 19:50:54 -0700185 TCTI_LOG( tctiContext, NO_PREFIX, "Locality = %d", ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality );
Will Arthur54e04e42015-07-15 11:29:25 -0400186 }
187#endif
188
189 // Send number of bytes.
190 cnt1 = cnt;
191 cnt = CHANGE_ENDIAN_DWORD(cnt);
Philip Triccabcc73032016-03-30 19:50:54 -0700192 rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&cnt, 4 );
wcarthureedecd62015-11-20 16:59:45 -0500193 if( rval != TSS2_RC_SUCCESS )
194 goto returnFromSocketSendTpmCommand;
Will Arthur54e04e42015-07-15 11:29:25 -0400195
196 // Send the TPM command buffer
Philip Triccabcc73032016-03-30 19:50:54 -0700197 rval = tctiSendBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)command_buffer, cnt1 );
wcarthureedecd62015-11-20 16:59:45 -0500198 if( rval != TSS2_RC_SUCCESS )
199 goto returnFromSocketSendTpmCommand;
Will Arthur54e04e42015-07-15 11:29:25 -0400200
201#ifdef DEBUG
202 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED )
203 {
Philip Tricca6960f1b2016-03-16 17:09:50 -0700204 DEBUG_PRINT_BUFFER( NO_PREFIX, command_buffer, cnt1 );
Will Arthur54e04e42015-07-15 11:29:25 -0400205 }
206#endif
207 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 1;
208
wcarthureedecd62015-11-20 16:59:45 -0500209returnFromSocketSendTpmCommand:
210
211 if( rval == TSS2_RC_SUCCESS )
212 {
213 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_SEND_COMMAND;
wcarthura05cdcc2015-12-04 18:07:16 -0500214 ((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;
wcarthureedecd62015-11-20 16:59:45 -0500217 }
218
219 return rval;
Will Arthur54e04e42015-07-15 11:29:25 -0400220}
221
222TSS2_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 Triccabcc73032016-03-30 19:50:54 -0700245 TCTI_LOG( tctiContext, NO_PREFIX, "%s sent cancel ON command:\n", interfaceName );
Will Arthur54e04e42015-07-15 11:29:25 -0400246 }
247 }
248#endif
249 }
250
251 return rval;
252}
253
254TSS2_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
280void CloseSockets( SOCKET otherSock, SOCKET tpmSock)
281{
282 closesocket(otherSock);
283 closesocket(tpmSock);
284}
285
wcarthur35aa5392016-03-22 16:52:44 -0400286void SocketFinalize(
Will Arthur54e04e42015-07-15 11:29:25 -0400287 TSS2_TCTI_CONTEXT *tctiContext /* in */
288 )
289{
wcarthur35aa5392016-03-22 16:52:44 -0400290 if( tctiContext != NULL )
wcarthurf979e352015-11-24 17:34:44 -0500291 {
292 // Send session end messages to servers.
Philip Triccad1bbedb2016-02-14 20:39:59 -0800293 SendSessionEndSocketTcti( tctiContext, 1 );
294 SendSessionEndSocketTcti( tctiContext, 0 );
Will Arthur54e04e42015-07-15 11:29:25 -0400295
wcarthurf979e352015-11-24 17:34:44 -0500296 CloseSockets( TCTI_CONTEXT_INTEL->otherSock, TCTI_CONTEXT_INTEL->tpmSock );
Will Arthur54e04e42015-07-15 11:29:25 -0400297
wcarthurf979e352015-11-24 17:34:44 -0500298 free( tctiContext );
299 }
Will Arthur54e04e42015-07-15 11:29:25 -0400300}
301
Will Arthur54e04e42015-07-15 11:29:25 -0400302TSS2_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 Arthur54e04e42015-07-15 11:29:25 -0400310 TSS2_RC rval = TSS2_RC_SUCCESS;
311 fd_set readFds;
312 struct timeval tv, *tvPtr;
313 int32_t timeoutMsecs = timeout % 1000;
314 int iResult;
wcarthura05cdcc2015-12-04 18:07:16 -0500315 unsigned char responseSizeDelta = 0;
wcarthureedecd62015-11-20 16:59:45 -0500316
317 rval = CommonReceiveChecks( tctiContext, response_size, response_buffer );
318 if( rval != TSS2_RC_SUCCESS )
319 {
320 goto retSocketReceiveTpmResponse;
321 }
Will Arthur54e04e42015-07-15 11:29:25 -0400322
wcarthur74a92a42015-12-07 16:02:12 -0500323 if( timeout == TSS2_TCTI_TIMEOUT_BLOCK )
Will Arthur54e04e42015-07-15 11:29:25 -0400324 {
wcarthur74a92a42015-12-07 16:02:12 -0500325 tvPtr = 0;
326 }
327 else
328 {
329 tv.tv_sec = timeout / 1000;
330 tv.tv_usec = timeoutMsecs * 1000;
331 tvPtr = &tv;
Will Arthur54e04e42015-07-15 11:29:25 -0400332 }
333
wcarthur74a92a42015-12-07 16:02:12 -0500334 FD_ZERO( &readFds );
335 FD_SET( TCTI_CONTEXT_INTEL->tpmSock, &readFds );
Will Arthur54e04e42015-07-15 11:29:25 -0400336
wcarthur74a92a42015-12-07 16:02:12 -0500337 iResult = select( TCTI_CONTEXT_INTEL->tpmSock+1, &readFds, 0, 0, tvPtr );
338 if( iResult == 0 )
Will Arthur54e04e42015-07-15 11:29:25 -0400339 {
Philip Triccabcc73032016-03-30 19:50:54 -0700340 TCTI_LOG( tctiContext, NO_PREFIX, "select failed due to timeout, socket #: 0x%x\n", TCTI_CONTEXT_INTEL->tpmSock );
wcarthur74a92a42015-12-07 16:02:12 -0500341 rval = TSS2_TCTI_RC_TRY_AGAIN;
342 goto retSocketReceiveTpmResponse;
343 }
344 else if( iResult == SOCKET_ERROR )
345 {
Philip Triccabcc73032016-03-30 19:50:54 -0700346 TCTI_LOG( tctiContext, NO_PREFIX, "select failed with socket error: %d\n", WSAGetLastError() );
wcarthur74a92a42015-12-07 16:02:12 -0500347 rval = TSS2_TCTI_RC_IO_ERROR;
348 goto retSocketReceiveTpmResponse;
349 }
350 else if ( iResult != 1 )
351 {
Philip Triccabcc73032016-03-30 19:50:54 -0700352 TCTI_LOG( tctiContext, NO_PREFIX, "select failed, read the wrong # of bytes: %d\n", iResult );
wcarthur74a92a42015-12-07 16:02:12 -0500353 rval = TSS2_TCTI_RC_IO_ERROR;
354 goto retSocketReceiveTpmResponse;
355 }
Will Arthur54e04e42015-07-15 11:29:25 -0400356
wcarthur74a92a42015-12-07 16:02:12 -0500357 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived != 1 )
358 {
Will Arthur54e04e42015-07-15 11:29:25 -0400359 // Receive the size of the response.
Philip Triccabcc73032016-03-30 19:50:54 -0700360 rval = tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)& (((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize ), 4 );
wcarthureedecd62015-11-20 16:59:45 -0500361 if( rval != TSS2_RC_SUCCESS )
362 goto retSocketReceiveTpmResponse;
363
wcarthur74a92a42015-12-07 16:02:12 -0500364 ((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 Arthur54e04e42015-07-15 11:29:25 -0400366 }
367
wcarthur74a92a42015-12-07 16:02:12 -0500368 if( response_buffer == NULL )
Will Arthur54e04e42015-07-15 11:29:25 -0400369 {
wcarthur74a92a42015-12-07 16:02:12 -0500370 // 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 Arthur54e04e42015-07-15 11:29:25 -0400384 {
Philip Triccabcc73032016-03-30 19:50:54 -0700385 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 -0500386 {
wcarthura05cdcc2015-12-04 18:07:16 -0500387 goto retSocketReceiveTpmResponse;
wcarthur2efe2912015-11-16 11:19:42 -0500388 }
wcarthur74a92a42015-12-07 16:02:12 -0500389 else
wcarthura05cdcc2015-12-04 18:07:16 -0500390 {
wcarthur74a92a42015-12-07 16:02:12 -0500391 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived = 1;
wcarthura05cdcc2015-12-04 18:07:16 -0500392 }
Will Arthur54e04e42015-07-15 11:29:25 -0400393 }
wcarthura05cdcc2015-12-04 18:07:16 -0500394
wcarthur74a92a42015-12-07 16:02:12 -0500395 // 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 Triccabcc73032016-03-30 19:50:54 -0700398 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 -0500399 {
wcarthureedecd62015-11-20 16:59:45 -0500400 goto retSocketReceiveTpmResponse;
wcarthur74a92a42015-12-07 16:02:12 -0500401 }
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-nuce0d3ec22015-12-09 15:09:49 -0500411 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 Triccabcc73032016-03-30 19:50:54 -0700415 TCTI_LOG( tctiContext, NO_PREFIX, "Response Received: " );
Will-nuce0d3ec22015-12-09 15:09:49 -0500416#endif
417#ifdef DEBUG_SOCKETS
Philip Triccabcc73032016-03-30 19:50:54 -0700418 TCTI_LOG( tctiContext, NO_PREFIX, "from socket #0x%x:\n", TCTI_CONTEXT_INTEL->tpmSock );
Will-nuce0d3ec22015-12-09 15:09:49 -0500419#endif
420 }
421
wcarthur74a92a42015-12-07 16:02:12 -0500422 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 Triccabcc73032016-03-30 19:50:54 -0700437 rval = tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)response_buffer, ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize - responseSizeDelta );
wcarthur74a92a42015-12-07 16:02:12 -0500438 if( rval != TSS2_RC_SUCCESS )
439 goto retSocketReceiveTpmResponse;
wcarthureedecd62015-11-20 16:59:45 -0500440
wcarthur2efe2912015-11-16 11:19:42 -0500441#ifdef DEBUG
wcarthur74a92a42015-12-07 16:02:12 -0500442 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED )
443 {
Philip Tricca6960f1b2016-03-16 17:09:50 -0700444 DEBUG_PRINT_BUFFER( NO_PREFIX, response_buffer, ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize );
wcarthur0651af22015-11-25 15:01:22 -0500445 }
wcarthur74a92a42015-12-07 16:02:12 -0500446#endif
wcarthur0651af22015-11-25 15:01:22 -0500447
wcarthur74a92a42015-12-07 16:02:12 -0500448 // Receive the appended four bytes of 0's
Philip Triccabcc73032016-03-30 19:50:54 -0700449 rval = tctiRecvBytes( tctiContext, TCTI_CONTEXT_INTEL->tpmSock, (unsigned char *)&trash, 4 );
wcarthur74a92a42015-12-07 16:02:12 -0500450 if( rval != TSS2_RC_SUCCESS )
451 goto retSocketReceiveTpmResponse;
Will Arthur54e04e42015-07-15 11:29:25 -0400452 }
453
wcarthura05cdcc2015-12-04 18:07:16 -0500454 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize < *response_size )
Will Arthur54e04e42015-07-15 11:29:25 -0400455 {
wcarthura05cdcc2015-12-04 18:07:16 -0500456 *response_size = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize;
Will Arthur54e04e42015-07-15 11:29:25 -0400457 }
458
459 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 0;
460
461 // Turn cancel off.
wcarthur2efe2912015-11-16 11:19:42 -0500462 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 Arthur54e04e42015-07-15 11:29:25 -0400471
472 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED )
473 {
Philip Triccabcc73032016-03-30 19:50:54 -0700474// TCTI_LOG( tctiContext, NO_PREFIX, "%s sent cancel OFF command:\n", interfaceName );
Will Arthur54e04e42015-07-15 11:29:25 -0400475 }
476
477retSocketReceiveTpmResponse:
wcarthura05cdcc2015-12-04 18:07:16 -0500478 if( rval == TSS2_RC_SUCCESS &&
479 response_buffer != NULL )
wcarthureedecd62015-11-20 16:59:45 -0500480 {
481 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_RECEIVE_RESPONSE;
482 }
483
Will Arthur54e04e42015-07-15 11:29:25 -0400484 return rval;
485}
486
487#ifdef __cplusplus
488}
489#endif
490
Will Arthur54e04e42015-07-15 11:29:25 -0400491#define HOSTNAME_LENGTH 200
492#define PORT_LENGTH 4
493
Philip Triccad1bbedb2016-02-14 20:39:59 -0800494TSS2_RC InitSocketTcti (
Will Arthur54e04e42015-07-15 11:29:25 -0400495 TSS2_TCTI_CONTEXT *tctiContext, // OUT
496 size_t *contextSize, // IN/OUT
Philip Tricca6feb9762016-02-23 10:44:09 -0800497 const TCTI_SOCKET_CONF *conf, // IN
Will Arthur54e04e42015-07-15 11:29:25 -0400498 const uint8_t serverSockets
499 )
500{
501 TSS2_RC rval = TSS2_RC_SUCCESS;
Will Arthur54e04e42015-07-15 11:29:25 -0400502 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 Arthur54e04e42015-07-15 11:29:25 -0400512 // Init TCTI context.
Philip Tricca74840532016-03-25 19:14:27 -0700513 ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->magic = TCTI_MAGIC;
514 ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->version = TCTI_VERSION;
Will Arthur54e04e42015-07-15 11:29:25 -0400515 ((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;
wcarthureedecd62015-11-20 16:59:45 -0500525 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_INITIALIZE;
wcarthura05cdcc2015-12-04 18:07:16 -0500526 ((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 Triccabcc73032016-03-30 19:50:54 -0700529 TCTI_LOG_CALLBACK( tctiContext ) = conf->logCallback;
Philip Tricca4eb2f352016-03-31 05:55:35 -0700530 TCTI_LOG_BUFFER_CALLBACK( tctiContext ) = conf->logBufferCallback;
Philip Triccabcc73032016-03-30 19:50:54 -0700531 TCTI_LOG_DATA( tctiContext ) = conf->logData;
Will Arthur54e04e42015-07-15 11:29:25 -0400532
Philip Triccabcc73032016-03-30 19:50:54 -0700533 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 -0400534 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 Arthurca8e7f32015-08-03 15:35:19 -0400541 CloseSockets( otherSock, tpmSock);
Will Arthur54e04e42015-07-15 11:29:25 -0400542 }
Will Arthur54e04e42015-07-15 11:29:25 -0400543 }
544
545 return rval;
546}
547
Philip Tricca6feb9762016-02-23 10:44:09 -0800548TSS2_RC TeardownSocketTcti (TSS2_TCTI_CONTEXT *tctiContext)
Will Arthur54e04e42015-07-15 11:29:25 -0400549{
Will Arthur54e04e42015-07-15 11:29:25 -0400550 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->finalize( tctiContext );
Will Arthur54e04e42015-07-15 11:29:25 -0400551
552 return TSS2_RC_SUCCESS;
553}
554
555