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 2012-01-05 SM
33 * name - value (Java Properties style)
35 * SRTM.integrity=valid/invalid/unverified
36 * DRTM.integrity=valid/invalid/unverified
37 * BIOS.integrity=valid/invalid/unverified
38 * IPL.integrity=valid/invalid/unverified
39 * OS.integrity=valid/invalid/unverified
42 * TCG did not define any Security Properties.:-(
50 #include <stdarg.h> /* va_ */
57 OPENPTS_PROPERTY * newProperty(char *name, char *value) {
58 OPENPTS_PROPERTY *prop;
62 LOG(LOG_ERR, "null input");
66 LOG(LOG_ERR, "null input");
70 prop = (OPENPTS_PROPERTY *) xmalloc(sizeof(OPENPTS_PROPERTY));
72 LOG(LOG_ERR, "no memory");
75 memset(prop, 0, sizeof(OPENPTS_PROPERTY));
77 prop->name = smalloc_assert(name);
78 if (prop->name == NULL) {
79 LOG(LOG_ERR, "no memory");
82 prop->value = smalloc_assert(value);
83 if (prop->value == NULL) {
84 LOG(LOG_ERR, "no memory");
94 void freeProperty(OPENPTS_PROPERTY *prop) {
97 LOG(LOG_ERR, "null input");
107 * Free Property Chain
109 int freePropertyChain(OPENPTS_PROPERTY *prop) {
116 if (prop->next != NULL) {
117 freePropertyChain(prop->next);
130 OPENPTS_PROPERTY* getProperty(OPENPTS_CONTEXT *ctx, char *name) {
131 OPENPTS_PROPERTY *prop;
135 LOG(LOG_ERR, "null input");
139 /* look for the prop with name */
140 prop = ctx->prop_start;
141 while (prop != NULL) {
142 if (prop->name == NULL) {
143 LOG(LOG_ERR, "getProperty(%s) fail, bad property entry exist", name);
147 if (!strcmp(name, prop->name)) {
152 prop = (OPENPTS_PROPERTY *) prop->next;
160 * add new property to chain
162 int addProperty(OPENPTS_CONTEXT *ctx, char *name, char *value) {
163 OPENPTS_PROPERTY *start;
164 OPENPTS_PROPERTY *end;
165 OPENPTS_PROPERTY *prop;
169 LOG(LOG_ERR, "null input");
173 LOG(LOG_ERR, "null input");
177 LOG(LOG_ERR, "null input");
181 start = ctx->prop_start;
184 /* malloc new prop */
185 prop = newProperty(name, value);
187 LOG(LOG_ERR, "newProperty() fail");
191 /* update the chain */
194 /* update the link */
195 ctx->prop_start = prop;
196 ctx->prop_end = prop;
200 /* update the link */
202 ctx->prop_end = prop;
213 * set/update property
215 int setProperty(OPENPTS_CONTEXT *ctx, char *name, char *value) {
216 OPENPTS_PROPERTY *hit;
220 LOG(LOG_ERR, "null input");
224 LOG(LOG_ERR, "null input");
228 LOG(LOG_ERR, "null input");
232 /* check existing prop */
233 hit = getProperty(ctx, name);
236 /* missing name, create new prop */
237 addProperty(ctx, name, value);
239 /* hit, update the value */
241 hit->value = smalloc_assert(value);
250 int setEventProperty(OPENPTS_CONTEXT *ctx, char *name, char *value, OPENPTS_PCR_EVENT_WRAPPER *eventWrapper) {
251 int rc = PTS_SUCCESS;
255 LOG(LOG_ERR, "null input");
259 LOG(LOG_ERR, "null input");
263 LOG(LOG_ERR, "null input");
268 if (!strcmp(value, "valid")) {
269 setProperty(ctx, name, value);
273 /* X = digest = base64(digest) */
274 if (!strcmp(value, "digest")) {
275 /* if value = digest, base64 -> set digest as value */
279 /* check, missing event */
280 if (eventWrapper == NULL) {
281 LOG(LOG_ERR, "setEventProperty() - eventWrapper is NULL\n");
284 if (eventWrapper->event == NULL) {
285 LOG(LOG_ERR, "setEventProperty() - event is NULL\n");
288 if (eventWrapper->event->rgbPcrValue == NULL) {
289 LOG(LOG_ERR, "setEventProperty() - rgbPcrValue is NULL\n");
294 (unsigned char *)eventWrapper->event->rgbPcrValue,
298 LOG(LOG_ERR, "encodeBase64 fail");
301 rc = setProperty(ctx, name, buf);
304 if (rc != PTS_SUCCESS) {
305 LOG(LOG_ERR, "setProperty() fail");
311 /* X = eventdata = base64(eventdata) */
312 if (!strcmp(value, "eventdata")) {
314 TSS_PCR_EVENT *event;
317 /* check, missing event */
318 if (eventWrapper == NULL) {
319 LOG(LOG_ERR, "setEventProperty() - eventWrapper is NULL\n");
322 event = eventWrapper->event;
324 LOG(LOG_ERR, "setEventProperty() - event is NULL\n");
327 if (event->ulEventLength > 0) {
329 if (event->rgbEvent == NULL) {
330 LOG(LOG_ERR, "setEventProperty() - rgbEvent is NULL\n");
335 str = snmalloc((char*)event->rgbEvent, event->ulEventLength);
337 LOG(LOG_ERR, "no memory");
338 return PTS_INTERNAL_ERROR;
341 rc = setProperty(ctx, name, str); // TODO 2011-02-03 SM implement
342 if (rc != PTS_SUCCESS) {
343 LOG(LOG_ERR, "setProperty() fail");
348 LOG(LOG_ERR, "missing rgbEvent");
349 return PTS_INTERNAL_ERROR;
353 if (!strcmp(value, "notexist")) {
354 rc = setProperty(ctx, name, value); // TODO
355 if (rc != PTS_SUCCESS) {
356 LOG(LOG_ERR, "setProperty() fail");
363 rc = setProperty(ctx, name, value);
364 if (rc != PTS_SUCCESS) {
365 LOG(LOG_ERR, "setProperty() fail");
380 * @param update BHV action
383 int validateProperty(OPENPTS_CONTEXT *ctx, char *name, char *value, char *action) {
384 int rc = OPENPTS_FSM_ERROR;
385 OPENPTS_PROPERTY* prop;
389 LOG(LOG_ERR, "null input");
393 LOG(LOG_ERR, "null input");
397 LOG(LOG_ERR, "null input");
405 prop = getProperty(ctx, name);
409 LOG(LOG_ERR, "validateProperty - property %s is missing\n", name);
410 rc = OPENPTS_FSM_ERROR;
412 /* name hit? check the value */
413 if (!strcmp(value, prop->value)) {
415 rc = OPENPTS_FSM_SUCCESS;
418 /* if value = base64 -> BHV model => value -> BIN model */
419 if (!strcmp(value, "base64")) {
420 // DEBUG("Update BIN-FSM %s=%s\n", name, prop->value);
421 snprintf(action, BUF_SIZE, "validateProperty( %s, %s )", name, prop->value);
422 rc = OPENPTS_FSM_SUCCESS;
423 } else if (!strcmp(value, "digest")) {
424 // DEBUG("Update BIN-FSM %s=%s\n", name, prop->value);
425 snprintf(action, BUF_SIZE, "validateProperty( %s, %s )", name, prop->value);
426 rc = OPENPTS_FSM_SUCCESS;
429 // INFO("validateProperty() %s != %s, but %s. There is an inconsistency between IR and RM\n",
430 // name, value, prop->value);
431 rc = OPENPTS_FSM_ERROR;
445 void printProperties(OPENPTS_CONTEXT *ctx) {
446 OPENPTS_PROPERTY *prop;
448 prop = ctx->prop_start;
452 LOG(LOG_ERR, "null input");
457 OUTPUT(NLS(MS_OPENPTS, OPENPTS_PRINT_PROPS, "Properties name-value\n"));
458 while (prop != NULL) {
459 OUTPUT("%5d %s=%s\n", i, prop->name, prop->value);
466 * save to File (plain text, Java Properties)
468 int saveProperties(OPENPTS_CONTEXT *ctx, char * filename) {
470 OPENPTS_PROPERTY *prop;
475 LOG(LOG_ERR, "null input");
478 if (filename == NULL) {
479 LOG(LOG_ERR, "null input");
484 if ((fp = fopen(filename, "w")) == NULL) {
485 LOG(LOG_ERR, "File %s open was failed\n", filename);
486 return PTS_INTERNAL_ERROR;
489 /* get properties chain*/
490 prop = ctx->prop_start;
492 LOG(LOG_ERR, "properties is NULL\n");
494 return PTS_INTERNAL_ERROR;
497 fprintf(fp, "# OpenPTS properties, name=value\n");
498 while (prop != NULL) {
499 fprintf(fp, "%s=%s\n", prop->name, prop->value); // TODO uninitialised byte(s)
503 fprintf(fp, "# %d props\n", i);
509 int addPropertiesFromConfig(OPENPTS_CONFIG *conf, OPENPTS_CONTEXT *ctx) {
512 LOG(LOG_ERR, "null input");
516 LOG(LOG_ERR, "null input");
520 /* additional properties from the pts config file */
521 if (conf->iml_maxcount > 0) {
523 snprintf(buf, sizeof(buf), "%d", conf->iml_maxcount);
524 addProperty(ctx, "iml.ipl.maxcount", buf);
525 DEBUG("Added automatic property iml.ipl.maxcount=%d\n", conf->iml_maxcount);