• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //** Introduction
36 // This file contains the functions that support the physical presence operations
37 // of the TPM.
38 
39 //** Includes
40 
41 #include "Tpm.h"
42 
43 //** Functions
44 
45 //*** PhysicalPresencePreInstall_Init()
46 // This function is used to initialize the array of commands that always require
47 // confirmation with physical presence. The array is an array of bits that
48 // has a correspondence with the command code.
49 //
50 // This command should only ever be executable in a manufacturing setting or in
51 // a simulation.
52 //
53 // When set, these cannot be cleared.
54 //
55 void
PhysicalPresencePreInstall_Init(void)56 PhysicalPresencePreInstall_Init(
57     void
58     )
59 {
60     COMMAND_INDEX        commandIndex;
61     // Clear all the PP commands
62     MemorySet(&gp.ppList, 0, sizeof(gp.ppList));
63 
64     // Any command that is PP_REQUIRED should be SET
65     for(commandIndex = 0; commandIndex < COMMAND_COUNT; commandIndex++)
66     {
67         if(s_commandAttributes[commandIndex] & IS_IMPLEMENTED
68            &&  s_commandAttributes[commandIndex] & PP_REQUIRED)
69             SET_BIT(commandIndex, gp.ppList);
70     }
71     // Write PP list to NV
72     NV_SYNC_PERSISTENT(ppList);
73     return;
74 }
75 
76 //*** PhysicalPresenceCommandSet()
77 // This function is used to set the indicator that a command requires
78 // PP confirmation.
79 void
PhysicalPresenceCommandSet(TPM_CC commandCode)80 PhysicalPresenceCommandSet(
81     TPM_CC           commandCode    // IN: command code
82     )
83 {
84     COMMAND_INDEX       commandIndex = CommandCodeToCommandIndex(commandCode);
85 
86     // if the command isn't implemented, the do nothing
87     if(commandIndex == UNIMPLEMENTED_COMMAND_INDEX)
88         return;
89 
90     // only set the bit if this is a command for which PP is allowed
91     if(s_commandAttributes[commandIndex] & PP_COMMAND)
92         SET_BIT(commandIndex, gp.ppList);
93     return;
94 }
95 
96 //*** PhysicalPresenceCommandClear()
97 // This function is used to clear the indicator that a command requires PP
98 // confirmation.
99 void
PhysicalPresenceCommandClear(TPM_CC commandCode)100 PhysicalPresenceCommandClear(
101     TPM_CC           commandCode    // IN: command code
102     )
103 {
104     COMMAND_INDEX       commandIndex = CommandCodeToCommandIndex(commandCode);
105 
106     // If the command isn't implemented, then don't do anything
107     if(commandIndex == UNIMPLEMENTED_COMMAND_INDEX)
108         return;
109 
110     // Only clear the bit if the command does not require PP
111     if((s_commandAttributes[commandIndex] & PP_REQUIRED) == 0)
112         CLEAR_BIT(commandIndex, gp.ppList);
113 
114     return;
115 }
116 
117 //*** PhysicalPresenceIsRequired()
118 // This function indicates if PP confirmation is required for a command.
119 //  Return Type: BOOL
120 //      TRUE(1)         physical presence is required
121 //      FALSE(0)        physical presence is not required
122 BOOL
PhysicalPresenceIsRequired(COMMAND_INDEX commandIndex)123 PhysicalPresenceIsRequired(
124     COMMAND_INDEX    commandIndex   // IN: command index
125     )
126 {
127     // Check the bit map.  If the bit is SET, PP authorization is required
128     return (TEST_BIT(commandIndex, gp.ppList));
129 }
130 
131 //*** PhysicalPresenceCapGetCCList()
132 // This function returns a list of commands that require PP confirmation. The
133 // list starts from the first implemented command that has a command code that
134 // the same or greater than 'commandCode'.
135 //  Return Type: TPMI_YES_NO
136 //      YES         if there are more command codes available
137 //      NO          all the available command codes have been returned
138 TPMI_YES_NO
PhysicalPresenceCapGetCCList(TPM_CC commandCode,UINT32 count,TPML_CC * commandList)139 PhysicalPresenceCapGetCCList(
140     TPM_CC           commandCode,   // IN: start command code
141     UINT32           count,         // IN: count of returned TPM_CC
142     TPML_CC         *commandList    // OUT: list of TPM_CC
143     )
144 {
145     TPMI_YES_NO     more = NO;
146     COMMAND_INDEX   commandIndex;
147 
148     // Initialize output handle list
149     commandList->count = 0;
150 
151     // The maximum count of command we may return is MAX_CAP_CC
152     if(count > MAX_CAP_CC) count = MAX_CAP_CC;
153 
154     // Collect PP commands
155     for(commandIndex = GetClosestCommandIndex(commandCode);
156     commandIndex != UNIMPLEMENTED_COMMAND_INDEX;
157         commandIndex = GetNextCommandIndex(commandIndex))
158     {
159         if(PhysicalPresenceIsRequired(commandIndex))
160         {
161             if(commandList->count < count)
162             {
163                 // If we have not filled up the return list, add this command
164                 // code to it
165                 commandList->commandCodes[commandList->count]
166                     = GetCommandCode(commandIndex);
167                 commandList->count++;
168             }
169             else
170             {
171                 // If the return list is full but we still have PP command
172                 // available, report this and stop iterating
173                 more = YES;
174                 break;
175             }
176         }
177     }
178     return more;
179 }