blob: f60df7060a04883ac992dc5e8c1f4f997a4236c0 [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
Philip Triccadd3b03c2017-03-05 11:38:08 -080028#include "sapi/tpm20.h"
Philip Triccac3dedc22016-01-15 13:47:22 -080029#include "sysapi_util.h"
Tadeusz Struk8203a372017-06-26 15:15:18 -070030#include "tss2_endian.h"
Will Arthur54e04e42015-07-15 11:29:25 -040031
32#define SESSION_MARSHAL_UINT32( buffer, size, currentPtr, value, rval, exitLoc ) \
33 Marshal_UINT32( buffer, size, currentPtr, value, rval ); \
34 if( *rval != TSS2_RC_SUCCESS ) goto exitLoc;
35
36#define SESSION_MARSHAL_UINT8( buffer, size, currentPtr, value, rval, exitLoc ) \
37 Marshal_UINT8( buffer, size, currentPtr, value, rval ); \
38 if( *rval != TSS2_RC_SUCCESS ) goto exitLoc;
39
40#define SESSION_MARSHAL_SIMPLE_TPM2B( buffer, size, currentPtr, value, rval, exitLoc ) \
41 Marshal_Simple_TPM2B( buffer, size, currentPtr, value, rval ); \
42 if( *rval != TSS2_RC_SUCCESS ) goto exitLoc;
43
44#define SESSION_UNMARSHAL_UINT32( buffer, size, currentPtr, value, rval, exitLoc ) \
45 Unmarshal_UINT32( buffer, size, currentPtr, value, rval ); \
46 if( *rval != TSS2_RC_SUCCESS ) goto exitLoc;
47
48#define SESSION_UNMARSHAL_UINT8( buffer, size, currentPtr, value, rval, exitLoc ) \
49 Unmarshal_UINT8( buffer, size, currentPtr, value, rval ); \
50 if( *rval != TSS2_RC_SUCCESS ) goto exitLoc;
51
52#define SESSION_UNMARSHAL_SIMPLE_TPM2B( buffer, size, currentPtr, value, rval, exitLoc ) \
wcarthur7b10eeb2015-12-03 16:40:41 -050053 Unmarshal_Simple_TPM2B_NoSizeCheck( buffer, size, currentPtr, value, rval ); \
Will Arthur54e04e42015-07-15 11:29:25 -040054 if( *rval != TSS2_RC_SUCCESS ) goto exitLoc;
55
56
57//static TPMI_SH_AUTH_SESSION authHandle1, authHandle2;
58
59//
60// Copy session data for commands that require it.
61//
62// Inputs:
63//
64// pointer to pointer to sessionData area of command
65//
66// pointer to session data to be copied into command buffer
67//
68// Outputs:
69//
70// sessionDataPtr points to end byte past command buffer. This allows
71// caller to set the commandSize field for the command.
72//
73TSS2_RC CopySessionDataIn( void **otherData, TPMS_AUTH_COMMAND const *sessionData, UINT32 *sessionSizePtr )
74{
75 TSS2_RC rval = TSS2_RC_SUCCESS;
76 UINT8 *inBuffPtr = *otherData;
77 TPMS_AUTH_COMMAND *sessionDataCopy = (TPMS_AUTH_COMMAND *)sessionData;
Philip Triccadfa41a52016-07-20 17:43:57 -070078
wcarthur12eb0502015-11-06 12:01:24 -050079 if( sessionData == 0 )
80 {
81 rval = TSS2_SYS_RC_BAD_VALUE;
82 goto exitCopySessionDataIn;
83 }
84
Will Arthur54e04e42015-07-15 11:29:25 -040085 // Size of session data
Tadeusz Struk8203a372017-06-26 15:15:18 -070086 *sessionSizePtr += BE_TO_HOST_32(sizeof(TPMI_SH_AUTH_SESSION) +
87 sizeof(UINT16) + sessionData->nonce.t.size + sizeof(UINT8) +
88 sizeof(UINT16) + sessionData->hmac.t.size);
Will Arthur54e04e42015-07-15 11:29:25 -040089
90 // copy session handle
91 SESSION_MARSHAL_UINT32( inBuffPtr, *sessionSizePtr, (UINT8 **)otherData, sessionDataCopy->sessionHandle, &rval, exitCopySessionDataIn );
92
93 // Copy nonce
94 SESSION_MARSHAL_SIMPLE_TPM2B( inBuffPtr, *sessionSizePtr, (UINT8 **)otherData, &( sessionDataCopy->nonce.b ), &rval, exitCopySessionDataIn );
95
96 // Copy attributes
97 SESSION_MARSHAL_UINT8( inBuffPtr, *sessionSizePtr, (UINT8 **)otherData, (UINT8)( sessionDataCopy->sessionAttributes.val ), &rval, exitCopySessionDataIn );
98
99 // Copy hmac data.
100 SESSION_MARSHAL_SIMPLE_TPM2B( inBuffPtr, *sessionSizePtr, (UINT8 **)otherData, &( sessionDataCopy->hmac.b ), &rval, exitCopySessionDataIn );
101
102exitCopySessionDataIn:
103 return rval;
104}
105
106//
107// Copy session data response from commands that return it.
108//
109// Inputs:
110//
111// otherData: pointer to pointer to start of sessions data in TPM output data stream
112//
113// sessionData: pointer to session data structure to be filled in with return data
114//
115// Outputs:
116//
117// sessionData points to returned session data.
118//
119// otherData points to next byte after the sessions data in the output data stream.
Philip Triccadfa41a52016-07-20 17:43:57 -0700120// This allows subsequent calls to this function to get the next session data. *nextData CXX0017: Error: symbol "nextData" not found
Will Arthur54e04e42015-07-15 11:29:25 -0400121
122//
123TSS2_RC CopySessionDataOut( TPMS_AUTH_RESPONSE *sessionData, void **otherData, UINT8* outBuffPtr, UINT32 outBuffSize )
124{
125 TSS2_RC rval = TSS2_RC_SUCCESS;
126 TPMS_AUTH_RESPONSE *sessionDataCopy = (TPMS_AUTH_RESPONSE *)sessionData;
Philip Triccadfa41a52016-07-20 17:43:57 -0700127
Will Arthur54e04e42015-07-15 11:29:25 -0400128 if( sessionData == 0 )
wcarthur12eb0502015-11-06 12:01:24 -0500129 return rval;
Will Arthur54e04e42015-07-15 11:29:25 -0400130
131 outBuffSize -= ((UINT8 *)*otherData - outBuffPtr + 1 );
132 outBuffPtr = *otherData;
Philip Triccadfa41a52016-07-20 17:43:57 -0700133
Will Arthur54e04e42015-07-15 11:29:25 -0400134 // Copy nonceTpm
135 SESSION_UNMARSHAL_SIMPLE_TPM2B( outBuffPtr, outBuffSize, (UINT8 **)otherData, &(sessionDataCopy->nonce.b), &rval, exitCopySessionDataOut );
Philip Triccadfa41a52016-07-20 17:43:57 -0700136
Will Arthur54e04e42015-07-15 11:29:25 -0400137 // Copy sessionAttributes
138 SESSION_UNMARSHAL_UINT8( outBuffPtr, outBuffSize, (UINT8 **)otherData, (UINT8 *)&( sessionDataCopy->sessionAttributes ), &rval, exitCopySessionDataOut );
139
140 // Copy hmac
141 SESSION_UNMARSHAL_SIMPLE_TPM2B( outBuffPtr, outBuffSize, (UINT8 **)otherData, &(sessionDataCopy->hmac.b), &rval, exitCopySessionDataOut );
142
143exitCopySessionDataOut:
144 return rval;
145}
146
147//
148// Copy all sessions data from sessions structure into command input byte stream.
149//
150TSS2_RC CopySessionsDataIn( void **otherData, TSS2_SYS_CMD_AUTHS const *sessionsDataIn )
151{
152 TSS2_RC rval = TSS2_RC_SUCCESS;
153 UINT8 i = 0;
154 UINT32 *sessionSizePtr = (UINT32 *)(*otherData);
155
156 if( sessionsDataIn != 0 )
157 {
158 *sessionSizePtr = 0;
159
160 if( sessionsDataIn->cmdAuthsCount != 0 )
161 {
162 // Skip over session size field
163 *otherData = ( ( UINT32 *)*otherData ) + 1;
164
165 for( i = 0; i < sessionsDataIn->cmdAuthsCount; i++ )
166 {
167 rval = CopySessionDataIn( otherData, sessionsDataIn->cmdAuths[i], sessionSizePtr );
168 if( rval != TSS2_RC_SUCCESS )
169 break;
170 }
171 }
172 }
173 return rval;
174}
175
176TSS2_RC CopySessionsDataOut(
Philip Triccadfa41a52016-07-20 17:43:57 -0700177 TSS2_SYS_RSP_AUTHS *rspAuthsArray,
Will Arthur54e04e42015-07-15 11:29:25 -0400178 void *otherData,
179 TPM_ST tag,
180 UINT8* outBuffPtr,
181 UINT32 outBuffSize
182 )
183{
184 TSS2_RC rval = TSS2_RC_SUCCESS;
185 UINT8 i;
186
187 if( rspAuthsArray != 0 )
188 {
189 if( tag == TPM_ST_SESSIONS )
190 {
Peter Huewebd70f3c2017-06-21 23:53:56 +0200191 for( i = 0; i < rspAuthsArray->rspAuthsCount; i++ )
Will Arthur54e04e42015-07-15 11:29:25 -0400192 {
Peter Huewebd70f3c2017-06-21 23:53:56 +0200193 rval = CopySessionDataOut( rspAuthsArray->rspAuths[i], &otherData, outBuffPtr, outBuffSize );
194 if( rval != TSS2_RC_SUCCESS )
195 break;
Will Arthur54e04e42015-07-15 11:29:25 -0400196 }
197 }
198 }
199 return rval;
200}
201
202
203