OSDN Git Service

bd3923acd25671d8456f4bcd0b877c2225f964d8
[openpts/openpts.git] / include / openpts.h
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 include/openpts.h
26  * \brief 
27  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
28  * @date 2010-06-17
29  * cleanup 2011-01-21 SM
30  *
31  */
32
33 #ifndef INCLUDE_OPENPTS_H_
34 #define INCLUDE_OPENPTS_H_
35
36 #include <stdio.h>
37 #include <unistd.h>
38
39 #include <syslog.h>
40
41 /* Hash table (AIDE) */
42 #define __USE_GNU  // set for reentrant functions
43 #include <search.h>
44
45 #ifdef CONFIG_SQLITE
46 #include <sqlite3.h>
47 #endif
48
49 // TODO do not need for build of verifier only
50 #include <tss.h>
51 #include <trousers.h>
52
53 /* TCG IWG IF-PTS definitions */
54 #include <iwgifpts.h>
55
56 #include <openpts_log.h>
57
58 #include <openpts_ifm.h>
59 #include <openpts_fsm.h>
60 #include <openpts_tpm.h>
61
62 #ifdef CONFIG_TBOOT
63 #include <openpts_tboot.h>
64 #endif
65
66
67 /* OpenPTS default configurations  */
68 // port NUM
69 //   http://www.iana.org/assignments/port-numbers
70 //   http://www.iana.org/cgi-bin/usr-port-number.pl
71 //   User ports [1024:49151]
72 //     6674-6686  Unassigned
73 // TODO 5556 is comfrict with Freeciv, => 6678
74 // note) The port is local. for the remote access, we use SSH tunnel (port 22)
75 #define PTSC_CONFIG_FILE  "/etc/ptsc.conf"
76 #define PTSV_CONFIG_FILE  "/etc/ptsv.conf"
77
78 #define PTSC_GROUP_NAME    "ptsc"
79
80 #define MAXDATA 1024
81
82 #define MAX_SSLEVEL  2   // platform, runtime
83
84
85 // TODO(munetoh) Adaptive
86 // 256 => SMBIOS can't fill
87 #define BUF_SIZE 4096
88
89 // TODO malloc this,  MAX 100K?
90 // #define EVENTDATA_BUF_SIZE 1024
91 // PC BIOS
92 // #define EVENTDATA_BUF_SIZE 4096
93 // UNIX - TODO malloc the buffer
94 #define EVENTDATA_BUF_SIZE 100000
95
96 // 20100627 pseudo event as IMA's last event
97 #define OPENPTS_PSEUDO_EVENT_TYPE 0xFFFFFFFF
98 #define OPENPTS_PSEUDO_EVENT_PCR  0x5a
99
100 /* XML */
101 #define XML_ENCODING "UTF-8"
102
103 /* TCG RIMM schemas */
104 #define XMLNS_CORE "http://www.trustedcomputinggroup.org/XML/SCHEMA/1_0_1/core_integrity#"
105 #define XMLNS_STUFF "http://www.trustedcomputinggroup.org/XML/SCHEMA/1_0/simple_object#"
106 #define XMLNS_XSI "http://www.w3.org/2001/XMLSchema-instance"
107 #define XMLNS_RIMM "http://www.trustedcomputinggroup.org/XML/SCHEMA/1_0/rimm#"
108 #define XMLNS_IR "http://www.trustedcomputinggroup.org/XML/SCHEMA/1_0/integrity_report#"
109
110 /* OpenPTS Result Codes */
111 // 0 - 62 defined by IF-PTS
112 // validation
113 #define OPENPTS_RESULT_VALID         0
114 #define OPENPTS_RESULT_UNVERIFIED  101
115 #define OPENPTS_RESULT_INVALID     102
116 #define OPENPTS_RESULT_UNKNOWN     103
117 #define OPENPTS_RESULT_IGNORE      104
118 // FSM functions
119 #define OPENPTS_FSM_SUCCESS          0
120 #define OPENPTS_FSM_FLASH          201
121 #define OPENPTS_FSM_FINISH         202
122 #define OPENPTS_FSM_TRANSIT        203
123 #define OPENPTS_FSM_FINISH_WO_HIT  204
124 #define OPENPTS_FSM_ERROR          205
125 #define OPENPTS_FSM_ERROR_LOOP     206
126 #define OPENPTS_FSM_MIGRATE_EVENT  207
127 // cui - collector
128 #define OPENPTS_SELFTEST_SUCCESS     0
129 #define OPENPTS_SELFTEST_RENEWED   301
130 #define OPENPTS_SELFTEST_FALLBACK  302
131 #define OPENPTS_SELFTEST_FAILED    303
132 #define OPENPTS_FILE_EXISTS        311
133 #define OPENPTS_FILE_MISSING       312
134 #define OPENPTS_DIR_EXISTS         313
135 #define OPENPTS_DIR_MISSING        314
136 #define OPENPTS_IML_MISSING        315
137
138 // IMV
139 #define IMV_ENROLLMENT_NONE          0
140 #define IMV_ENROLLMENT_CREDENTIAL    1
141 #define IMV_ENROLLMENT_AUTO          2
142
143 // malloc should never really fail
144 // #define ALWAYS_ASSERT_ON_BAD_ALLOC
145 // Undefined this for daemons
146 // #define NEVER_FREE_MEMORY
147
148 #define isFlagSet(bits, flagToTest) (flagToTest == ((bits) & (flagToTest)))
149
150 /* structures */
151
152 /**
153  * Security Properties
154  */
155 typedef struct {
156     int num;     /**< */
157     char *name;  /**< name */
158     char *value; /**< value */
159     void *next;  /**< ptr to the next property */
160 } OPENPTS_PROPERTY;
161
162 /**
163  * Security Policy
164  */
165 typedef struct {
166     int num;              /**< */
167     // TODO malloc this
168     char name[BUF_SIZE];  /**< name */
169     char value[BUF_SIZE]; /**< value */
170     int line;             /**< line # */
171     void * next;          /**< tr to the next policy */
172 } OPENPTS_POLICY;
173
174 /**
175  * Actions (UML doActivity)
176  */
177 typedef struct {
178     char name[BUF_SIZE];  /**< */
179 } OPENPTS_ACTION;
180
181 #define ACTION_TYPE_PROPERTY
182
183
184 /**
185  * Snapshot (snapshot.c)
186  */
187 typedef struct {
188     int event_num; /**< num of event */
189     int pcrIndex;  /**< */
190     int level;     /**< e.g. 0:BIOS, 1:VMM/OS, 2:App/Userland */
191
192     int update_num; /**< num of update */
193     void *update; /**< link to the last update */
194
195     int reset_pcr;  /**< resetPCR(n) in FSM */
196
197     /* events */
198     OPENPTS_PCR_EVENT_WRAPPER  *start; /**< */
199     OPENPTS_PCR_EVENT_WRAPPER  *end;   /**< */
200
201     /* PCR values -  calc duering IR generation  */
202     BYTE tpm_pcr[MAX_DIGEST_SIZE];   /**< PCR values -  calc when get the IML */
203     BYTE start_pcr[MAX_DIGEST_SIZE]; /**< PCR start value of this IML */
204     BYTE curr_pcr[MAX_DIGEST_SIZE];  /**< PCR  of this IML */
205
206     /* FSM */
207     OPENPTS_FSM_CONTEXT    *fsm_behavior; /**< Behavior Model */
208     OPENPTS_FSM_CONTEXT    *fsm_binary;   /**< Binary Model (= RM)*/
209 } OPENPTS_SNAPSHOT;
210
211 /**
212  * Snapshot Table (snapshot.c)
213  */
214 typedef struct {
215     OPENPTS_SNAPSHOT *snapshot[MAX_PCRNUM][MAX_SSLEVEL];  /**< ptr to the snapshot */
216     int event_num; /**< Total event num */
217     int snapshots_level[MAX_PCRNUM]; /**< indicate active level */
218     int error[MAX_PCRNUM];
219     int update_num[MAX_SSLEVEL]; /**< remenber the update by ss level */
220 } OPENPTS_SNAPSHOT_TABLE;
221
222 /**
223  * Reference Manifest
224  */
225 #define RM_SAX_BUF_SIZE 256
226
227 typedef struct {
228     /* for SAX parser */
229     int  sax_state;
230     int  sax_error;
231
232     /* FSM */
233     int  pcr_index;
234     int level;
235     OPENPTS_SNAPSHOT     *snapshot;
236     OPENPTS_FSM_CONTEXT  *fsm;
237     char subvertex_name[RM_SAX_BUF_SIZE];
238     char subvertex_xmitype[RM_SAX_BUF_SIZE];
239     char subvertex_xmiid[RM_SAX_BUF_SIZE];
240     char doactivity_name[RM_SAX_BUF_SIZE];
241     char charbuf[RM_SAX_BUF_SIZE];
242     char source_xmiid[RM_SAX_BUF_SIZE];
243     char target_xmiid[RM_SAX_BUF_SIZE];
244 } OPENPTS_RM_CONTEXT;
245
246
247 /**
248  * Integrity Report (ir.c)
249  */
250 #define VALID   0
251 #define INVALID 1
252 #define UNKNWON 2
253
254 /* Structure for SAX parser */
255 typedef struct {
256     /* for SAX parser */
257     int  sax_state;
258     int  sax_error;
259     int  char_size;
260     char *buf;  /**< buffer for the text element */
261     int  bad_quote;
262     /* IML -> FSM */
263     int  event_index;
264     int  pcr_index;
265     BYTE pcr[MAX_DIGEST_SIZE];
266     TSS_PCR_EVENT *event;
267     OPENPTS_PCR_EVENT_WRAPPER *ew_new;
268     OPENPTS_PCR_EVENT_WRAPPER *ew_last;
269     /* FSM transition */
270     int fsm_error_count;
271     int integrity;  /**< VALID, INVALID */
272 } OPENPTS_IR_CONTEXT;
273
274 /* Element tag */
275 #define IR_SAX_STATE_IDOL       0
276 #define IR_SAX_STATE_PCR_INDEX  1
277 #define IR_SAX_STATE_EVENT_TYPE 2
278 #define IR_SAX_STATE_DIGEST     3
279 #define IR_SAX_STATE_EVENT_DATA 4
280 #define IR_SAX_STATE_PCR        5
281
282 /**
283  * FSM (uml.c)
284  */
285
286 /* Context */
287 #define UML2SAX_SUBVERTEX  10
288 #define UML2SAX_DOACTIVITY 15
289 #define UML2SAX_TRANSITION 20
290 #define UML2SAX_BODY       25
291
292
293 #ifdef CONFIG_AIDE
294 /**
295  * AIDE metadata (= IMA event)
296  */
297 typedef struct {
298     /* AIDE */
299     char *name;          /**< file name (full path) */
300     char *lname;
301     int   attr;
302     BYTE  *sha1;
303     BYTE  *sha256;
304     BYTE  *sha512;
305     char *hash_key;  /**< base64 of selected digest */
306     /* PTS */
307     int status;          /**< 0:AIDE 1:AIDE==PTS, 2: AIDE!=PTS, 2:PTS */
308     char * ima_name;     /**< name of IMA's eventlog (short) */
309     void * event_wrapper; /**< link to the eventlog */
310     /* link ptr */
311     void *prev;
312     void *next;
313 } AIDE_METADATA;
314
315 /**
316  * list for ignore name, ext
317  */ 
318 typedef struct {
319     char *name;
320     void *next;
321 } AIDE_LIST;
322
323 /**
324  * AIDE context
325  */
326 typedef struct {
327     AIDE_METADATA *start;
328     AIDE_METADATA *end;
329     int metadata_num;
330
331 #ifdef CONFIG_SQLITE
332     /* SQLite */
333     sqlite3 *sqlite_db;
334 #endif
335
336     /* Hash table */
337     struct hsearch_data *aide_md_table;  // hash table for metadata
338     int aide_md_table_size;
339
340     /* ignore list for 2.6.31-3X IMA, defectiveness name */
341     AIDE_LIST *ignore_name_start;
342     AIDE_LIST *ignore_name_end;
343
344     /* Hash Table*/
345     struct hsearch_data *aide_in_table;  // hash table for ignore name
346     int aide_in_table_size;
347 } AIDE_CONTEXT;
348
349 #define OPENPTS_AIDE_MD_STATUS_NEW          0
350 #define OPENPTS_AIDE_MD_STATUS_HIT          1
351 #define OPENPTS_AIDE_MD_STATUS_IML_VALID    2
352 #define OPENPTS_AIDE_MD_STATUS_IML_INVALID  3
353 #endif  // CONFIG_AIDE
354
355 /* Validation modes */
356
357 #define OPENPTS_VALIDATION_MODE_NONE  0
358 #define OPENPTS_VALIDATION_MODE_RM    1
359
360 #define OPENPTS_VALIDATION_MODE_AIDE  2
361 #define OPENPTS_VALIDATION_MODE_IIDB  3
362 #define OPENPTS_VALIDATION_MODE_AIXTE 4
363
364 #define OPENPTS_SSH_MODE_OFF 0
365 #define OPENPTS_SSH_MODE_ON  1
366
367 #define OPENPTS_RM_STATE_UNKNOWN   0
368 #define OPENPTS_RM_STATE_NOW       1
369 #define OPENPTS_RM_STATE_OLD       2
370 #define OPENPTS_RM_STATE_NEW       3
371 #define OPENPTS_RM_STATE_TRASH     4
372
373 /**
374  * RM set
375  */
376 typedef struct {
377     PTS_UUID     *uuid;
378     char         *str_uuid;
379     PTS_DateTime *time;
380     int          state; /**< OPENPTS_RM_STATE_XXX  */
381     char         *dir;
382 } OPENPTS_RMSET;
383
384 typedef struct {
385     int           rmset_num;
386     int           current_id;
387     int           update_id;
388     OPENPTS_RMSET rmset[];
389 } OPENPTS_RMSETS;
390
391 /**
392  * collector/target set
393  */
394 typedef struct {
395     /* UUID */
396     PTS_UUID     *uuid;
397     char         *str_uuid;
398     PTS_DateTime *time;
399     /* location */
400     char         *dir;
401     char         *target_conf_filename;
402     void         *target_conf;
403     /* TBD */
404     int           state; /**<   */
405 } OPENPTS_TARGET;
406
407 typedef struct {
408     int           target_num;
409     OPENPTS_TARGET target[];
410 } OPENPTS_TARGET_LIST;
411
412 /* UUID status */
413 #define OPENPTS_UUID_EMPTY         0
414 #define OPENPTS_UUID_FILENAME_ONLY 1
415 #define OPENPTS_UUID_UUID_ONLY     2
416 #define OPENPTS_UUID_FILLED        3
417 #define OPENPTS_UUID_CHANGED       4
418
419 typedef struct {
420     char         *filename;
421     PTS_UUID     *uuid;
422     char         *str;
423     PTS_DateTime *time;
424     int status;
425 } OPENPTS_UUID;
426
427 //<<<<<<< HEAD
428 /* information about the components described by the models */
429 typedef struct {
430     char *SimpleName;
431     char *ModelName;
432     char *ModelNumber;
433     char *ModelSerialNumber;
434     char *ModelSystemClass;
435     char *VersionMajor;
436     char *VersionMinor;
437     char *VersionBuild;
438     char *VersionString;
439     char *MfgDate;
440     char *PatchLevel;
441     char *DiscretePatches;
442     char *VendorID_Name;
443     enum {
444         VENDORID_TYPE_TCG,
445         VENDORID_TYPE_SMI,
446         VENDORID_TYPE_GUID,
447     } VendorID_type;
448     char *VendorID_Value;
449 } OPENPTS_COMPID;
450 //=======
451 /* Attestation(sign) key */
452 #define OPENPTS_AIK_STORAGE_TYPE_TSS  0
453 #define OPENPTS_AIK_STORAGE_TYPE_BLOB 1
454 #define OPENPTS_AIK_AUTH_TYPE_NULL    0
455 #define OPENPTS_AIK_AUTH_TYPE_COMMON  1
456 //>>>>>>> 042e40b0979f3e44e75200271e4d1282ce08f72c
457
458 /**
459  * Config
460  */
461 #define MAX_RM_NUM 3
462 typedef struct {
463     /* misc */
464     char *config_file;
465     char *config_dir;
466     int openpts_pcr_index;  /**< openpts.pcr.index */
467
468     BYTE pts_flag[4];
469     TPM_VERSION tpm_version;
470     TSS_VERSION tss_version;
471     TSS_VERSION pts_version;
472
473     /* Attestation(sign) key */
474     int   aik_storage_type;
475     char *aik_storage_filename;
476     int   aik_auth_type;
477
478     /* UUID */
479     OPENPTS_UUID * uuid;         /**< Platform(collector) UUID */
480     OPENPTS_UUID * rm_uuid;      /**< RM(now) UUID */
481     OPENPTS_UUID * newrm_uuid;   /**< RM(next) UUID */
482     OPENPTS_UUID * oldrm_uuid;   /**< RM(old/previous) UUID */
483     OPENPTS_UUID * tmp_uuid;     /**< Platform(collector) UUID - changed */
484     OPENPTS_UUID * tmp_rm_uuid;  /**< RM(now) UUID - changed */
485
486     /* Daemon UUID */
487     PTS_UUID     *daemon_uuid;
488     char         *str_daemon_uuid;
489     PTS_DateTime *time_daemon_uuid;
490
491     /* collector settings */
492     int iml_mode;                /**< 0: via tss, 1:securityfs */
493     char *bios_iml_filename;
494     char *runtime_iml_filename;
495     int  runtime_iml_type;
496     char *pcrs_filename;
497
498     int selftest;    /**< 1:run selftest at start */
499     int autoupdate;  /**< 1:run autoupdate if selftest was failed at start */
500
501     int srk_password_mode;
502     int tpm_resetdalock; /**< tpm.resetdalock=on|off=1|0 */
503     int tpm_quote_type;  /**< tpm.quote.type=quote|quote2=1:0 */
504
505     /* multiple manifest */
506     OPENPTS_RMSETS *rmsets;
507
508     /* manifest */
509     char *rm_basedir;
510     int   rm_num;
511     char *rm_filename[MAX_RM_NUM];
512
513     int   newrm_num;
514     char *newrm_filename[MAX_RM_NUM];
515
516     char *ir_dir;          /**< collector side */
517     char *ir_filename;     /**< vefirier side */
518
519     char *prop_filename;
520
521     int iml_endian;                /**< 0: same, 2:conv */
522     int iml_aligned;               /**< 0: byte, 4: 4-byte aligned */
523
524     /* FSM models */
525     char *model_dir; /**< */
526     char *model_filename[MAX_RM_NUM][MAX_PCRNUM];
527     int iml_maxcount;
528
529     /* Component ID */
530     OPENPTS_COMPID compIDs[MAX_RM_NUM];
531
532     /* verifier setting */
533     char *verifier_logging_dir;
534     char *policy_filename;
535     char *property_filename;
536     PTS_UUID *target_uuid;
537     char *str_target_uuid;
538     BYTE *pubkey;           /**< TPM PUBKEY */
539     int pubkey_length;      /**< TPM PUBKEY length */
540
541     /* target list */
542     OPENPTS_TARGET_LIST *target_list;
543
544     /* IMA and AIDE */
545     int ima_validation_mode;     /**< 0:NA 2:AIDE 3:IIDB */
546     int ima_validation_unknown;  /**< 0:ignore 1:invalid  */
547     char *aide_database_filename;
548 #ifdef CONFIG_SQLITE
549     char *aide_sqlite_filename;  /**> SQLite DB filename */
550 #endif
551     char *aide_ignorelist_filename;
552
553     /* BIOS */
554     int smbios_length;
555     BYTE *smbios;  // link to event
556     char *bios_vendor;
557     char *bios_version;
558
559     /* IF-M collector(ptsc) */
560     char *hostname;
561     char *ssh_username;
562     char *ssh_port;
563
564     /* IF-M verifier(IMV) */
565     int enrollment;
566
567 #ifdef CONFIG_AUTO_RM_UPDATE
568     int enable_aru;              /**> Enable update scan */
569     int update_exist;            /**> Update exist, used by collector */
570     int target_newrm_exist;      /**> NewRM exist, used by verifier */
571     PTS_UUID *target_newrm_uuid; /**> NewRM UUID */
572     void *update;                /**> Hold update*/
573     BYTE *newRmSet;
574 #endif
575
576     /* misc */
577     int ir_without_quote; /**< 1:IR without quote */
578 } OPENPTS_CONFIG;
579
580
581 /**
582  * OpenPTS reason(remidiation)
583  */
584 typedef struct {
585     int num;       /**< */
586     int pcr;
587     char *message; /**< */
588     void * next;   /**< */
589 } OPENPTS_REASON;
590
591
592
593 /**
594  * OPENPTS_CONTEXT - OpenPTS context
595  * by each IF-M connection
596  */
597 typedef struct {
598     /* Config */
599     OPENPTS_CONFIG *conf; /**< OpenPTS Configulation (global) */
600     OPENPTS_CONFIG *target_conf;
601
602     /* Target Confg */
603     char *target_conf_filename;
604
605     /* Platform Validation */
606     int platform_validation_mode;  // TODO(munetoh) -> conf?
607
608     /* TPM emu */
609     OPENPTS_TPM_CONTEXT tpm; /**< */
610     int drtm;
611
612     /* PCRs */
613     int pcr_num;  // TODO(munetoh) move to pcrs->pcr_num
614     OPENPTS_PCRS *pcrs;
615
616     /* Quote */
617     // TODO(munetoh) move to OPENPTS_QUOTE?
618     TSS_VALIDATION *validation_data;
619
620     /* IML */
621     OPENPTS_SNAPSHOT_TABLE *ss_table;
622     int update_num;               /**< total num of update */
623
624     /* Properties */
625     OPENPTS_PROPERTY *prop_start; /**< */  // prop.c
626     OPENPTS_PROPERTY *prop_end;
627     int prop_count;
628
629     /* Policy */
630     OPENPTS_POLICY *policy_start; /**< */  // policy.c
631     OPENPTS_POLICY *policy_end;
632
633     /* Reason */
634     OPENPTS_REASON *reason_start;
635     OPENPTS_REASON *reason_end;
636     int reason_count;
637
638
639     /* Reference Manifest */
640     OPENPTS_RM_CONTEXT *rm_ctx;
641
642     /* Integrity Report */
643     OPENPTS_IR_CONTEXT *ir_ctx;
644     char *ir_filename;
645
646
647
648     /* Runtime Validation */
649     int bios_action_count;  // by snapshot
650 #ifdef CONFIG_AIDE
651     void *aide_ctx;  // AIDE_CONTEXT
652 #endif
653     int  ima_count;
654     int  ima_valid;
655     int  ima_invalid;
656     int  ima_unknown;
657     int  ima_ignore;  // they are included in the valid count
658
659     /* Component ID */
660     OPENPTS_COMPID compIDs[MAX_RM_NUM];
661
662     /* IF-M */
663     BYTE *read_msg;
664     OPENPTS_NONCE *nonce;
665     PTS_UUID *uuid; /**< uuid of otherside, own uuid is ctx->conf->uuid */
666     char *str_uuid;
667     UINT32 ifm_errno;  /**<  PTS error code */
668     char * ifm_strerror;
669
670     OPENPTS_UUID *collector_uuid;
671     OPENPTS_UUID *rm_uuid;
672
673     /* TNC */
674     int tnc_state;
675
676     /* misc ? */
677     int cid; /**< */
678     int count;  // TODO used by FSM, location is temp
679 } OPENPTS_CONTEXT;
680
681
682
683 /* functions */
684
685 /* conf.c */
686 OPENPTS_CONFIG * newPtsConfig();
687 int freePtsConfig(OPENPTS_CONFIG *conf);
688 int readPtsConfig(OPENPTS_CONFIG *conf, char *filename);
689 int writeTargetConf(OPENPTS_CONFIG *conf, PTS_UUID *uuid, char *filename);
690 int readTargetConf(OPENPTS_CONFIG *conf, char *filename);
691 int writeOpenptsConf(OPENPTS_CONFIG *conf, char *filename);
692 int readOpenptsConf(OPENPTS_CONFIG *conf, char *filename);
693 int setModelFile(OPENPTS_CONFIG *conf, int pcr_index, int level, char *filename);
694 OPENPTS_TARGET_LIST *newTargetList(int num);
695 void freeTargetList(OPENPTS_TARGET_LIST *list);
696
697
698 /* ctx.c */
699 OPENPTS_CONTEXT  * newPtsContext(OPENPTS_CONFIG *conf);
700 int freePtsContext(OPENPTS_CONTEXT *ctx);
701 char * getAlgString(int type);
702 int readFsmFromPropFile(OPENPTS_CONTEXT *ctx, char * filename);  // fsm.c -> ctx.c
703
704 #define ALGTYPE_SHA1 0
705 #define ALGTYPE_MD5  1
706
707 /* ifm.c */
708 int writePtsTlv(OPENPTS_CONTEXT *ctx, int fd, int type);
709 // int setTargetCapability(OPENPTS_CONTEXT *ctx, OPENPTS_IF_M_Capability *cap);
710
711
712 /* collector.c */
713 int collector(
714     OPENPTS_CONFIG *conf,
715     int forground, int debug, const char* dirname);
716
717 /* verifier.c */
718 int verifier(
719     OPENPTS_CONTEXT *ctx,
720     char *host, char *ssh_username, char *ssh_port, char *conf_dir, int mode);
721 int enroll(
722     OPENPTS_CONTEXT *ctx,
723     char *host, char *ssh_username, char *ssh_port, char *conf_dir, int force);
724 int writeAideIgnoreList(OPENPTS_CONTEXT *ctx, char *filename);
725 int updateRm(
726     OPENPTS_CONTEXT *ctx,
727     char *host, char *ssh_username, char *ssh_port, char *conf_dir);
728 int extendEvCollectorStart(OPENPTS_CONFIG *conf);
729 /* verifier mode */
730 #define OPENPTS_VERIFY_MODE 0
731 #define OPENPTS_UPDATE_MODE 1
732
733
734
735
736
737 /* snapshot.c */
738 OPENPTS_SNAPSHOT * newSnapshot();
739 int freeSnapshot(OPENPTS_SNAPSHOT * ss);
740 OPENPTS_SNAPSHOT_TABLE * newSnapshotTable();
741 int freeSnapshotTable(OPENPTS_SNAPSHOT_TABLE * sst);
742 int addSnapshotToTable(OPENPTS_SNAPSHOT_TABLE * sst, OPENPTS_SNAPSHOT * ss, int pcr_index, int level);
743 OPENPTS_SNAPSHOT *getSnapshotFromTable(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index, int level);
744 OPENPTS_SNAPSHOT *getNewSnapshotFromTable(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index, int level);
745 OPENPTS_SNAPSHOT *getActiveSnapshotFromTable(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index);
746 int setActiveSnapshotLevel(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index, int level);
747 int getActiveSnapshotLevel(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index);
748 int incActiveSnapshotLevel(OPENPTS_SNAPSHOT_TABLE * sst, int pcr_index);
749
750 /* iml.c */
751 // TODO(munetoh) assign IMA type to TCG EventType :-(
752 #define BINARY_IML_TYPE_BIOS          0x00000000
753 #define BINARY_IML_TYPE_IMA_ORIGINAL  0x00010000
754 #define BINARY_IML_TYPE_IMA_31        0x00011000  // 2.6.30?, 31, 32
755 #define BINARY_IML_TYPE_IMA           0x00012000
756 #define BINARY_IML_TYPE_IMA_NG        0x00013000
757 #define BINARY_IML_TYPE_IMA_NGLONG    0x00014000
758
759 /* mode of getBiosImlFile(), getImaImlFile() */
760 #define USE_BHV_FSM    0
761 #define USE_BIN_FSM    1
762 #define USE_BHV_FSM_EC 2
763
764 // extern SNAPSHOT snapshots[MAX_PCRNUM];
765 OPENPTS_SNAPSHOT * newSnapshot();
766 int freeSnapshot(OPENPTS_SNAPSHOT * ss);
767 int resetSnapshot(OPENPTS_SNAPSHOT *snapshots);
768 int addEventToSnapshotBhv(
769     OPENPTS_CONTEXT * ctx, OPENPTS_PCR_EVENT_WRAPPER * eventWrapper);
770 int addEventToSnapshotBin(
771     OPENPTS_CONTEXT * ctx, OPENPTS_PCR_EVENT_WRAPPER * eventWrapper);
772 int getIml(OPENPTS_CONTEXT *ctx, int option);
773 int readBiosImlFile(OPENPTS_CONTEXT *ctx, const char *filename, int mode);
774 int readImaImlFile(
775     OPENPTS_CONTEXT * ctx,
776     const char *filename,
777     int type,
778     int mode, int *count);  // 20100613
779 int setPcrsToSnapshot(OPENPTS_CONTEXT *ctx, OPENPTS_PCRS *pcrs);
780 int getPcrBySysfsFile(OPENPTS_CONTEXT *ctx, const char *filename);
781 int validatePcr(OPENPTS_CONTEXT * ctx);
782 int getPcr(OPENPTS_CONTEXT * ctx);
783 int flashSnapshot(
784         OPENPTS_CONTEXT * ctx,
785         int index);
786 void printSnapshots(OPENPTS_CONTEXT *ctx);
787 void printSnapshotsInfo(OPENPTS_CONTEXT *ctx);
788 void freeEventWrapperChain(OPENPTS_PCR_EVENT_WRAPPER * ew);
789 int printImlByPcr(
790         OPENPTS_CONTEXT * ctx,
791         UINT32 index,
792         UINT32 offset);
793 int printIml(OPENPTS_CONTEXT * ctx);
794 UINT32 freadUint32(FILE * stream, int endian);
795 OPENPTS_PCR_EVENT_WRAPPER * newEventWrapper();
796
797 /* base64.c */
798 char *encodeBase64(unsigned char * in, int inlen, int *outlen);
799 unsigned char *decodeBase64(char * in, int inlen, int *outlen);
800
801 /* fsm.c */
802 // TODO refectoring
803 int updateFsm(
804     OPENPTS_CONTEXT *ctx,
805     OPENPTS_FSM_CONTEXT *fsm,
806     OPENPTS_PCR_EVENT_WRAPPER *eventWrapper);  // aru,iml
807
808
809
810 /* rm.c */
811 OPENPTS_RM_CONTEXT *newRmContext();
812 void freeRmContext(OPENPTS_RM_CONTEXT *ctx);
813 int writeRm(OPENPTS_CONTEXT * ctx, const char *file, int level);
814 int readRmFile(OPENPTS_CONTEXT *ctx, const char *filename, int level);
815 int getRmSetDir(OPENPTS_CONFIG *conf);
816 int getNewRmSetDir(OPENPTS_CONFIG *conf);
817 int makeRmSetDir(OPENPTS_CONFIG *conf);
818 int makeNewRmSetDir(OPENPTS_CONFIG *conf);
819
820 /* ir.c */
821 OPENPTS_IR_CONTEXT *newIrContext();
822 void freeIrContext(OPENPTS_IR_CONTEXT *ctx);
823 int writeIr(OPENPTS_CONTEXT *ctx, const char *filename, int *savedFd);
824 // int validateIr(OPENPTS_CONTEXT *ctx, const char *file);
825 int validateIr(OPENPTS_CONTEXT *ctx);
826 int genIr(OPENPTS_CONTEXT *ctx, int *savedFd);
827
828 /* action.c */
829 int doActivity(
830     OPENPTS_CONTEXT *ctx,
831     char *action,
832     OPENPTS_PCR_EVENT_WRAPPER *eventWrapper);
833 char * trim(char *str);
834 int setLinuxKernelCmdlineAssertion(
835     OPENPTS_CONTEXT *ctx, OPENPTS_PCR_EVENT_WRAPPER *eventWrapper);
836
837
838
839 /* prop.c */
840 OPENPTS_PROPERTY * newProperty(char *name, char *value);
841 int freePropertyChain(OPENPTS_PROPERTY *prop);
842 int freeReasonChain(OPENPTS_REASON *reason);
843 OPENPTS_PROPERTY* getProperty(OPENPTS_CONTEXT *ctx, char *name);
844 int addProperty(OPENPTS_CONTEXT *ctx, char *name, char *value);
845 int updateProperty(OPENPTS_CONTEXT *ctx, char *name, char *value);
846 int setProperty(
847     OPENPTS_CONTEXT *ctx,
848     char *name,
849     char *value);
850 int setEventProperty(
851     OPENPTS_CONTEXT *ctx,
852     char *name,
853     char *value,
854     OPENPTS_PCR_EVENT_WRAPPER *eventWrapper);
855 int saveProperties(OPENPTS_CONTEXT *ctx, char * filename);
856 void printProperties(OPENPTS_CONTEXT *ctx);
857 int validateProperty(
858     OPENPTS_CONTEXT *ctx, char *name, char *value, char *action);
859 int addPropertiesFromConfig(OPENPTS_CONFIG *conf, OPENPTS_CONTEXT *ctx);
860
861 /* reason.c */
862 int addReason(OPENPTS_CONTEXT *ctx, int pcr, const char *format, ...);
863 void printReason(OPENPTS_CONTEXT *ctx, int print_pcr_hints);
864
865
866 /* log.c */
867 int openLogging();
868 int closeLogging();
869
870 /* policy.c */
871 int freePolicyChain(OPENPTS_POLICY *pol);
872 int loadPolicyFile(OPENPTS_CONTEXT *ctx, char * filename);
873 int checkPolicy(OPENPTS_CONTEXT *ctx);
874 int printPolicy(OPENPTS_CONTEXT *ctx);
875
876 #ifdef CONFIG_AIDE
877 /* aide.c */
878 AIDE_METADATA * newAideMetadata();
879 void freeAideMetadata(AIDE_METADATA *md);
880 AIDE_CONTEXT * newAideContext();
881 void freeAideContext(AIDE_CONTEXT *ctx);
882 int loadAideDatabaseFile(AIDE_CONTEXT *ctx, char *filename);
883 int readAideIgnoreNameFile(AIDE_CONTEXT *ctx, char *filename);
884 int checkFileByAide(AIDE_CONTEXT *ctx, AIDE_METADATA *metadata);
885 int checkEventByAide(
886     AIDE_CONTEXT *ctx, OPENPTS_PCR_EVENT_WRAPPER *eventWrapper);
887 int printAideData(AIDE_CONTEXT *ctx);
888 int convertImlToAideDbFile(OPENPTS_CONTEXT *ctx, char *filename);
889 int writeReducedAidbDatabase(AIDE_CONTEXT *ctx, char *filename);
890 #ifdef CONFIG_SQLITE
891 int convertAideDbfileToSQLiteDbFile(char * aide_filename, char * sqlite_filename);
892 int loadSQLiteDatabaseFile(AIDE_CONTEXT *ctx, char *filename);
893 int verifyBySQLite(AIDE_CONTEXT *ctx, char * key);
894 #endif  // CONFIG_SQLITE
895 #endif  // CONFIG_AIDE
896
897
898
899 /* smbios.c */
900 int readSmbiosFile(char * filename, BYTE **data, int *len);
901 int printSmbios(BYTE *data, int length);
902 int genSmbiosFileByDmidecode(char * filename);
903 int parseSmbios(OPENPTS_CONTEXT *ctx, BYTE *data, int length);
904
905 /* misc.c */
906 void *xmalloc_assert(size_t len);
907 char *smalloc_assert(char *str);
908 #ifdef ALWAYS_ASSERT_ON_BAD_ALLOC
909 #define xmalloc(len) xmalloc_assert(len)
910 #define smalloc(str) smalloc_assert(str)
911 #else
912 void *xmalloc(size_t len);
913 char *smalloc(char *str);
914 #endif
915 char *snmalloc(char *str, int len);
916 BYTE *snmalloc2(BYTE * buf, int offset, int len);
917 void xfree(void *ptr);
918 UINT32 byte2uint32(BYTE *b);
919 char * trim(char *str);
920 char *getHexString(BYTE *bin, int size);
921 void printHex(char *head, BYTE *data, int num, char *tail);
922 void fprintHex(FILE *fp, BYTE *data, int num);
923 UINT32 b2l(UINT32 in);
924 void debugHex(char *head, BYTE *data, int num, char *tail);
925
926 int saveToFile(char * filename, int len, BYTE * msg);
927 int getUint32(BYTE *buf);
928 int makeDir(char *dirname);
929 int checkDir(char *dirname);
930 int checkFile(char *filename);
931 ssize_t wrapRead(int fd, void *buf, size_t count);
932 ssize_t wrapWrite(int fd, const void *buf, size_t count);
933 char *getFullpathName(char *base_path, char *filename);
934 char *getFullpathDir(char *filename);
935 int unlinkDir(const char *dirPath);
936
937 /* uuid.c */
938 PTS_UUID *newUuid();
939 void freeUuid(PTS_UUID *uuid);
940 char * getStringOfUuid(PTS_UUID *uuid);
941 PTS_UUID *getUuidFromString(char *str);
942 PTS_DateTime * getDateTimeOfUuid(PTS_UUID *uuid);
943 PTS_DateTime * getDateTime();
944 int writeUuidFile(char *str_uuid, char *filename, int overwrite);
945 int readUuidFile(char *filename, char **str_uuid, PTS_UUID **uuid);
946 int getRmList(OPENPTS_CONFIG *conf, char * config_dir);
947 int purgeRenewedRm(OPENPTS_CONFIG *conf);
948 void printRmList(OPENPTS_CONFIG *conf, char *indent);
949 int getTargetList(OPENPTS_CONFIG *conf, char * config_dir);
950 void printTargetList(OPENPTS_CONFIG *conf, char *indent);
951 char *getTargetConfDir(OPENPTS_CONFIG *conf);
952 OPENPTS_TARGET *getTargetCollector(OPENPTS_CONFIG *conf);
953 OPENPTS_TARGET *getTargetCollectorByUUID(OPENPTS_CONFIG *conf, const char *uuid);
954 /* OPENPTS_UUID */
955 OPENPTS_UUID *newOpenptsUuid();
956 OPENPTS_UUID *newOpenptsUuid2(PTS_UUID *pts_uuid);
957 OPENPTS_UUID *newOpenptsUuidFromFile(char * filename);
958 void freeOpenptsUuid(OPENPTS_UUID *uuid);
959 int genOpenptsUuid(OPENPTS_UUID *uuid);
960 int readOpenptsUuidFile(OPENPTS_UUID *uuid);
961 int writeOpenptsUuidFile(OPENPTS_UUID *uuid, int overwrite);
962
963 /* collector.c */
964 int init(OPENPTS_CONFIG *conf, int prop_count, OPENPTS_PROPERTY *prop_start, OPENPTS_PROPERTY *prop_end);
965 int printCollectorStatus(OPENPTS_CONFIG *conf);
966 int selftest(OPENPTS_CONFIG *conf, int prop_count, OPENPTS_PROPERTY *prop_start, OPENPTS_PROPERTY *prop_end);
967 int newrm(OPENPTS_CONFIG *conf, int prop_count, OPENPTS_PROPERTY *prop_start, OPENPTS_PROPERTY *prop_end);
968 int clear(OPENPTS_CONFIG *conf, int force);
969
970 #ifdef CONFIG_AUTO_RM_UPDATE
971 #include "./openpts_aru.h"
972 #endif
973
974 /* ssh.c */
975 pid_t ssh_connect(char *host, char *ssh_username, char *ssh_port, char *key_file, int *socket);
976
977 #endif  // INCLUDE_OPENPTS_H_