d01c182d6d30e246a01b27118e8014b7e05f904c
[strongswan.git] / src / libpts / plugins / imv_attestation / attest_db.c
1 /*
2 * Copyright (C) 2011-2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "attest_db.h"
17
18 #include "libpts.h"
19 #include "pts/pts_meas_algo.h"
20 #include "pts/pts_file_meas.h"
21 #include "pts/components/pts_comp_func_name.h"
22
23 #include <libgen.h>
24 #include <time.h>
25
26 #define IMA_MAX_NAME_LEN 255
27
28 typedef struct private_attest_db_t private_attest_db_t;
29
30 /**
31 * Private data of an attest_db_t object.
32 */
33 struct private_attest_db_t {
34
35 /**
36 * Public members of attest_db_state_t
37 */
38 attest_db_t public;
39
40 /**
41 * Component Functional Name to be queried
42 */
43 pts_comp_func_name_t *cfn;
44
45 /**
46 * Primary key of the Component Functional Name to be queried
47 */
48 int cid;
49
50 /**
51 * TRUE if Component Functional Name has been set
52 */
53 bool comp_set;
54
55 /**
56 * Directory containing the Measurement file to be queried
57 */
58 char *dir;
59
60 /**
61 * Primary key of the directory to be queried
62 */
63 int did;
64
65 /**
66 * TRUE if directory has been set
67 */
68 bool dir_set;
69
70 /**
71 * Measurement file to be queried
72 */
73 char *file;
74
75 /**
76 * Primary key of measurement file to be queried
77 */
78 int fid;
79
80 /**
81 * TRUE if file has been set
82 */
83 bool file_set;
84
85 /**
86 * AIK to be queried
87 */
88 chunk_t key;
89
90 /**
91 * Primary key of the AIK to be queried
92 */
93 int kid;
94
95 /**
96 * TRUE if AIK has been set
97 */
98 bool key_set;
99
100 /**
101 * Software package to be queried
102 */
103 char *package;
104
105 /**
106 * Primary key of software package to be queried
107 */
108 int gid;
109
110 /**
111 * TRUE if package has been set
112 */
113 bool package_set;
114
115 /**
116 * Software product to be queried
117 */
118 char *product;
119
120 /**
121 * Primary key of software product to be queried
122 */
123 int pid;
124
125 /**
126 * TRUE if product has been set
127 */
128 bool product_set;
129
130 /**
131 * Software package version to be queried
132 */
133 char *version;
134
135 /**
136 * TRUE if version has been set
137 */
138 bool version_set;
139
140 /**
141 * TRUE if relative filenames are to be used
142 */
143 bool relative;
144
145 /**
146 * Package security state
147 */
148 os_package_state_t security;
149
150 /**
151 * Sequence number for ordering entries
152 */
153 int seq_no;
154
155 /**
156 * File measurement hash algorithm
157 */
158 pts_meas_algorithms_t algo;
159
160 /**
161 * Optional owner (user/host name)
162 */
163 char *owner;
164
165 /**
166 * Attestation database
167 */
168 database_t *db;
169
170 };
171
172 char* print_cfn(pts_comp_func_name_t *cfn)
173 {
174 static char buf[BUF_LEN];
175 char flags[8];
176 int type, vid, name, qualifier, n;
177 enum_name_t *names, *types;
178
179 vid = cfn->get_vendor_id(cfn),
180 name = cfn->get_name(cfn);
181 qualifier = cfn->get_qualifier(cfn);
182 n = snprintf(buf, BUF_LEN, "0x%06x/0x%08x-0x%02x", vid, name, qualifier);
183
184 names = pts_components->get_comp_func_names(pts_components, vid);
185 types = pts_components->get_qualifier_type_names(pts_components, vid);
186 type = pts_components->get_qualifier(pts_components, cfn, flags);
187 if (names && types)
188 {
189 n = snprintf(buf + n, BUF_LEN - n, " %N/%N [%s] %N",
190 pen_names, vid, names, name, flags, types, type);
191 }
192 return buf;
193 }
194
195 METHOD(attest_db_t, set_component, bool,
196 private_attest_db_t *this, char *comp, bool create)
197 {
198 enumerator_t *e;
199 char *pos1, *pos2;
200 int vid, name, qualifier;
201 pts_comp_func_name_t *cfn;
202
203 if (this->comp_set)
204 {
205 printf("component has already been set\n");
206 return FALSE;
207 }
208
209 /* parse component string */
210 pos1 = strchr(comp, '/');
211 pos2 = strchr(comp, '-');
212 if (!pos1 || !pos2)
213 {
214 printf("component string must have the form \"vendor_id/name-qualifier\"\n");
215 return FALSE;
216 }
217 vid = atoi(comp);
218 name = atoi(pos1 + 1);
219 qualifier = atoi(pos2 + 1);
220 cfn = pts_comp_func_name_create(vid, name, qualifier);
221
222 e = this->db->query(this->db,
223 "SELECT id FROM components "
224 "WHERE vendor_id = ? AND name = ? AND qualifier = ?",
225 DB_UINT, vid, DB_INT, name, DB_INT, qualifier, DB_INT);
226 if (e)
227 {
228 if (e->enumerate(e, &this->cid))
229 {
230 this->comp_set = TRUE;
231 this->cfn = cfn;
232 }
233 e->destroy(e);
234 }
235 if (this->comp_set)
236 {
237 return TRUE;
238 }
239
240 if (!create)
241 {
242 printf("component '%s' not found in database\n", print_cfn(cfn));
243 cfn->destroy(cfn);
244 return FALSE;
245 }
246
247 /* Add a new database entry */
248 this->comp_set = this->db->execute(this->db, &this->cid,
249 "INSERT INTO components (vendor_id, name, qualifier) "
250 "VALUES (?, ?, ?)",
251 DB_INT, vid, DB_INT, name, DB_INT, qualifier) == 1;
252
253 printf("component '%s' %sinserted into database\n", print_cfn(cfn),
254 this->comp_set ? "" : "could not be ");
255 if (this->comp_set)
256 {
257 this->cfn = cfn;
258 }
259 else
260 {
261 cfn->destroy(cfn);
262 }
263 return this->comp_set;
264 }
265
266 METHOD(attest_db_t, set_cid, bool,
267 private_attest_db_t *this, int cid)
268 {
269 enumerator_t *e;
270 int vid, name, qualifier;
271
272 if (this->comp_set)
273 {
274 printf("component has already been set\n");
275 return FALSE;
276 }
277 this->cid = cid;
278
279 e = this->db->query(this->db, "SELECT vendor_id, name, qualifier "
280 "FROM components WHERE id = ?",
281 DB_UINT, cid, DB_INT, DB_INT, DB_INT);
282 if (e)
283 {
284 if (e->enumerate(e, &vid, &name, &qualifier))
285 {
286 this->cfn = pts_comp_func_name_create(vid, name, qualifier);
287 this->comp_set = TRUE;
288 }
289 else
290 {
291 printf("no component found with cid %d\n", cid);
292 }
293 e->destroy(e);
294 }
295 return this->comp_set;
296 }
297
298 METHOD(attest_db_t, set_directory, bool,
299 private_attest_db_t *this, char *dir, bool create)
300 {
301 enumerator_t *e;
302 size_t len;
303
304 if (this->dir_set)
305 {
306 printf("directory has already been set\n");
307 return FALSE;
308 }
309 free(this->dir);
310
311 /* remove trailing '/' character */
312 len = strlen(dir);
313 if (len && dir[len-1] == '/')
314 {
315 dir[len-1] = '\0';
316 }
317 this->dir = strdup(dir);
318
319 e = this->db->query(this->db,
320 "SELECT id FROM files WHERE type = 1 AND path = ?",
321 DB_TEXT, dir, DB_INT);
322 if (e)
323 {
324 if (e->enumerate(e, &this->did))
325 {
326 this->dir_set = TRUE;
327 }
328 e->destroy(e);
329 }
330 if (this->dir_set)
331 {
332 return TRUE;
333 }
334
335 if (!create)
336 {
337 printf("directory '%s' not found in database\n", dir);
338 return FALSE;
339 }
340
341 /* Add a new database entry */
342 this->dir_set = this->db->execute(this->db, &this->did,
343 "INSERT INTO files (type, path) VALUES (1, ?)",
344 DB_TEXT, dir) == 1;
345
346 printf("directory '%s' %sinserted into database\n", dir,
347 this->dir_set ? "" : "could not be ");
348
349 return this->dir_set;
350 }
351
352 METHOD(attest_db_t, set_did, bool,
353 private_attest_db_t *this, int did)
354 {
355 enumerator_t *e;
356 char *dir;
357
358 if (this->dir_set)
359 {
360 printf("directory has already been set\n");
361 return FALSE;
362 }
363 this->did = did;
364
365 e = this->db->query(this->db, "SELECT path FROM files WHERE id = ?",
366 DB_UINT, did, DB_TEXT);
367 if (e)
368 {
369 if (e->enumerate(e, &dir))
370 {
371 free(this->dir);
372 this->dir = strdup(dir);
373 this->dir_set = TRUE;
374 }
375 else
376 {
377 printf("no directory found with did %d\n", did);
378 }
379 e->destroy(e);
380 }
381 return this->dir_set;
382 }
383
384 METHOD(attest_db_t, set_file, bool,
385 private_attest_db_t *this, char *file, bool create)
386 {
387 enumerator_t *e;
388 char *filename;
389
390 if (this->file_set)
391 {
392 printf("file has already been set\n");
393 return FALSE;
394 }
395 this->file = strdup(file);
396 filename = this->relative ? basename(file) : file;
397
398 e = this->db->query(this->db, "SELECT id FROM files WHERE path = ?",
399 DB_TEXT, filename, DB_INT);
400 if (e)
401 {
402 if (e->enumerate(e, &this->fid))
403 {
404 this->file_set = TRUE;
405 }
406 e->destroy(e);
407 }
408 if (this->file_set)
409 {
410 return TRUE;
411 }
412
413 if (!create)
414 {
415 printf("file '%s' not found in database\n", file);
416 return FALSE;
417 }
418
419 /* Add a new database entry */
420 this->file_set = this->db->execute(this->db, &this->fid,
421 "INSERT INTO files (type, path) VALUES (0, ?)",
422 DB_TEXT, filename) == 1;
423
424 printf("file '%s' %sinserted into database\n", filename,
425 this->file_set ? "" : "could not be ");
426
427 return this->file_set;
428 }
429
430 METHOD(attest_db_t, set_fid, bool,
431 private_attest_db_t *this, int fid)
432 {
433 enumerator_t *e;
434 char *file;
435
436 if (this->file_set)
437 {
438 printf("file has already been set\n");
439 return FALSE;
440 }
441 this->fid = fid;
442
443 e = this->db->query(this->db, "SELECT path FROM files WHERE id = ?",
444 DB_UINT, fid, DB_TEXT);
445 if (e)
446 {
447 if (e->enumerate(e, &file))
448 {
449 this->file = strdup(file);
450 this->file_set = TRUE;
451 }
452 else
453 {
454 printf("no file found with fid %d\n", fid);
455 }
456 e->destroy(e);
457 }
458 return this->file_set;
459 }
460
461 METHOD(attest_db_t, set_key, bool,
462 private_attest_db_t *this, chunk_t key, bool create)
463 {
464 enumerator_t *e;
465 char *owner;
466
467 if (this->key_set)
468 {
469 printf("key has already been set\n");
470 return FALSE;
471 }
472 this->key = key;
473
474 e = this->db->query(this->db, "SELECT id, owner FROM keys WHERE keyid= ?",
475 DB_BLOB, this->key, DB_INT, DB_TEXT);
476 if (e)
477 {
478 if (e->enumerate(e, &this->kid, &owner))
479 {
480 free(this->owner);
481 this->owner = strdup(owner);
482 this->key_set = TRUE;
483 }
484 e->destroy(e);
485 }
486 if (this->key_set)
487 {
488 return TRUE;
489 }
490
491 if (!create)
492 {
493 printf("key '%#B' not found in database\n", &this->key);
494 return FALSE;
495 }
496
497 /* Add a new database entry */
498 if (!this->owner)
499 {
500 this->owner = strdup("");
501 }
502 this->key_set = this->db->execute(this->db, &this->kid,
503 "INSERT INTO keys (keyid, owner) VALUES (?, ?)",
504 DB_BLOB, this->key, DB_TEXT, this->owner) == 1;
505
506 printf("key '%#B' %sinserted into database\n", &this->key,
507 this->key_set ? "" : "could not be ");
508
509 return this->key_set;
510
511 };
512
513 METHOD(attest_db_t, set_kid, bool,
514 private_attest_db_t *this, int kid)
515 {
516 enumerator_t *e;
517 chunk_t key;
518 char *owner;
519
520 if (this->key_set)
521 {
522 printf("key has already been set\n");
523 return FALSE;
524 }
525 this->kid = kid;
526
527 e = this->db->query(this->db, "SELECT keyid, owner FROM keys WHERE id = ?",
528 DB_UINT, kid, DB_BLOB, DB_TEXT);
529 if (e)
530 {
531 if (e->enumerate(e, &key, &owner))
532 {
533 this->owner = strdup(owner);
534 this->key = chunk_clone(key);
535 this->key_set = TRUE;
536 }
537 else
538 {
539 printf("no key found with kid %d\n", kid);
540 }
541 e->destroy(e);
542 }
543 return this->key_set;
544
545 };
546
547 METHOD(attest_db_t, set_product, bool,
548 private_attest_db_t *this, char *product, bool create)
549 {
550 enumerator_t *e;
551
552 if (this->product_set)
553 {
554 printf("product has already been set\n");
555 return FALSE;
556 }
557 this->product = strdup(product);
558
559 e = this->db->query(this->db, "SELECT id FROM products WHERE name = ?",
560 DB_TEXT, product, DB_INT);
561 if (e)
562 {
563 if (e->enumerate(e, &this->pid))
564 {
565 this->product_set = TRUE;
566 }
567 e->destroy(e);
568 }
569 if (this->product_set)
570 {
571 return TRUE;
572 }
573
574 if (!create)
575 {
576 printf("product '%s' not found in database\n", product);
577 return FALSE;
578 }
579
580 /* Add a new database entry */
581 this->product_set = this->db->execute(this->db, &this->pid,
582 "INSERT INTO products (name) VALUES (?)",
583 DB_TEXT, product) == 1;
584
585 printf("product '%s' %sinserted into database\n", product,
586 this->product_set ? "" : "could not be ");
587
588 return this->product_set;
589 }
590
591 METHOD(attest_db_t, set_pid, bool,
592 private_attest_db_t *this, int pid)
593 {
594 enumerator_t *e;
595 char *product;
596
597 if (this->product_set)
598 {
599 printf("product has already been set\n");
600 return FALSE;
601 }
602 this->pid = pid;
603
604 e = this->db->query(this->db, "SELECT name FROM products WHERE id = ?",
605 DB_UINT, pid, DB_TEXT);
606 if (e)
607 {
608 if (e->enumerate(e, &product))
609 {
610 this->product = strdup(product);
611 this->product_set = TRUE;
612 }
613 else
614 {
615 printf("no product found with pid %d in database\n", pid);
616 }
617 e->destroy(e);
618 }
619 return this->product_set;
620 }
621
622 METHOD(attest_db_t, set_package, bool,
623 private_attest_db_t *this, char *package, bool create)
624 {
625 enumerator_t *e;
626
627 if (this->package_set)
628 {
629 printf("package has already been set\n");
630 return FALSE;
631 }
632 this->package = strdup(package);
633
634 e = this->db->query(this->db, "SELECT id FROM packages WHERE name = ?",
635 DB_TEXT, package, DB_INT);
636 if (e)
637 {
638 if (e->enumerate(e, &this->gid))
639 {
640 this->package_set = TRUE;
641 }
642 e->destroy(e);
643 }
644 if (this->package_set)
645 {
646 return TRUE;
647 }
648
649 if (!create)
650 {
651 printf("package '%s' not found in database\n", package);
652 return FALSE;
653 }
654
655 /* Add a new database entry */
656 this->package_set = this->db->execute(this->db, &this->gid,
657 "INSERT INTO packages (name) VALUES (?)",
658 DB_TEXT, package) == 1;
659
660 printf("package '%s' %sinserted into database\n", package,
661 this->package_set ? "" : "could not be ");
662
663 return this->package_set;
664 }
665
666 METHOD(attest_db_t, set_gid, bool,
667 private_attest_db_t *this, int gid)
668 {
669 enumerator_t *e;
670 char *package;
671
672 if (this->package_set)
673 {
674 printf("package has already been set\n");
675 return FALSE;
676 }
677 this->gid = gid;
678
679 e = this->db->query(this->db, "SELECT name FROM packages WHERE id = ?",
680 DB_UINT, gid, DB_TEXT);
681 if (e)
682 {
683 if (e->enumerate(e, &package))
684 {
685 this->package = strdup(package);
686 this->package_set = TRUE;
687 }
688 else
689 {
690 printf("no package found with gid %d in database\n", gid);
691 }
692 e->destroy(e);
693 }
694 return this->package_set;
695 }
696
697 METHOD(attest_db_t, set_version, bool,
698 private_attest_db_t *this, char *version)
699 {
700 if (this->version_set)
701 {
702 printf("version has already been set\n");
703 return FALSE;
704 }
705 this->version = strdup(version);
706 this->version_set = TRUE;
707
708 return TRUE;
709 }
710
711
712 METHOD(attest_db_t, set_algo, void,
713 private_attest_db_t *this, pts_meas_algorithms_t algo)
714 {
715 this->algo = algo;
716 }
717
718 METHOD(attest_db_t, set_relative, void,
719 private_attest_db_t *this)
720 {
721 this->relative = TRUE;
722 }
723
724 METHOD(attest_db_t, set_security, void,
725 private_attest_db_t *this, os_package_state_t security)
726 {
727 this->security = security;
728 }
729
730 METHOD(attest_db_t, set_sequence, void,
731 private_attest_db_t *this, int seq_no)
732 {
733 this->seq_no = seq_no;
734 }
735
736 METHOD(attest_db_t, set_owner, void,
737 private_attest_db_t *this, char *owner)
738 {
739 free(this->owner);
740 this->owner = strdup(owner);
741 }
742
743 METHOD(attest_db_t, list_components, void,
744 private_attest_db_t *this)
745 {
746 enumerator_t *e;
747 pts_comp_func_name_t *cfn;
748 int seq_no, cid, vid, name, qualifier, count = 0;
749
750 if (this->kid)
751 {
752 e = this->db->query(this->db,
753 "SELECT kc.seq_no, c.id, c.vendor_id, c.name, c.qualifier "
754 "FROM components AS c "
755 "JOIN key_component AS kc ON c.id = kc.component "
756 "WHERE kc.key = ? ORDER BY kc.seq_no",
757 DB_UINT, this->kid, DB_INT, DB_INT, DB_INT, DB_INT, DB_INT);
758 if (e)
759 {
760 while (e->enumerate(e, &cid, &seq_no, &vid, &name, &qualifier))
761 {
762 cfn = pts_comp_func_name_create(vid, name, qualifier);
763 printf("%4d: #%-2d %s\n", seq_no, cid, print_cfn(cfn));
764 cfn->destroy(cfn);
765 count++;
766 }
767 e->destroy(e);
768 printf("%d component%s found for key %#B\n", count,
769 (count == 1) ? "" : "s", &this->key);
770 }
771 }
772 else
773 {
774 e = this->db->query(this->db,
775 "SELECT id, vendor_id, name, qualifier FROM components "
776 "ORDER BY vendor_id, name, qualifier",
777 DB_INT, DB_INT, DB_INT, DB_INT);
778 if (e)
779 {
780 while (e->enumerate(e, &cid, &vid, &name, &qualifier))
781 {
782 cfn = pts_comp_func_name_create(vid, name, qualifier);
783 printf("%4d: %s\n", cid, print_cfn(cfn));
784 cfn->destroy(cfn);
785 count++;
786 }
787 e->destroy(e);
788 printf("%d component%s found\n", count, (count == 1) ? "" : "s");
789 }
790 }
791 }
792
793 METHOD(attest_db_t, list_devices, void,
794 private_attest_db_t *this)
795 {
796 enumerator_t *e;
797 chunk_t value;
798 int id, count = 0;
799
800 e = this->db->query(this->db,
801 "SELECT id, value FROM devices", DB_INT, DB_BLOB);
802 if (e)
803 {
804 while (e->enumerate(e, &id, &value))
805 {
806 printf("%4d: %.*s\n", id, value.len, value.ptr);
807 count++;
808 }
809 e->destroy(e);
810 printf("%d device%s found\n", count, (count == 1) ? "" : "s");
811 }
812 }
813
814 METHOD(attest_db_t, list_keys, void,
815 private_attest_db_t *this)
816 {
817 enumerator_t *e;
818 chunk_t keyid;
819 char *owner;
820 int kid, count = 0;
821
822 if (this->cid)
823 {
824 e = this->db->query(this->db,
825 "SELECT k.id, k.keyid, k.owner FROM keys AS k "
826 "JOIN key_component AS kc ON k.id = kc.key "
827 "WHERE kc.component = ? ORDER BY k.keyid",
828 DB_UINT, this->cid, DB_INT, DB_BLOB, DB_TEXT);
829 if (e)
830 {
831 while (e->enumerate(e, &kid, &keyid, &owner))
832 {
833 printf("%4d: %#B '%s'\n", kid, &keyid, owner);
834 count++;
835 }
836 e->destroy(e);
837 }
838 }
839 else
840 {
841 e = this->db->query(this->db, "SELECT id, keyid, owner FROM keys "
842 "ORDER BY keyid",
843 DB_INT, DB_BLOB, DB_TEXT);
844 if (e)
845 {
846 while (e->enumerate(e, &kid, &keyid, &owner))
847 {
848 printf("%4d: %#B '%s'\n", kid, &keyid, owner);
849 count++;
850 }
851 e->destroy(e);
852 }
853 }
854
855 printf("%d key%s found", count, (count == 1) ? "" : "s");
856 if (this->comp_set)
857 {
858 printf(" for component '%s'", print_cfn(this->cfn));
859 }
860 printf("\n");
861 }
862
863 METHOD(attest_db_t, list_files, void,
864 private_attest_db_t *this)
865 {
866 enumerator_t *e;
867 char *file, *file_type[] = { " ", "d", "r" };
868 int fid, type, meas, meta, count = 0;
869
870 if (this->pid)
871 {
872 e = this->db->query(this->db,
873 "SELECT f.id, f.type, f.path, pf.measurement, pf.metadata "
874 "FROM files AS f "
875 "JOIN product_file AS pf ON f.id = pf.file "
876 "WHERE pf.product = ? ORDER BY f.path",
877 DB_UINT, this->pid, DB_INT, DB_INT, DB_TEXT, DB_INT, DB_INT);
878 if (e)
879 {
880 while (e->enumerate(e, &fid, &type, &file, &meas, &meta))
881 {
882 type = (type < 0 || type > 2) ? 0 : type;
883 printf("%4d: |%s%s| %s %s\n", fid, meas ? "M":" ", meta ? "T":" ",
884 file_type[type], file);
885 count++;
886 }
887 e->destroy(e);
888 }
889 }
890 else
891 {
892 e = this->db->query(this->db,
893 "SELECT id, type, path FROM files "
894 "ORDER BY path",
895 DB_INT, DB_INT, DB_TEXT);
896 if (e)
897 {
898 while (e->enumerate(e, &fid, &type, &file))
899 {
900 type = (type < 0 || type > 2) ? 0 : type;
901 printf("%4d: %s %s\n", fid, file_type[type], file);
902 count++;
903 }
904 e->destroy(e);
905 }
906 }
907
908 printf("%d file%s found", count, (count == 1) ? "" : "s");
909 if (this->product_set)
910 {
911 printf(" for product '%s'", this->product);
912 }
913 printf("\n");
914 }
915
916 METHOD(attest_db_t, list_packages, void,
917 private_attest_db_t *this)
918 {
919 enumerator_t *e;
920 char *package, *version;
921 os_package_state_t security;
922 int gid, gid_old = 0, spaces, count = 0;
923 time_t t;
924
925 if (this->pid)
926 {
927 e = this->db->query(this->db,
928 "SELECT p.id, p.name, v.release, v.security, v.time "
929 "FROM packages AS p JOIN versions AS v ON v.package = p.id "
930 "WHERE v.product = ? ORDER BY p.name, v.release",
931 DB_INT, this->pid, DB_INT, DB_TEXT, DB_TEXT, DB_INT, DB_INT);
932 if (e)
933 {
934 while (e->enumerate(e, &gid, &package, &version, &security, &t))
935 {
936 if (gid != gid_old)
937 {
938 printf("%5d: %s,", gid, package);
939 gid_old = gid;
940 }
941 else
942 {
943 spaces = 8 + strlen(package);
944 while (spaces--)
945 {
946 printf(" ");
947 }
948 }
949 printf(" %T (%s)%N\n", &t, TRUE, version,
950 os_package_state_names, security);
951 count++;
952 }
953 e->destroy(e);
954 }
955 }
956 else
957 {
958 e = this->db->query(this->db, "SELECT id, name FROM packages "
959 "ORDER BY name",
960 DB_INT, DB_TEXT);
961 if (e)
962 {
963 while (e->enumerate(e, &gid, &package))
964 {
965 printf("%4d: %s\n", gid, package);
966 count++;
967 }
968 e->destroy(e);
969 }
970 }
971
972 printf("%d package%s found", count, (count == 1) ? "" : "s");
973 if (this->product_set)
974 {
975 printf(" for product '%s'", this->product);
976 }
977 printf("\n");
978 }
979
980 METHOD(attest_db_t, list_products, void,
981 private_attest_db_t *this)
982 {
983 enumerator_t *e;
984 char *product;
985 int pid, meas, meta, count = 0;
986
987 if (this->fid)
988 {
989 e = this->db->query(this->db,
990 "SELECT p.id, p.name, pf.measurement, pf.metadata "
991 "FROM products AS p "
992 "JOIN product_file AS pf ON p.id = pf.product "
993 "WHERE pf.file = ? ORDER BY p.name",
994 DB_UINT, this->fid, DB_INT, DB_TEXT, DB_INT, DB_INT);
995 if (e)
996 {
997 while (e->enumerate(e, &pid, &product, &meas, &meta))
998 {
999 printf("%4d: |%s%s| %s\n", pid, meas ? "M":" ", meta ? "T":" ",
1000 product);
1001 count++;
1002 }
1003 e->destroy(e);
1004 }
1005 }
1006 else
1007 {
1008 e = this->db->query(this->db, "SELECT id, name FROM products "
1009 "ORDER BY name",
1010 DB_INT, DB_TEXT);
1011 if (e)
1012 {
1013 while (e->enumerate(e, &pid, &product))
1014 {
1015 printf("%4d: %s\n", pid, product);
1016 count++;
1017 }
1018 e->destroy(e);
1019 }
1020 }
1021
1022 printf("%d product%s found", count, (count == 1) ? "" : "s");
1023 if (this->file_set)
1024 {
1025 printf(" for file '%s'", this->file);
1026 }
1027 printf("\n");
1028 }
1029
1030 /**
1031 * get the directory if there is one from the files tables
1032 */
1033 static void get_directory(private_attest_db_t *this, int did, char **directory)
1034 {
1035 enumerator_t *e;
1036 char *dir;
1037
1038 free(*directory);
1039 *directory = strdup("");
1040
1041 if (did)
1042 {
1043 e = this->db->query(this->db,
1044 "SELECT path from files WHERE id = ?",
1045 DB_UINT, did, DB_TEXT);
1046 if (e)
1047 {
1048 if (e->enumerate(e, &dir))
1049 {
1050 free(*directory);
1051 *directory = strdup(dir);
1052 }
1053 e->destroy(e);
1054 }
1055 }
1056 }
1057
1058 static bool slash(char *directory, char *file)
1059 {
1060 return *file != '/' && directory[max(0, strlen(directory)-1)] != '/';
1061 }
1062
1063 METHOD(attest_db_t, list_hashes, void,
1064 private_attest_db_t *this)
1065 {
1066 enumerator_t *e;
1067 chunk_t hash;
1068 char *file, *dir, *product;
1069 int fid, fid_old = 0, did, did_old = 0, count = 0;
1070
1071 dir = strdup("");
1072
1073 if (this->pid && this->fid & this->did)
1074 {
1075 e = this->db->query(this->db,
1076 "SELECT hash FROM file_hashes "
1077 "WHERE algo = ? AND file = ? AND directory = ? AND product = ?",
1078 DB_INT, this->algo, DB_INT, this->fid, DB_INT, this->did,
1079 DB_INT, this->pid, DB_BLOB);
1080 if (e)
1081 {
1082 while (e->enumerate(e, &hash))
1083 {
1084 if (this->fid != fid_old)
1085 {
1086 printf("%4d: %s%s%s\n", this->fid, this->dir,
1087 slash(this->dir, this->file) ? "/" : "", this->file);
1088 fid_old = this->fid;
1089 }
1090 printf(" %#B\n", &hash);
1091 count++;
1092 }
1093 e->destroy(e);
1094
1095 printf("%d %N value%s found for product '%s'\n", count,
1096 pts_meas_algorithm_names, this->algo,
1097 (count == 1) ? "" : "s", this->product);
1098 }
1099 }
1100 else if (this->pid && this->fid)
1101 {
1102 e = this->db->query(this->db,
1103 "SELECT f.path, fh.hash FROM file_hashes AS fh "
1104 "JOIN files AS f ON f.id = fh.file "
1105 "WHERE algo = ? AND file = ? AND product = ?",
1106 DB_INT, this->algo, DB_INT, this->fid, DB_INT, this->pid,
1107 DB_TEXT, DB_BLOB);
1108 if (e)
1109 {
1110 free(dir);
1111 while (e->enumerate(e, &dir, &hash))
1112 {
1113 printf("%4d: %s%s%s\n", this->fid, dir,
1114 slash(dir, this->file) ? "/" : "", this->file);
1115 printf(" %#B\n", &hash);
1116 count++;
1117 }
1118 e->destroy(e);
1119
1120 printf("%d %N value%s found for product '%s'\n", count,
1121 pts_meas_algorithm_names, this->algo,
1122 (count == 1) ? "" : "s", this->product);
1123 dir = NULL;
1124 }
1125 }
1126 else if (this->pid)
1127 {
1128 e = this->db->query(this->db,
1129 "SELECT f.id, f. f.path, fh.hash, fh.directory "
1130 "FROM file_hashes AS fh "
1131 "JOIN files AS f ON f.id = fh.file "
1132 "WHERE fh.algo = ? AND fh.product = ? "
1133 "ORDER BY fh.directory, f.path",
1134 DB_INT, this->algo, DB_UINT, this->pid,
1135 DB_INT, DB_TEXT, DB_BLOB, DB_INT);
1136 if (e)
1137 {
1138 while (e->enumerate(e, &fid, &file, &hash, &did))
1139 {
1140 if (fid != fid_old || did != did_old)
1141 {
1142 if (did != did_old)
1143 {
1144 get_directory(this, did, &dir);
1145 }
1146 printf("%4d: %s%s%s\n", fid,
1147 dir, slash(dir, file) ? "/" : "", file);
1148 fid_old = fid;
1149 did_old = did;
1150 }
1151 printf(" %#B\n", &hash);
1152 count++;
1153 }
1154 e->destroy(e);
1155
1156 printf("%d %N value%s found for product '%s'\n", count,
1157 pts_meas_algorithm_names, this->algo,
1158 (count == 1) ? "" : "s", this->product);
1159 }
1160 }
1161 else if (this->fid)
1162 {
1163 e = this->db->query(this->db,
1164 "SELECT p.name, fh.hash, fh.directory "
1165 "FROM file_hashes AS fh "
1166 "JOIN products AS p ON p.id = fh.product "
1167 "WHERE fh.algo = ? AND fh.file = ? AND fh.directory = ?"
1168 "ORDER BY p.name",
1169 DB_INT, this->algo, DB_UINT, this->fid, DB_UINT, this->did,
1170 DB_TEXT, DB_BLOB, DB_INT);
1171 if (e)
1172 {
1173 while (e->enumerate(e, &product, &hash, &did))
1174 {
1175 printf("%#B '%s'\n", &hash, product);
1176 count++;
1177 }
1178 e->destroy(e);
1179
1180 printf("%d %N value%s found for file '%s%s%s'\n",
1181 count, pts_meas_algorithm_names, this->algo,
1182 (count == 1) ? "" : "s", this->dir,
1183 slash(this->dir, this->file) ? "/" : "", this->file);
1184 }
1185 }
1186 else
1187 {
1188 e = this->db->query(this->db,
1189 "SELECT f.id, f.path, p.name, fh.hash, fh.directory "
1190 "FROM file_hashes AS fh "
1191 "JOIN files AS f ON f.id = fh.file "
1192 "JOIN products AS p ON p.id = fh.product "
1193 "WHERE fh.algo = ? "
1194 "ORDER BY fh.directory, f.path, p.name",
1195 DB_INT, this->algo,
1196 DB_INT, DB_TEXT, DB_TEXT, DB_BLOB, DB_INT);
1197 if (e)
1198 {
1199 while (e->enumerate(e, &fid, &file, &product, &hash, &did))
1200 {
1201 if (fid != fid_old || did != did_old)
1202 {
1203 if (did != did_old)
1204 {
1205 get_directory(this, did, &dir);
1206 did_old = did;
1207 }
1208 printf("%4d: %s%s%s\n", fid,
1209 dir, slash(dir, file) ? "/" : "", file);
1210 fid_old = fid;
1211 }
1212 printf(" %#B '%s'\n", &hash, product);
1213 count++;
1214 }
1215 e->destroy(e);
1216
1217 printf("%d %N value%s found\n", count, pts_meas_algorithm_names,
1218 this->algo, (count == 1) ? "" : "s");
1219 }
1220 }
1221 free(dir);
1222 }
1223
1224 METHOD(attest_db_t, list_measurements, void,
1225 private_attest_db_t *this)
1226 {
1227 enumerator_t *e;
1228 chunk_t hash, keyid;
1229 pts_comp_func_name_t *cfn;
1230 char *owner;
1231 int seq_no, pcr, vid, name, qualifier;
1232 int cid, cid_old = 0, kid, kid_old = 0, count = 0;
1233
1234 if (this->kid && this->cid)
1235 {
1236 e = this->db->query(this->db,
1237 "SELECT ch.seq_no, ch.pcr, ch.hash, k.owner "
1238 "FROM component_hashes AS ch "
1239 "JOIN keys AS k ON k.id = ch.key "
1240 "WHERE ch.algo = ? AND ch.key = ? AND ch.component = ? "
1241 "ORDER BY seq_no",
1242 DB_INT, this->algo, DB_UINT, this->kid, DB_UINT, this->cid,
1243 DB_INT, DB_INT, DB_BLOB, DB_TEXT);
1244 if (e)
1245 {
1246 while (e->enumerate(e, &seq_no, &pcr, &hash, &owner))
1247 {
1248 if (this->kid != kid_old)
1249 {
1250 printf("%4d: %#B '%s'\n", this->kid, &this->key, owner);
1251 kid_old = this->kid;
1252 }
1253 printf("%7d %02d %#B\n", seq_no, pcr, &hash);
1254 count++;
1255 }
1256 e->destroy(e);
1257
1258 printf("%d %N value%s found for component '%s'\n", count,
1259 pts_meas_algorithm_names, this->algo,
1260 (count == 1) ? "" : "s", print_cfn(this->cfn));
1261 }
1262 }
1263 else if (this->cid)
1264 {
1265 e = this->db->query(this->db,
1266 "SELECT ch.seq_no, ch.pcr, ch.hash, k.id, k.keyid, k.owner "
1267 "FROM component_hashes AS ch "
1268 "JOIN keys AS k ON k.id = ch.key "
1269 "WHERE ch.algo = ? AND ch.component = ? "
1270 "ORDER BY keyid, seq_no",
1271 DB_INT, this->algo, DB_UINT, this->cid,
1272 DB_INT, DB_INT, DB_BLOB, DB_INT, DB_BLOB, DB_TEXT);
1273 if (e)
1274 {
1275 while (e->enumerate(e, &seq_no, &pcr, &hash, &kid, &keyid, &owner))
1276 {
1277 if (kid != kid_old)
1278 {
1279 printf("%4d: %#B '%s'\n", kid, &keyid, owner);
1280 kid_old = kid;
1281 }
1282 printf("%7d %02d %#B\n", seq_no, pcr, &hash);
1283 count++;
1284 }
1285 e->destroy(e);
1286
1287 printf("%d %N value%s found for component '%s'\n", count,
1288 pts_meas_algorithm_names, this->algo,
1289 (count == 1) ? "" : "s", print_cfn(this->cfn));
1290 }
1291
1292 }
1293 else if (this->kid)
1294 {
1295 e = this->db->query(this->db,
1296 "SELECT ch.seq_no, ch.pcr, ch.hash, "
1297 "c.id, c.vendor_id, c.name, c.qualifier "
1298 "FROM component_hashes AS ch "
1299 "JOIN components AS c ON c.id = ch.component "
1300 "WHERE ch.algo = ? AND ch.key = ? "
1301 "ORDER BY vendor_id, name, qualifier, seq_no",
1302 DB_INT, this->algo, DB_UINT, this->kid, DB_INT, DB_INT, DB_BLOB,
1303 DB_INT, DB_INT, DB_INT, DB_INT);
1304 if (e)
1305 {
1306 while (e->enumerate(e, &seq_no, &pcr, &hash, &cid, &vid, &name,
1307 &qualifier))
1308 {
1309 if (cid != cid_old)
1310 {
1311 cfn = pts_comp_func_name_create(vid, name, qualifier);
1312 printf("%4d: %s\n", cid, print_cfn(cfn));
1313 cfn->destroy(cfn);
1314 cid_old = cid;
1315 }
1316 printf("%5d %02d %#B\n", seq_no, pcr, &hash);
1317 count++;
1318 }
1319 e->destroy(e);
1320
1321 printf("%d %N value%s found for key %#B '%s'\n", count,
1322 pts_meas_algorithm_names, this->algo,
1323 (count == 1) ? "" : "s", &this->key, this->owner);
1324 }
1325 }
1326 }
1327
1328 bool insert_file_hash(private_attest_db_t *this, pts_meas_algorithms_t algo,
1329 chunk_t measurement, int fid, int did, bool ima,
1330 int *hashes_added)
1331 {
1332 enumerator_t *e;
1333 chunk_t hash;
1334 char *label;
1335
1336 label = "could not be created";
1337
1338 e = this->db->query(this->db,
1339 "SELECT hash FROM file_hashes WHERE algo = ? "
1340 "AND file = ? AND directory = ? AND product = ? and key = 0",
1341 DB_INT, algo, DB_UINT, fid, DB_UINT, did, DB_UINT, this->pid, DB_BLOB);
1342 if (!e)
1343 {
1344 printf("file_hashes query failed\n");
1345 return FALSE;
1346 }
1347 if (e->enumerate(e, &hash))
1348 {
1349 label = chunk_equals(measurement, hash) ?
1350 "exists and equals" : "exists and differs";
1351 }
1352 else
1353 {
1354 if (this->db->execute(this->db, NULL,
1355 "INSERT INTO file_hashes "
1356 "(file, directory, product, key, algo, hash) "
1357 "VALUES (?, ?, ?, 0, ?, ?)",
1358 DB_UINT, fid, DB_UINT, did, DB_UINT, this->pid,
1359 DB_INT, algo, DB_BLOB, measurement) == 1)
1360 {
1361 label = "created";
1362 (*hashes_added)++;
1363 }
1364 }
1365 e->destroy(e);
1366
1367 printf(" %#B - %s%s\n", &measurement, ima ? "ima - " : "", label);
1368 return TRUE;
1369 }
1370
1371 METHOD(attest_db_t, add, bool,
1372 private_attest_db_t *this)
1373 {
1374 bool success = FALSE;
1375
1376 /* add key/component pair */
1377 if (this->kid && this->cid)
1378 {
1379 success = this->db->execute(this->db, NULL,
1380 "INSERT INTO key_component (key, component, seq_no) "
1381 "VALUES (?, ?, ?)",
1382 DB_UINT, this->kid, DB_UINT, this->cid,
1383 DB_UINT, this->seq_no) == 1;
1384
1385 printf("key/component pair (%d/%d) %sinserted into database at "
1386 "position %d\n", this->kid, this->cid,
1387 success ? "" : "could not be ", this->seq_no);
1388
1389 return success;
1390 }
1391
1392 /* add directory or file measurement for a given product */
1393 if ((this->did || this->fid) && this->pid)
1394 {
1395 char *pathname, *filename, *label;
1396 char ima_buffer[IMA_MAX_NAME_LEN + 1];
1397 chunk_t measurement, ima_template;
1398 pts_file_meas_t *measurements;
1399 hasher_t *hasher = NULL;
1400 bool ima = FALSE;
1401 int fid, did;
1402 int files_added = 0, hashes_added = 0, ima_hashes_added = 0;
1403 enumerator_t *enumerator, *e;
1404
1405 if (this->algo == PTS_MEAS_ALGO_SHA1_IMA)
1406 {
1407 ima = TRUE;
1408 this->algo = PTS_MEAS_ALGO_SHA1;
1409 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1410 if (!hasher)
1411 {
1412 printf("could not create hasher\n");
1413 return FALSE;
1414 }
1415 }
1416
1417 pathname = this->did ? this->dir : this->file;
1418 measurements = pts_file_meas_create_from_path(0, pathname, this->did,
1419 this->relative, this->algo);
1420 if (!measurements)
1421 {
1422 printf("file measurement failed\n");
1423 DESTROY_IF(hasher);
1424 return FALSE;
1425 }
1426 if (this->fid && this->relative)
1427 {
1428 set_directory(this, dirname(pathname), TRUE);
1429 }
1430 did = this->relative ? this->did : 0;
1431
1432 enumerator = measurements->create_enumerator(measurements);
1433 while (enumerator->enumerate(enumerator, &filename, &measurement))
1434 {
1435 /* retrieve or create filename */
1436 label = "could not be created";
1437
1438 e = this->db->query(this->db,
1439 "SELECT id FROM files WHERE path = ?",
1440 DB_TEXT, filename, DB_INT);
1441 if (!e)
1442 {
1443 printf("files query failed\n");
1444 break;
1445 }
1446 if (e->enumerate(e, &fid))
1447 {
1448 label = "exists";
1449 }
1450 else
1451 {
1452 if (this->db->execute(this->db, &fid,
1453 "INSERT INTO files (type, path) VALUES (0, ?)",
1454 DB_TEXT, filename) == 1)
1455 {
1456 label = "created";
1457 files_added++;
1458 }
1459 }
1460 e->destroy(e);
1461
1462 printf("%4d: %s - %s\n", fid, filename, label);
1463
1464 /* compute file measurement hash */
1465 if (!insert_file_hash(this, this->algo, measurement,
1466 fid, did, FALSE, &hashes_added))
1467 {
1468 break;
1469 }
1470
1471 if (!ima)
1472 {
1473 continue;
1474 }
1475
1476 /* compute IMA template hash */
1477 strncpy(ima_buffer, filename, IMA_MAX_NAME_LEN);
1478 ima_buffer[IMA_MAX_NAME_LEN] = '\0';
1479 ima_template = chunk_create(ima_buffer, sizeof(ima_buffer));
1480 if (!hasher->get_hash(hasher, measurement, NULL) ||
1481 !hasher->get_hash(hasher, ima_template, measurement.ptr))
1482 {
1483 printf("could not compute IMA template hash\n");
1484 break;
1485 }
1486 if (!insert_file_hash(this, PTS_MEAS_ALGO_SHA1_IMA, measurement,
1487 fid, did, TRUE, &ima_hashes_added))
1488 {
1489 break;
1490 }
1491 }
1492 enumerator->destroy(enumerator);
1493
1494 printf("%d measurements, added %d new files, %d new file hashes",
1495 measurements->get_file_count(measurements),
1496 files_added, hashes_added);
1497 if (ima)
1498 {
1499 printf(" , %d new ima hashes", ima_hashes_added);
1500 hasher->destroy(hasher);
1501 }
1502 printf("\n");
1503 measurements->destroy(measurements);
1504 success = TRUE;
1505 }
1506
1507 /* insert package version */
1508 if (this->version_set && this->gid && this->pid)
1509 {
1510 time_t t = time(NULL);
1511
1512 success = this->db->execute(this->db, NULL,
1513 "INSERT INTO versions "
1514 "(package, product, release, security, time) "
1515 "VALUES (?, ?, ?, ?, ?)",
1516 DB_UINT, this->gid, DB_UINT, this->pid, DB_TEXT,
1517 this->version, DB_UINT, this->security, DB_INT, t) == 1;
1518
1519 printf("'%s' package %s (%s)%N %sinserted into database\n",
1520 this->product, this->package, this->version,
1521 os_package_state_names, this->security,
1522 success ? "" : "could not be ");
1523 }
1524 return success;
1525 }
1526
1527 METHOD(attest_db_t, delete, bool,
1528 private_attest_db_t *this)
1529 {
1530 bool success;
1531
1532 /* delete a file measurement hash for a given product */
1533 if (this->algo && this->pid && this->fid)
1534 {
1535 success = this->db->execute(this->db, NULL,
1536 "DELETE FROM file_hashes "
1537 "WHERE algo = ? AND product = ? "
1538 "AND file = ? AND directory = ?",
1539 DB_UINT, this->algo, DB_UINT, this->pid,
1540 DB_UINT, this->fid, DB_UINT, this->did) > 0;
1541
1542 printf("%4d: %s%s%s\n", this->fid, this->dir, this->did ? "/":"",
1543 this->file);
1544 printf("%N value for product '%s' %sdeleted from database\n",
1545 pts_meas_algorithm_names, this->algo, this->product,
1546 success ? "" : "could not be ");
1547
1548 return success;
1549 }
1550
1551 /* delete product/file entries */
1552 if (this->pid && (this->fid || this->did))
1553 {
1554 success = this->db->execute(this->db, NULL,
1555 "DELETE FROM product_file "
1556 "WHERE product = ? AND file = ?",
1557 DB_UINT, this->pid,
1558 DB_UINT, this->fid ? this->fid : this->did) > 0;
1559
1560 printf("product/file pair (%d/%d) %sdeleted from database\n",
1561 this->pid, this->fid ? this->fid : this->did,
1562 success ? "" : "could not be ");
1563
1564 return success;
1565 }
1566
1567 /* delete key/component pair */
1568 if (this->kid && this->cid)
1569 {
1570 success = this->db->execute(this->db, NULL,
1571 "DELETE FROM key_component "
1572 "WHERE key = ? AND component = ?",
1573 DB_UINT, this->kid, DB_UINT, this->cid) > 0;
1574
1575 printf("key/component pair (%d/%d) %sdeleted from database\n",
1576 this->kid, this->cid, success ? "" : "could not be ");
1577 return success;
1578 }
1579
1580 if (this->cid)
1581 {
1582 success = this->db->execute(this->db, NULL,
1583 "DELETE FROM components WHERE id = ?",
1584 DB_UINT, this->cid) > 0;
1585
1586 printf("component '%s' %sdeleted from database\n", print_cfn(this->cfn),
1587 success ? "" : "could not be ");
1588 return success;
1589 }
1590
1591 if (this->did)
1592 {
1593 success = this->db->execute(this->db, NULL,
1594 "DELETE FROM files WHERE type = 1 AND id = ?",
1595 DB_UINT, this->did) > 0;
1596
1597 printf("directory '%s' %sdeleted from database\n", this->dir,
1598 success ? "" : "could not be ");
1599 return success;
1600 }
1601
1602 if (this->fid)
1603 {
1604 success = this->db->execute(this->db, NULL,
1605 "DELETE FROM files WHERE id = ?",
1606 DB_UINT, this->fid) > 0;
1607
1608 printf("file '%s' %sdeleted from database\n", this->file,
1609 success ? "" : "could not be ");
1610 return success;
1611 }
1612
1613 if (this->kid)
1614 {
1615 success = this->db->execute(this->db, NULL,
1616 "DELETE FROM keys WHERE id = ?",
1617 DB_UINT, this->kid) > 0;
1618
1619 printf("key %#B %sdeleted from database\n", &this->key,
1620 success ? "" : "could not be ");
1621 return success;
1622 }
1623 if (this->pid)
1624 {
1625 success = this->db->execute(this->db, NULL,
1626 "DELETE FROM products WHERE id = ?",
1627 DB_UINT, this->pid) > 0;
1628
1629 printf("product '%s' %sdeleted from database\n", this->product,
1630 success ? "" : "could not be ");
1631 return success;
1632 }
1633
1634 printf("empty delete command\n");
1635 return FALSE;
1636 }
1637
1638 METHOD(attest_db_t, destroy, void,
1639 private_attest_db_t *this)
1640 {
1641 DESTROY_IF(this->db);
1642 DESTROY_IF(this->cfn);
1643 free(this->package);
1644 free(this->product);
1645 free(this->version);
1646 free(this->file);
1647 free(this->dir);
1648 free(this->owner);
1649 free(this->key.ptr);
1650 free(this);
1651 }
1652
1653 /**
1654 * Described in header.
1655 */
1656 attest_db_t *attest_db_create(char *uri)
1657 {
1658 private_attest_db_t *this;
1659
1660 INIT(this,
1661 .public = {
1662 .set_component = _set_component,
1663 .set_cid = _set_cid,
1664 .set_directory = _set_directory,
1665 .set_did = _set_did,
1666 .set_file = _set_file,
1667 .set_fid = _set_fid,
1668 .set_key = _set_key,
1669 .set_kid = _set_kid,
1670 .set_package = _set_package,
1671 .set_gid = _set_gid,
1672 .set_product = _set_product,
1673 .set_pid = _set_pid,
1674 .set_version = _set_version,
1675 .set_algo = _set_algo,
1676 .set_relative = _set_relative,
1677 .set_security = _set_security,
1678 .set_sequence = _set_sequence,
1679 .set_owner = _set_owner,
1680 .list_packages = _list_packages,
1681 .list_products = _list_products,
1682 .list_files = _list_files,
1683 .list_components = _list_components,
1684 .list_devices = _list_devices,
1685 .list_keys = _list_keys,
1686 .list_hashes = _list_hashes,
1687 .list_measurements = _list_measurements,
1688 .add = _add,
1689 .delete = _delete,
1690 .destroy = _destroy,
1691 },
1692 .dir = strdup(""),
1693 .db = lib->db->create(lib->db, uri),
1694 );
1695
1696 if (!this->db)
1697 {
1698 fprintf(stderr, "opening database failed.\n");
1699 destroy(this);
1700 return NULL;
1701 }
1702
1703 return &this->public;
1704 }