OSDN Git Service

Re-organize console out and logging out
[openpts/openpts.git] / src / snapshot.c
1 /*
2  * This file is part of the OpenPTS project.
3  *
4  * The Initial Developer of the Original Code is International
5  * Business Machines Corporation. Portions created by IBM
6  * Corporation are Copyright (C) 2010 International Business
7  * Machines Corporation. All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the Common Public License as published by
11  * IBM Corporation; either version 1 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * Common Public License for more details.
18  *
19  * You should have received a copy of the Common Public License
20  * along with this program; if not, a copy can be viewed at
21  * http://www.opensource.org/licenses/cpl1.0.php.
22  */
23
24 /**
25  * \file src/snapshot.c
26  * \brief Functions for snapshot
27  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
28  * @date 2010-11-02
29  * cleanup 2011-01-20 SM
30  *
31  * divided from IML.c
32  * 
33  * The snapshot is part of IR and holds the eventlog par PCR and domain (== Manifest)
34  * OpenPTS manage the snapshot by two demantions, pcr index and level
35  *
36  *   OPENPTS_SNAPSHOT
37  *   OPENPTS_SNAPSHOT_TABLE
38  *
39  * e.g. PC platform
40  *
41  * PCR          Level
42  * NUM   0      1          2
43  * -------------------------------
44  *  0    BIOS
45  *  1    BIOS
46  *  2    BIOS
47  *  3    BIOS
48  *  4    BIOS   IPL
49  *  5    BIOS   IPL
50  *  6    BIOS
51  *  7    BIOS
52  *  8           OS
53  *  9
54  * 10           OS(IMA)
55  * 11           OS(PTS)
56  * 12
57  * 13
58  * 14
59  * 15
60  * 16
61  * 17           DRTM
62  * 18           DRTM
63  * 19           DRTM
64  * 20
65  * 21
66  * 22
67  * 23
68  * --------------------------------
69  */
70
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74
75 #include <tss/platform.h>
76 #include <tss/tss_defines.h>
77 #include <tss/tss_typedef.h>
78 #include <tss/tss_structs.h>
79 #include <tss/tss_error.h>
80 #include <tss/tspi.h>
81
82 #include <openssl/sha.h>
83
84 #include <openpts.h>
85 // #include <log.h>
86
87 /**
88  * New Snapshot
89  *
90  * Return
91  *  ptr to new OPENPTS_SNAPSHOT struct
92  *  NULL for error
93  */
94 OPENPTS_SNAPSHOT * newSnapshot() {
95     OPENPTS_SNAPSHOT *ss = NULL;
96
97     ss = (OPENPTS_SNAPSHOT*) xmalloc(sizeof(OPENPTS_SNAPSHOT));  // leaked
98     if (ss == NULL) {
99         LOG(LOG_ERR, "no memory");
100         return NULL;
101     }
102     memset(ss, 0, sizeof(OPENPTS_SNAPSHOT));
103
104     ss->fsm_behavior = NULL;
105     ss->fsm_binary = NULL;
106     ss->level = 0;
107     ss->event_num = 0;
108
109     return ss;
110 }
111
112
113 /**
114  * Free Snapshot
115  *
116  * return 0:success, -1:error
117  */
118 int freeSnapshot(OPENPTS_SNAPSHOT * ss) {
119
120     /* check */
121     if (ss == NULL) {
122         LOG(LOG_ERR, "null input");
123         return PTS_FATAL;
124     }
125
126     /* Event Wrapper Chain - free */
127     if (ss->start != NULL) {
128         freeEventWrapperChain(ss->start);
129         ss->start = NULL;
130     }
131
132     /* Behavior model - free */
133     if (ss->fsm_behavior != NULL) {
134         freeFsmContext(ss->fsm_behavior);
135         ss->fsm_behavior = NULL;
136     }
137
138     /* Binary model - free */
139     if (ss->fsm_binary != NULL) {
140         freeFsmContext(ss->fsm_binary);
141         ss->fsm_binary = NULL;
142     }
143
144     xfree(ss);
145
146     return PTS_SUCCESS;
147 }
148
149
150 /**
151  * New Snapshot Table
152  *
153  * Return
154  *  ptr to new OPENPTS_SNAPSHOT struct
155  *  NULL for error
156  */
157 OPENPTS_SNAPSHOT_TABLE * newSnapshotTable() {
158     OPENPTS_SNAPSHOT_TABLE *sst = NULL;
159
160     sst = (OPENPTS_SNAPSHOT_TABLE *) xmalloc(sizeof(OPENPTS_SNAPSHOT_TABLE));  // leaked
161     if (sst == NULL) {
162         LOG(LOG_ERR, "no memory");
163         return NULL;
164     }
165     memset(sst, 0, sizeof(OPENPTS_SNAPSHOT_TABLE));
166
167     return sst;
168 }
169
170
171 /**
172  * Free Snapshot Table 
173  *
174  * return 0:success, -1:error
175  */
176 int freeSnapshotTable(OPENPTS_SNAPSHOT_TABLE * sst) {
177     int i, j;
178
179     /* check */
180     if (sst == NULL) {
181         LOG(LOG_ERR, " OPENPTS_SNAPSHOT_TABLE was NULL\n");
182         return PTS_FATAL;
183     }
184
185     for (i = 0; i < MAX_PCRNUM; i++) {
186         for (j = 0; j < MAX_SSLEVEL; j++) {
187             if (sst->snapshot[i][j] != NULL) {
188                 freeSnapshot(sst->snapshot[i][j]);
189             }
190         }
191     }
192
193     xfree(sst);
194     return PTS_SUCCESS;
195 }
196
197
198 /**
199  *  Add snapshot to the table
200  *
201  */
202 int addSnapshotToTable(OPENPTS_SNAPSHOT_TABLE * sst, OPENPTS_SNAPSHOT * ss, int pcr_index, int level) {
203     /* check 1 */
204     if (sst == NULL) {
205         LOG(LOG_ERR, "null input");
206         return PTS_FATAL;
207     }
208     if (ss == NULL) {
209         LOG(LOG_ERR, "null input");
210         return PTS_FATAL;
211     }
212
213     if ((pcr_index < 0) || (MAX_PCRNUM <= pcr_index )) {
214         LOG(LOG_ERR, "bad PCR index, %d\n", pcr_index);
215         return PTS_INTERNAL_ERROR;
216     }
217     if ((level < 0) || (MAX_SSLEVEL <= level)) {
218         LOG(LOG_ERR, "bad level, %d\n", level);
219         return PTS_INTERNAL_ERROR;
220     }
221
222     /* check 2 */
223     if (sst->snapshot[pcr_index][level] != NULL) {
224         LOG(LOG_ERR, "snapshot[%d][%d] already exist", pcr_index, level);
225         return PTS_INTERNAL_ERROR;
226     }
227
228     sst->snapshot[pcr_index][level] = ss;
229
230     return PTS_SUCCESS;
231 }
232
233 /**
234  *  Get snapshot from the table
235  *  Return
236  *    *SNAPSHOT - Hit
237  *    NULL      - missing
238  */
239 OPENPTS_SNAPSHOT *getSnapshotFromTable(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index, int level) {
240     /* check 1 */
241     if (sst == NULL) {
242         LOG(LOG_ERR, "null input");
243         return NULL;
244     }
245
246     if ((pcr_index < 0) || (MAX_PCRNUM <= pcr_index)) {
247         LOG(LOG_ERR, "getSnapshotFromTable() - bad PCR index, %d\n", pcr_index);
248         return NULL;
249     }
250     if ((level < 0) || (MAX_SSLEVEL <= level)) {
251         LOG(LOG_ERR, "getSnapshotFromTable() - bad level, %d\n", level);
252         return NULL;
253     }
254
255     /* check 2 */
256     if (sst->snapshot[pcr_index][level] == NULL) {
257         // DEBUG("sst->snapshot[%d][%d] is null", pcr_index, level);
258         return NULL;
259     }
260
261     return sst->snapshot[pcr_index][level];
262 }
263
264
265 /**
266  *  Get snapshot from the table
267  *  if missing assign new snapshot
268  */
269 OPENPTS_SNAPSHOT *getNewSnapshotFromTable(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index, int level) {
270     /* check 1 */
271     if (sst == NULL) {
272         LOG(LOG_ERR, "null input");
273         return NULL;
274     }
275
276     if ((pcr_index < 0) || (MAX_PCRNUM <= pcr_index)) {
277         LOG(LOG_ERR, "getSnapshotFromTable() - bad PCR index, %d\n", pcr_index);
278         return NULL;
279     }
280     if ((level < 0) || (MAX_SSLEVEL <= level)) {
281         LOG(LOG_ERR, "getSnapshotFromTable() - bad level, %d\n", level);
282         return NULL;
283     }
284
285     /* assign */
286     if (sst->snapshot[pcr_index][level] == NULL) {
287         /* assigne new snapshot */
288         DEBUG_FSM("getNewSnapshotFromTable() - newSnapshot pcr=%d level=%d\n", pcr_index, level);
289         sst->snapshot[pcr_index][level] = newSnapshot();
290         sst->snapshot[pcr_index][level]->pcrIndex = pcr_index;
291         sst->snapshot[pcr_index][level]->level = level;
292     } else {
293         LOG(LOG_ERR, "getNewSnapshotFromTable() - SS pcr=%d,level=%d already exist\n", pcr_index, level);
294         return NULL;
295     }
296
297     return sst->snapshot[pcr_index][level];
298 }
299
300 /**
301  *  Get active snapshot from the table
302  *  level
303  */
304 OPENPTS_SNAPSHOT *getActiveSnapshotFromTable(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index) {
305     int level;
306     /* check 1 */
307     if (sst == NULL) {
308         LOG(LOG_ERR, "null input");
309         return NULL;
310     }
311
312     if ((pcr_index < 0) || (MAX_PCRNUM <= pcr_index)) {
313         LOG(LOG_ERR, "getSnapshotFromTable() - bad PCR index, %d\n", pcr_index);
314         return NULL;
315     }
316
317     /* get the level */
318     level = sst->snapshots_level[pcr_index];
319
320     return sst->snapshot[pcr_index][level];
321 }
322
323
324 /**
325  * Set active level at snapshot[pcr_index]
326  */
327 int setActiveSnapshotLevel(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index, int level) {
328     /* check */
329     if (sst == NULL) {
330         LOG(LOG_ERR, "null input");
331         return PTS_FATAL;
332     }
333
334     if ((pcr_index < 0) || (MAX_PCRNUM <= pcr_index)) {
335         LOG(LOG_ERR, "setActiveSnapshotLevel() - bad PCR index, %d\n", pcr_index);
336         return PTS_INTERNAL_ERROR;
337     }
338     if ((level < 0) || (MAX_SSLEVEL <= level)) {
339         LOG(LOG_ERR, "setActiveSnapshotLevel() - bad level, %d\n", level);
340         return PTS_INTERNAL_ERROR;
341     }
342
343     sst->snapshots_level[pcr_index] = level;
344
345     return PTS_SUCCESS;
346 }
347
348 /**
349  * Increment active level at snapshot[pcr_index]
350  */
351 int incActiveSnapshotLevel(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index) {
352     /* check */
353     if (sst == NULL) {
354         LOG(LOG_ERR, "null input");
355         return PTS_FATAL;
356     }
357
358     if ((pcr_index < 0) || (MAX_PCRNUM <= pcr_index)) {
359         LOG(LOG_ERR, "bad PCR index, %d\n", pcr_index);
360         return PTS_INTERNAL_ERROR;
361     }
362
363     sst->snapshots_level[pcr_index]++;
364
365     return PTS_SUCCESS;
366 }
367
368 /**
369  * Get active level at snapshot[pcr_index]
370  * Error -1
371  */
372 int getActiveSnapshotLevel(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index) {
373     /* check */
374     if (sst == NULL) {
375         LOG(LOG_ERR, "null input");
376         return -1;
377     }
378
379     if ((pcr_index < 0) || (MAX_PCRNUM <= pcr_index)) {
380         LOG(LOG_ERR, "bad PCR index, %d\n", pcr_index);
381         return PTS_INTERNAL_ERROR;
382     }
383
384     return sst->snapshots_level[pcr_index];
385 }