blob: a63258c2278f1c9980d15be0b9d7a11058f496da [file] [log] [blame]
Will Arthurca8e7f32015-08-03 15:35:19 -04001//**********************************************************************;
2// Copyright (c) 2015, Intel Corporation
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
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
13// and/or other materials provided with the distribution.
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
25// THE POSSIBILITY OF SUCH DAMAGE.
26//**********************************************************************;
27
28#include <stdio.h>
29#include <stdlib.h> // Needed for _wtoi
30
31#include <tpm20.h>
32//#include "resourcemgr.h"
33//#include <sample.h>
34#include <tss2_sysapi_util.h>
35#include <errno.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <fcntl.h>
Will-nuc3ebfeb92015-10-29 15:53:27 -040039#include "debug.h"
wcarthureedecd62015-11-20 16:59:45 -050040#include "commonchecks.h"
Will Arthurca8e7f32015-08-03 15:35:19 -040041
42#ifdef _WIN32
43#define ssize_t int
44#elif __linux
45#include <unistd.h>
46#endif
47
48#define HOSTNAME_LENGTH 200
49
50extern void OpenOutFile( FILE **outFp );
51
52extern void CloseOutFile( FILE **outFp );
53
Will Arthurca8e7f32015-08-03 15:35:19 -040054extern FILE *outFp;
55
56extern int ResMgrPrintf( UINT8 type, const char *format, ... );
57int (*tpmLocalTpmPrintf)( UINT8 type, const char *format, ...) = ResMgrPrintf;
58
59TSS2_RC LocalTpmSendTpmCommand(
60 TSS2_TCTI_CONTEXT *tctiContext, /* in */
61 size_t command_size, /* in */
62 uint8_t *command_buffer /* in */
63 )
64{
65 TSS2_RC rval = TSS2_RC_SUCCESS;
66 ssize_t size;
67
Will-nuc3ebfeb92015-10-29 15:53:27 -040068#ifdef DEBUG
wcarthureedecd62015-11-20 16:59:45 -050069 UINT32 commandCode;
70 UINT32 cnt;
71#endif
72
73 rval = CommonSendChecks( tctiContext, command_buffer );
Will-nuc3ebfeb92015-10-29 15:53:27 -040074
wcarthureedecd62015-11-20 16:59:45 -050075 if( rval == TSS2_RC_SUCCESS )
Will-nuc3ebfeb92015-10-29 15:53:27 -040076 {
wcarthureedecd62015-11-20 16:59:45 -050077#ifdef DEBUG
78 commandCode = CHANGE_ENDIAN_DWORD( ( (TPM20_Header_In *)command_buffer )->commandCode );
79 cnt = CHANGE_ENDIAN_DWORD(((TPM20_Header_In *) command_buffer)->commandSize);
80
81 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED )
82 {
83 (*tpmLocalTpmPrintf)( rmDebugPrefix, "\n" );
84 (*tpmLocalTpmPrintf)(rmDebugPrefix, "Cmd sent: %s\n", commandCodeStrings[ commandCode - TPM_CC_FIRST ] );
85 DEBUG_PRINT_BUFFER( command_buffer, cnt );
86 }
Will-nuc3ebfeb92015-10-29 15:53:27 -040087#endif
88
wcarthureedecd62015-11-20 16:59:45 -050089 size = write( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile, command_buffer, command_size );
Will Arthurca8e7f32015-08-03 15:35:19 -040090
wcarthureedecd62015-11-20 16:59:45 -050091 if( size < 0 )
92 {
93 (*tpmLocalTpmPrintf)(NO_PREFIX, "send failed with error: %d\n", errno );
94 rval = TSS2_TCTI_RC_IO_ERROR;
95 }
96 else if( (size_t)size != command_size )
97 {
98 rval = TSS2_TCTI_RC_IO_ERROR;
99 }
Will Arthurca8e7f32015-08-03 15:35:19 -0400100
wcarthureedecd62015-11-20 16:59:45 -0500101 if( rval == TSS2_RC_SUCCESS )
102 {
103 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_SEND_COMMAND;
104 }
105 }
106
Will Arthurca8e7f32015-08-03 15:35:19 -0400107 return rval;
108}
109
110TSS2_RC LocalTpmReceiveTpmResponse(
111 TSS2_TCTI_CONTEXT *tctiContext, /* in */
112 size_t *response_size, /* out */
113 unsigned char *response_buffer, /* in */
114 int32_t timeout
115 )
116{
117 TSS2_RC rval = TSS2_RC_SUCCESS;
118 ssize_t size;
Will-nuc3ebfeb92015-10-29 15:53:27 -0400119
wcarthurf979e352015-11-24 17:34:44 -0500120 rval = CommonReceiveChecks( tctiContext, response_size, response_buffer );
121
122 if( rval == TSS2_RC_SUCCESS )
Will Arthurca8e7f32015-08-03 15:35:19 -0400123 {
wcarthureedecd62015-11-20 16:59:45 -0500124 size = read( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile, response_buffer, *response_size );
Will-nuc9ab34612015-10-29 17:02:47 -0400125
wcarthureedecd62015-11-20 16:59:45 -0500126 if( size < 0 )
Will-nuc9ab34612015-10-29 17:02:47 -0400127 {
wcarthureedecd62015-11-20 16:59:45 -0500128 (*tpmLocalTpmPrintf)(NO_PREFIX, "send failed with error: %d\n", errno );
129 rval = TSS2_TCTI_RC_IO_ERROR;
130 *response_size = 0;
Will-nuc9ab34612015-10-29 17:02:47 -0400131 }
wcarthureedecd62015-11-20 16:59:45 -0500132 else
133 {
134#ifdef DEBUG
135 UINT32 cnt = CHANGE_ENDIAN_DWORD(((TPM20_Header_Out *) response_buffer)->responseSize);
136
137 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgLevel == TSS2_TCTI_DEBUG_MSG_ENABLED )
138 {
139 (*tpmLocalTpmPrintf)( rmDebugPrefix, "\n" );
140 (*tpmLocalTpmPrintf)( rmDebugPrefix, "Response Received: " );
141 DEBUG_PRINT_BUFFER( response_buffer, cnt );
142 }
Will-nuc9ab34612015-10-29 17:02:47 -0400143#endif
wcarthureedecd62015-11-20 16:59:45 -0500144
145 *response_size = size;
146 }
Will Arthurca8e7f32015-08-03 15:35:19 -0400147 }
148
wcarthureedecd62015-11-20 16:59:45 -0500149 if( rval == TSS2_RC_SUCCESS )
150 {
151 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_RECEIVE_RESPONSE;
152 }
153
Will Arthurca8e7f32015-08-03 15:35:19 -0400154 return rval;
155}
156
wcarthurf979e352015-11-24 17:34:44 -0500157TSS2_RC LocalTpmFinalize(
Will Arthurca8e7f32015-08-03 15:35:19 -0400158 TSS2_TCTI_CONTEXT *tctiContext /* in */
159 )
160{
wcarthurf979e352015-11-24 17:34:44 -0500161 TSS2_RC rval = TSS2_RC_SUCCESS;
162
163 if( tctiContext == NULL )
164 {
165 rval = TSS2_TCTI_RC_BAD_REFERENCE;
166 }
167 else
168 {
169 close( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile );
170 }
171
172 return rval;
Will Arthurca8e7f32015-08-03 15:35:19 -0400173}
174
175TSS2_RC LocalTpmCancel(
176 TSS2_TCTI_CONTEXT *tctiContext
177 )
178{
179 TSS2_RC rval = TSS2_RC_SUCCESS;
180
181 // TBD. Needs support from device driver.
182
183 return rval;
184}
185
186TSS2_RC LocalTpmSetLocality(
187 TSS2_TCTI_CONTEXT *tctiContext, /* in */
188 uint8_t locality /* in */
189 )
190{
191 TSS2_RC rval = TSS2_RC_SUCCESS;
192
193 // TBD: how do I do this?
194
195 return rval;
196}
197
198TSS2_RC InitLocalTpmTcti (
199 TSS2_TCTI_CONTEXT *tctiContext, // OUT
200 size_t *contextSize, // IN/OUT
201 const char *config, // IN
202 const uint64_t magic,
203 const uint32_t version,
204 const char *interfaceName,
205 const uint8_t serverSockets // Unused for local TPM.
206 )
207{
208 TSS2_RC rval = TSS2_RC_SUCCESS;
209 char fileName[200];
210
211 if( tctiContext == NULL )
212 {
213 *contextSize = sizeof( TSS2_TCTI_CONTEXT_INTEL );
214 return TSS2_RC_SUCCESS;
215 }
216 else
217 {
218 OpenOutFile( &outFp );
219 (*tpmLocalTpmPrintf)(NO_PREFIX, "Initializing %s Interface\n", interfaceName );
220
221 // Init TCTI context.
222 ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->magic = magic;
223 ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->version = version;
224 ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->transmit = LocalTpmSendTpmCommand;
225 ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->receive = LocalTpmReceiveTpmResponse;
226 ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->finalize = LocalTpmFinalize;
227 ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->cancel = LocalTpmCancel;
228 ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->getPollHandles = 0;
229 ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->setLocality = LocalTpmSetLocality;
230 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality = 3;
231 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 0;
232 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.rmDebugPrefix = 0;
233 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->currentTctiContext = 0;
wcarthureedecd62015-11-20 16:59:45 -0500234 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_INITIALIZE;
Will Arthurca8e7f32015-08-03 15:35:19 -0400235
236 // Get hostname and port.
237 if( ( strlen( config ) + 2 ) <= ( HOSTNAME_LENGTH ) )
238 {
239 if( 1 != sscanf( config, "%199s", fileName ) )
240 {
241 return( TSS2_TCTI_RC_BAD_VALUE );
242 }
243 }
244 else
245 {
246 return( TSS2_TCTI_RC_INSUFFICIENT_BUFFER );
247 }
248
249 ( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile ) = open( fileName, O_RDWR );
250 if( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile < 0 )
251 {
252 return( TSS2_TCTI_RC_IO_ERROR );
253 }
254
255 CloseOutFile( &outFp );
256 }
257
258 return rval;
259}
260
261TSS2_RC TeardownLocalTpmTcti (
262 TSS2_TCTI_CONTEXT *tctiContext, // OUT
263 const char *config, // IN
264 const char *interfaceName
265 )
266{
267 OpenOutFile( &outFp );
268 (*tpmLocalTpmPrintf)(NO_PREFIX, "Tearing down %s Interface\n", interfaceName );
269 CloseOutFile( &outFp );
270
271 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->finalize( tctiContext );
272
273
274 return TSS2_RC_SUCCESS;
275}
276