TSS2_RC InitSocketsTcti ( TSS2_TCTI_CONTEXT *tctiContext, // OUT size_t *contextSize, // IN/OUT const char *config, // IN const uint64_t magic, const uint32_t version, const char *interfaceName, const uint8_t serverSockets ) { TSS2_RC rval = TSS2_RC_SUCCESS; char hostName[200]; int port; SOCKET otherSock; SOCKET tpmSock; if( tctiContext == NULL ) { *contextSize = sizeof( TSS2_TCTI_CONTEXT_INTEL ); return TSS2_RC_SUCCESS; } else { OpenOutFile( &outFp ); (*printfFunction)(NO_PREFIX, "Initializing %s Interface\n", interfaceName ); // Init TCTI context. ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->magic = magic; ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->version = version; ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->transmit = SocketSendTpmCommand; ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->receive = SocketReceiveTpmResponse; ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->finalize = SocketFinalize; ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->cancel = SocketCancel; ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->getPollHandles = 0; ((TSS2_TCTI_CONTEXT_COMMON_V1 *)tctiContext)->setLocality = SocketSetLocality; ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality = 3; ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 0; ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.rmDebugPrefix = 0; ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->currentTctiContext = 0; // Get hostname and port. if( ( strlen( config ) + 2 ) <= ( HOSTNAME_LENGTH ) ) { if( 1 == sscanf( config, "%199s", hostName ) ) { if( strlen( config) - ( strlen( hostName ) + 2 ) <= PORT_LENGTH ) { if( 1 != sscanf( &config[strlen( hostName )], "%d", &port ) ) { return( TSS2_TCTI_RC_BAD_VALUE ); } } else { return( TSS2_TCTI_RC_BAD_VALUE ); } } else { return( TSS2_TCTI_RC_BAD_VALUE ); } } else { return( TSS2_TCTI_RC_INSUFFICIENT_BUFFER ); } rval = (TSS2_RC) InitSockets( &hostName[0], port, serverSockets, &otherSock, &tpmSock ); if( rval == TSS2_RC_SUCCESS ) { ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->otherSock = otherSock; ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->tpmSock = tpmSock; } else { CloseSockets( otherSock, tpmSock); } CloseOutFile( &outFp ); } return rval; }
// // This function calculates the session HMAC and updates session state. // UINT32 TpmComputeSessionHmac( TSS2_SYS_CONTEXT *sysContext, TPMS_AUTH_COMMAND *pSessionDataIn, // Pointer to session input struct TPM_HANDLE entityHandle, // Used to determine if we're accessing a different // resource than the bound resoure. TPM_RC responseCode, // Response code for the command, 0xffff for "none" is // used to indicate that no response code is present // (used for calculating command HMACs vs response HMACs). TPM_HANDLE handle1, // First handle == 0xff000000 indicates no handle TPM_HANDLE handle2, // Second handle == 0xff000000 indicates no handle TPMA_SESSION sessionAttributes, // Current session attributes TPM2B_DIGEST *result, // Where the result hash is saved. TPM_RC sessionCmdRval ) { TPM2B_MAX_BUFFER hmacKey; TPM2B *bufferList[7]; TPM2B_DIGEST pHash; SESSION *pSession = 0; TPM2B_AUTH authValue; TPM2B sessionAttributesByteBuffer; UINT16 i; TPM_RC rval; UINT8 nvNameChanged = 0; ENTITY *nvEntity; UINT8 commandCode[4] = { 0, 0, 0, 0 }; UINT32 *cmdCodePtr; UINT32 cmdCode; hmacKey.b.size = 0; rval = GetSessionStruct( pSessionDataIn->sessionHandle, &pSession ); if( rval != TPM_RC_SUCCESS ) { return rval; } rval = ( *CalcPHash )( sysContext, handle1, handle2, pSession->authHash, responseCode, &pHash ); if( rval != TPM_RC_SUCCESS ) return rval; // Use entityHandle to get authValue, if any. if( ( pSession->bind == TPM_RH_NULL ) || ( ( pSession->bind != TPM_RH_NULL ) && ( pSession->bind == entityHandle ) ) ) { rval = GetEntityAuth( entityHandle, &authValue ); if( rval != TPM_RC_SUCCESS ) authValue.t.size = 0; } else { authValue.t.size = 0; } rval = Tss2_Sys_GetCommandCode( sysContext, &commandCode ); if( rval != TPM_RC_SUCCESS ) return rval; if( ( entityHandle >> HR_SHIFT ) == TPM_HT_NV_INDEX ) { // If NV index, get status wrt to name change. If name has changed, // we have to treat it as if its not the bound entity, even if it was // the bound entity. nvNameChanged = pSession->nvNameChanged; } rval = ConcatSizedByteBuffer( (TPM2B_MAX_BUFFER *)&hmacKey, &( pSession->sessionKey.b ) ); if( ( pSession->bind == TPM_RH_NULL ) || ( pSession->bind != entityHandle ) || nvNameChanged ) { rval = ConcatSizedByteBuffer( (TPM2B_MAX_BUFFER *)&hmacKey, &( authValue.b ) ); } #ifdef DEBUG OpenOutFile( &outFp ); TpmClientPrintf( 0, "\n\nhmacKey = " ); PrintSizedBuffer( &(hmacKey.b) ); CloseOutFile( &outFp ); #endif // Create buffer list i = 0; bufferList[i++] = &pHash.b; bufferList[i++] = &( pSession->nonceNewer.b ); bufferList[i++] = &( pSession->nonceOlder.b ); bufferList[i++] = &( pSession->nonceTpmDecrypt.b ); bufferList[i++] = &( pSession->nonceTpmEncrypt.b ); sessionAttributesByteBuffer.size = 1; sessionAttributesByteBuffer.buffer[0] = *(UINT8 *)&sessionAttributes; bufferList[i++] = &( sessionAttributesByteBuffer ); bufferList[i++] = 0; cmdCodePtr = (UINT32 *)&commandCode[0]; cmdCode = *cmdCodePtr; #ifdef DEBUG OpenOutFile( &outFp ); for( i = 0; bufferList[i] != 0; i++ ) { TpmClientPrintf( 0, "\n\nbufferlist[%d]:\n", i ); PrintSizedBuffer( bufferList[i] ); } CloseOutFile( &outFp ); #endif rval = (*HmacFunctionPtr)( pSession->authHash, &hmacKey.b, &( bufferList[0] ), result ); if( rval != TPM_RC_SUCCESS ) return rval; if( ( responseCode != TPM_RC_NO_RESPONSE ) && ( cmdCode == TPM_CC_NV_Write || cmdCode == TPM_CC_NV_Increment || cmdCode == TPM_CC_NV_SetBits ) ) { rval = GetEntity( entityHandle, &nvEntity ); if( rval != TPM_RC_SUCCESS ) { return rval; } else { // Only change session's nvNameChanged parameter when // the NV index's name changes due to a write. if( nvEntity->nvNameChanged == 0 ) { pSession->nvNameChanged = 1; nvEntity->nvNameChanged = 1; } } } return rval; }
// // This function is a helper function used to calculate cpHash and rpHash. // // NOTE: for calculating cpHash, set responseCode to TPM_RC_NO_RESPONSE; this // tells the function to leave it out of the calculation. // TPM_RC TpmCalcPHash( TSS2_SYS_CONTEXT *sysContext, TPM_HANDLE handle1, TPM_HANDLE handle2, TPMI_ALG_HASH authHash, TPM_RC responseCode, TPM2B_DIGEST *pHash ) { TPM_RC rval = TPM_RC_SUCCESS; UINT32 i; TPM2B_NAME name1; TPM2B_NAME name2; TPM2B_MAX_BUFFER hashInput; // Byte stream to be hashed to create pHash UINT8 *hashInputPtr; size_t parametersSize; const uint8_t *startParams; UINT8 cmdCode[4] = {0,0,0,0}; UINT8 *cmdCodePtr = &cmdCode[0]; name1.b.size = name2.b.size = 0; // Calculate pHash // // Only get names for commands if( responseCode == TPM_RC_NO_RESPONSE ) { // Get names for the handles rval = TpmHandleToName( handle1, &name1 ); if( rval != TPM_RC_SUCCESS ) return rval; } #ifdef DEBUG OpenOutFile( &outFp ); TpmClientPrintf( 0, "\n\nNAME1 = \n" ); PrintSizedBuffer( &(name1.b) ); CloseOutFile( &outFp ); #endif // Only get names for commands if( responseCode == TPM_RC_NO_RESPONSE ) { rval = Tss2_Sys_GetCpBuffer( sysContext, ¶metersSize, &startParams); if( rval != TPM_RC_SUCCESS ) return rval; rval = TpmHandleToName( handle2, &name2 ); if( rval != TPM_RC_SUCCESS ) return rval; } else { rval = Tss2_Sys_GetRpBuffer( sysContext, ¶metersSize, &startParams); if( rval != TPM_RC_SUCCESS ) return rval; } #ifdef DEBUG OpenOutFile( &outFp ); TpmClientPrintf( 0, "\n\nNAME2 = \n" ); PrintSizedBuffer( &(name2.b) ); CloseOutFile( &outFp ); #endif // Create pHash input byte stream: first add response code, if any. hashInput.b.size = 0; if( responseCode != TPM_RC_NO_RESPONSE ) { hashInputPtr = &( hashInput.t.buffer[hashInput.b.size] ); *(UINT32 *)hashInputPtr = CHANGE_ENDIAN_DWORD( responseCode ); hashInput.b.size += 4; hashInputPtr += 4; } // Create pHash input byte stream: now add command code. rval = Tss2_Sys_GetCommandCode( sysContext, &cmdCode ); if( rval != TPM_RC_SUCCESS ) return rval; hashInputPtr = &( hashInput.t.buffer[hashInput.b.size] ); *(UINT32 *)hashInputPtr = CHANGE_ENDIAN_DWORD( *(UINT32 *)cmdCodePtr ); hashInput.t.size += 4; // Create pHash input byte stream: now add in names for the handles. rval = ConcatSizedByteBuffer( &hashInput, &( name1.b ) ); if( rval != TPM_RC_SUCCESS ) return rval; rval = ConcatSizedByteBuffer( &hashInput, &( name2.b ) ); if( rval != TPM_RC_SUCCESS ) return rval; if( ( hashInput.t.size + parametersSize ) <= sizeof( hashInput.t.buffer ) ) { // Create pHash input byte stream: now add in parameters byte stream for( i = 0; i < parametersSize; i++ ) hashInput.t.buffer[hashInput.t.size + i ] = startParams[i]; hashInput.t.size += (UINT16)parametersSize; } else { return( APPLICATION_ERROR( TSS2_BASE_RC_INSUFFICIENT_BUFFER ) ); } #ifdef DEBUG OpenOutFile( &outFp ); TpmClientPrintf( 0, "\n\nPHASH input bytes= \n" ); PrintSizedBuffer( &(hashInput.b) ); CloseOutFile( &outFp ); #endif // Now hash the whole mess. if( hashInput.t.size > sizeof( hashInput.t.buffer ) ) { rval = APPLICATION_ERROR( TSS2_BASE_RC_INSUFFICIENT_BUFFER ); } else { rval = TpmHash( authHash, hashInput.t.size, &( hashInput.t.buffer[0] ), pHash ); if( rval != TPM_RC_SUCCESS ) return rval; #ifdef DEBUG OpenOutFile( &outFp ); TpmClientPrintf( 0, "\n\nPHASH = " ); PrintSizedBuffer( &(pHash->b) ); CloseOutFile( &outFp ); #endif } return rval; }
/* convert file ipInFN to UNIX format text and write to file ipOutFN * RetVal: 0 if success * -1 otherwise */ int ConvertDosToUnixNewFile(char *ipInFN, char *ipOutFN, CFlag *ipFlag) { int RetVal = 0; FILE *InF = NULL; FILE *TempF = NULL; char *TempPath; struct stat StatBuf; struct utimbuf UTimeBuf; #ifndef NO_FCHMOD mode_t mask; #endif #ifdef NO_MKSTEMP FILE* fd; #else int fd; #endif if ((ipFlag->Force == 0) && regfile(ipInFN)) { ipFlag->status |= NO_REGFILE ; return -1; } else ipFlag->status = 0 ; /* retrieve ipInFN file date stamp */ if (stat(ipInFN, &StatBuf)) RetVal = -1; #ifdef NO_MKSTEMP if((fd = MakeTempFileFrom(ipOutFN, &TempPath))==NULL) { #else if((fd = MakeTempFileFrom (ipOutFN, &TempPath)) < 0) { #endif perror(_("dos2unix: Failed to open temporary output file")); RetVal = -1; } #ifdef DEBUG fprintf(stderr, _("dos2unix: using %s as temporary file\n"), TempPath); #endif /* can open in file? */ if ((!RetVal) && ((InF=OpenInFile(ipInFN)) == NULL)) RetVal = -1; /* can open output file? */ #ifdef NO_MKSTEMP if ((!RetVal) && (InF) && ((TempF=fd) == NULL)) #else if ((!RetVal) && (InF) && ((TempF=OpenOutFile(fd)) == NULL)) #endif { fclose (InF); InF = NULL; RetVal = -1; } #ifndef NO_FCHMOD /* preserve original mode as modified by umask */ mask = umask(0); umask(mask); if (!RetVal && fchmod(fd, StatBuf.st_mode & ~mask)) RetVal = -1; #endif /* conversion sucessful? */ if ((!RetVal) && (ConvertDosToUnix(InF, TempF, ipFlag))) RetVal = -1; /* can close in file? */ if ((InF) && (fclose(InF) == EOF)) RetVal = -1; /* can close output file? */ if ((TempF) && (fclose(TempF) == EOF)) RetVal = -1; #ifdef NO_MKSTEMP if(fd!=NULL) fclose(fd); #else if(fd>=0) close(fd); #endif if ((!RetVal) && (ipFlag->KeepDate)) { UTimeBuf.actime = StatBuf.st_atime; UTimeBuf.modtime = StatBuf.st_mtime; /* can change output file time to in file time? */ if (utime(TempPath, &UTimeBuf) == -1) RetVal = -1; } /* any error? */ if ((RetVal) && (remove(TempPath))) RetVal = -1; /* can rename temporary file to output file? */ if (!RetVal) { #ifdef NEED_REMOVE remove(ipOutFN); #endif if ((rename(TempPath, ipOutFN) == -1) && (!ipFlag->Quiet)) { fprintf(stderr, _("dos2unix: problems renaming '%s' to '%s'\n"), TempPath, ipOutFN); fprintf(stderr, _(" output file remains in '%s'\n"), TempPath); RetVal = -1; } } free(TempPath); return RetVal; } /* convert file ipInFN to UNIX format text * RetVal: 0 if success * -1 otherwise */ int ConvertDosToUnixOldFile(char* ipInFN, CFlag *ipFlag) { int RetVal = 0; FILE *InF = NULL; FILE *TempF = NULL; char *TempPath; struct stat StatBuf; struct utimbuf UTimeBuf; #ifndef NO_FCHMOD mode_t mode = S_IRUSR | S_IWUSR; #endif #ifdef NO_MKSTEMP FILE* fd; #else int fd; #endif if ((ipFlag->Force == 0) && regfile(ipInFN)) { ipFlag->status |= NO_REGFILE ; return -1; } else ipFlag->status = 0 ; /* retrieve ipInFN file date stamp */ if (stat(ipInFN, &StatBuf)) RetVal = -1; #ifndef NO_FCHMOD else mode = StatBuf.st_mode; #endif #ifdef NO_MKSTEMP if((fd = MakeTempFileFrom(ipInFN, &TempPath))==NULL) { #else if((fd = MakeTempFileFrom(ipInFN, &TempPath)) < 0) { #endif perror(_("dos2unix: Failed to open temporary output file")); RetVal = -1; } #ifndef NO_FCHMOD if (!RetVal && fchmod (fd, mode) && fchmod (fd, S_IRUSR | S_IWUSR)) RetVal = -1; #endif #ifdef DEBUG fprintf(stderr, _("dos2unix: using %s as temporary file\n"), TempPath); #endif /* can open in file? */ if ((!RetVal) && ((InF=OpenInFile(ipInFN)) == NULL)) RetVal = -1; /* can open output file? */ #ifdef NO_MKSTEMP if ((!RetVal) && (InF) && ((TempF=fd) == NULL)) #else if ((!RetVal) && (InF) && ((TempF=OpenOutFile(fd)) == NULL)) #endif { fclose (InF); InF = NULL; RetVal = -1; } /* conversion sucessful? */ if ((!RetVal) && (ConvertDosToUnix(InF, TempF, ipFlag))) RetVal = -1; /* can close in file? */ if ((InF) && (fclose(InF) == EOF)) RetVal = -1; /* can close output file? */ if ((TempF) && (fclose(TempF) == EOF)) RetVal = -1; #ifdef NO_MKSTEMP if(fd!=NULL) fclose(fd); #else if(fd>=0) close(fd); #endif if ((!RetVal) && (ipFlag->KeepDate)) { UTimeBuf.actime = StatBuf.st_atime; UTimeBuf.modtime = StatBuf.st_mtime; /* can change output file time to in file time? */ if (utime(TempPath, &UTimeBuf) == -1) RetVal = -1; } /* any error? */ if ((RetVal) && (remove(TempPath))) RetVal = -1; #ifdef NEED_REMOVE if (!RetVal) remove(ipInFN); #endif /* can rename output file to in file? */ if ((!RetVal) && (rename(TempPath, ipInFN) == -1)) { if (!ipFlag->Quiet) { fprintf(stderr, _("dos2unix: problems renaming '%s' to '%s'\n"), TempPath, ipInFN); fprintf(stderr, _(" output file remains in '%s'\n"), TempPath); } RetVal = -1; } free(TempPath); return RetVal; } /* convert stdin to UNIX format text and write to stdout * RetVal: 0 if success * -1 otherwise */ int ConvertDosToUnixStdio(CFlag *ipFlag) { ipFlag->NewFile = 1; ipFlag->Quiet = 1; ipFlag->KeepDate = 0; ipFlag->Force = 1; #ifdef WIN32 /* stdin and stdout are by default text streams. We need * to set them to binary mode. Otherwise an LF will * automatically be converted to CR-LF on DOS/Windows. * Erwin */ /* 'setmode' was deprecated by MicroSoft * since Visual C++ 2005. Use '_setmode' instead. */ _setmode(fileno(stdout), O_BINARY); _setmode(fileno(stdin), O_BINARY); return (ConvertDosToUnix(stdin, stdout, ipFlag)); #elif defined(MSDOS) || defined(__OS2__) setmode(fileno(stdout), O_BINARY); setmode(fileno(stdin), O_BINARY); return (ConvertDosToUnix(stdin, stdout, ipFlag)); #else return (ConvertDosToUnix(stdin, stdout, ipFlag)); #endif }
void main(int argc, char **argv) { FILE *table_h, *srv_h, *xg_h, *clt_h, *mak_h; int i, j; rpcArgs args; char *name, *ifName; char *serverName = NULL; char *ipFileName = NULL; char *outputDir = NULL; int sign_no = 0, start_no, srv_no; ProcessCmdLine(argc, argv, &serverName, &ipFileName, &outputDir); /* open input file */ table_h = fopen(ipFileName, "r"); MEM_CHK(table_h, "main: Unable to open input file\n"); srv_no = 1; start_no = sign_no; /* * open the first set of output files */ name = GetName(serverName, srv_no); srv_h = OpenOutFile(outputDir, name, "Srv.c"); xg_h = OpenOutFile(outputDir, name, ".xg"); clt_h = OpenOutFile(outputDir, name, "Clt.c"); mak_h = OpenOutFile(outputDir, name, ".mak"); WriteXGHeader(serverName, xg_h, srv_no); WriteServHeader(srv_h, serverName, srv_no); WriteCltHeader(serverName, srv_no, clt_h); WriteMake(serverName, srv_no, mak_h); ifName = name; /* read the table */ while (fscanf(table_h, "%d", &(args.argCount)) != EOF) { /* increment signature number 8.3 format-- only 10^7 dif sign */ sign_no++; if (sign_no > 1.0e+7) FATAL("Max no: of signatures overflow\n"); /* allocate for the arg struct */ args.argDescr = (arg_tuple *) calloc(args.argCount, sizeof(arg_tuple)); MEM_CHK(args.argDescr, "main: Out of memory -- args.argDescr\n"); /* pick out the dirs and the types */ for (i = 0; i < args.argCount; i++) { if (!fscanf (table_h, " ( %s %s )", args.argDescr[i].direction, args.argDescr[i].type)) { FATAL("main: Incorrect input file format\n"); } } /* * switch files when we hit TESTS_PER_FILE */ if (sign_no - start_no >= TESTS_PER_FILE) { /* * Finish up the current files */ WriteServTrailer(srv_h); WriteCltTrailer(serverName, start_no, sign_no, clt_h); fclose(xg_h); fclose(srv_h); fclose(clt_h); fclose(mak_h); /* * Open the next set of output files */ srv_no++; free(ifName); name = GetName(serverName, srv_no); srv_h = OpenOutFile(outputDir, name, "Srv.c"); xg_h = OpenOutFile(outputDir, name, ".xg"); clt_h = OpenOutFile(outputDir, name, "Clt.c"); mak_h = OpenOutFile(outputDir, name, ".mak"); WriteXGHeader(serverName, xg_h, srv_no); WriteServHeader(srv_h, serverName, srv_no); WriteCltHeader(serverName, srv_no, clt_h); WriteMake(serverName, srv_no, mak_h); start_no = sign_no; ifName = name; } /* initialize parameter values */ for (i = 0; i < args.argCount; i++) { for (j = 0; j < IDL_FIX_ARRAY_SIZE; j++) { args.argDescr[i].inValue[j] = NULL; args.argDescr[i].inValue2[j] = NULL; args.argDescr[i].outValue[j] = NULL; args.argDescr[i].outValue2[j] = NULL; } } GenParamValues(&args); /* write rpc desc into body of the interface */ WriteXG(&args, xg_h, serverName, sign_no); /* write the rpc into the manager file */ WriteServC(&args, srv_h, serverName, sign_no); /* write out ITL test */ WriteClt(&args, serverName, sign_no, clt_h); /* free saved values */ for (i = 0; i < args.argCount; i++) { for (j = 0; j < IDL_FIX_ARRAY_SIZE; j++) { if (args.argDescr[i].inValue[j]) free(args.argDescr[i].inValue[j]); if (args.argDescr[i].inValue2[j]) free(args.argDescr[i].inValue2[j]); if (args.argDescr[i].outValue[j]) free(args.argDescr[i].outValue[j]); if (args.argDescr[i].outValue2[j]) free(args.argDescr[i].outValue2[j]); } } free(args.argDescr); } WriteServTrailer(srv_h); WriteCltTrailer(serverName, start_no, (sign_no + 1), clt_h); fclose(clt_h); fclose(table_h); fclose(xg_h); fclose(srv_h); fclose(mak_h); /* * create 1 makefile that drives all the rest */ mak_h = OpenOutFile(outputDir, "Makefile", ""); fprintf(mak_h, "\ntest:all\ntests:all\nall:\n"); fprintf(mak_h, "%s", platform[8]); for (i = 1; i <= srv_no; i++) fprintf(mak_h, "\t%s %s%d.mak %s\n", platform[0], serverName, i, platform[5]); fprintf(mak_h, "\nclean:\n"); for (i = 1; i <= srv_no; i++) fprintf(mak_h, "\t%s %s%d.mak clean\n", platform[0], serverName, i); fclose(mak_h); exit(0); }