2 * This file is part of the OpenPTS project.
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.
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)
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.
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.
25 * \file src/snapshot.c
26 * \brief Functions for snapshot
27 * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
29 * cleanup 2011-01-20 SM
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
37 * OPENPTS_SNAPSHOT_TABLE
43 * -------------------------------
68 * --------------------------------
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>
82 #include <openssl/sha.h>
91 * ptr to new OPENPTS_SNAPSHOT struct
94 OPENPTS_SNAPSHOT * newSnapshot() {
95 OPENPTS_SNAPSHOT *ss = NULL;
97 ss = (OPENPTS_SNAPSHOT*) xmalloc(sizeof(OPENPTS_SNAPSHOT)); // leaked
99 LOG(LOG_ERR, "no memory");
102 memset(ss, 0, sizeof(OPENPTS_SNAPSHOT));
104 ss->fsm_behavior = NULL;
105 ss->fsm_binary = NULL;
116 * return 0:success, -1:error
118 int freeSnapshot(OPENPTS_SNAPSHOT * ss) {
122 LOG(LOG_ERR, "null input");
126 /* Event Wrapper Chain - free */
127 if (ss->start != NULL) {
128 freeEventWrapperChain(ss->start);
132 /* Behavior model - free */
133 if (ss->fsm_behavior != NULL) {
134 freeFsmContext(ss->fsm_behavior);
135 ss->fsm_behavior = NULL;
138 /* Binary model - free */
139 if (ss->fsm_binary != NULL) {
140 freeFsmContext(ss->fsm_binary);
141 ss->fsm_binary = NULL;
154 * ptr to new OPENPTS_SNAPSHOT struct
157 OPENPTS_SNAPSHOT_TABLE * newSnapshotTable() {
158 OPENPTS_SNAPSHOT_TABLE *sst = NULL;
160 sst = (OPENPTS_SNAPSHOT_TABLE *) xmalloc(sizeof(OPENPTS_SNAPSHOT_TABLE)); // leaked
162 LOG(LOG_ERR, "no memory");
165 memset(sst, 0, sizeof(OPENPTS_SNAPSHOT_TABLE));
172 * Free Snapshot Table
174 * return 0:success, -1:error
176 int freeSnapshotTable(OPENPTS_SNAPSHOT_TABLE * sst) {
181 LOG(LOG_ERR, " OPENPTS_SNAPSHOT_TABLE was NULL\n");
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]);
199 * Add snapshot to the table
202 int addSnapshotToTable(OPENPTS_SNAPSHOT_TABLE * sst, OPENPTS_SNAPSHOT * ss, int pcr_index, int level) {
205 LOG(LOG_ERR, "null input");
209 LOG(LOG_ERR, "null input");
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;
217 if ((level < 0) || (MAX_SSLEVEL <= level)) {
218 LOG(LOG_ERR, "bad level, %d\n", level);
219 return PTS_INTERNAL_ERROR;
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;
228 sst->snapshot[pcr_index][level] = ss;
234 * Get snapshot from the table
239 OPENPTS_SNAPSHOT *getSnapshotFromTable(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index, int level) {
242 LOG(LOG_ERR, "null input");
246 if ((pcr_index < 0) || (MAX_PCRNUM <= pcr_index)) {
247 LOG(LOG_ERR, "getSnapshotFromTable() - bad PCR index, %d\n", pcr_index);
250 if ((level < 0) || (MAX_SSLEVEL <= level)) {
251 LOG(LOG_ERR, "getSnapshotFromTable() - bad level, %d\n", level);
256 if (sst->snapshot[pcr_index][level] == NULL) {
257 // DEBUG("sst->snapshot[%d][%d] is null", pcr_index, level);
261 return sst->snapshot[pcr_index][level];
266 * Get snapshot from the table
267 * if missing assign new snapshot
269 OPENPTS_SNAPSHOT *getNewSnapshotFromTable(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index, int level) {
272 LOG(LOG_ERR, "null input");
276 if ((pcr_index < 0) || (MAX_PCRNUM <= pcr_index)) {
277 LOG(LOG_ERR, "getSnapshotFromTable() - bad PCR index, %d\n", pcr_index);
280 if ((level < 0) || (MAX_SSLEVEL <= level)) {
281 LOG(LOG_ERR, "getSnapshotFromTable() - bad level, %d\n", level);
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;
293 LOG(LOG_ERR, "getNewSnapshotFromTable() - SS pcr=%d,level=%d already exist\n", pcr_index, level);
297 return sst->snapshot[pcr_index][level];
301 * Get active snapshot from the table
304 OPENPTS_SNAPSHOT *getActiveSnapshotFromTable(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index) {
308 LOG(LOG_ERR, "null input");
312 if ((pcr_index < 0) || (MAX_PCRNUM <= pcr_index)) {
313 LOG(LOG_ERR, "getSnapshotFromTable() - bad PCR index, %d\n", pcr_index);
318 level = sst->snapshots_level[pcr_index];
320 return sst->snapshot[pcr_index][level];
325 * Set active level at snapshot[pcr_index]
327 int setActiveSnapshotLevel(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index, int level) {
330 LOG(LOG_ERR, "null input");
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;
338 if ((level < 0) || (MAX_SSLEVEL <= level)) {
339 LOG(LOG_ERR, "setActiveSnapshotLevel() - bad level, %d\n", level);
340 return PTS_INTERNAL_ERROR;
343 sst->snapshots_level[pcr_index] = level;
349 * Increment active level at snapshot[pcr_index]
351 int incActiveSnapshotLevel(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index) {
354 LOG(LOG_ERR, "null input");
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;
363 sst->snapshots_level[pcr_index]++;
369 * Get active level at snapshot[pcr_index]
372 int getActiveSnapshotLevel(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index) {
375 LOG(LOG_ERR, "null input");
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;
384 return sst->snapshots_level[pcr_index];