• 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 #include "Tpm.h"
36 #include "ContextLoad_fp.h"
37 
38 #if CC_ContextLoad  // Conditional expansion of this file
39 
40 #include "Context_spt_fp.h"
41 
42 /*(See part 3 specification)
43 // Load context
44 */
45 
46 //  Return Type: TPM_RC
47 //      TPM_RC_CONTEXT_GAP          there is only one available slot and this is not
48 //                                  the oldest saved session context
49 //      TPM_RC_HANDLE               'context.savedHandle' does not reference a saved
50 //                                  session
51 //      TPM_RC_HIERARCHY            'context.hierarchy' is disabled
52 //      TPM_RC_INTEGRITY            'context' integrity check fail
53 //      TPM_RC_OBJECT_MEMORY        no free slot for an object
54 //      TPM_RC_SESSION_MEMORY       no free session slots
55 //      TPM_RC_SIZE                 incorrect context blob size
56 TPM_RC
TPM2_ContextLoad(ContextLoad_In * in,ContextLoad_Out * out)57 TPM2_ContextLoad(
58     ContextLoad_In      *in,            // IN: input parameter list
59     ContextLoad_Out     *out            // OUT: output parameter list
60     )
61 {
62     TPM_RC              result;
63     TPM2B_DIGEST        integrityToCompare;
64     TPM2B_DIGEST        integrity;
65     BYTE                *buffer;    // defined to save some typing
66     INT32               size;       // defined to save some typing
67     TPM_HT              handleType;
68     TPM2B_SYM_KEY       symKey;
69     TPM2B_IV            iv;
70 
71 // Input Validation
72 
73 // See discussion about the context format in TPM2_ContextSave Detailed Actions
74 
75     // IF this is a session context, make sure that the sequence number is
76     // consistent with the version in the slot
77 
78     // Check context blob size
79     handleType = HandleGetType(in->context.savedHandle);
80 
81     // Get integrity from context blob
82     buffer = in->context.contextBlob.t.buffer;
83     size = (INT32)in->context.contextBlob.t.size;
84     result = TPM2B_DIGEST_Unmarshal(&integrity, &buffer, &size);
85     if(result != TPM_RC_SUCCESS)
86         return result;
87 
88     // the size of the integrity value has to match the size of digest produced
89     // by the integrity hash
90     if(integrity.t.size != CryptHashGetDigestSize(CONTEXT_INTEGRITY_HASH_ALG))
91         return TPM_RCS_SIZE + RC_ContextLoad_context;
92 
93     // Make sure that the context blob has enough space for the fingerprint. This
94     // is elastic pants to go with the belt and suspenders we already have to make
95     // sure that the context is complete and untampered.
96     if((unsigned)size < sizeof(in->context.sequence))
97         return TPM_RCS_SIZE + RC_ContextLoad_context;
98 
99     // After unmarshaling the integrity value, 'buffer' is pointing at the first
100     // byte of the integrity protected and encrypted buffer and 'size' is the number
101     // of integrity protected and encrypted bytes.
102 
103     // Compute context integrity
104     ComputeContextIntegrity(&in->context, &integrityToCompare);
105 
106     // Compare integrity
107     if(!MemoryEqual2B(&integrity.b, &integrityToCompare.b))
108         return TPM_RCS_INTEGRITY + RC_ContextLoad_context;
109     // Compute context encryption key
110     ComputeContextProtectionKey(&in->context, &symKey, &iv);
111 
112     // Decrypt context data in place
113     CryptSymmetricDecrypt(buffer, CONTEXT_ENCRYPT_ALG, CONTEXT_ENCRYPT_KEY_BITS,
114                           symKey.t.buffer, &iv, TPM_ALG_CFB, size, buffer);
115     // See if the fingerprint value matches. If not, it is symptomatic of either
116     // a broken TPM or that the TPM is under attack so go into failure mode.
117     if(!MemoryEqual(buffer, &in->context.sequence, sizeof(in->context.sequence)))
118         FAIL(FATAL_ERROR_INTERNAL);
119 
120     // step over fingerprint
121     buffer += sizeof(in->context.sequence);
122 
123     // set the remaining size of the context
124     size -= sizeof(in->context.sequence);
125 
126     // Perform object or session specific input check
127     switch(handleType)
128     {
129         case TPM_HT_TRANSIENT:
130         {
131             OBJECT      *outObject;
132 
133             if(size > (INT32)sizeof(OBJECT))
134                 FAIL(FATAL_ERROR_INTERNAL);
135 
136             // Discard any changes to the handle that the TRM might have made
137             in->context.savedHandle = TRANSIENT_FIRST;
138 
139             // If hierarchy is disabled, no object context can be loaded in this
140             // hierarchy
141             if(!HierarchyIsEnabled(in->context.hierarchy))
142                 return TPM_RCS_HIERARCHY + RC_ContextLoad_context;
143 
144             // Restore object. If there is no empty space, indicate as much
145             outObject = ObjectContextLoad((ANY_OBJECT_BUFFER *)buffer,
146                                           &out->loadedHandle);
147             if(outObject == NULL)
148                 return TPM_RC_OBJECT_MEMORY;
149 
150             break;
151         }
152         case TPM_HT_POLICY_SESSION:
153         case TPM_HT_HMAC_SESSION:
154         {
155             if(size != sizeof(SESSION))
156                 FAIL(FATAL_ERROR_INTERNAL);
157 
158             // This command may cause the orderlyState to be cleared due to
159             // the update of state reset data.  If this is the case, check if NV is
160             // available first
161             RETURN_IF_ORDERLY;
162 
163             // Check if input handle points to a valid saved session and that the
164             // sequence number makes sense
165             if(!SequenceNumberForSavedContextIsValid(&in->context))
166                 return TPM_RCS_HANDLE + RC_ContextLoad_context;
167 
168             // Restore session.  A TPM_RC_SESSION_MEMORY, TPM_RC_CONTEXT_GAP error
169             // may be returned at this point
170             result = SessionContextLoad((SESSION_BUF *)buffer,
171                                         &in->context.savedHandle);
172             if(result != TPM_RC_SUCCESS)
173                 return result;
174 
175             out->loadedHandle = in->context.savedHandle;
176 
177             // orderly state should be cleared because of the update of state
178             // reset and state clear data
179             g_clearOrderly = TRUE;
180 
181             break;
182         }
183         default:
184             // Context blob may only have an object handle or a session handle.
185             // All the other handle type should be filtered out at unmarshal
186             FAIL(FATAL_ERROR_INTERNAL);
187             break;
188     }
189 
190     return TPM_RC_SUCCESS;
191 }
192 
193 #endif // CC_ContextLoad