Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -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 <tpm20.h> |
| 29 | #include <tss2_sysapi_util.h> |
| 30 | |
| 31 | #define SESSION_MARSHAL_UINT32( buffer, size, currentPtr, value, rval, exitLoc ) \ |
| 32 | Marshal_UINT32( buffer, size, currentPtr, value, rval ); \ |
| 33 | if( *rval != TSS2_RC_SUCCESS ) goto exitLoc; |
| 34 | |
| 35 | #define SESSION_MARSHAL_UINT8( buffer, size, currentPtr, value, rval, exitLoc ) \ |
| 36 | Marshal_UINT8( buffer, size, currentPtr, value, rval ); \ |
| 37 | if( *rval != TSS2_RC_SUCCESS ) goto exitLoc; |
| 38 | |
| 39 | #define SESSION_MARSHAL_SIMPLE_TPM2B( buffer, size, currentPtr, value, rval, exitLoc ) \ |
| 40 | Marshal_Simple_TPM2B( buffer, size, currentPtr, value, rval ); \ |
| 41 | if( *rval != TSS2_RC_SUCCESS ) goto exitLoc; |
| 42 | |
| 43 | #define SESSION_UNMARSHAL_UINT32( buffer, size, currentPtr, value, rval, exitLoc ) \ |
| 44 | Unmarshal_UINT32( buffer, size, currentPtr, value, rval ); \ |
| 45 | if( *rval != TSS2_RC_SUCCESS ) goto exitLoc; |
| 46 | |
| 47 | #define SESSION_UNMARSHAL_UINT8( buffer, size, currentPtr, value, rval, exitLoc ) \ |
| 48 | Unmarshal_UINT8( buffer, size, currentPtr, value, rval ); \ |
| 49 | if( *rval != TSS2_RC_SUCCESS ) goto exitLoc; |
| 50 | |
| 51 | #define SESSION_UNMARSHAL_SIMPLE_TPM2B( buffer, size, currentPtr, value, rval, exitLoc ) \ |
| 52 | Unmarshal_Simple_TPM2B( buffer, size, currentPtr, value, rval ); \ |
| 53 | if( *rval != TSS2_RC_SUCCESS ) goto exitLoc; |
| 54 | |
| 55 | |
| 56 | //static TPMI_SH_AUTH_SESSION authHandle1, authHandle2; |
| 57 | |
| 58 | // |
| 59 | // Copy session data for commands that require it. |
| 60 | // |
| 61 | // Inputs: |
| 62 | // |
| 63 | // pointer to pointer to sessionData area of command |
| 64 | // |
| 65 | // pointer to session data to be copied into command buffer |
| 66 | // |
| 67 | // Outputs: |
| 68 | // |
| 69 | // sessionDataPtr points to end byte past command buffer. This allows |
| 70 | // caller to set the commandSize field for the command. |
| 71 | // |
| 72 | TSS2_RC CopySessionDataIn( void **otherData, TPMS_AUTH_COMMAND const *sessionData, UINT32 *sessionSizePtr ) |
| 73 | { |
| 74 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 75 | UINT8 *inBuffPtr = *otherData; |
| 76 | TPMS_AUTH_COMMAND *sessionDataCopy = (TPMS_AUTH_COMMAND *)sessionData; |
| 77 | |
| 78 | // Size of session data |
| 79 | *sessionSizePtr += CHANGE_ENDIAN_DWORD( |
| 80 | sizeof( TPMI_SH_AUTH_SESSION ) + sizeof( UINT16 ) + |
| 81 | sessionData->nonce.t.size + sizeof( UINT8 ) + |
| 82 | sizeof( UINT16 ) + sessionData->hmac.t.size ); |
| 83 | |
| 84 | // copy session handle |
| 85 | SESSION_MARSHAL_UINT32( inBuffPtr, *sessionSizePtr, (UINT8 **)otherData, sessionDataCopy->sessionHandle, &rval, exitCopySessionDataIn ); |
| 86 | |
| 87 | // Copy nonce |
| 88 | SESSION_MARSHAL_SIMPLE_TPM2B( inBuffPtr, *sessionSizePtr, (UINT8 **)otherData, &( sessionDataCopy->nonce.b ), &rval, exitCopySessionDataIn ); |
| 89 | |
| 90 | // Copy attributes |
| 91 | SESSION_MARSHAL_UINT8( inBuffPtr, *sessionSizePtr, (UINT8 **)otherData, (UINT8)( sessionDataCopy->sessionAttributes.val ), &rval, exitCopySessionDataIn ); |
| 92 | |
| 93 | // Copy hmac data. |
| 94 | SESSION_MARSHAL_SIMPLE_TPM2B( inBuffPtr, *sessionSizePtr, (UINT8 **)otherData, &( sessionDataCopy->hmac.b ), &rval, exitCopySessionDataIn ); |
| 95 | |
| 96 | exitCopySessionDataIn: |
| 97 | return rval; |
| 98 | } |
| 99 | |
| 100 | // |
| 101 | // Copy session data response from commands that return it. |
| 102 | // |
| 103 | // Inputs: |
| 104 | // |
| 105 | // otherData: pointer to pointer to start of sessions data in TPM output data stream |
| 106 | // |
| 107 | // sessionData: pointer to session data structure to be filled in with return data |
| 108 | // |
| 109 | // Outputs: |
| 110 | // |
| 111 | // sessionData points to returned session data. |
| 112 | // |
| 113 | // otherData points to next byte after the sessions data in the output data stream. |
| 114 | // This allows subsequent calls to this function to get the next session data. *nextData CXX0017: Error: symbol "nextData" not found |
| 115 | |
| 116 | // |
| 117 | TSS2_RC CopySessionDataOut( TPMS_AUTH_RESPONSE *sessionData, void **otherData, UINT8* outBuffPtr, UINT32 outBuffSize ) |
| 118 | { |
| 119 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 120 | TPMS_AUTH_RESPONSE *sessionDataCopy = (TPMS_AUTH_RESPONSE *)sessionData; |
| 121 | |
| 122 | if( sessionData == 0 ) |
| 123 | return TSS2_SYS_RC_BAD_REFERENCE; |
| 124 | |
| 125 | outBuffSize -= ((UINT8 *)*otherData - outBuffPtr + 1 ); |
| 126 | outBuffPtr = *otherData; |
| 127 | |
| 128 | // Copy nonceTpm |
| 129 | SESSION_UNMARSHAL_SIMPLE_TPM2B( outBuffPtr, outBuffSize, (UINT8 **)otherData, &(sessionDataCopy->nonce.b), &rval, exitCopySessionDataOut ); |
| 130 | |
| 131 | // Copy sessionAttributes |
| 132 | SESSION_UNMARSHAL_UINT8( outBuffPtr, outBuffSize, (UINT8 **)otherData, (UINT8 *)&( sessionDataCopy->sessionAttributes ), &rval, exitCopySessionDataOut ); |
| 133 | |
| 134 | // Copy hmac |
| 135 | SESSION_UNMARSHAL_SIMPLE_TPM2B( outBuffPtr, outBuffSize, (UINT8 **)otherData, &(sessionDataCopy->hmac.b), &rval, exitCopySessionDataOut ); |
| 136 | |
| 137 | exitCopySessionDataOut: |
| 138 | return rval; |
| 139 | } |
| 140 | |
| 141 | // |
| 142 | // Copy all sessions data from sessions structure into command input byte stream. |
| 143 | // |
| 144 | TSS2_RC CopySessionsDataIn( void **otherData, TSS2_SYS_CMD_AUTHS const *sessionsDataIn ) |
| 145 | { |
| 146 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 147 | UINT8 i = 0; |
| 148 | UINT32 *sessionSizePtr = (UINT32 *)(*otherData); |
| 149 | |
| 150 | if( sessionsDataIn != 0 ) |
| 151 | { |
| 152 | *sessionSizePtr = 0; |
| 153 | |
| 154 | if( sessionsDataIn->cmdAuthsCount != 0 ) |
| 155 | { |
| 156 | // Skip over session size field |
| 157 | *otherData = ( ( UINT32 *)*otherData ) + 1; |
| 158 | |
| 159 | for( i = 0; i < sessionsDataIn->cmdAuthsCount; i++ ) |
| 160 | { |
| 161 | rval = CopySessionDataIn( otherData, sessionsDataIn->cmdAuths[i], sessionSizePtr ); |
| 162 | if( rval != TSS2_RC_SUCCESS ) |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | return rval; |
| 168 | } |
| 169 | |
| 170 | TSS2_RC CopySessionsDataOut( |
| 171 | TSS2_SYS_RSP_AUTHS *rspAuthsArray, |
| 172 | void *otherData, |
| 173 | TPM_ST tag, |
| 174 | UINT8* outBuffPtr, |
| 175 | UINT32 outBuffSize |
| 176 | ) |
| 177 | { |
| 178 | TSS2_RC rval = TSS2_RC_SUCCESS; |
| 179 | UINT8 i; |
| 180 | |
| 181 | if( rspAuthsArray != 0 ) |
| 182 | { |
| 183 | if( tag == TPM_ST_SESSIONS ) |
| 184 | { |
| 185 | if( rspAuthsArray != 0 ) |
| 186 | { |
| 187 | for( i = 0; i < rspAuthsArray->rspAuthsCount; i++ ) |
| 188 | { |
| 189 | rval = CopySessionDataOut( rspAuthsArray->rspAuths[i], &otherData, outBuffPtr, outBuffSize ); |
| 190 | if( rval != TSS2_RC_SUCCESS ) |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | return rval; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |