Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/src/main/CommandDispatcher.c

raw
1 /* Microsoft Reference Implementation for TPM 2.0
2 *
3 * The copyright in this software is being made available under the BSD License,
4 * included below. This software may be subject to other third party and
5 * contributor rights, including patent rights, and no such rights are granted
6 * under this license.
7 *
8 * Copyright (c) Microsoft Corporation
9 *
10 * All rights reserved.
11 *
12 * BSD License
13 *
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 *
17 * Redistributions of source code must retain the above copyright notice, this list
18 * of conditions and the following disclaimer.
19 *
20 * Redistributions in binary form must reproduce the above copyright notice, this
21 * list of conditions and the following disclaimer in the documentation and/or
22 * other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 //** Includes and Typedefs
36 #include "Tpm.h"
37
38 #if TABLE_DRIVEN_DISPATCH
39
40 typedef TPM_RC(NoFlagFunction)(void *target, BYTE **buffer, INT32 *size);
41 typedef TPM_RC(FlagFunction)(void *target, BYTE **buffer, INT32 *size, BOOL flag);
42
43 typedef FlagFunction *UNMARSHAL_t;
44
45 typedef INT16(MarshalFunction)(void *source, BYTE **buffer, INT32 *size);
46 typedef MarshalFunction *MARSHAL_t;
47
48 typedef TPM_RC(COMMAND_NO_ARGS)(void);
49 typedef TPM_RC(COMMAND_IN_ARG)(void *in);
50 typedef TPM_RC(COMMAND_OUT_ARG)(void *out);
51 typedef TPM_RC(COMMAND_INOUT_ARG)(void *in, void *out);
52
53 typedef union COMMAND_t
54 {
55 COMMAND_NO_ARGS *noArgs;
56 COMMAND_IN_ARG *inArg;
57 COMMAND_OUT_ARG *outArg;
58 COMMAND_INOUT_ARG *inOutArg;
59 } COMMAND_t;
60
61 // This structure is used by ParseHandleBuffer() and CommandDispatcher(). The
62 // parameters in this structure are unique for each command. The parameters are:
63 // command holds the address of the command processing function that is called
64 // by Command Dispatcher.
65 // inSize this is the size of the command-dependent input structure. The
66 // input structure holds the unmarshaled handles and command
67 // parameters. If the command takes no arguments (handles or
68 // parameters) then inSize will have a value of 0.
69 // outSize this is the size of the command-dependent output structure. The
70 // output structure holds the results of the command in an unmarshaled
71 // form. When command processing is completed, these values are
72 // marshaled into the output buffer. It is always the case that the
73 // unmarshaled version of an output structure is larger then the
74 // marshaled version. This is because the marshaled version contains
75 // the exact same number of significant bytes but with padding removed.
76 // typesOffsets this parameter points to the list of data types that are to be
77 // marshaled or unmarshaled. The list of types follows the 'offsets'
78 // array. The offsets array is variable sized so the typesOffset filed
79 // is necessary for the handle and command processing to be able to
80 // find the types that are being handled. The 'offsets' array may be
81 // empty. The types structure is described below.
82 // offsets this is an array of offsets of each of the parameters in the
83 // command or response. When processing the command parameters (not
84 // handles) the list contains the offset of the next parameter. For
85 // example, if the first command parameter has a size of 4 and there is
86 // a second command parameter, then the offset would be 4, indicating
87 // that the second parameter starts at 4. If the second parameter has
88 // a size of 8, and there is a third parameter, then the second entry
89 // in offsets is 12 (4 for the first parameter and 8 for the second).
90 // An offset value of 0 in the list indicates the start of the response
91 // parameter list. When CommandDispatcher hits this value, it will stop
92 // unmarshaling the parameters and call 'command'. If a command has no
93 // response parameters and only one command parameter, then offsets can
94 // be an empty list.
95
96 typedef struct COMMAND_DESCRIPTOR_t
97 {
98 COMMAND_t command; // Address of the command
99 UINT16 inSize; // Maximum size of the input structure
100 UINT16 outSize; // Maximum size of the output structure
101 UINT16 typesOffset; // address of the types field
102 UINT16 offsets[1];
103 } COMMAND_DESCRIPTOR_t;
104
105 // The 'types' list is an encoded byte array. The byte value has two parts. The most
106 // significant bit is used when a parameter takes a flag and indicates if the flag
107 // should be SET or not. The remaining 7 bits are an index into an array of
108 // addresses of marshaling and unmarshaling functions.
109 // The array of functions is divided into 6 sections with a value assigned
110 // to denote the start of that section (and the end of the previous section). The
111 // defined offset values for each section are:
112 // 0 unmarshaling for handles that do not take flags
113 // HANDLE_FIRST_FLAG_TYPE unmarshaling for handles that take flags
114 // PARAMETER_FIRST_TYPE unmarshaling for parameters that do not take flags
115 // PARAMETER_FIRST_FLAG_TYPE unmarshaling for parameters that take flags
116 // PARAMETER_LAST_TYPE + 1 marshaling for handles
117 // RESPONSE_PARAMETER_FIRST_TYPE marshaling for parameters
118 // RESPONSE_PARAMETER_LAST_TYPE is the last value in the list of marshaling and
119 // unmarshaling functions.
120 //
121 // The types list is constructed with a byte of 0xff at the end of the command
122 // parameters and with an 0xff at the end of the response parameters.
123
124 #if COMPRESSED_LISTS
125 # define PAD_LIST 0
126 #else
127 # define PAD_LIST 1
128 #endif
129 #define _COMMAND_TABLE_DISPATCH_
130 #include "CommandDispatchData.h"
131
132 #define TEST_COMMAND TPM_CC_Startup
133
134 #define NEW_CC
135
136 #else
137
138 #include "Commands.h"
139
140 #endif
141
142 //** Marshal/Unmarshal Functions
143
144 //*** ParseHandleBuffer()
145 // This is the table-driven version of the handle buffer unmarshaling code
146 TPM_RC
147 ParseHandleBuffer(
148 COMMAND *command
149 )
150 {
151 TPM_RC result;
152 #if TABLE_DRIVEN_DISPATCH
153 COMMAND_DESCRIPTOR_t *desc;
154 BYTE *types;
155 BYTE type;
156 BYTE dType;
157
158 // Make sure that nothing strange has happened
159 pAssert(command->index
160 < sizeof(s_CommandDataArray) / sizeof(COMMAND_DESCRIPTOR_t *));
161 // Get the address of the descriptor for this command
162 desc = s_CommandDataArray[command->index];
163
164 pAssert(desc != NULL);
165 // Get the associated list of unmarshaling data types.
166 types = &((BYTE *)desc)[desc->typesOffset];
167
168 // if(s_ccAttr[commandIndex].commandIndex == TEST_COMMAND)
169 // commandIndex = commandIndex;
170 // No handles yet
171 command->handleNum = 0;
172
173 // Get the first type value
174 for(type = *types++;
175 // check each byte to make sure that we have not hit the start
176 // of the parameters
177 (dType = (type & 0x7F)) < PARAMETER_FIRST_TYPE;
178 // get the next type
179 type = *types++)
180 {
181 // See if unmarshaling of this handle type requires a flag
182 if(dType < HANDLE_FIRST_FLAG_TYPE)
183 {
184 // Look up the function to do the unmarshaling
185 NoFlagFunction *f = (NoFlagFunction *)UnmarshalArray[dType];
186 // call it
187 result = f(&(command->handles[command->handleNum]),
188 &command->parameterBuffer,
189 &command->parameterSize);
190 }
191 else
192 {
193 // Look up the function
194 FlagFunction *f = UnmarshalArray[dType];
195
196 // Call it setting the flag to the appropriate value
197 result = f(&(command->handles[command->handleNum]),
198 &command->parameterBuffer,
199 &command->parameterSize, (type & 0x80) != 0);
200 }
201 // Got a handle
202 // We do this first so that the match for the handle offset of the
203 // response code works correctly.
204 command->handleNum += 1;
205 if(result != TPM_RC_SUCCESS)
206 // if the unmarshaling failed, return the response code with the
207 // handle indication set
208 return result + TPM_RC_H + (command->handleNum * TPM_RC_1);
209 }
210 #else
211 BYTE **handleBufferStart = &command->parameterBuffer;
212 INT32 *bufferRemainingSize = &command->parameterSize;
213 TPM_HANDLE *handles = &command->handles[0];
214 UINT32 *handleCount = &command->handleNum;
215 *handleCount = 0;
216 switch(command->code)
217 {
218 #include "HandleProcess.h"
219 #undef handles
220 default:
221 FAIL(FATAL_ERROR_INTERNAL);
222 break;
223 }
224 #endif
225 return TPM_RC_SUCCESS;
226 }
227
228 //*** CommandDispatcher()
229 // Function to unmarshal the command parameters, call the selected action code, and
230 // marshal the response parameters.
231 TPM_RC
232 CommandDispatcher(
233 COMMAND *command
234 )
235 {
236 #if !TABLE_DRIVEN_DISPATCH
237 TPM_RC result;
238 BYTE **paramBuffer = &command->parameterBuffer;
239 INT32 *paramBufferSize = &command->parameterSize;
240 BYTE **responseBuffer = &command->responseBuffer;
241 INT32 *respParmSize = &command->parameterSize;
242 INT32 rSize;
243 TPM_HANDLE *handles = &command->handles[0];
244 //
245 command->handleNum = 0; // The command-specific code knows how
246 // many handles there are. This is for
247 // cataloging the number of response
248 // handles
249 MemoryIoBufferAllocationReset(); // Initialize so that allocation will
250 // work properly
251 switch(GetCommandCode(command->index))
252 {
253 #include "CommandDispatcher.h"
254
255 default:
256 FAIL(FATAL_ERROR_INTERNAL);
257 break;
258 }
259 Exit:
260 MemoryIoBufferZero();
261 return result;
262 #else
263 COMMAND_DESCRIPTOR_t *desc;
264 BYTE *types;
265 BYTE type;
266 UINT16 *offsets;
267 UINT16 offset = 0;
268 UINT32 maxInSize;
269 BYTE *commandIn;
270 INT32 maxOutSize;
271 BYTE *commandOut;
272 COMMAND_t cmd;
273 TPM_HANDLE *handles;
274 UINT32 hasInParameters = 0;
275 BOOL hasOutParameters = FALSE;
276 UINT32 pNum = 0;
277 BYTE dType; // dispatch type
278 TPM_RC result;
279 //
280 // Get the address of the descriptor for this command
281 pAssert(command->index
282 < sizeof(s_CommandDataArray) / sizeof(COMMAND_DESCRIPTOR_t *));
283 desc = s_CommandDataArray[command->index];
284
285 // Get the list of parameter types for this command
286 pAssert(desc != NULL);
287 types = &((BYTE *)desc)[desc->typesOffset];
288
289 // Get a pointer to the list of parameter offsets
290 offsets = &desc->offsets[0];
291 // pointer to handles
292 handles = command->handles;
293
294 // Get the size required to hold all the unmarshaled parameters for this command
295 maxInSize = desc->inSize;
296 // and the size of the output parameter structure returned by this command
297 maxOutSize = desc->outSize;
298
299 MemoryIoBufferAllocationReset();
300 // Get a buffer for the input parameters
301 commandIn = MemoryGetInBuffer(maxInSize);
302 // And the output parameters
303 commandOut = (BYTE *)MemoryGetOutBuffer((UINT32)maxOutSize);
304
305 // Get the address of the action code dispatch
306 cmd = desc->command;
307
308 // Copy any handles into the input buffer
309 for(type = *types++; (type & 0x7F) < PARAMETER_FIRST_TYPE; type = *types++)
310 {
311 // 'offset' was initialized to zero so the first unmarshaling will always
312 // be to the start of the data structure
313 *(TPM_HANDLE *)&(commandIn[offset]) = *handles++;
314 // This check is used so that we don't have to add an additional offset
315 // value to the offsets list to correspond to the stop value in the
316 // command parameter list.
317 if(*types != 0xFF)
318 offset = *offsets++;
319 // maxInSize -= sizeof(TPM_HANDLE);
320 hasInParameters++;
321 }
322 // Exit loop with type containing the last value read from types
323 // maxInSize has the amount of space remaining in the command action input
324 // buffer. Make sure that we don't have more data to unmarshal than is going to
325 // fit.
326
327 // type contains the last value read from types so it is not necessary to
328 // reload it, which is good because *types now points to the next value
329 for(; (dType = (type & 0x7F)) <= PARAMETER_LAST_TYPE; type = *types++)
330 {
331 pNum++;
332 if(dType < PARAMETER_FIRST_FLAG_TYPE)
333 {
334 NoFlagFunction *f = (NoFlagFunction *)UnmarshalArray[dType];
335 result = f(&commandIn[offset], &command->parameterBuffer,
336 &command->parameterSize);
337 }
338 else
339 {
340 FlagFunction *f = UnmarshalArray[dType];
341 result = f(&commandIn[offset], &command->parameterBuffer,
342 &command->parameterSize,
343 (type & 0x80) != 0);
344 }
345 if(result != TPM_RC_SUCCESS)
346 {
347 result += TPM_RC_P + (TPM_RC_1 * pNum);
348 goto Exit;
349 }
350
351 // This check is used so that we don't have to add an additional offset
352 // value to the offsets list to correspond to the stop value in the
353 // command parameter list.
354 if(*types != 0xFF)
355 offset = *offsets++;
356 hasInParameters++;
357 }
358 // Should have used all the bytes in the input
359 if(command->parameterSize != 0)
360 {
361 result = TPM_RC_SIZE;
362 goto Exit;
363 }
364
365 // The command parameter unmarshaling stopped when it hit a value that was out
366 // of range for unmarshaling values and left *types pointing to the first
367 // marshaling type. If that type happens to be the STOP value, then there
368 // are no response parameters. So, set the flag to indicate if there are
369 // output parameters.
370 hasOutParameters = *types != 0xFF;
371
372 // There are four cases for calling, with and without input parameters and with
373 // and without output parameters.
374 if(hasInParameters > 0)
375 {
376 if(hasOutParameters)
377 result = cmd.inOutArg(commandIn, commandOut);
378 else
379 result = cmd.inArg(commandIn);
380 }
381 else
382 {
383 if(hasOutParameters)
384 result = cmd.outArg(commandOut);
385 else
386 result = cmd.noArgs();
387 }
388 if(result != TPM_RC_SUCCESS)
389 goto Exit;
390
391 // Offset in the marshaled output structure
392 offset = 0;
393
394 // Process the return handles, if any
395 command->handleNum = 0;
396
397 // Could make this a loop to process output handles but there is only ever
398 // one handle in the outputs (for now).
399 type = *types++;
400 if((dType = (type & 0x7F)) < RESPONSE_PARAMETER_FIRST_TYPE)
401 {
402 // The out->handle value was referenced as TPM_HANDLE in the
403 // action code so it has to be properly aligned.
404 command->handles[command->handleNum++] =
405 *((TPM_HANDLE *)&(commandOut[offset]));
406 maxOutSize -= sizeof(UINT32);
407 type = *types++;
408 offset = *offsets++;
409 }
410 // Use the size of the command action output buffer as the maximum for the
411 // number of bytes that can get marshaled. Since the marshaling code has
412 // no pointers to data, all of the data being returned has to be in the
413 // command action output buffer. If we try to marshal more bytes than
414 // could fit into the output buffer, we need to fail.
415 for(;(dType = (type & 0x7F)) <= RESPONSE_PARAMETER_LAST_TYPE
416 && !g_inFailureMode; type = *types++)
417 {
418 const MARSHAL_t f = MarshalArray[dType];
419
420 command->parameterSize += f(&commandOut[offset],
421 &command->responseBuffer,
422 &maxOutSize);
423 offset = *offsets++;
424 }
425 result = (maxOutSize < 0) ? TPM_RC_FAILURE : TPM_RC_SUCCESS;
426 Exit:
427 MemoryIoBufferZero();
428 return result;
429 #endif
430 }