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.
27 * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
29 * cleanup 2011-07-06 SM
31 * OpenPTS main context
40 #include <openssl/sha.h>
45 * New OpenPTS context (New)
47 OPENPTS_CONTEXT * newPtsContext(OPENPTS_CONFIG *conf) {
48 OPENPTS_CONTEXT *ctx = NULL;
50 DEBUG_CAL("newPtsContext - start\n");
52 ctx = (OPENPTS_CONTEXT *) xmalloc(sizeof(OPENPTS_CONTEXT));
56 memset(ctx, 0, sizeof(OPENPTS_CONTEXT));
58 /* config - use given config */
62 resetTpm(&ctx->tpm, ctx->drtm);
65 ctx->nonce = newNonceContext();
66 if (ctx->nonce == NULL) {
70 DEBUG_CAL("newPtsContext - done\n");
81 * free OpenPTS context, but keep conf (shared)
83 * TODO(munetoh) check memory leak
85 int freePtsContext(OPENPTS_CONTEXT *ctx) {
87 DEBUG_CAL("freePtsContext - start\n");
90 DEBUG("freePtsContext - NULL\n");
97 /* PCRs - free, malloc at ifm.c */
98 if (ctx->pcrs != NULL) {
99 if (ctx->pcrs->pcr_select_byte != NULL) {
100 xfree(ctx->pcrs->pcr_select_byte);
105 /* Quote - free, malloc at ifm.c, ir.c */
106 if (ctx->validation_data != NULL) {
107 if (ctx->validation_data->rgbExternalData != NULL) {
108 xfree(ctx->validation_data->rgbExternalData);
110 if (ctx->validation_data->rgbData != NULL) {
111 xfree(ctx->validation_data->rgbData);
113 if (ctx->validation_data->rgbValidationData != NULL) {
114 xfree(ctx->validation_data->rgbValidationData);
116 xfree(ctx->validation_data);
120 if (ctx->uuid != NULL) {
123 if (ctx->str_uuid != NULL) {
124 xfree(ctx->str_uuid);
127 /* IML - reset & free */
128 if (ctx->ss_table != NULL) {
129 freeSnapshotTable(ctx->ss_table);
132 /* Properties - free */
133 freePropertyChain(ctx->prop_start);
136 if (ctx->policy_start != NULL) {
137 freePolicyChain(ctx->policy_start);
141 if (ctx->reason_start != NULL) {
142 freeReasonChain(ctx->reason_start);
145 /* RM - free malloc at rm.c */
146 if (ctx->rm_ctx != NULL) {
147 freeRmContext(ctx->rm_ctx);
150 /* IR - free, malloc at ir.c */
151 if (ctx->ir_ctx != NULL) {
152 freeIrContext(ctx->ir_ctx);
155 /* Runtime Validation - free */
158 if (ctx->read_msg != NULL) {
159 xfree(ctx->read_msg);
162 if (ctx->nonce != NULL) {
163 freeNonceContext(ctx->nonce);
166 if (ctx->target_conf_filename != NULL) {
167 xfree(ctx->target_conf_filename);
170 for (i = 0; i < MAX_RM_NUM; i++) {
171 if (ctx->compIDs[i].SimpleName != NULL) xfree(ctx->compIDs[i].SimpleName);
172 if (ctx->compIDs[i].ModelName != NULL) xfree(ctx->compIDs[i].ModelName);
173 if (ctx->compIDs[i].ModelNumber != NULL) xfree(ctx->compIDs[i].ModelNumber);
174 if (ctx->compIDs[i].ModelSerialNumber != NULL) xfree(ctx->compIDs[i].ModelSerialNumber);
175 if (ctx->compIDs[i].ModelSystemClass != NULL) xfree(ctx->compIDs[i].ModelSystemClass);
176 if (ctx->compIDs[i].VersionMajor != NULL) xfree(ctx->compIDs[i].VersionMajor);
177 if (ctx->compIDs[i].VersionMinor != NULL) xfree(ctx->compIDs[i].VersionMinor);
178 if (ctx->compIDs[i].VersionBuild != NULL) xfree(ctx->compIDs[i].VersionBuild);
179 if (ctx->compIDs[i].VersionString != NULL) xfree(ctx->compIDs[i].VersionString);
180 if (ctx->compIDs[i].MfgDate != NULL) xfree(ctx->compIDs[i].MfgDate);
181 if (ctx->compIDs[i].PatchLevel != NULL) xfree(ctx->compIDs[i].PatchLevel);
182 if (ctx->compIDs[i].DiscretePatches != NULL) xfree(ctx->compIDs[i].DiscretePatches);
183 if (ctx->compIDs[i].VendorID_Name != NULL) xfree(ctx->compIDs[i].VendorID_Name);
184 if (ctx->compIDs[i].VendorID_Value != NULL) xfree(ctx->compIDs[i].VendorID_Value);
190 DEBUG_CAL("freePtsContext - done\n");
197 * get Hash Alg string by ID
201 char * getAlgString(int type) {
202 if (type == ALGTYPE_SHA1) {
204 } else if (type == ALGTYPE_MD5) {
207 LOG(LOG_ERR, "unknown type %d\n", type);
214 * rm.model.0.pcr.1=bios_pcr1.uml
215 * rm.model.0.pcr.4=grub_pcr4.uml
218 * level 0 = Platform(BIOS)
219 * level 1 = Runtime(IPL,OS,IMA)
220 * level 2 = apps (TBD)
230 int readFsmFromPropFile(OPENPTS_CONTEXT *ctx, char * filename) {
231 int rc = PTS_SUCCESS;
232 OPENPTS_CONFIG *conf;
235 char buf[FSM_BUF_SIZE];
236 char buf2[FSM_BUF_SIZE];
240 char *model_filename = NULL;
243 OPENPTS_FSM_CONTEXT *fsm = NULL;
244 OPENPTS_SNAPSHOT *ss = NULL;
248 /* new snapshot table */
249 if (ctx->ss_table == NULL) {
250 ctx->ss_table = newSnapshotTable();
255 if ((fp = fopen(filename, "r")) == NULL) {
256 OUTPUT(NLS(MS_OPENPTS, OPENPTS_CONFIG_MISSING, "Cannot open config file '%s'\n"), filename);
261 while (fgets(buf, FSM_BUF_SIZE, fp) != NULL) { // read line
264 /* check for line length */
265 if (len == FSM_BUF_SIZE) {
266 LOG(LOG_ERR, "Line too long in %s\n", filename);
267 OUTPUT(NLS(MS_OPENPTS, OPENPTS_CONFIG_BAD_CONFIG_FILE, "Bad configuration file\n"));
272 /* ignore comment, null line */
275 } else if ((eqp = strstr(buf, "=")) != NULL) {
276 /* this is property line */
279 if (buf[len-1] == '\n') buf[len-1] = '\0';
281 model_filename = NULL;
284 // Using config file <= version 0.2.3
285 if (strstr(buf, "platform.model.") != NULL) {
286 LOG(LOG_ERR, "ptsc.conf has old format <=v0.2.3 %s\n", filename);
287 LOG(LOG_ERR, "change platform.model to rm.model.0\n");
288 OUTPUT(NLS(MS_OPENPTS, OPENPTS_COLLECTOR_BAD_CONFIG_FILE, "Bad configuration file\n"));
293 if (strstr(buf, "runtime.model.") != NULL) {
294 LOG(LOG_ERR, "ptsc.conf has old format <=v0.2.3 %s\n", filename);
295 LOG(LOG_ERR, "change runtime.model to rm.model.1\n");
296 OUTPUT(NLS(MS_OPENPTS, OPENPTS_COLLECTOR_BAD_CONFIG_FILE, "Bad configuration file\n"));
305 if (!strncmp(buf, "rm.model.", 9)) {
306 level = (int) strtol(&buf[9], NULL, 10);
307 pcr_index = (int) strtol(&buf[15], NULL, 10);
308 model_filename = eqp + 1;
310 setModelFile(conf, pcr_index, level, model_filename);
313 fsm = newFsmContext();
315 fsm->pcr_index = pcr_index;
321 conf->model_dir, model_filename);
322 rc = readUmlModel(fsm, buf2);
323 // TODO(munetoh) cehck rc
324 if (rc != PTS_SUCCESS) {
325 LOG(LOG_ERR, "addFsmByPropFile - [%s] / [%s] -> [%s] fail rc=%d, pwd = %s\n",
326 conf->model_dir, model_filename, buf2, rc,
328 goto error; // return -1;
331 /* setup the NEW snapshots, BIOS, GRUB */
332 ss = getNewSnapshotFromTable(ctx->ss_table, pcr_index, level);
334 LOG(LOG_ERR, "FSM has been assigned at lvl=%d pcr=%d %s. check the config file\n",
335 level, pcr_index, buf);
340 ss->fsm_behavior = fsm;
342 // TODO set by getNewSnapshotFromTable
343 // s s->level = level;
344 // ss->pcrIndex = pcr_index;
346 // 2011-02-07 SM added
347 if (ctx->pcrs != NULL && OPENPTS_PCR_INDEX != pcr_index) {
348 ctx->pcrs->pcr_select[pcr_index] = 1;
351 DEBUG_FSM("platform(level%d) pcr[%d] [%s] ss=%p\n",
354 conf->model_filename[level][pcr_index],
358 /* accept only blank lines */
362 while (*ptr != '\0') {
363 if (!isspace(*ptr)) {
364 LOG(LOG_ERR, "Syntax error in %s\n", filename);
365 OUTPUT(NLS(MS_OPENPTS, OPENPTS_COLLECTOR_BAD_CONFIG_FILE, "Bad configuration file\n"));