OSDN Git Service

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