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
36 #include "Tpm.h"
37 #include "NV_spt_fp.h"
38
39 //** Functions
40
41 //*** NvReadAccessChecks()
42 // Common routine for validating a read
43 // Used by TPM2_NV_Read, TPM2_NV_ReadLock and TPM2_PolicyNV
44 // Return Type: TPM_RC
45 // TPM_RC_NV_AUTHORIZATION autHandle is not allowed to authorize read
46 // of the index
47 // TPM_RC_NV_LOCKED Read locked
48 // TPM_RC_NV_UNINITIALIZED Try to read an uninitialized index
49 //
50 TPM_RC
NvReadAccessChecks(TPM_HANDLE authHandle,TPM_HANDLE nvHandle,TPMA_NV attributes)51 NvReadAccessChecks(
52 TPM_HANDLE authHandle, // IN: the handle that provided the
53 // authorization
54 TPM_HANDLE nvHandle, // IN: the handle of the NV index to be read
55 TPMA_NV attributes // IN: the attributes of 'nvHandle'
56 )
57 {
58 // If data is read locked, returns an error
59 if(IS_ATTRIBUTE(attributes, TPMA_NV, READLOCKED))
60 return TPM_RC_NV_LOCKED;
61 // If the authorization was provided by the owner or platform, then check
62 // that the attributes allow the read. If the authorization handle
63 // is the same as the index, then the checks were made when the authorization
64 // was checked..
65 if(authHandle == TPM_RH_OWNER)
66 {
67 // If Owner provided authorization then ONWERWRITE must be SET
68 if(!IS_ATTRIBUTE(attributes, TPMA_NV, OWNERREAD))
69 return TPM_RC_NV_AUTHORIZATION;
70 }
71 else if(authHandle == TPM_RH_PLATFORM)
72 {
73 // If Platform provided authorization then PPWRITE must be SET
74 if(!IS_ATTRIBUTE(attributes, TPMA_NV, PPREAD))
75 return TPM_RC_NV_AUTHORIZATION;
76 }
77 // If neither Owner nor Platform provided authorization, make sure that it was
78 // provided by this index.
79 else if(authHandle != nvHandle)
80 return TPM_RC_NV_AUTHORIZATION;
81
82 // If the index has not been written, then the value cannot be read
83 // NOTE: This has to come after other access checks to make sure that
84 // the proper authorization is given to TPM2_NV_ReadLock()
85 if(!IS_ATTRIBUTE(attributes, TPMA_NV, WRITTEN))
86 return TPM_RC_NV_UNINITIALIZED;
87
88 return TPM_RC_SUCCESS;
89 }
90
91 //*** NvWriteAccessChecks()
92 // Common routine for validating a write
93 // Used by TPM2_NV_Write, TPM2_NV_Increment, TPM2_SetBits, and TPM2_NV_WriteLock
94 // Return Type: TPM_RC
95 // TPM_RC_NV_AUTHORIZATION Authorization fails
96 // TPM_RC_NV_LOCKED Write locked
97 //
98 TPM_RC
NvWriteAccessChecks(TPM_HANDLE authHandle,TPM_HANDLE nvHandle,TPMA_NV attributes)99 NvWriteAccessChecks(
100 TPM_HANDLE authHandle, // IN: the handle that provided the
101 // authorization
102 TPM_HANDLE nvHandle, // IN: the handle of the NV index to be written
103 TPMA_NV attributes // IN: the attributes of 'nvHandle'
104 )
105 {
106 // If data is write locked, returns an error
107 if(IS_ATTRIBUTE(attributes, TPMA_NV, WRITELOCKED))
108 return TPM_RC_NV_LOCKED;
109 // If the authorization was provided by the owner or platform, then check
110 // that the attributes allow the write. If the authorization handle
111 // is the same as the index, then the checks were made when the authorization
112 // was checked..
113 if(authHandle == TPM_RH_OWNER)
114 {
115 // If Owner provided authorization then ONWERWRITE must be SET
116 if(!IS_ATTRIBUTE(attributes, TPMA_NV, OWNERWRITE))
117 return TPM_RC_NV_AUTHORIZATION;
118 }
119 else if(authHandle == TPM_RH_PLATFORM)
120 {
121 // If Platform provided authorization then PPWRITE must be SET
122 if(!IS_ATTRIBUTE(attributes, TPMA_NV, PPWRITE))
123 return TPM_RC_NV_AUTHORIZATION;
124 }
125 // If neither Owner nor Platform provided authorization, make sure that it was
126 // provided by this index.
127 else if(authHandle != nvHandle)
128 return TPM_RC_NV_AUTHORIZATION;
129 return TPM_RC_SUCCESS;
130 }
131
132 //*** NvClearOrderly()
133 // This function is used to cause gp.orderlyState to be cleared to the
134 // non-orderly state.
135 TPM_RC
NvClearOrderly(void)136 NvClearOrderly(
137 void
138 )
139 {
140 if(gp.orderlyState < SU_DA_USED_VALUE)
141 RETURN_IF_NV_IS_NOT_AVAILABLE;
142 g_clearOrderly = TRUE;
143 return TPM_RC_SUCCESS;
144 }
145
146 //*** NvIsPinPassIndex()
147 // Function to check to see if an NV index is a PIN Pass Index
148 // Return Type: BOOL
149 // TRUE(1) is pin pass
150 // FALSE(0) is not pin pass
151 BOOL
NvIsPinPassIndex(TPM_HANDLE index)152 NvIsPinPassIndex(
153 TPM_HANDLE index // IN: Handle to check
154 )
155 {
156 if(HandleGetType(index) == TPM_HT_NV_INDEX)
157 {
158 NV_INDEX *nvIndex = NvGetIndexInfo(index, NULL);
159
160 return IsNvPinPassIndex(nvIndex->publicArea.attributes);
161 }
162 return FALSE;
163 }
164