Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 1 | //**********************************************************************; |
| 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-nuc | 3ebfeb9 | 2015-10-29 15:53:27 -0400 | [diff] [blame] | 39 | #include "debug.h" |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 40 | #include "commonchecks.h" |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 41 | |
| 42 | #ifdef _WIN32 |
| 43 | #define ssize_t int |
| 44 | #elif __linux |
| 45 | #include <unistd.h> |
| 46 | #endif |
| 47 | |
| 48 | #define HOSTNAME_LENGTH 200 |
| 49 | |
| 50 | extern void OpenOutFile( FILE **outFp ); |
| 51 | |
| 52 | extern void CloseOutFile( FILE **outFp ); |
| 53 | |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 54 | extern FILE *outFp; |
| 55 | |
| 56 | extern int ResMgrPrintf( UINT8 type, const char *format, ... ); |
| 57 | int (*tpmLocalTpmPrintf)( UINT8 type, const char *format, ...) = ResMgrPrintf; |
| 58 | |
| 59 | TSS2_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-nuc | 3ebfeb9 | 2015-10-29 15:53:27 -0400 | [diff] [blame] | 68 | #ifdef DEBUG |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 69 | UINT32 commandCode; |
| 70 | UINT32 cnt; |
| 71 | #endif |
| 72 | |
| 73 | rval = CommonSendChecks( tctiContext, command_buffer ); |
Will-nuc | 3ebfeb9 | 2015-10-29 15:53:27 -0400 | [diff] [blame] | 74 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 75 | if( rval == TSS2_RC_SUCCESS ) |
Will-nuc | 3ebfeb9 | 2015-10-29 15:53:27 -0400 | [diff] [blame] | 76 | { |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 77 | #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-nuc | 3ebfeb9 | 2015-10-29 15:53:27 -0400 | [diff] [blame] | 87 | #endif |
| 88 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 89 | size = write( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile, command_buffer, command_size ); |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 90 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 91 | 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 Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 100 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 101 | if( rval == TSS2_RC_SUCCESS ) |
| 102 | { |
| 103 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_SEND_COMMAND; |
| 104 | } |
| 105 | } |
| 106 | |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 107 | return rval; |
| 108 | } |
| 109 | |
| 110 | TSS2_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-nuc | 3ebfeb9 | 2015-10-29 15:53:27 -0400 | [diff] [blame] | 119 | |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame^] | 120 | rval = CommonReceiveChecks( tctiContext, response_size, response_buffer ); |
| 121 | |
| 122 | if( rval == TSS2_RC_SUCCESS ) |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 123 | { |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 124 | size = read( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile, response_buffer, *response_size ); |
Will-nuc | 9ab3461 | 2015-10-29 17:02:47 -0400 | [diff] [blame] | 125 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 126 | if( size < 0 ) |
Will-nuc | 9ab3461 | 2015-10-29 17:02:47 -0400 | [diff] [blame] | 127 | { |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 128 | (*tpmLocalTpmPrintf)(NO_PREFIX, "send failed with error: %d\n", errno ); |
| 129 | rval = TSS2_TCTI_RC_IO_ERROR; |
| 130 | *response_size = 0; |
Will-nuc | 9ab3461 | 2015-10-29 17:02:47 -0400 | [diff] [blame] | 131 | } |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 132 | 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-nuc | 9ab3461 | 2015-10-29 17:02:47 -0400 | [diff] [blame] | 143 | #endif |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 144 | |
| 145 | *response_size = size; |
| 146 | } |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 147 | } |
| 148 | |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 149 | if( rval == TSS2_RC_SUCCESS ) |
| 150 | { |
| 151 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_RECEIVE_RESPONSE; |
| 152 | } |
| 153 | |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 154 | return rval; |
| 155 | } |
| 156 | |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame^] | 157 | TSS2_RC LocalTpmFinalize( |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 158 | TSS2_TCTI_CONTEXT *tctiContext /* in */ |
| 159 | ) |
| 160 | { |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame^] | 161 | 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 Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | TSS2_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 | |
| 186 | TSS2_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 | |
| 198 | TSS2_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; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 234 | ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_INITIALIZE; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 235 | |
| 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 | |
| 261 | TSS2_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 | |