blob: 0a1a49b03a2c787ea7b5c3b7c583658b7139ab23 [file] [log] [blame]
Will Arthur54e04e42015-07-15 11:29:25 -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 <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//
72TSS2_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
wcarthur12eb0502015-11-06 12:01:24 -050078 if( sessionData == 0 )
79 {
80 rval = TSS2_SYS_RC_BAD_VALUE;
81 goto exitCopySessionDataIn;
82 }
83
Will Arthur54e04e42015-07-15 11:29:25 -040084 // Size of session data
85 *sessionSizePtr += CHANGE_ENDIAN_DWORD(
86 sizeof( TPMI_SH_AUTH_SESSION ) + sizeof( UINT16 ) +
87 sessionData->nonce.t.size + sizeof( UINT8 ) +
88 sizeof( UINT16 ) + sessionData->hmac.t.size );
89
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.
120// This allows subsequent calls to this function to get the next session data. *nextData CXX0017: Error: symbol "nextData" not found
121
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;
127
128 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;
133
134 // Copy nonceTpm
135 SESSION_UNMARSHAL_SIMPLE_TPM2B( outBuffPtr, outBuffSize, (UINT8 **)otherData, &(sessionDataCopy->nonce.b), &rval, exitCopySessionDataOut );
136
137 // 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(
177 TSS2_SYS_RSP_AUTHS *rspAuthsArray,
178 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 {
191 if( rspAuthsArray != 0 )
192 {
193 for( i = 0; i < rspAuthsArray->rspAuthsCount; i++ )
194 {
195 rval = CopySessionDataOut( rspAuthsArray->rspAuths[i], &otherData, outBuffPtr, outBuffSize );
196 if( rval != TSS2_RC_SUCCESS )
197 break;
198 }
199 }
200 }
201 }
202 return rval;
203}
204
205
206