1 | // =================================================================== |
---|
2 | // |
---|
3 | // Copyright (c) 2005, Intel Corp. |
---|
4 | // All rights reserved. |
---|
5 | // |
---|
6 | // Redistribution and use in source and binary forms, with or without |
---|
7 | // modification, are permitted provided that the following conditions |
---|
8 | // are met: |
---|
9 | // |
---|
10 | // * Redistributions of source code must retain the above copyright |
---|
11 | // notice, this list of conditions and the following disclaimer. |
---|
12 | // * Redistributions in binary form must reproduce the above |
---|
13 | // copyright notice, this list of conditions and the following |
---|
14 | // disclaimer in the documentation and/or other materials provided |
---|
15 | // with the distribution. |
---|
16 | // * Neither the name of Intel Corporation nor the names of its |
---|
17 | // contributors may be used to endorse or promote products derived |
---|
18 | // from this software without specific prior written permission. |
---|
19 | // |
---|
20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
---|
23 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
---|
24 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
25 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
26 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
27 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
28 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
---|
29 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
---|
31 | // OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | // =================================================================== |
---|
33 | // |
---|
34 | // contextmgr.c |
---|
35 | // |
---|
36 | // This file contains the context management functions for TCS. |
---|
37 | // |
---|
38 | // ================================================================== |
---|
39 | |
---|
40 | #include <stdio.h> |
---|
41 | #include <string.h> |
---|
42 | #include <malloc.h> |
---|
43 | #include "tcs.h" |
---|
44 | #include "contextmgr.h" |
---|
45 | #include "log.h" |
---|
46 | #include "hashtable.h" |
---|
47 | |
---|
48 | BYTE* AddMemBlock(CONTEXT_HANDLE* pContextHandle, // in |
---|
49 | int BlockSize) { // in |
---|
50 | |
---|
51 | BLOCK* pCurrentBlock = NULL; |
---|
52 | BLOCK* pBlock = NULL; |
---|
53 | |
---|
54 | // check incoming params |
---|
55 | if (pContextHandle == NULL || BlockSize == 0) |
---|
56 | return NULL; |
---|
57 | |
---|
58 | // Create New Block |
---|
59 | pBlock = (BLOCK *)malloc(sizeof(BLOCK)); |
---|
60 | if (pBlock == NULL) |
---|
61 | return (0); |
---|
62 | |
---|
63 | pBlock->aMemory = (BYTE *)malloc(sizeof(BYTE) * BlockSize); |
---|
64 | if (pBlock->aMemory == NULL) |
---|
65 | return (0); |
---|
66 | |
---|
67 | memset(pBlock->aMemory, 0, BlockSize); |
---|
68 | pBlock->nBlockSize = BlockSize; |
---|
69 | pBlock->pNextBlock = NULL; |
---|
70 | |
---|
71 | // search for the last block created where to add the |
---|
72 | // newly created block |
---|
73 | if(pContextHandle->pTopBlock != NULL) { |
---|
74 | pCurrentBlock = pContextHandle->pTopBlock; |
---|
75 | while(pCurrentBlock->pNextBlock != NULL) |
---|
76 | pCurrentBlock = pCurrentBlock->pNextBlock; |
---|
77 | |
---|
78 | |
---|
79 | pCurrentBlock->pNextBlock= pBlock; |
---|
80 | } else |
---|
81 | pContextHandle->pTopBlock = pBlock; |
---|
82 | |
---|
83 | |
---|
84 | pContextHandle->nBlockCount++; |
---|
85 | |
---|
86 | return pBlock->aMemory; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | BOOL DeleteMemBlock(CONTEXT_HANDLE* pContextHandle, // in |
---|
91 | BYTE* pTCPA_BYTEs) { // in |
---|
92 | BLOCK* pCurrentBlock = NULL; |
---|
93 | BLOCK* pParentBlock = NULL; |
---|
94 | BOOL bFound = FALSE; |
---|
95 | |
---|
96 | if (pContextHandle == NULL) |
---|
97 | return FALSE; |
---|
98 | |
---|
99 | |
---|
100 | // Search for the Block in the context by aMemory pointer |
---|
101 | pParentBlock = NULL; |
---|
102 | pCurrentBlock = pContextHandle->pTopBlock; |
---|
103 | |
---|
104 | while(pCurrentBlock != NULL) { |
---|
105 | // If aMemory block is found, delete it |
---|
106 | if(pCurrentBlock->aMemory == pTCPA_BYTEs || pTCPA_BYTEs == NULL) { |
---|
107 | // if it is the top Block, remove it from the top, |
---|
108 | // otherwise remove it from the ParentBlock and stitch |
---|
109 | // the NextBlock to the ParentBlock |
---|
110 | if(pParentBlock == NULL) |
---|
111 | pContextHandle->pTopBlock = pContextHandle->pTopBlock->pNextBlock; |
---|
112 | else |
---|
113 | pParentBlock->pNextBlock = pCurrentBlock->pNextBlock; |
---|
114 | |
---|
115 | // delete memory Block associated with pointer pTCPA_BYTEs |
---|
116 | free(pCurrentBlock->aMemory); |
---|
117 | pCurrentBlock->aMemory = NULL; |
---|
118 | |
---|
119 | free(pCurrentBlock); |
---|
120 | pCurrentBlock = pParentBlock; |
---|
121 | |
---|
122 | pContextHandle->nBlockCount--; |
---|
123 | bFound = TRUE; |
---|
124 | } |
---|
125 | |
---|
126 | if(pCurrentBlock != NULL) { |
---|
127 | pParentBlock = pCurrentBlock; |
---|
128 | pCurrentBlock = pCurrentBlock->pNextBlock; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | return bFound; |
---|
133 | } |
---|
134 | |
---|
135 | BOOL AddHandleToList(TCS_CONTEXT_HANDLE hContext, // in |
---|
136 | TPM_RESOURCE_TYPE type, // in |
---|
137 | TPM_HANDLE handle) { // in |
---|
138 | HANDLE_LIST* pNewHandle = NULL; |
---|
139 | |
---|
140 | vtpmloginfo(VTPM_LOG_TCS_DEEP, "Adding Handle to list\n"); |
---|
141 | CONTEXT_HANDLE* pContextHandle = LookupContext(hContext); |
---|
142 | |
---|
143 | if (pContextHandle == NULL) |
---|
144 | return 0; |
---|
145 | |
---|
146 | pNewHandle = (HANDLE_LIST *)malloc(sizeof(HANDLE_LIST)); |
---|
147 | |
---|
148 | if (pNewHandle == NULL) |
---|
149 | return (0); |
---|
150 | |
---|
151 | pNewHandle->handle = handle; |
---|
152 | pNewHandle->type = type; |
---|
153 | pNewHandle->pNextHandle = pContextHandle->pHandleList; |
---|
154 | |
---|
155 | pContextHandle->pHandleList = pNewHandle; |
---|
156 | |
---|
157 | return 1; |
---|
158 | } |
---|
159 | |
---|
160 | BOOL DeleteHandleFromList( TCS_CONTEXT_HANDLE hContext, // in |
---|
161 | TPM_HANDLE handle) { // in |
---|
162 | |
---|
163 | CONTEXT_HANDLE* pContextHandle = LookupContext(hContext); |
---|
164 | |
---|
165 | HANDLE_LIST *pCurrentHandle = pContextHandle->pHandleList, |
---|
166 | *pLastHandle = pCurrentHandle; |
---|
167 | |
---|
168 | vtpmloginfo(VTPM_LOG_TCS_DEEP, "Deleting Handle from list\n"); |
---|
169 | |
---|
170 | if (pContextHandle == NULL) |
---|
171 | return 0; |
---|
172 | |
---|
173 | while (1) { |
---|
174 | |
---|
175 | if (pCurrentHandle->handle == handle) { // Found element |
---|
176 | if (pCurrentHandle == pLastHandle) { // First element in list |
---|
177 | pContextHandle->pHandleList = pCurrentHandle->pNextHandle; |
---|
178 | free(pCurrentHandle); |
---|
179 | } else { // Ordinary element |
---|
180 | pLastHandle->pNextHandle = pCurrentHandle->pNextHandle; |
---|
181 | free(pCurrentHandle); |
---|
182 | } |
---|
183 | |
---|
184 | return 1; |
---|
185 | |
---|
186 | } else { // Not found yet; |
---|
187 | pLastHandle = pCurrentHandle; |
---|
188 | pCurrentHandle = pCurrentHandle->pNextHandle; |
---|
189 | if (pCurrentHandle == NULL) // Found end of list |
---|
190 | return 0; |
---|
191 | } |
---|
192 | |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | BOOL FreeHandleList( CONTEXT_HANDLE* pContextHandle) { // in |
---|
197 | HANDLE_LIST* pCurrentHandle; |
---|
198 | BOOL returncode = TRUE; |
---|
199 | |
---|
200 | vtpmloginfo(VTPM_LOG_TCS_DEEP, "Freeing all handles for context\n"); |
---|
201 | |
---|
202 | if (pContextHandle == NULL) |
---|
203 | return 1; |
---|
204 | |
---|
205 | pCurrentHandle = pContextHandle->pHandleList; |
---|
206 | while (pCurrentHandle != NULL) { |
---|
207 | |
---|
208 | switch (pCurrentHandle->type) { |
---|
209 | case TPM_RT_KEY: |
---|
210 | returncode = returncode && !TCSP_EvictKey(pContextHandle->handle, pCurrentHandle->handle); |
---|
211 | break; |
---|
212 | case TPM_RT_AUTH: |
---|
213 | returncode = returncode && !TCSP_TerminateHandle(pContextHandle->handle, pCurrentHandle->handle); |
---|
214 | break; |
---|
215 | default: |
---|
216 | returncode = FALSE; |
---|
217 | } |
---|
218 | |
---|
219 | pCurrentHandle = pCurrentHandle->pNextHandle; |
---|
220 | |
---|
221 | } |
---|
222 | |
---|
223 | return 1; |
---|
224 | } |
---|